Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add event to allow manage $resources. #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Classes/Event/ProcessResourcesEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace B13\Http2\Event;

class ProcessResourcesEvent
{
private array $resources;

public function __construct(array $resources)
{
$this->resources = $resources;
}

public function getResources(): array
{
return $this->resources;
}

public function setResources(array $resources): void
{
$this->resources = $resources;
}
}
10 changes: 8 additions & 2 deletions Classes/Http/ResourcePusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
* of the License, or any later version.
*/

use B13\Http2\Event\ProcessResourcesEvent;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

Expand All @@ -36,10 +39,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
/** @var NormalizedParams $normalizedParams */
$normalizedParams = $request->getAttribute('normalizedParams');
if (is_array($resources) && $normalizedParams->isHttps()) {
foreach ($resources['scripts'] ?? [] as $resource) {
$eventDispatcher = GeneralUtility::makeInstance(EventDispatcher::class);
$event = $eventDispatcher->dispatch(new ProcessResourcesEvent($resources));
$resources = $event->getResources();
foreach ($resources['scripts'] ?? [] as $resource) {
$response = $this->addPreloadHeaderToResponse($response, $resource, 'script');
}
foreach ($resources['styles'] ?? [] as $resource) {
foreach ($resources['styles'] ?? [] as $resource) {
$response = $this->addPreloadHeaderToResponse($response, $resource, 'style');
}
}
Expand Down