[TASK] Streamline phpdoc annotations in EXT:fluid
[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 file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Utility\PathUtility;
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
22
23 /**
24 * A view helper for creating URIs to resources.
25 *
26 * = Examples =
27 *
28 * <code title="Defaults">
29 * <link href="{f:uri.resource(path:'css/stylesheet.css')}" rel="stylesheet" />
30 * </code>
31 * <output>
32 * <link href="Resources/Packages/MyPackage/stylesheet.css" rel="stylesheet" />
33 * (depending on current package)
34 * </output>
35 */
36 class ResourceViewHelper extends AbstractViewHelper
37 {
38 use CompileWithRenderStatic;
39
40 /**
41 * Initialize arguments
42 */
43 public function initializeArguments()
44 {
45 $this->registerArgument('path', 'string', 'The path and filename of the resource (relative to Public resource directory of the extension).', true);
46 $this->registerArgument('extensionName', 'string', 'Target extension name. If not set, the current extension name will be used');
47 $this->registerArgument('absolute', 'bool', 'If set, an absolute URI is rendered', false, false);
48 }
49
50 /**
51 * Render the URI to the resource. The filename is used from child content.
52 *
53 * @param array $arguments
54 * @param \Closure $renderChildrenClosure
55 * @param RenderingContextInterface $renderingContext
56 * @return string The URI to the resource
57 */
58 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
59 {
60 $path = $arguments['path'];
61 $extensionName = $arguments['extensionName'];
62 $absolute = $arguments['absolute'];
63
64 if ($extensionName === null) {
65 $extensionName = $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName();
66 }
67 $uri = 'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/Resources/Public/' . $path;
68 $uri = GeneralUtility::getFileAbsFileName($uri);
69 if ($absolute === false && $uri !== false) {
70 $uri = PathUtility::getAbsoluteWebPath($uri);
71 }
72 if ($absolute === true) {
73 $uri = PathUtility::stripPathSitePrefix($uri);
74 $uri = $renderingContext->getControllerContext()->getRequest()->getBaseUri() . $uri;
75 }
76 return $uri;
77 }
78 }