From f5ab7e45e5bda58cb6fd051ef0ba88790090a314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Lochm=C3=BCller?= Date: Mon, 30 Sep 2024 21:57:31 +0200 Subject: [PATCH] Migration of HttpPush services to events --- Classes/Configuration.php | 17 --------- Classes/Event/HttpPushHeaderEvent.php | 36 +++++++++++++++++++ Classes/Service/HttpPush/AbstractHttpPush.php | 14 ++++++-- Classes/Service/HttpPush/FontHttpPush.php | 5 +++ Classes/Service/HttpPushService.php | 17 ++++----- Configuration/Services.yaml | 30 ++++++++++++++++ 6 files changed, 89 insertions(+), 30 deletions(-) create mode 100644 Classes/Event/HttpPushHeaderEvent.php diff --git a/Classes/Configuration.php b/Classes/Configuration.php index 03de886b854..85f7ec7a2ce 100644 --- a/Classes/Configuration.php +++ b/Classes/Configuration.php @@ -65,7 +65,6 @@ public function extLocalconf(): void $this->registerHooks() ->registerRules() ->registerCachingFramework() - ->registerHttpPushServices() ->adjustSystemSettings() ; } @@ -132,22 +131,6 @@ protected function registerCachingFramework(): self return $this; } - /** - * Register HTTP push services. - */ - protected function registerHttpPushServices(): self - { - GeneralUtility::makeInstance(ObjectFactoryService::class)->set('HttpPush', [ - 'style' => StyleHttpPush::class, - 'script' => ScriptHttpPush::class, - 'image' => ImageHttpPush::class, - 'font' => FontHttpPush::class, - 'svg' => SvgHttpPush::class, - ]); - - return $this; - } - protected function adjustSystemSettings(): self { // aim for cacheable frontend responses when using TYPO3's `Content-Security-Policy` behavior diff --git a/Classes/Event/HttpPushHeaderEvent.php b/Classes/Event/HttpPushHeaderEvent.php new file mode 100644 index 00000000000..d209d04eee9 --- /dev/null +++ b/Classes/Event/HttpPushHeaderEvent.php @@ -0,0 +1,36 @@ +headers; + } + + public function setHeaders(array $headers): void + { + $this->headers = $headers; + } + + public function getExtensions(): array + { + return $this->extensions; + } + + public function getContent(): string + { + return $this->content; + } + + +} diff --git a/Classes/Service/HttpPush/AbstractHttpPush.php b/Classes/Service/HttpPush/AbstractHttpPush.php index 24d1521c51a..9ec217c4846 100644 --- a/Classes/Service/HttpPush/AbstractHttpPush.php +++ b/Classes/Service/HttpPush/AbstractHttpPush.php @@ -4,13 +4,21 @@ namespace SFC\Staticfilecache\Service\HttpPush; +use SFC\Staticfilecache\Event\HttpPushHeaderEvent; use TYPO3\CMS\Core\Utility\GeneralUtility; -/** - * AbstractHttpPush. - */ abstract class AbstractHttpPush { + public function __invoke(HttpPushHeaderEvent $event): void + { + foreach ($event->getExtensions() as $extension) { + if ($this->canHandleExtension($extension)) { + $event->setHeaders(array_merge($event->getHeaders(), $this->getHeaders($event->getContent()))); + ; + } + } + } + /** * Check if the class can handle the file extension. */ diff --git a/Classes/Service/HttpPush/FontHttpPush.php b/Classes/Service/HttpPush/FontHttpPush.php index 202603ff64b..cce350ccfb7 100644 --- a/Classes/Service/HttpPush/FontHttpPush.php +++ b/Classes/Service/HttpPush/FontHttpPush.php @@ -4,6 +4,10 @@ namespace SFC\Staticfilecache\Service\HttpPush; +use SFC\Staticfilecache\Event\HttpPushHeaderEvent; +use SFC\Staticfilecache\Service\ObjectFactoryService; +use TYPO3\CMS\Core\Utility\GeneralUtility; + class FontHttpPush extends AbstractHttpPush { /** @@ -16,6 +20,7 @@ class FontHttpPush extends AbstractHttpPush */ private $fontsExtensions = ['woff', 'woff2']; + /** * Check if the class can handle the file extension. */ diff --git a/Classes/Service/HttpPushService.php b/Classes/Service/HttpPushService.php index 19cfdaaf05a..3a9162e9cee 100644 --- a/Classes/Service/HttpPushService.php +++ b/Classes/Service/HttpPushService.php @@ -4,11 +4,14 @@ namespace SFC\Staticfilecache\Service; -use SFC\Staticfilecache\Service\HttpPush\AbstractHttpPush; +use Psr\EventDispatcher\EventDispatcherInterface; +use SFC\Staticfilecache\Event\HttpPushHeaderEvent; use TYPO3\CMS\Core\Utility\GeneralUtility; class HttpPushService { + public function __construct(readonly protected EventDispatcherInterface $eventDispatcher) {} + /** * Get http push headers. */ @@ -28,16 +31,10 @@ public function getHttpPushHeaders(string $content): array $content = (string) $limitToAreaMatch[0]; } - foreach (GeneralUtility::makeInstance(ObjectFactoryService::class)->get('HttpPush') as $handler) { - foreach ($extensions as $extension) { - /** @var AbstractHttpPush $handler */ - if ($handler->canHandleExtension($extension)) { - $headers = array_merge($headers, $handler->getHeaders($content)); - } - } - } + $event = new HttpPushHeaderEvent($headers, $content, $extensions); + $this->eventDispatcher->dispatch($event); - $headers = \array_slice($headers, 0, $limit); + $headers = \array_slice($event->getHeaders(), 0, $limit); } return $headers; diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index 570b0e64cac..047e66c13d6 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -93,6 +93,36 @@ services: identifier: 'SfcCacheRuleNoBackendUserListener' event: SFC\Staticfilecache\Event\CacheRuleEvent + SFC\Staticfilecache\Service\HttpPush\StyleHttpPush: + tags: + - name: event.listener + identifier: 'SfcStyleHttpPush' + event: SFC\Staticfilecache\Event\HttpPushHeaderEvent + + SFC\Staticfilecache\Service\HttpPush\ScriptHttpPush: + tags: + - name: event.listener + identifier: 'SfcStyleHttpPush' + event: SFC\Staticfilecache\Event\HttpPushHeaderEvent + + SFC\Staticfilecache\Service\HttpPush\ImageHttpPush: + tags: + - name: event.listener + identifier: 'SfcStyleHttpPush' + event: SFC\Staticfilecache\Event\HttpPushHeaderEvent + + SFC\Staticfilecache\Service\HttpPush\FontHttpPush: + tags: + - name: event.listener + identifier: 'SfcStyleHttpPush' + event: SFC\Staticfilecache\Event\HttpPushHeaderEvent + + SFC\Staticfilecache\Service\HttpPush\SvgHttpPush: + tags: + - name: event.listener + identifier: 'SfcStyleHttpPush' + event: SFC\Staticfilecache\Event\HttpPushHeaderEvent + SFC\Staticfilecache\Generator\ConfigGenerator: tags: - name: event.listener