TYPO3 CMS  TYPO3_6-2
TemplateView.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\View;
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
16 
23 
31  protected $templateRootPathPattern = '@packageResourcesPath/Private/Templates';
32 
40  protected $partialRootPathPattern = '@packageResourcesPath/Private/Partials';
41 
49  protected $layoutRootPathPattern = '@packageResourcesPath/Private/Layouts';
50 
56  protected $templateRootPaths = NULL;
57 
63  protected $partialRootPaths = NULL;
64 
70  protected $layoutRootPaths = NULL;
71 
84  protected $templatePathAndFilenamePattern = '@templateRoot/@subpackage/@controller/@action.@format';
85 
98  private $partialPathAndFilenamePattern = '@partialRoot/@subpackage/@partial.@format';
99 
112  protected $layoutPathAndFilenamePattern = '@layoutRoot/@layout.@format';
113 
119  protected $templatePathAndFilename = NULL;
120 
126  protected $layoutPathAndFilename = NULL;
127 
128  public function __construct() {
129  $this->templateParser = \TYPO3\CMS\Fluid\Compatibility\TemplateParserBuilder::build();
130  $this->objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
131  $this->setRenderingContext($this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Rendering\\RenderingContextInterface'));
132  }
133 
134  public function initializeView() {
135  }
136  // Here, the backporter can insert a constructor method, which is needed for the TYPO3 CMS extension
137 
147  $this->templatePathAndFilename = $templatePathAndFilename;
148  }
149 
158  $this->layoutPathAndFilename = $layoutPathAndFilename;
159  }
160 
170  public function setTemplateRootPath($templateRootPath) {
171  $this->setTemplateRootPaths(array($templateRootPath));
172  }
173 
178  protected function getTemplateRootPath() {
180  $templateRootPaths = $this->getTemplateRootPaths();
181  return array_shift($templateRootPaths);
182  }
183 
189  public function getTemplateRootPaths() {
190  if ($this->templateRootPaths !== NULL) {
192  }
194  $actionRequest = $this->controllerContext->getRequest();
195  return array(str_replace('@packageResourcesPath', \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($actionRequest->getControllerExtensionKey()) . 'Resources/', $this->templateRootPathPattern));
196  }
197 
206  public function setTemplateRootPaths(array $templateRootPaths) {
207  $this->templateRootPaths = $templateRootPaths;
208  }
209 
219  public function setPartialRootPath($partialRootPath) {
220  $this->setPartialRootPaths(array($partialRootPath));
221  }
222 
227  protected function getPartialRootPath() {
229  $partialRootPaths = $this->getPartialRootPaths();
230  return array_shift($partialRootPaths);
231  }
232 
241  public function setPartialRootPaths(array $partialRootPaths) {
242  $this->partialRootPaths = $partialRootPaths;
243  }
244 
250  protected function getPartialRootPaths() {
251  if ($this->partialRootPaths !== NULL) {
253  }
255  $actionRequest = $this->controllerContext->getRequest();
256  return array(str_replace('@packageResourcesPath', \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($actionRequest->getControllerExtensionKey()) . 'Resources/', $this->partialRootPathPattern));
257  }
258 
268  public function setLayoutRootPath($layoutRootPath) {
269  $this->setLayoutRootPaths(array($layoutRootPath));
270  }
271 
280  public function setLayoutRootPaths(array $layoutRootPaths) {
281  $this->layoutRootPaths = $layoutRootPaths;
282  }
283 
288  protected function getLayoutRootPath() {
290  $layoutRootPaths = $this->getLayoutRootPaths();
291  return array_shift($layoutRootPaths);
292  }
293 
299  protected function getLayoutRootPaths() {
300  if ($this->layoutRootPaths !== NULL) {
301  return $this->layoutRootPaths;
302  }
304  $actionRequest = $this->controllerContext->getRequest();
305  return array(str_replace('@packageResourcesPath', \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($actionRequest->getControllerExtensionKey()) . 'Resources/', $this->layoutRootPathPattern));
306  }
307 
315  protected function getTemplateIdentifier($actionName = NULL) {
316  $templatePathAndFilename = $this->getTemplatePathAndFilename($actionName);
317  if ($actionName === NULL) {
319  $actionRequest = $this->controllerContext->getRequest();
320  $actionName = $actionRequest->getControllerActionName();
321  }
322  $prefix = 'action_' . $actionName;
323  return $this->createIdentifierForFile($templatePathAndFilename, $prefix);
324  }
325 
334  protected function getTemplateSource($actionName = NULL) {
335  $templatePathAndFilename = $this->getTemplatePathAndFilename($actionName);
336  $templateSource = file_get_contents($templatePathAndFilename);
337  if ($templateSource === FALSE) {
338  throw new Exception\InvalidTemplateResourceException('"' . $templatePathAndFilename . '" is not a valid template resource URI.', 1257246929);
339  }
340  return $templateSource;
341  }
342 
351  protected function getTemplatePathAndFilename($actionName = NULL) {
352  if ($this->templatePathAndFilename !== NULL) {
354  }
355  if ($actionName === NULL) {
357  $actionRequest = $this->controllerContext->getRequest();
358  $actionName = $actionRequest->getControllerActionName();
359  }
360  $actionName = ucfirst($actionName);
361 
362  $paths = $this->expandGenericPathPattern($this->templatePathAndFilenamePattern, FALSE, FALSE);
363  foreach ($paths as &$templatePathAndFilename) {
364  $templatePathAndFilename = $this->resolveFileNamePath(str_replace('@action', $actionName, $templatePathAndFilename));
365  if (is_file($templatePathAndFilename)) {
367  }
368  }
369  throw new Exception\InvalidTemplateResourceException('Template could not be loaded. I tried "' . implode('", "', $paths) . '"', 1225709595);
370  }
371 
379  protected function getLayoutIdentifier($layoutName = 'Default') {
380  $layoutPathAndFilename = $this->getLayoutPathAndFilename($layoutName);
381  $prefix = 'layout_' . $layoutName;
382  return $this->createIdentifierForFile($layoutPathAndFilename, $prefix);
383  }
384 
397  protected function getLayoutSource($layoutName = 'Default') {
398  $layoutPathAndFilename = $this->getLayoutPathAndFilename($layoutName);
399  $layoutSource = file_get_contents($layoutPathAndFilename);
400  if ($layoutSource === FALSE) {
401  throw new Exception\InvalidTemplateResourceException('"' . $layoutPathAndFilename . '" is not a valid template resource URI.', 1257246930);
402  }
403  return $layoutSource;
404  }
405 
418  protected function getLayoutPathAndFilename($layoutName = 'Default') {
419  if ($this->layoutPathAndFilename !== NULL) {
421  }
422  $paths = $this->expandGenericPathPattern($this->layoutPathAndFilenamePattern, TRUE, TRUE);
423  $layoutName = ucfirst($layoutName);
424  foreach ($paths as &$layoutPathAndFilename) {
425  $layoutPathAndFilename = $this->resolveFileNamePath(str_replace('@layout', $layoutName, $layoutPathAndFilename));
426  if (is_file($layoutPathAndFilename)) {
427  return $layoutPathAndFilename;
428  }
429  }
430  throw new Exception\InvalidTemplateResourceException('The template files "' . implode('", "', $paths) . '" could not be loaded.', 1225709596);
431  }
432 
440  protected function getPartialIdentifier($partialName) {
441  $partialPathAndFilename = $this->getPartialPathAndFilename($partialName);
442  $prefix = 'partial_' . $partialName;
443  return $this->createIdentifierForFile($partialPathAndFilename, $prefix);
444  }
445 
453  protected function getPartialSource($partialName) {
454  $partialPathAndFilename = $this->getPartialPathAndFilename($partialName);
455  $partialSource = file_get_contents($partialPathAndFilename);
456  if ($partialSource === FALSE) {
457  throw new Exception\InvalidTemplateResourceException('"' . $partialPathAndFilename . '" is not a valid template resource URI.', 1257246931);
458  }
459  return $partialSource;
460  }
461 
469  protected function getPartialPathAndFilename($partialName) {
470  $paths = $this->expandGenericPathPattern($this->partialPathAndFilenamePattern, TRUE, TRUE);
471  foreach ($paths as &$partialPathAndFilename) {
472  $partialPathAndFilename = $this->resolveFileNamePath(str_replace('@partial', $partialName, $partialPathAndFilename));
473  if (is_file($partialPathAndFilename)) {
474  return $partialPathAndFilename;
475  }
476  }
477  throw new Exception\InvalidTemplateResourceException('The template files "' . implode('", "', $paths) . '" could not be loaded.', 1225709597);
478  }
479 
488  $this->setControllerContext($controllerContext);
489  try {
490  $this->getTemplateSource();
491  return TRUE;
493  return FALSE;
494  }
495  }
496 
532  protected function expandGenericPathPattern($pattern, $bubbleControllerAndSubpackage, $formatIsOptional) {
533  $paths = array($pattern);
534  $this->expandPatterns($paths, '@templateRoot', $this->getTemplateRootPaths());
535  $this->expandPatterns($paths, '@partialRoot', $this->getPartialRootPaths());
536  $this->expandPatterns($paths, '@layoutRoot', $this->getLayoutRootPaths());
537 
539  $actionRequest = $this->controllerContext->getRequest();
540  $subpackageKey = $actionRequest->getControllerSubpackageKey();
541  $controllerName = $actionRequest->getControllerName();
542  if ($subpackageKey !== NULL) {
543  if (strpos($subpackageKey, \TYPO3\CMS\Fluid\Fluid::NAMESPACE_SEPARATOR) !== FALSE) {
544  $namespaceSeparator = \TYPO3\CMS\Fluid\Fluid::NAMESPACE_SEPARATOR;
545  } else {
547  }
548  $subpackageKeyParts = explode($namespaceSeparator, $subpackageKey);
549  } else {
550  $subpackageKeyParts = array();
551  }
552  if ($bubbleControllerAndSubpackage) {
553  $numberOfPathsBeforeSubpackageExpansion = count($paths);
554  $numberOfSubpackageParts = count($subpackageKeyParts);
555  $subpackageReplacements = array();
556  for ($i = 0; $i <= $numberOfSubpackageParts; $i++) {
557  $subpackageReplacements[] = implode('/', ($i < 0 ? $subpackageKeyParts : array_slice($subpackageKeyParts, $i)));
558  }
559  $this->expandPatterns($paths, '@subpackage', $subpackageReplacements);
560 
561  for ($i = ($numberOfPathsBeforeSubpackageExpansion - 1) * ($numberOfSubpackageParts + 1); $i >= 0; $i -= ($numberOfSubpackageParts + 1)) {
562  array_splice($paths, $i, 0, str_replace('@controller', $controllerName, $paths[$i]));
563  }
564  $this->expandPatterns($paths, '@controller', array(''));
565  } else {
566  $i = $controllerName === NULL ? 0 : -1;
567  $this->expandPatterns($paths, '@subpackage', array(implode('/', $i < 0 ? $subpackageKeyParts :
568  array_slice($subpackageKeyParts, $i))));
569  $this->expandPatterns($paths, '@controller', array($controllerName));
570  }
571 
572  if ($formatIsOptional) {
573  $this->expandPatterns($paths, '.@format', array('.' . $actionRequest->getFormat(), ''));
574  $this->expandPatterns($paths, '@format', array($actionRequest->getFormat(), ''));
575  } else {
576  $this->expandPatterns($paths, '.@format', array('.' . $actionRequest->getFormat()));
577  $this->expandPatterns($paths, '@format', array($actionRequest->getFormat()));
578  }
579  return array_values(array_unique($paths));
580  }
581 
591  protected function expandPatterns(array &$patterns, $search, array $replacements) {
592  $patternsWithReplacements = array();
593  foreach ($patterns as $pattern) {
594  foreach ($replacements as $replacement) {
595  $patternsWithReplacements[] = GeneralUtility::fixWindowsFilePath(str_replace($search, $replacement, $pattern));
596  }
597  }
598  $patterns = $patternsWithReplacements;
599  }
600 
610  protected function createIdentifierForFile($pathAndFilename, $prefix) {
612  $actionRequest = $this->controllerContext->getRequest();
613  $extensionName = $actionRequest->getControllerExtensionName();
614  $subPackageKey = $actionRequest->getControllerSubpackageKey();
615  if ($subPackageKey !== NULL) {
616  $extensionName .= '_' . $subPackageKey;
617  }
618  $controllerName = $actionRequest->getControllerName();
619  $templateModifiedTimestamp = filemtime($pathAndFilename);
620  $templateIdentifier = sprintf('%s_%s_%s_%s', $extensionName, $controllerName, $prefix, sha1($pathAndFilename . '|' . $templateModifiedTimestamp));
621  return $templateIdentifier;
622  }
623 
631  protected function resolveFileNamePath($pathAndFilename) {
632  return GeneralUtility::getFileAbsFileName($pathAndFilename);
633  }
634 }
setTemplatePathAndFilename($templatePathAndFilename)
setTemplateRootPath($templateRootPath)
setPartialRootPath($partialRootPath)
canRender(ControllerContext $controllerContext)
setRenderingContext(\TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
const NAMESPACE_SEPARATOR
Definition: Fluid.php:19
resolveFileNamePath($pathAndFilename)
getLayoutPathAndFilename($layoutName='Default')
getLayoutIdentifier($layoutName='Default')
setPartialRootPaths(array $partialRootPaths)
getLayoutSource($layoutName='Default')
setControllerContext(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext)
expandPatterns(array &$patterns, $search, array $replacements)
const LEGACY_NAMESPACE_SEPARATOR
Definition: Fluid.php:18
setLayoutPathAndFilename($layoutPathAndFilename)
setTemplateRootPaths(array $templateRootPaths)
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)
setLayoutRootPaths(array $layoutRootPaths)