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