‪TYPO3CMS  ‪main
ResourceViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
26 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
28 
79 final class ‪ResourceViewHelper extends AbstractViewHelper
80 {
81  use CompileWithRenderStatic;
82 
83  public function ‪initializeArguments(): void
84  {
85  $this->registerArgument('path', 'string', 'The path and filename of the resource (relative to Public resource directory of the extension).', true);
86  $this->registerArgument('extensionName', 'string', 'Target extension name. If not set, the current extension name will be used');
87  $this->registerArgument('absolute', 'bool', 'If set, an absolute URI is rendered', false, false);
88  $this->registerArgument('useCacheBusting', 'bool', 'If set, the URI is rendered with a cache buster', false, true);
89  }
90 
98  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
99  {
100  $uri = ‪PathUtility::getPublicResourceWebPath(self::resolveExtensionPath($arguments, $renderingContext));
101  if ($arguments['useCacheBusting']) {
102  $uri = GeneralUtility::createVersionNumberedFilename($uri);
103  }
104  if ($arguments['absolute']) {
105  $uri = GeneralUtility::locationHeaderUrl($uri);
106  }
107  return $uri;
108  }
109 
113  private static function ‪resolveExtensionPath(array $arguments, RenderingContextInterface $renderingContext): string
114  {
115  $path = $arguments['path'];
116  if (‪PathUtility::isExtensionPath($path)) {
117  return $path;
118  }
119  return sprintf(
120  'EXT:%s/Resources/Public/%s',
121  self::resolveExtensionKey($arguments, $renderingContext),
122  ltrim($path, '/')
123  );
124  }
125 
129  private static function ‪resolveExtensionKey(array $arguments, RenderingContextInterface $renderingContext): string
130  {
131  $extensionName = $arguments['extensionName'];
132  if ($extensionName === null) {
133  return ‪self::resolveValidatedRequest($arguments, $renderingContext)->getControllerExtensionKey();
134  }
136  }
137 
141  private static function ‪resolveValidatedRequest(array $arguments, RenderingContextInterface $renderingContext): RequestInterface
142  {
143  if (!$renderingContext instanceof RenderingContext) {
144  throw new \RuntimeException(
145  sprintf(
146  'RenderingContext must be instance of "%s", but is instance of "%s"',
147  RenderingContext::class,
148  get_class($renderingContext)
149  ),
150  1640095993
151  );
152  }
153  $request = $renderingContext->getRequest();
154  if (!$request instanceof RequestInterface) {
155  throw new \RuntimeException(
156  sprintf(
157  'ViewHelper f:uri.resource needs an Extbase Request object to resolve extension name for given path "%s".'
158  . ' If not in Extbase context, either set argument "extensionName",'
159  . ' or (better) use the standard EXT: syntax for path attribute like \'path="EXT:indexed_search/Resources/Public/Icons/Extension.svg"\'.',
160  $arguments['path']
161  ),
162  1639672666
163  );
164  }
165  if ($request->getControllerExtensionKey() === '') {
166  throw new \RuntimeException(
167  sprintf(
168  'Can not resolve extension key for given path "%s".'
169  . ' If not in Extbase context, either set argument "extensionName",'
170  . ' or (better) use the standard EXT: syntax for path attribute like \'path="EXT:indexed_search/Resources/Public/Icons/Extension.svg"\'.',
171  $arguments['path']
172  ),
173  1640097205
174  );
175  }
176  return $request;
177  }
178 }
‪TYPO3\CMS\Core\Utility\PathUtility\isExtensionPath
‪static isExtensionPath(string $path)
Definition: PathUtility.php:118
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ResourceViewHelper
Definition: ResourceViewHelper.php:80
‪TYPO3\CMS\Core\Utility\GeneralUtility\camelCaseToLowerCaseUnderscored
‪static string camelCaseToLowerCaseUnderscored($string)
Definition: GeneralUtility.php:683
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:98
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ResourceViewHelper\resolveExtensionPath
‪static resolveExtensionPath(array $arguments, RenderingContextInterface $renderingContext)
Definition: ResourceViewHelper.php:112
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ResourceViewHelper\resolveValidatedRequest
‪static resolveValidatedRequest(array $arguments, RenderingContextInterface $renderingContext)
Definition: ResourceViewHelper.php:140
‪TYPO3\CMS\Core\Resource\Exception\InvalidFileException
Definition: InvalidFileException.php:23
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ResourceViewHelper\initializeArguments
‪initializeArguments()
Definition: ResourceViewHelper.php:82
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ResourceViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: ResourceViewHelper.php:97
‪TYPO3\CMS\Fluid\ViewHelpers\Uri
Definition: ActionViewHelper.php:18
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ResourceViewHelper\resolveExtensionKey
‪static resolveExtensionKey(array $arguments, RenderingContextInterface $renderingContext)
Definition: ResourceViewHelper.php:128
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35