‪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 
74 final class ‪ResourceViewHelper extends AbstractViewHelper
75 {
76  use CompileWithRenderStatic;
77 
78  public function ‪initializeArguments(): void
79  {
80  $this->registerArgument('path', 'string', 'The path and filename of the resource (relative to Public resource directory of the extension).', true);
81  $this->registerArgument('extensionName', 'string', 'Target extension name. If not set, the current extension name will be used');
82  $this->registerArgument('absolute', 'bool', 'If set, an absolute URI is rendered', false, false);
83  }
84 
92  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
93  {
94  $uri = ‪PathUtility::getPublicResourceWebPath(self::resolveExtensionPath($arguments, $renderingContext));
95  if ($arguments['absolute']) {
96  $uri = GeneralUtility::locationHeaderUrl($uri);
97  }
98  return $uri;
99  }
100 
104  private static function ‪resolveExtensionPath(array $arguments, RenderingContextInterface $renderingContext): string
105  {
106  $path = $arguments['path'];
107  if (‪PathUtility::isExtensionPath($path)) {
108  return $path;
109  }
110  return sprintf(
111  'EXT:%s/Resources/Public/%s',
112  self::resolveExtensionKey($arguments, $renderingContext),
113  ltrim($path, '/')
114  );
115  }
116 
120  private static function ‪resolveExtensionKey(array $arguments, RenderingContextInterface $renderingContext): string
121  {
122  $extensionName = $arguments['extensionName'];
123  if ($extensionName === null) {
124  return ‪self::resolveValidatedRequest($arguments, $renderingContext)->getControllerExtensionKey();
125  }
127  }
128 
132  private static function ‪resolveValidatedRequest(array $arguments, RenderingContextInterface $renderingContext): RequestInterface
133  {
134  if (!$renderingContext instanceof RenderingContext) {
135  throw new \RuntimeException(
136  sprintf(
137  'RenderingContext must be instance of "%s", but is instance of "%s"',
138  RenderingContext::class,
139  get_class($renderingContext)
140  ),
141  1640095993
142  );
143  }
144  $request = $renderingContext->getRequest();
145  if (!$request instanceof RequestInterface) {
146  throw new \RuntimeException(
147  sprintf(
148  'ViewHelper f:uri.resource needs an Extbase Request object to resolve extension name for given path "%s".'
149  . ' If not in Extbase context, either set argument "extensionName",'
150  . ' or (better) use the standard EXT: syntax for path attribute like \'path="EXT:indexed_search/Resources/Public/Icons/Extension.svg"\'.',
151  $arguments['path']
152  ),
153  1639672666
154  );
155  }
156  if ($request->getControllerExtensionKey() === '') {
157  throw new \RuntimeException(
158  sprintf(
159  'Can not resolve extension key for given path "%s".'
160  . ' If not in Extbase context, either set argument "extensionName",'
161  . ' or (better) use the standard EXT: syntax for path attribute like \'path="EXT:indexed_search/Resources/Public/Icons/Extension.svg"\'.',
162  $arguments['path']
163  ),
164  1640097205
165  );
166  }
167  return $request;
168  }
169 }
‪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:75
‪TYPO3\CMS\Core\Utility\GeneralUtility\camelCaseToLowerCaseUnderscored
‪static string camelCaseToLowerCaseUnderscored($string)
Definition: GeneralUtility.php:661
‪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:103
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ResourceViewHelper\resolveValidatedRequest
‪static resolveValidatedRequest(array $arguments, RenderingContextInterface $renderingContext)
Definition: ResourceViewHelper.php:131
‪TYPO3\CMS\Core\Resource\Exception\InvalidFileException
Definition: InvalidFileException.php:24
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ResourceViewHelper\initializeArguments
‪initializeArguments()
Definition: ResourceViewHelper.php:77
‪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:91
‪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:119
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35