TYPO3 CMS  TYPO3_6-2
StandaloneView.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\View;
3 
27 
35 
41  protected $templateSource = NULL;
42 
48  protected $templatePathAndFilename = NULL;
49 
50 
56  protected $layoutRootPath = NULL;
57 
63  protected $partialRootPath = NULL;
64 
70  protected $partialRootPaths = NULL;
71 
77  protected $layoutRootPaths = NULL;
78 
82  protected $templateCompiler;
83 
91  public function __construct(ContentObjectRenderer $contentObject = NULL) {
92  $this->objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
94  $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
95  if ($contentObject === NULL) {
97  $contentObject = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
98  }
99  $configurationManager->setContentObject($contentObject);
100  $this->templateParser = $this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Parser\\TemplateParser');
101  $this->setRenderingContext($this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Rendering\\RenderingContext'));
103  $request = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request');
104  $request->setRequestURI(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
105  $request->setBaseURI(GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
107  $uriBuilder = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder');
108  $uriBuilder->setRequest($request);
110  $controllerContext = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext');
111  $controllerContext->setRequest($request);
112  $controllerContext->setUriBuilder($uriBuilder);
114  $this->templateCompiler = $this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Compiler\\TemplateCompiler');
115  // singleton
116  $this->templateCompiler->setTemplateCache(GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->getCache('fluid_template'));
117  }
118 
126  public function setFormat($format) {
127  $this->getRequest()->setFormat($format);
128  }
129 
136  public function getFormat() {
137  return $this->getRequest()->getFormat();
138  }
139 
145  public function getRequest() {
146  return $this->controllerContext->getRequest();
147  }
148 
157  $this->templatePathAndFilename = $templatePathAndFilename;
158  }
159 
166  public function getTemplatePathAndFilename() {
168  }
169 
179  $this->templateSource = $templateSource;
180  }
181 
191  $this->layoutRootPath = $layoutRootPath;
192  $this->setLayoutRootPaths(array($layoutRootPath));
193  }
194 
202  public function setLayoutRootPaths(array $layoutRootPaths) {
203  $this->layoutRootPaths = $layoutRootPaths;
204  }
205 
213  public function getLayoutRootPath() {
215  return array_shift($layoutRootPaths);
216  }
217 
225  public function getLayoutRootPaths() {
226  if ($this->layoutRootPaths === NULL && $this->templatePathAndFilename === NULL) {
227  throw new InvalidTemplateResourceException('No layout root path has been specified. Use setLayoutRootPaths().', 1288091419);
228  }
229  if ($this->layoutRootPaths === NULL) {
230  $this->layoutRootPaths = array(dirname($this->templatePathAndFilename) . '/Layouts');
231  }
232  return $this->layoutRootPaths;
233  }
234 
245  $this->partialRootPath = $partialRootPath;
246  $this->setPartialRootPaths(array($partialRootPath));
247  }
248 
256  public function getPartialRootPath() {
258  return array_shift($partialRootPaths);
259  }
260 
269  public function setPartialRootPaths(array $partialRootPaths) {
270  $this->partialRootPaths = $partialRootPaths;
271  }
272 
280  public function getPartialRootPaths() {
281  if ($this->partialRootPaths === NULL && $this->templatePathAndFilename === NULL) {
282  throw new InvalidTemplateResourceException('No partial root path has been specified. Use setPartialRootPaths().', 1288094511);
283  }
284  if ($this->partialRootPaths === NULL) {
285  $this->partialRootPaths = array(dirname($this->templatePathAndFilename) . '/Partials');
286  }
288  }
289 
296  public function hasTemplate() {
297  try {
298  $this->getTemplateSource();
299  return TRUE;
300  } catch (InvalidTemplateResourceException $e) {
301  return FALSE;
302  }
303  }
304 
313  protected function getTemplateIdentifier($actionName = NULL) {
314  if ($this->templateSource === NULL) {
316  $templatePathAndFilenameInfo = pathinfo($templatePathAndFilename);
317  $templateFilenameWithoutExtension = basename($templatePathAndFilename, '.' . $templatePathAndFilenameInfo['extension']);
318  $prefix = sprintf('template_file_%s', $templateFilenameWithoutExtension);
319  return $this->createIdentifierForFile($templatePathAndFilename, $prefix);
320  } else {
322  $prefix = 'template_source';
323  $templateIdentifier = sprintf('Standalone_%s_%s', $prefix, sha1($templateSource));
324  return $templateIdentifier;
325  }
326  }
327 
335  protected function getTemplateSource($actionName = NULL) {
336  if ($this->templateSource === NULL && $this->templatePathAndFilename === NULL) {
337  throw new InvalidTemplateResourceException('No template has been specified. Use either setTemplateSource() or setTemplatePathAndFilename().', 1288085266);
338  }
339  if ($this->templateSource === NULL) {
340  if (!is_file($this->templatePathAndFilename)) {
341  throw new InvalidTemplateResourceException('Template could not be found at "' . $this->templatePathAndFilename . '".', 1288087061);
342  }
343  $this->templateSource = file_get_contents($this->templatePathAndFilename);
344  }
345  return $this->templateSource;
346  }
347 
356  protected function getLayoutIdentifier($layoutName = 'Default') {
357  $layoutPathAndFilename = $this->getLayoutPathAndFilename($layoutName);
358  $prefix = 'layout_' . $layoutName;
359  return $this->createIdentifierForFile($layoutPathAndFilename, $prefix);
360  }
361 
370  protected function getLayoutSource($layoutName = 'Default') {
371  $layoutPathAndFilename = $this->getLayoutPathAndFilename($layoutName);
372  $layoutSource = file_get_contents($layoutPathAndFilename);
373  if ($layoutSource === FALSE) {
374  throw new InvalidTemplateResourceException('"' . $layoutPathAndFilename . '" is not a valid template resource URI.', 1312215888);
375  }
376  return $layoutSource;
377  }
378 
391  protected function getLayoutPathAndFilename($layoutName = 'Default') {
392  $upperCasedLayoutName = ucfirst($layoutName);
393  $possibleLayoutPaths = array();
395  $paths = array_reverse($paths, TRUE);
396  foreach ($paths as $layoutRootPath) {
397  $possibleLayoutPaths[] = GeneralUtility::fixWindowsFilePath($layoutRootPath . '/' . $upperCasedLayoutName . '.' . $this->getRequest()->getFormat());
398  $possibleLayoutPaths[] = GeneralUtility::fixWindowsFilePath($layoutRootPath . '/' . $upperCasedLayoutName);
399  if ($upperCasedLayoutName !== $layoutName) {
400  $possibleLayoutPaths[] = GeneralUtility::fixWindowsFilePath($layoutRootPath . '/' . $layoutName . '.' . $this->getRequest()->getFormat());
401  $possibleLayoutPaths[] = GeneralUtility::fixWindowsFilePath($layoutRootPath . '/' . $layoutName);
402  }
403  }
404  foreach ($possibleLayoutPaths as $layoutPathAndFilename) {
405  if ($this->testFileExistence($layoutPathAndFilename)) {
406  return $layoutPathAndFilename;
407  }
408  }
409 
410  throw new InvalidTemplateResourceException('Could not load layout file. Tried following paths: "' . implode('", "', $possibleLayoutPaths) . '".', 1288092555);
411  }
412 
419  protected function testFileExistence($filePath) {
420  return is_file($filePath);
421  }
422 
431  protected function getPartialIdentifier($partialName) {
432  $partialPathAndFilename = $this->getPartialPathAndFilename($partialName);
433  $prefix = 'partial_' . $partialName;
434  return $this->createIdentifierForFile($partialPathAndFilename, $prefix);
435  }
436 
445  protected function getPartialSource($partialName) {
446  $partialPathAndFilename = $this->getPartialPathAndFilename($partialName);
447  $partialSource = file_get_contents($partialPathAndFilename);
448  if ($partialSource === FALSE) {
449  throw new InvalidTemplateResourceException('"' . $partialPathAndFilename . '" is not a valid template resource URI.', 1257246932);
450  }
451  return $partialSource;
452  }
453 
461  protected function getPartialPathAndFilename($partialName) {
462  $upperCasedPartialName = ucfirst($partialName);
464  $paths = array_reverse($paths, TRUE);
465  $possiblePartialPaths = array();
466  foreach ($paths as $partialRootPath) {
467  $possiblePartialPaths[] = GeneralUtility::fixWindowsFilePath($partialRootPath . '/' . $upperCasedPartialName . '.' . $this->getRequest()->getFormat());
468  $possiblePartialPaths[] = GeneralUtility::fixWindowsFilePath($partialRootPath . '/' . $upperCasedPartialName);
469  if ($upperCasedPartialName !== $partialName) {
470  $possiblePartialPaths[] = GeneralUtility::fixWindowsFilePath($partialRootPath . '/' . $partialName . '.' . $this->getRequest()->getFormat());
471  $possiblePartialPaths[] = GeneralUtility::fixWindowsFilePath($partialRootPath . '/' . $partialName);
472  }
473  }
474  foreach ($possiblePartialPaths as $partialPathAndFilename) {
475  if ($this->testFileExistence($partialPathAndFilename)) {
476  return $partialPathAndFilename;
477  }
478  }
479  throw new InvalidTemplateResourceException('Could not load partial file. Tried following paths: "' . implode('", "', $possiblePartialPaths) . '".', 1288092556);
480  }
481 
491  protected function createIdentifierForFile($pathAndFilename, $prefix) {
492  $templateModifiedTimestamp = filemtime($pathAndFilename);
493  $templateIdentifier = sprintf('Standalone_%s_%s', $prefix, sha1($pathAndFilename . '|' . $templateModifiedTimestamp));
494  $templateIdentifier = str_replace('/', '_', str_replace('.', '_', $templateIdentifier));
495  return $templateIdentifier;
496  }
497 }
setPartialRootPaths(array $partialRootPaths)
setTemplatePathAndFilename($templatePathAndFilename)
createIdentifierForFile($pathAndFilename, $prefix)
setRenderingContext(\TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
getLayoutSource($layoutName='Default')
setLayoutRootPaths(array $layoutRootPaths)
getLayoutIdentifier($layoutName='Default')
setControllerContext(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext)
getLayoutPathAndFilename($layoutName='Default')