[CLEANUP] Adjust code to CGL and fix small namespace bugs
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / View / TemplateView.php
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 /**
14 * The main template view. Should be used as view if you want Fluid Templating
15 *
16 * @api
17 */
18 class TemplateView extends \TYPO3\CMS\Fluid\View\AbstractTemplateView {
19
20 /**
21 * Pattern to be resolved for "@templateRoot" in the other patterns.
22 *
23 * @var string
24 */
25 protected $templateRootPathPattern = '@packageResourcesPath/Private/Templates';
26
27 /**
28 * Pattern to be resolved for "@partialRoot" in the other patterns.
29 *
30 * @var string
31 */
32 protected $partialRootPathPattern = '@packageResourcesPath/Private/Partials';
33
34 /**
35 * Pattern to be resolved for "@layoutRoot" in the other patterns.
36 *
37 * @var string
38 */
39 protected $layoutRootPathPattern = '@packageResourcesPath/Private/Layouts';
40
41 /**
42 * Path to the template root. If NULL, then $this->templateRootPathPattern will be used.
43 *
44 * @var string
45 */
46 protected $templateRootPath = NULL;
47
48 /**
49 * Path to the partial root. If NULL, then $this->partialRootPathPattern will be used.
50 *
51 * @var string
52 */
53 protected $partialRootPath = NULL;
54
55 /**
56 * Path to the layout root. If NULL, then $this->layoutRootPathPattern will be used.
57 *
58 * @var string
59 */
60 protected $layoutRootPath = NULL;
61
62 /**
63 * File pattern for resolving the template file
64 *
65 * @var string
66 */
67 protected $templatePathAndFilenamePattern = '@templateRoot/@subpackage/@controller/@action.@format';
68
69 /**
70 * Directory pattern for global partials. Not part of the public API, should not be changed for now.
71 *
72 * @var string
73 */
74 private $partialPathAndFilenamePattern = '@partialRoot/@subpackage/@partial.@format';
75
76 /**
77 * File pattern for resolving the layout
78 *
79 * @var string
80 */
81 protected $layoutPathAndFilenamePattern = '@layoutRoot/@layout.@format';
82
83 /**
84 * Path and filename of the template file. If set, overrides the templatePathAndFilenamePattern
85 *
86 * @var string
87 */
88 protected $templatePathAndFilename = NULL;
89
90 /**
91 * Path and filename of the layout file. If set, overrides the layoutPathAndFilenamePattern
92 *
93 * @var string
94 */
95 protected $layoutPathAndFilename = NULL;
96
97 public function __construct() {
98 $this->injectTemplateParser(\TYPO3\CMS\Fluid\Compatibility\TemplateParserBuilder::build());
99 $this->injectObjectManager(\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'));
100 $this->setRenderingContext($this->objectManager->create('TYPO3\\CMS\\Fluid\\Core\\Rendering\\RenderingContextInterface'));
101 }
102
103 public function initializeView() {
104 }
105
106 // Here, the backporter can insert a constructor method, which is needed for Fluid v4.
107 /**
108 * Sets the path and name of of the template file. Effectively overrides the
109 * dynamic resolving of a template file.
110 *
111 * @param string $templatePathAndFilename Template file path
112 * @return void
113 * @api
114 */
115 public function setTemplatePathAndFilename($templatePathAndFilename) {
116 $this->templatePathAndFilename = $templatePathAndFilename;
117 }
118
119 /**
120 * Sets the path and name of the layout file. Overrides the dynamic resolving of the layout file.
121 *
122 * @param string $layoutPathAndFilename Path and filename of the layout file
123 * @return void
124 * @api
125 */
126 public function setLayoutPathAndFilename($layoutPathAndFilename) {
127 $this->layoutPathAndFilename = $layoutPathAndFilename;
128 }
129
130 /**
131 * Checks whether a template can be resolved for the current request context.
132 *
133 * @param \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext Controller context which is available inside the view
134 * @return boolean
135 * @api
136 */
137 public function canRender(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext) {
138 $this->setControllerContext($controllerContext);
139 try {
140 $this->getTemplateSource();
141 return TRUE;
142 } catch (\TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException $e) {
143 return FALSE;
144 }
145 }
146
147 /**
148 * Set the root path to the templates.
149 * If set, overrides the one determined from $this->templateRootPathPattern
150 *
151 * @param string $templateRootPath Root path to the templates. If set, overrides the one determined from $this->templateRootPathPattern
152 * @return void
153 * @api
154 */
155 public function setTemplateRootPath($templateRootPath) {
156 $this->templateRootPath = $templateRootPath;
157 }
158
159 /**
160 * Returns a unique identifier for the resolved template file
161 * This identifier is based on the template path and last modification date
162 *
163 * @param string $actionName Name of the action. If NULL, will be taken from request.
164 * @return string template identifier
165 */
166 protected function getTemplateIdentifier($actionName = NULL) {
167 $templatePathAndFilename = $this->getTemplatePathAndFilename($actionName);
168 if ($actionName === NULL) {
169 $actionName = $this->controllerContext->getRequest()->getControllerActionName();
170 }
171 $prefix = 'action_' . $actionName;
172 return $this->createIdentifierForFile($templatePathAndFilename, $prefix);
173 }
174
175 /**
176 * Resolve the template path and filename for the given action. If $actionName
177 * is NULL, looks into the current request.
178 *
179 * @param string $actionName Name of the action. If NULL, will be taken from request.
180 * @return string Full path to template
181 * @throws \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
182 */
183 protected function getTemplateSource($actionName = NULL) {
184 $templatePathAndFilename = $this->getTemplatePathAndFilename($actionName);
185 $templateSource = file_get_contents($templatePathAndFilename);
186 if ($templateSource === FALSE) {
187 throw new \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException('"' . $templatePathAndFilename . '" is not a valid template resource URI.', 1257246929);
188 }
189 return $templateSource;
190 }
191
192 /**
193 * Resolve the template path and filename for the given action. If $actionName
194 * is NULL, looks into the current request.
195 *
196 * @param string $actionName Name of the action. If NULL, will be taken from request.
197 * @return string Full path to template
198 * @throws \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
199 */
200 protected function getTemplatePathAndFilename($actionName = NULL) {
201 if ($this->templatePathAndFilename !== NULL) {
202 return $this->templatePathAndFilename;
203 }
204 if ($actionName === NULL) {
205 $actionName = $this->controllerContext->getRequest()->getControllerActionName();
206 }
207 $actionName = ucfirst($actionName);
208 $paths = $this->expandGenericPathPattern($this->templatePathAndFilenamePattern, FALSE, FALSE);
209 foreach ($paths as &$templatePathAndFilename) {
210 $templatePathAndFilename = str_replace('@action', $actionName, $templatePathAndFilename);
211 if (file_exists($templatePathAndFilename)) {
212 return $templatePathAndFilename;
213 }
214 }
215 throw new \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException('Template could not be loaded. I tried "' . implode('", "', $paths) . '"', 1225709595);
216 }
217
218 /**
219 * Returns a unique identifier for the resolved layout file.
220 * This identifier is based on the template path and last modification date
221 *
222 * @param string $layoutName The name of the layout
223 * @return string layout identifier
224 */
225 protected function getLayoutIdentifier($layoutName = 'Default') {
226 $layoutPathAndFilename = $this->getLayoutPathAndFilename($layoutName);
227 $prefix = 'layout_' . $layoutName;
228 return $this->createIdentifierForFile($layoutPathAndFilename, $prefix);
229 }
230
231 /**
232 * Resolve the path and file name of the layout file, based on
233 * $this->layoutPathAndFilename and $this->layoutPathAndFilenamePattern.
234 *
235 * In case a layout has already been set with setLayoutPathAndFilename(),
236 * this method returns that path, otherwise a path and filename will be
237 * resolved using the layoutPathAndFilenamePattern.
238 *
239 * @param string $layoutName Name of the layout to use. If none given, use "Default
240 * @return string contents of the layout template
241 * @throws \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
242 */
243 protected function getLayoutSource($layoutName = 'Default') {
244 $layoutPathAndFilename = $this->getLayoutPathAndFilename($layoutName);
245 $layoutSource = file_get_contents($layoutPathAndFilename);
246 if ($layoutSource === FALSE) {
247 throw new \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException('"' . $layoutPathAndFilename . '" is not a valid template resource URI.', 1257246929);
248 }
249 return $layoutSource;
250 }
251
252 /**
253 * Resolve the path and file name of the layout file, based on
254 * $this->layoutPathAndFilename and $this->layoutPathAndFilenamePattern.
255 *
256 * In case a layout has already been set with setLayoutPathAndFilename(),
257 * this method returns that path, otherwise a path and filename will be
258 * resolved using the layoutPathAndFilenamePattern.
259 *
260 * @param string $layoutName Name of the layout to use. If none given, use "Default
261 * @return string Path and filename of layout files
262 * @throws \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
263 */
264 protected function getLayoutPathAndFilename($layoutName = 'Default') {
265 if ($this->layoutPathAndFilename !== NULL) {
266 return $this->layoutPathAndFilename;
267 }
268 $paths = $this->expandGenericPathPattern($this->layoutPathAndFilenamePattern, TRUE, TRUE);
269 $layoutName = ucfirst($layoutName);
270 foreach ($paths as &$layoutPathAndFilename) {
271 $layoutPathAndFilename = str_replace('@layout', $layoutName, $layoutPathAndFilename);
272 if (file_exists($layoutPathAndFilename)) {
273 return $layoutPathAndFilename;
274 }
275 }
276 throw new \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException('The template files "' . implode('", "', $paths) . '" could not be loaded.', 1225709595);
277 }
278
279 /**
280 * Returns a unique identifier for the resolved partial file.
281 * This identifier is based on the template path and last modification date
282 *
283 * @param string $partialName The name of the partial
284 * @return string partial identifier
285 */
286 protected function getPartialIdentifier($partialName) {
287 $partialPathAndFilename = $this->getPartialPathAndFilename($partialName);
288 $prefix = 'partial_' . $partialName;
289 return $this->createIdentifierForFile($partialPathAndFilename, $prefix);
290 }
291
292 /**
293 * Figures out which partial to use.
294 *
295 * @param string $partialName The name of the partial
296 * @return string contents of the partial template
297 * @throws \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
298 */
299 protected function getPartialSource($partialName) {
300 $partialPathAndFilename = $this->getPartialPathAndFilename($partialName);
301 $partialSource = file_get_contents($partialPathAndFilename);
302 if ($partialSource === FALSE) {
303 throw new \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException('"' . $partialPathAndFilename . '" is not a valid template resource URI.', 1257246929);
304 }
305 return $partialSource;
306 }
307
308 /**
309 * Resolve the partial path and filename based on $this->partialPathAndFilenamePattern.
310 *
311 * @param string $partialName The name of the partial
312 * @return string the full path which should be used. The path definitely exists.
313 * @throws \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException
314 */
315 protected function getPartialPathAndFilename($partialName) {
316 $paths = $this->expandGenericPathPattern($this->partialPathAndFilenamePattern, TRUE, TRUE);
317 foreach ($paths as &$partialPathAndFilename) {
318 $partialPathAndFilename = str_replace('@partial', $partialName, $partialPathAndFilename);
319 if (file_exists($partialPathAndFilename)) {
320 return $partialPathAndFilename;
321 }
322 }
323 throw new \TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException('The template files "' . implode('", "', $paths) . '" could not be loaded.', 1225709595);
324 }
325
326 /**
327 * Resolves the template root to be used inside other paths.
328 *
329 * @return string Path to template root directory
330 */
331 protected function getTemplateRootPath() {
332 if ($this->templateRootPath !== NULL) {
333 return $this->templateRootPath;
334 } else {
335 return str_replace('@packageResourcesPath', \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($this->controllerContext->getRequest()->getControllerExtensionKey()) . 'Resources/', $this->templateRootPathPattern);
336 }
337 }
338
339 /**
340 * Set the root path to the partials.
341 * If set, overrides the one determined from $this->partialRootPathPattern
342 *
343 * @param string $partialRootPath Root path to the partials. If set, overrides the one determined from $this->partialRootPathPattern
344 * @return void
345 * @api
346 */
347 public function setPartialRootPath($partialRootPath) {
348 $this->partialRootPath = $partialRootPath;
349 }
350
351 /**
352 * Resolves the partial root to be used inside other paths.
353 *
354 * @return string Path to partial root directory
355 */
356 protected function getPartialRootPath() {
357 if ($this->partialRootPath !== NULL) {
358 return $this->partialRootPath;
359 } else {
360 return str_replace('@packageResourcesPath', \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($this->controllerContext->getRequest()->getControllerExtensionKey()) . 'Resources/', $this->partialRootPathPattern);
361 }
362 }
363
364 /**
365 * Set the root path to the layouts.
366 * If set, overrides the one determined from $this->layoutRootPathPattern
367 *
368 * @param string $layoutRootPath Root path to the layouts. If set, overrides the one determined from $this->layoutRootPathPattern
369 * @return void
370 * @api
371 */
372 public function setLayoutRootPath($layoutRootPath) {
373 $this->layoutRootPath = $layoutRootPath;
374 }
375
376 /**
377 * Resolves the layout root to be used inside other paths.
378 *
379 * @return string Path to layout root directory
380 */
381 protected function getLayoutRootPath() {
382 if ($this->layoutRootPath !== NULL) {
383 return $this->layoutRootPath;
384 } else {
385 return str_replace('@packageResourcesPath', \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($this->controllerContext->getRequest()->getControllerExtensionKey()) . 'Resources/', $this->layoutRootPathPattern);
386 }
387 }
388
389 /**
390 * Processes "@templateRoot", "@subpackage", "@controller", and "@format" placeholders inside $pattern.
391 * This method is used to generate "fallback chains" for file system locations where a certain Partial can reside.
392 *
393 * If $bubbleControllerAndSubpackage is FALSE and $formatIsOptional is FALSE, then the resulting array will only have one element
394 * with all the above placeholders replaced.
395 *
396 * If you set $bubbleControllerAndSubpackage to TRUE, then you will get an array with potentially many elements:
397 * The first element of the array is like above. The second element has the @ controller part set to "" (the empty string)
398 * The third element now has the @ controller part again stripped off, and has the last subpackage part stripped off as well.
399 * This continues until both "@subpackage" and "@controller" are empty.
400 *
401 * Example for $bubbleControllerAndSubpackage is TRUE, we have the Tx_MyExtension_MySubPackage_Controller_MyController
402 * as Controller Object Name and the current format is "html"
403 *
404 * If pattern is "@templateRoot/@subpackage/@controller/@action.@format", then the resulting array is:
405 * - "Resources/Private/Templates/MySubPackage/My/@action.html"
406 * - "Resources/Private/Templates/MySubPackage/@action.html"
407 * - "Resources/Private/Templates/@action.html"
408 *
409 * If you set $formatIsOptional to TRUE, then for any of the above arrays, every element will be duplicated - once with "@format"
410 * replaced by the current request format, and once with ."@format" stripped off.
411 *
412 * @param string $pattern Pattern to be resolved
413 * @param boolean $bubbleControllerAndSubpackage if TRUE, then we successively split off parts from "@controller" and "@subpackage" until both are empty.
414 * @param boolean $formatIsOptional if TRUE, then half of the resulting strings will have ."@format" stripped off, and the other half will have it.
415 * @return array unix style path
416 */
417 protected function expandGenericPathPattern($pattern, $bubbleControllerAndSubpackage, $formatIsOptional) {
418 $pattern = str_replace('@templateRoot', $this->getTemplateRootPath(), $pattern);
419 $pattern = str_replace('@partialRoot', $this->getPartialRootPath(), $pattern);
420 $pattern = str_replace('@layoutRoot', $this->getLayoutRootPath(), $pattern);
421 $subpackageKey = $this->controllerContext->getRequest()->getControllerSubpackageKey();
422 $controllerName = $this->controllerContext->getRequest()->getControllerName();
423 if ($subpackageKey !== NULL) {
424 if (strpos($subpackageKey, \TYPO3\CMS\Fluid\Fluid::NAMESPACE_SEPARATOR) !== FALSE) {
425 $namespaceSeparator = \TYPO3\CMS\Fluid\Fluid::NAMESPACE_SEPARATOR;
426 } else {
427 $namespaceSeparator = \TYPO3\CMS\Fluid\Fluid::LEGACY_NAMESPACE_SEPARATOR;
428 }
429 $subpackageParts = explode($namespaceSeparator, $subpackageKey);
430 } else {
431 $subpackageParts = array();
432 }
433 $results = array();
434 $i = $controllerName === NULL ? 0 : -1;
435 do {
436 $temporaryPattern = $pattern;
437 if ($i < 0) {
438 $temporaryPattern = str_replace('@controller', $controllerName, $temporaryPattern);
439 } else {
440 $temporaryPattern = str_replace('//', '/', str_replace('@controller', '', $temporaryPattern));
441 }
442 $temporaryPattern = str_replace('@subpackage', implode('/', $i < 0 ? $subpackageParts : array_slice($subpackageParts, $i)), $temporaryPattern);
443 $results[] = \TYPO3\CMS\Core\Utility\GeneralUtility::fixWindowsFilePath(str_replace('@format', $this->controllerContext->getRequest()->getFormat(), $temporaryPattern));
444 if ($formatIsOptional) {
445 $results[] = \TYPO3\CMS\Core\Utility\GeneralUtility::fixWindowsFilePath(str_replace('.@format', '', $temporaryPattern));
446 }
447 } while ($i++ < count($subpackageParts) && $bubbleControllerAndSubpackage);
448 return $results;
449 }
450
451 /**
452 * Returns a unique identifier for the given file in the format
453 * <PackageKey>_<SubPackageKey>_<ControllerName>_<prefix>_<SHA1>
454 * The SH1 hash is a checksum that is based on the file path and last modification date
455 *
456 * @param string $pathAndFilename
457 * @param string $prefix
458 * @return string
459 */
460 protected function createIdentifierForFile($pathAndFilename, $prefix) {
461 $request = $this->controllerContext->getRequest();
462 $extensionName = $request->getControllerExtensionName();
463 $subPackageKey = $request->getControllerSubpackageKey();
464 if ($subPackageKey !== NULL) {
465 $extensionName .= '_' . $subPackageKey;
466 }
467 $controllerName = $request->getControllerName();
468 $templateModifiedTimestamp = filemtime($pathAndFilename);
469 $templateIdentifier = sprintf('%s_%s_%s_%s', $extensionName, $controllerName, $prefix, sha1($pathAndFilename . '|' . $templateModifiedTimestamp));
470 return $templateIdentifier;
471 }
472 }
473
474 ?>