TYPO3 CMS  TYPO3_7-6
StandaloneView.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\View;
3 
36 
44 {
50  protected $templateSource = null;
51 
57  protected $templatePathAndFilename = null;
58 
64  protected $templateRootPaths = null;
65 
71  protected $partialRootPaths = null;
72 
78  protected $layoutRootPaths = null;
79 
87  public function __construct(ContentObjectRenderer $contentObject = null)
88  {
89  $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
91  $configurationManager = $this->objectManager->get(ConfigurationManagerInterface::class);
92  if ($contentObject === null) {
94  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
95  }
96  $configurationManager->setContentObject($contentObject);
97  $this->templateParser = $this->objectManager->get(TemplateParser::class);
98  $this->setRenderingContext($this->objectManager->get(RenderingContext::class));
100  $request = $this->objectManager->get(WebRequest::class);
101  $request->setRequestURI(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
102  $request->setBaseURI(GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
104  $uriBuilder = $this->objectManager->get(UriBuilder::class);
105  $uriBuilder->setRequest($request);
107  $controllerContext = $this->objectManager->get(ControllerContext::class);
108  $controllerContext->setRequest($request);
109  $controllerContext->setUriBuilder($uriBuilder);
111  $this->templateCompiler = $this->objectManager->get(TemplateCompiler::class);
112  // singleton
113  $this->templateCompiler->setTemplateCache(GeneralUtility::makeInstance(CacheManager::class)->getCache('fluid_template'));
114  }
115 
123  public function setFormat($format)
124  {
125  $this->getRequest()->setFormat($format);
126  }
127 
134  public function getFormat()
135  {
136  return $this->getRequest()->getFormat();
137  }
138 
144  public function getRequest()
145  {
146  return $this->controllerContext->getRequest();
147  }
148 
157  {
158  $this->templatePathAndFilename = $templatePathAndFilename;
159  }
160 
167  public function getTemplatePathAndFilename()
168  {
170  }
171 
181  {
182  $this->templateSource = $templateSource;
183  }
184 
193  {
194  $this->templateRootPaths = $templateRootPaths;
195  }
196 
205  public function setTemplate($templateName)
206  {
207  if ($this->templateRootPaths === null) {
208  throw new InvalidTemplateResourceException('No template root path has been specified. Use setTemplateRootPaths().', 1430635895);
209  }
210  $format = $this->getRequest()->getFormat();
212  $possibleTemplatePaths = $this->buildListOfTemplateCandidates($templateName, $this->templateRootPaths, $format);
213  foreach ($possibleTemplatePaths as $possibleTemplatePath) {
214  if ($this->testFileExistence($possibleTemplatePath)) {
215  $templatePathAndFilename = $possibleTemplatePath;
216  break;
217  }
218  }
219  if ($templatePathAndFilename !== null) {
221  } else {
222  throw new InvalidTemplateResourceException('Could not load template file. Tried following paths: "' . implode('", "', $possibleTemplatePaths) . '".', 1430635896);
223  }
224  }
225 
235  public function setLayoutRootPath($layoutRootPath)
236  {
238  $this->setLayoutRootPaths([$layoutRootPath]);
239  }
240 
248  public function setLayoutRootPaths(array $layoutRootPaths)
249  {
250  $this->layoutRootPaths = $layoutRootPaths;
251  }
252 
261  public function getLayoutRootPath()
262  {
265  return array_shift($layoutRootPaths);
266  }
267 
275  public function getLayoutRootPaths()
276  {
277  if ($this->layoutRootPaths === null && $this->templatePathAndFilename === null) {
278  throw new InvalidTemplateResourceException('No layout root path has been specified. Use setLayoutRootPaths().', 1288091419);
279  }
280  if ($this->layoutRootPaths === null) {
281  $this->layoutRootPaths = [dirname($this->templatePathAndFilename) . '/Layouts'];
282  }
283  return $this->layoutRootPaths;
284  }
285 
296  public function setPartialRootPath($partialRootPath)
297  {
299  $this->setPartialRootPaths([$partialRootPath]);
300  }
301 
310  public function getPartialRootPath()
311  {
314  return array_shift($partialRootPaths);
315  }
316 
325  public function setPartialRootPaths(array $partialRootPaths)
326  {
327  $this->partialRootPaths = $partialRootPaths;
328  }
329 
337  public function getPartialRootPaths()
338  {
339  if ($this->partialRootPaths === null && $this->templatePathAndFilename === null) {
340  throw new InvalidTemplateResourceException('No partial root path has been specified. Use setPartialRootPaths().', 1288094511);
341  }
342  if ($this->partialRootPaths === null) {
343  $this->partialRootPaths = [dirname($this->templatePathAndFilename) . '/Partials'];
344  }
346  }
347 
354  public function hasTemplate()
355  {
356  try {
357  $this->getTemplateSource();
358  return true;
359  } catch (InvalidTemplateResourceException $e) {
360  return false;
361  }
362  }
363 
372  protected function getTemplateIdentifier($actionName = null)
373  {
374  if ($this->templateSource === null) {
376  $templatePathAndFilenameInfo = pathinfo($templatePathAndFilename);
377  $templateFilenameWithoutExtension = basename($templatePathAndFilename, '.' . $templatePathAndFilenameInfo['extension']);
378  $prefix = sprintf('template_file_%s', $templateFilenameWithoutExtension);
379  return $this->createIdentifierForFile($templatePathAndFilename, $prefix);
380  } else {
382  $prefix = 'template_source';
383  $templateIdentifier = sprintf('Standalone_%s_%s', $prefix, sha1($templateSource));
384  return $templateIdentifier;
385  }
386  }
387 
395  protected function getTemplateSource($actionName = null)
396  {
397  if ($this->templateSource === null && $this->templatePathAndFilename === null) {
398  throw new InvalidTemplateResourceException('No template has been specified. Use either setTemplateSource() or setTemplatePathAndFilename().', 1288085266);
399  }
400  if ($this->templateSource === null) {
401  if (!$this->testFileExistence($this->templatePathAndFilename)) {
402  throw new InvalidTemplateResourceException('Template could not be found at "' . $this->templatePathAndFilename . '".', 1288087061);
403  }
404  $this->templateSource = file_get_contents($this->templatePathAndFilename);
405  }
406  return $this->templateSource;
407  }
408 
417  protected function getLayoutIdentifier($layoutName = 'Default')
418  {
419  $layoutPathAndFilename = $this->getLayoutPathAndFilename($layoutName);
420  $prefix = 'layout_' . $layoutName;
421  return $this->createIdentifierForFile($layoutPathAndFilename, $prefix);
422  }
423 
432  protected function getLayoutSource($layoutName = 'Default')
433  {
434  $layoutPathAndFilename = $this->getLayoutPathAndFilename($layoutName);
435  $layoutSource = file_get_contents($layoutPathAndFilename);
436  if ($layoutSource === false) {
437  throw new InvalidTemplateResourceException('"' . $layoutPathAndFilename . '" is not a valid template resource URI.', 1312215888);
438  }
439  return $layoutSource;
440  }
441 
454  protected function getLayoutPathAndFilename($layoutName = 'Default')
455  {
456  $possibleLayoutPaths = $this->buildListOfTemplateCandidates($layoutName, $this->getLayoutRootPaths(), $this->getRequest()->getFormat());
457  foreach ($possibleLayoutPaths as $layoutPathAndFilename) {
458  if ($this->testFileExistence($layoutPathAndFilename)) {
459  return $layoutPathAndFilename;
460  }
461  }
462 
463  throw new InvalidTemplateResourceException('Could not load layout file. Tried following paths: "' . implode('", "', $possibleLayoutPaths) . '".', 1288092555);
464  }
465 
474  protected function getPartialIdentifier($partialName)
475  {
476  $partialPathAndFilename = $this->getPartialPathAndFilename($partialName);
477  $prefix = 'partial_' . $partialName;
478  return $this->createIdentifierForFile($partialPathAndFilename, $prefix);
479  }
480 
489  protected function getPartialSource($partialName)
490  {
491  $partialPathAndFilename = $this->getPartialPathAndFilename($partialName);
492  $partialSource = file_get_contents($partialPathAndFilename);
493  if ($partialSource === false) {
494  throw new InvalidTemplateResourceException('"' . $partialPathAndFilename . '" is not a valid template resource URI.', 1257246932);
495  }
496  return $partialSource;
497  }
498 
506  protected function getPartialPathAndFilename($partialName)
507  {
508  $possiblePartialPaths = $this->buildListOfTemplateCandidates($partialName, $this->getPartialRootPaths(), $this->getRequest()->getFormat());
509  foreach ($possiblePartialPaths as $partialPathAndFilename) {
510  if ($this->testFileExistence($partialPathAndFilename)) {
511  return $partialPathAndFilename;
512  }
513  }
514  throw new InvalidTemplateResourceException('Could not load partial file. Tried following paths: "' . implode('", "', $possiblePartialPaths) . '".', 1288092556);
515  }
516 
525  protected function buildListOfTemplateCandidates($templateName, array $paths, $format)
526  {
527  $upperCasedTemplateName = $this->ucFileNameInPath($templateName);
528  $possibleTemplatePaths = [];
530  $paths = array_reverse($paths, true);
531  foreach ($paths as $layoutRootPath) {
532  $possibleTemplatePaths[] = $this->resolveFileNamePath($layoutRootPath . '/' . $upperCasedTemplateName . '.' . $format);
533  $possibleTemplatePaths[] = $this->resolveFileNamePath($layoutRootPath . '/' . $upperCasedTemplateName);
534  if ($upperCasedTemplateName !== $templateName) {
535  $possibleTemplatePaths[] = $this->resolveFileNamePath($layoutRootPath . '/' . $templateName . '.' . $format);
536  $possibleTemplatePaths[] = $this->resolveFileNamePath($layoutRootPath . '/' . $templateName);
537  }
538  }
539  return $possibleTemplatePaths;
540  }
541 
551  protected function createIdentifierForFile($pathAndFilename, $prefix)
552  {
553  $templateModifiedTimestamp = filemtime($pathAndFilename);
554  $templateIdentifier = sprintf('Standalone_%s_%s', $prefix, sha1($pathAndFilename . '|' . $templateModifiedTimestamp));
555  $templateIdentifier = str_replace('/', '_', str_replace('.', '_', $templateIdentifier));
556  return $templateIdentifier;
557  }
558 
565  protected function resolveFileNamePath($pathAndFilename)
566  {
568  }
569 }
setPartialRootPaths(array $partialRootPaths)
buildListOfTemplateCandidates($templateName, array $paths, $format)
setTemplatePathAndFilename($templatePathAndFilename)
setTemplateRootPaths(array $templateRootPaths)
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')
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)