‪TYPO3CMS  ‪main
ResourceUtility.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 
26 
27 class ResourceUtility
28 {
37  public static function getAdditionalResourcesForModules(array $modules, array $attributes = []): array
38  {
39  $result = [
40  'js' => '',
41  'css' => '',
42  ];
43  foreach ($modules as $module) {
44  if ($module instanceof ResourceProviderInterface) {
45  foreach ($module->getJavaScriptFiles() as $file) {
46  $result['js'] .= static::getJsTag($file, $attributes);
47  }
48  foreach ($module->getCssFiles() as $file) {
49  $result['css'] .= static::getCssTag($file, $attributes);
50  }
51  }
52  if ($module instanceof SubmoduleProviderInterface) {
53  $subResult = self::getAdditionalResourcesForModules($module->getSubModules());
54  $result['js'] .= $subResult['js'];
55  $result['css'] .= $subResult['css'];
56  }
57  }
58  return $result;
59  }
60 
66  protected static function getCssTag(string $cssFileLocation, array $attributes): string
67  {
68  return sprintf(
69  '<link %s />',
70  GeneralUtility::implodeAttributes([
71  ...$attributes,
72  'rel' => 'stylesheet',
73  'media' => 'all',
74  'href' => ‪PathUtility::getPublicResourceWebPath($cssFileLocation),
75  ], true)
76  );
77  }
78 
84  protected static function getJsTag(string $jsFileLocation, array $attributes): string
85  {
86  return sprintf(
87  '<script %s></script>',
88  GeneralUtility::implodeAttributes([
89  ...$attributes,
90  'src' => ‪PathUtility::getPublicResourceWebPath($jsFileLocation),
91  ], true)
92  );
93  }
94 
100  public static function getResources(array $attributes = []): array
101  {
102  $jsFileLocation = 'EXT:adminpanel/Resources/Public/JavaScript/admin-panel.js';
103  $js = self::getJsTag($jsFileLocation, $attributes);
104  $cssFileLocation = 'EXT:adminpanel/Resources/Public/Css/adminpanel.css';
105  $css = self::getCssTag($cssFileLocation, $attributes);
106 
107  return [
108  'css' => $css,
109  'js' => $js,
110  ];
111  }
112 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Adminpanel\Utility
Definition: HtmlDumper.php:18
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\ConsumableNonce
Definition: ConsumableNonce.php:24
‪TYPO3\CMS\Adminpanel\ModuleApi\ResourceProviderInterface
Definition: ResourceProviderInterface.php:27
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:97
‪TYPO3\CMS\Adminpanel\ModuleApi\ModuleInterface
Definition: ModuleInterface.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Adminpanel\ModuleApi\SubmoduleProviderInterface
Definition: SubmoduleProviderInterface.php:30