‪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  }
89 
97  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
98  {
99  $uri = ‪PathUtility::getPublicResourceWebPath(self::resolveExtensionPath($arguments, $renderingContext));
100  if ($arguments['absolute']) {
101  $uri = GeneralUtility::locationHeaderUrl($uri);
102  }
103  return $uri;
104  }
105 
109  private static function ‪resolveExtensionPath(array $arguments, RenderingContextInterface $renderingContext): string
110  {
111  $path = $arguments['path'];
112  if (‪PathUtility::isExtensionPath($path)) {
113  return $path;
114  }
115  return sprintf(
116  'EXT:%s/Resources/Public/%s',
117  self::resolveExtensionKey($arguments, $renderingContext),
118  ltrim($path, '/')
119  );
120  }
121 
125  private static function ‪resolveExtensionKey(array $arguments, RenderingContextInterface $renderingContext): string
126  {
127  $extensionName = $arguments['extensionName'];
128  if ($extensionName === null) {
129  return ‪self::resolveValidatedRequest($arguments, $renderingContext)->getControllerExtensionKey();
130  }
132  }
133 
137  private static function ‪resolveValidatedRequest(array $arguments, RenderingContextInterface $renderingContext): RequestInterface
138  {
139  if (!$renderingContext instanceof RenderingContext) {
140  throw new \RuntimeException(
141  sprintf(
142  'RenderingContext must be instance of "%s", but is instance of "%s"',
143  RenderingContext::class,
144  get_class($renderingContext)
145  ),
146  1640095993
147  );
148  }
149  $request = $renderingContext->getRequest();
150  if (!$request instanceof RequestInterface) {
151  throw new \RuntimeException(
152  sprintf(
153  'ViewHelper f:uri.resource needs an Extbase Request object to resolve extension name for given path "%s".'
154  . ' If not in Extbase context, either set argument "extensionName",'
155  . ' or (better) use the standard EXT: syntax for path attribute like \'path="EXT:indexed_search/Resources/Public/Icons/Extension.svg"\'.',
156  $arguments['path']
157  ),
158  1639672666
159  );
160  }
161  if ($request->getControllerExtensionKey() === '') {
162  throw new \RuntimeException(
163  sprintf(
164  'Can not resolve extension key for given path "%s".'
165  . ' If not in Extbase context, either set argument "extensionName",'
166  . ' or (better) use the standard EXT: syntax for path attribute like \'path="EXT:indexed_search/Resources/Public/Icons/Extension.svg"\'.',
167  $arguments['path']
168  ),
169  1640097205
170  );
171  }
172  return $request;
173  }
174 }
‪TYPO3\CMS\Core\Utility\PathUtility\isExtensionPath
‪static isExtensionPath(string $path)
Definition: PathUtility.php:117
‪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:678
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:97
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ResourceViewHelper\resolveExtensionPath
‪static resolveExtensionPath(array $arguments, RenderingContextInterface $renderingContext)
Definition: ResourceViewHelper.php:108
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ResourceViewHelper\resolveValidatedRequest
‪static resolveValidatedRequest(array $arguments, RenderingContextInterface $renderingContext)
Definition: ResourceViewHelper.php:136
‪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:96
‪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:124
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35