|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the "typo3_encore" Extension for TYPO3 CMS. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please read the |
| 9 | + * LICENSE.txt file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Ssch\Typo3Encore\Integration; |
| 13 | + |
| 14 | +use Ssch\Typo3Encore\Asset\EntrypointLookupInterface; |
| 15 | +use Ssch\Typo3Encore\Asset\TagRendererInterface; |
| 16 | +use Ssch\Typo3Encore\ValueObject\LinkTag; |
| 17 | +use Ssch\Typo3Encore\ValueObject\ScriptTag; |
| 18 | +use TYPO3\CMS\Core\Page\PageRenderer; |
| 19 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 20 | + |
| 21 | +final class PageRendererHooks |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + private const ENCORE_PREFIX = 'PKG:ssch/typo3-encore:'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var int |
| 30 | + */ |
| 31 | + private const PART_FOOTER = 2; |
| 32 | + |
| 33 | + public function __construct( |
| 34 | + private readonly TagRendererInterface $tagRenderer |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + public function renderPreProcess(array $params, PageRenderer $pageRenderer): void |
| 39 | + { |
| 40 | + // At this point, TYPO3 provides all javascript includes in only 'Files' or 'Libs' |
| 41 | + foreach (TagRendererInterface::ALLOWED_JS_POSITIONS as $includeType) { |
| 42 | + if (! isset($params[$includeType])) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + // Is the include type 'jsLibs' and should be treated as a library |
| 47 | + $isLibrary = TagRendererInterface::POSITION_JS_LIBRARY === $includeType; |
| 48 | + |
| 49 | + foreach ($params[$includeType] as $key => $jsFile) { |
| 50 | + if (! $this->isEncoreEntryName($jsFile['file'])) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + $buildAndEntryName = $this->createBuildAndEntryName($jsFile['file']); |
| 55 | + $buildName = EntrypointLookupInterface::DEFAULT_BUILD; |
| 56 | + |
| 57 | + if (2 === count($buildAndEntryName)) { |
| 58 | + [$buildName, $entryName] = $buildAndEntryName; |
| 59 | + } else { |
| 60 | + $entryName = $buildAndEntryName[0]; |
| 61 | + } |
| 62 | + |
| 63 | + $position = ($jsFile['section'] ?? '') === self::PART_FOOTER ? TagRendererInterface::POSITION_FOOTER : ''; |
| 64 | + |
| 65 | + unset($params[$includeType][$key], $jsFile['file'], $jsFile['section'], $jsFile['integrity']); |
| 66 | + |
| 67 | + $scriptTag = new ScriptTag($entryName, $position, $buildName, $pageRenderer, $jsFile, true, $isLibrary); |
| 68 | + |
| 69 | + $this->tagRenderer->renderWebpackScriptTags($scriptTag); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Add CSS-Files by entryNames |
| 74 | + foreach (TagRendererInterface::ALLOWED_CSS_POSITIONS as $includeType) { |
| 75 | + if (! isset($params[$includeType])) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + foreach ($params[$includeType] as $key => $cssFile) { |
| 80 | + if (! $this->isEncoreEntryName($cssFile['file'])) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + $buildAndEntryName = $this->createBuildAndEntryName($cssFile['file']); |
| 84 | + $buildName = EntrypointLookupInterface::DEFAULT_BUILD; |
| 85 | + |
| 86 | + if (2 === count($buildAndEntryName)) { |
| 87 | + [$buildName, $entryName] = $buildAndEntryName; |
| 88 | + } else { |
| 89 | + $entryName = $buildAndEntryName[0]; |
| 90 | + } |
| 91 | + |
| 92 | + unset($params[$includeType][$key], $cssFile['file'], $cssFile['rel']); |
| 93 | + |
| 94 | + $linkTag = new LinkTag($entryName, 'all', $buildName, $pageRenderer, $cssFile); |
| 95 | + $this->tagRenderer->renderWebpackLinkTags($linkTag); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private function isEncoreEntryName(string $file): bool |
| 101 | + { |
| 102 | + return \str_starts_with($file, self::ENCORE_PREFIX); |
| 103 | + } |
| 104 | + |
| 105 | + private function removePrefix(string $file): string |
| 106 | + { |
| 107 | + return str_replace(self::ENCORE_PREFIX, '', $file); |
| 108 | + } |
| 109 | + |
| 110 | + private function createBuildAndEntryName(string $file): array |
| 111 | + { |
| 112 | + return GeneralUtility::trimExplode(':', $this->removePrefix($file), true, 2); |
| 113 | + } |
| 114 | +} |
0 commit comments