187ef7338c752eac0e4c92122c6b46018ce24814
[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 use TYPO3\CMS\Core\Utility\GeneralUtility;
17 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
19
20 /**
21 * A view helper for creating URIs to resources.
22 *
23 * = Examples =
24 *
25 * <code title="Defaults">
26 * <link href="{f:uri.resource(path:'css/stylesheet.css')}" rel="stylesheet" />
27 * </code>
28 * <output>
29 * <link href="Resources/Packages/MyPackage/stylesheet.css" rel="stylesheet" />
30 * (depending on current package)
31 * </output>
32 */
33 class ResourceViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper implements CompilableInterface {
34
35 /**
36 * Render the URI to the resource. The filename is used from child content.
37 *
38 * @param string $path The path and filename of the resource (relative to Public resource directory of the extension).
39 * @param string $extensionName Target extension name. If not set, the current extension name will be used
40 * @param bool $absolute If set, an absolute URI is rendered
41 * @return string The URI to the resource
42 * @api
43 */
44 public function render($path, $extensionName = NULL, $absolute = FALSE) {
45 return self::renderStatic(
46 array(
47 'path' => $path,
48 'extensionName' => $extensionName,
49 'absolute' => $absolute
50 ),
51 $this->buildRenderChildrenClosure(),
52 $this->renderingContext
53 );
54 }
55
56 /**
57 * @param array $arguments
58 * @param callable $renderChildrenClosure
59 * @param RenderingContextInterface $renderingContext
60 * @return string
61 */
62 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
63 $path = $arguments['path'];
64 $extensionName = $arguments['extensionName'];
65 $absolute = $arguments['absolute'];
66
67 if ($extensionName === NULL) {
68 $extensionName = $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName();
69 }
70 $uri = 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/Resources/Public/' . $path;
71 $uri = GeneralUtility::getFileAbsFileName($uri);
72 $uri = \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($uri);
73 if (TYPO3_MODE === 'BE' && $absolute === FALSE && $uri !== FALSE) {
74 $uri = '../' . $uri;
75 }
76 if ($absolute === TRUE) {
77 $uri = $renderingContext->getControllerContext()->getRequest()->getBaseUri() . $uri;
78 }
79 return $uri;
80 }
81
82 }