Skip to content

Commit

Permalink
Migration of HttpPush services to events
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Sep 30, 2024
1 parent a008724 commit f5ab7e4
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 30 deletions.
17 changes: 0 additions & 17 deletions Classes/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function extLocalconf(): void
$this->registerHooks()
->registerRules()
->registerCachingFramework()
->registerHttpPushServices()
->adjustSystemSettings()
;
}
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions Classes/Event/HttpPushHeaderEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace SFC\Staticfilecache\Event;

final class HttpPushHeaderEvent
{
public function __construct(
private array $headers,
private string $content,
private array $extensions
) {}

public function getHeaders(): array
{
return $this->headers;
}

public function setHeaders(array $headers): void
{
$this->headers = $headers;
}

public function getExtensions(): array
{
return $this->extensions;
}

public function getContent(): string
{
return $this->content;
}


}
14 changes: 11 additions & 3 deletions Classes/Service/HttpPush/AbstractHttpPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
5 changes: 5 additions & 0 deletions Classes/Service/HttpPush/FontHttpPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand All @@ -16,6 +20,7 @@ class FontHttpPush extends AbstractHttpPush
*/
private $fontsExtensions = ['woff', 'woff2'];


/**
* Check if the class can handle the file extension.
*/
Expand Down
17 changes: 7 additions & 10 deletions Classes/Service/HttpPushService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f5ab7e4

Please sign in to comment.