[BUGFIX] Use late static binding for compilable viewhelpers
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Uri / ResourceViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
3
4 /* *
5 * This script is part of the TYPO3 project - inspiring people to share! *
6 * *
7 * TYPO3 is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License version 2 as published by *
9 * the Free Software Foundation. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14 * Public License for more details. *
15 * */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
20
21 /**
22 * A view helper for creating URIs to resources.
23 *
24 * = Examples =
25 *
26 * <code title="Defaults">
27 * <link href="{f:uri.resource(path:'css/stylesheet.css')}" rel="stylesheet" />
28 * </code>
29 * <output>
30 * <link href="Resources/Packages/MyPackage/stylesheet.css" rel="stylesheet" />
31 * (depending on current package)
32 * </output>
33 */
34 class ResourceViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper implements CompilableInterface {
35
36 /**
37 * Render the URI to the resource. The filename is used from child content.
38 *
39 * @param string $path The path and filename of the resource (relative to Public resource directory of the extension).
40 * @param string $extensionName Target extension name. If not set, the current extension name will be used
41 * @param bool $absolute If set, an absolute URI is rendered
42 * @return string The URI to the resource
43 * @api
44 */
45 public function render($path, $extensionName = NULL, $absolute = FALSE) {
46 return static::renderStatic(
47 array(
48 'path' => $path,
49 'extensionName' => $extensionName,
50 'absolute' => $absolute
51 ),
52 $this->buildRenderChildrenClosure(),
53 $this->renderingContext
54 );
55 }
56
57 /**
58 * @param array $arguments
59 * @param callable $renderChildrenClosure
60 * @param RenderingContextInterface $renderingContext
61 * @return string
62 */
63 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
64 $path = $arguments['path'];
65 $extensionName = $arguments['extensionName'];
66 $absolute = $arguments['absolute'];
67
68 if ($extensionName === NULL) {
69 $extensionName = $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName();
70 }
71 $uri = 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/Resources/Public/' . $path;
72 $uri = GeneralUtility::getFileAbsFileName($uri);
73 $uri = \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($uri);
74 if (TYPO3_MODE === 'BE' && $absolute === FALSE && $uri !== FALSE) {
75 $uri = '../' . $uri;
76 }
77 if ($absolute === TRUE) {
78 $uri = $renderingContext->getControllerContext()->getRequest()->getBaseUri() . $uri;
79 }
80 return $uri;
81 }
82
83 }