diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index fdea8ec..abb6124 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -8,14 +8,13 @@ jobs: publish: name: Publish new version to TER if: startsWith(github.ref, 'refs/tags/') - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest env: - TYPO3_EXTENSION_KEY: ${{ secrets.TYPO3_EXTENSION_KEY }} TYPO3_API_TOKEN: ${{ secrets.TYPO3_API_TOKEN }} steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Check tag run: | @@ -31,14 +30,14 @@ jobs: run: | readonly local comment=$(git tag -n10 -l ${{ steps.get-version.outputs.version }} | sed "s/^[0-9.]*[ ]*//g") if [[ -z "${comment// }" ]]; then - echo ::set-output name=comment::Released version ${{ steps.get-version.outputs.version }} of ${{ env.TYPO3_EXTENSION_KEY }} + echo ::set-output name=comment::Released version ${{ steps.get-version.outputs.version }} of http2 else echo ::set-output name=comment::$comment fi - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 7.4 + php-version: 8.3 extensions: intl, mbstring, json, zip, curl - name: Install tailor diff --git a/Classes/Http/ResourcePusher.php b/Classes/Http/ResourcePusher.php index 3cbbe69..dbc6bc3 100644 --- a/Classes/Http/ResourcePusher.php +++ b/Classes/Http/ResourcePusher.php @@ -1,5 +1,7 @@ handle($request); - if (($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController) { - $resources = $GLOBALS['TSFE']->config['b13/http2'] ?? null; - /** @var NormalizedParams $normalizedParams */ - $normalizedParams = $request->getAttribute('normalizedParams'); - if (is_array($resources) && $normalizedParams->isHttps()) { - foreach ($resources['scripts'] ?? [] as $resource) { - $response = $this->addPreloadHeaderToResponse($response, $resource, 'script'); - } - foreach ($resources['styles'] ?? [] as $resource) { - $response = $this->addPreloadHeaderToResponse($response, $resource, 'style'); - } + /** @var TypoScriptFrontendController $frontendController */ + $frontendController = $request->getAttribute('frontend.controller'); + $resources = $frontendController->config['b13/http2'] ?? null; + /** @var NormalizedParams $normalizedParams */ + $normalizedParams = $request->getAttribute('normalizedParams'); + if (is_array($resources) && $normalizedParams->isHttps()) { + foreach ($resources['scripts'] ?? [] as $resource) { + $response = $this->addPreloadHeaderToResponse($response, $resource, 'script'); + } + foreach ($resources['styles'] ?? [] as $resource) { + $response = $this->addPreloadHeaderToResponse($response, $resource, 'style'); } } return $response; @@ -49,10 +51,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface protected function addPreloadHeaderToResponse(ResponseInterface $response, string $uri, string $type): ResponseInterface { - if(str_contains($uri, '.mjs')) { + if (str_contains($uri, '.mjs')) { return $response->withAddedHeader('Link', '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=modulepreload; as=' . $type); - } else { - return $response->withAddedHeader('Link', '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=preload; as=' . $type); } + return $response->withAddedHeader('Link', '<' . htmlspecialchars(PathUtility::getAbsoluteWebPath($uri)) . '>; rel=preload; as=' . $type); + } } diff --git a/Classes/PageRendererHook.php b/Classes/PageRendererHook.php index 68868d9..b74fc61 100644 --- a/Classes/PageRendererHook.php +++ b/Classes/PageRendererHook.php @@ -1,5 +1,7 @@ matcher = $matcher ?: GeneralUtility::makeInstance(ResourceMatcher::class); - } + public function __construct(protected ResourceMatcher $matcher) {} /** * @param array $params @@ -42,9 +35,15 @@ public function __construct(ResourceMatcher $matcher = null) */ public function accumulateResources(array $params, PageRenderer $pageRenderer) { + $request = $this->getRequest(); + if ($request === null) { + return; + } // If this is a second run (non-cached cObjects adding more data), then the existing cached data is fetched - if ($this->getTypoScriptFrontendController() instanceof TypoScriptFrontendController) { - $allResources = $this->getTypoScriptFrontendController()->config['b13/http2'] ?? []; + if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) { + /** @var TypoScriptFrontendController $frontendController */ + $frontendController = $request->getAttribute('frontend.controller'); + $allResources = $frontendController->config['b13/http2'] ?? []; } else { $allResources = []; } @@ -64,7 +63,7 @@ public function accumulateResources(array $params, PageRenderer $pageRenderer) $allResources['scripts'] = array_unique($allResources['scripts'] ?? []); $allResources['styles'] = array_unique($allResources['styles'] ?? []); - $this->process($allResources); + $this->process($allResources, $request); } /** @@ -73,18 +72,20 @@ public function accumulateResources(array $params, PageRenderer $pageRenderer) * * @param array $allResources */ - protected function process(array $allResources) + protected function process(array $allResources, ServerRequestInterface $request): void { - if ($this->getTypoScriptFrontendController() instanceof TypoScriptFrontendController) { - $this->getTypoScriptFrontendController()->config['b13/http2'] = $allResources; + if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) { + /** @var TypoScriptFrontendController $frontendController */ + $frontendController = $request->getAttribute('frontend.controller'); + $frontendController->config['b13/http2'] = $allResources; } elseif (GeneralUtility::getIndpEnv('TYPO3_SSL')) { // Push directly into the TYPO3 Backend, but only if TYPO3 is running in SSL GeneralUtility::makeInstance(ResourcePusher::class)->pushAll($allResources); } } - protected function getTypoScriptFrontendController() + protected function getRequest(): ?ServerRequestInterface { - return $GLOBALS['TSFE'] ?? null; + return $GLOBALS['TYPO3_REQUEST'] ?? null; } } diff --git a/Classes/ResourceMatcher.php b/Classes/ResourceMatcher.php index 4a53fcc..671f576 100644 --- a/Classes/ResourceMatcher.php +++ b/Classes/ResourceMatcher.php @@ -1,5 +1,7 @@ array_values(array_filter($matches[1])), - 'styles' => array_values($styles) + 'styles' => array_values($styles), ]; } diff --git a/Classes/ResourcePusher.php b/Classes/ResourcePusher.php index 5d7f7c9..d2a23d9 100644 --- a/Classes/ResourcePusher.php +++ b/Classes/ResourcePusher.php @@ -1,5 +1,7 @@ headers. diff --git a/Configuration/RequestMiddlewares.php b/Configuration/RequestMiddlewares.php index d504444..422859e 100755 --- a/Configuration/RequestMiddlewares.php +++ b/Configuration/RequestMiddlewares.php @@ -1,4 +1,5 @@ [ 'b13/http2/push-resources' => [ @@ -7,8 +8,8 @@ 'typo3/cms-frontend/prepare-tsfe-rendering', ], 'after' => [ - 'typo3/cms-frontend/authentication', + 'typo3/cms-frontend/tsfe', ], ], - ] + ], ]; diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml new file mode 100644 index 0000000..b8227c0 --- /dev/null +++ b/Configuration/Services.yaml @@ -0,0 +1,11 @@ +services: + _defaults: + autowire: true + autoconfigure: true + public: false + + B13\Http2\: + resource: '../Classes/*' + + B13\Http2\PageRendererHook: + public: true diff --git a/Tests/Unit/ResourceMatcherTest.php b/Tests/Unit/ResourceMatcherTest.php index 91c2541..ba48989 100644 --- a/Tests/Unit/ResourceMatcherTest.php +++ b/Tests/Unit/ResourceMatcherTest.php @@ -1,5 +1,7 @@ [ 'Good things must come', [], - [] + [], ], 'simple script tag' => [ '', ['https://www.example.com/myfile.js', '/myfile.js'], - [] + [], ], 'multiple script tags with the same value finds duplicate hits' => [ '', ['https://www.example.com/myfile.js', '/myfile.js'], - [] + [], ], 'multiple script and valid link tags' => [ '', ['https://www.example.com/myfile.js', '/myfile.js'], - ['http://example.com/base.css'] + ['http://example.com/base.css'], ], ]; } - /** - * @test - * @dataProvider matchDataProvider - */ + #[DataProvider('matchDataProvider')] + #[Test] public function matchExtractsProperInformation($input, $expectedScripts, $expectedStyles) { $expectedResult = [ 'scripts' => $expectedScripts, - 'styles' => $expectedStyles + 'styles' => $expectedStyles, ]; $result = (new ResourceMatcher())->match($input); - $this->assertEquals($expectedResult, $result); + self::assertEquals($expectedResult, $result); } } diff --git a/composer.json b/composer.json index 6d8ffa3..c22e615 100644 --- a/composer.json +++ b/composer.json @@ -3,15 +3,12 @@ "type": "typo3-cms-extension", "description": "Speed up TYPO3 rendering via HTTP/2 Server Push", "require": { - "php": "^7.4 || ^8.0", - "psr/http-server-middleware": "^1.0", - "typo3/cms-core": "^10.0 || ^11.0 || ^12.0", - "typo3/cms-frontend": "^10.0 || ^11.0 || ^12.0" + "typo3/cms-core": "^12.4 || ^13.4", + "typo3/cms-frontend": "^12.4 || ^13.4" }, "require-dev": { - "phpunit/phpunit": "~7.0", - "squizlabs/php_codesniffer": "^2.3", - "typo3/tailor": "^1.4.0" + "phpunit/phpunit": "^11.0", + "squizlabs/php_codesniffer": "^3.0" }, "homepage": "https://b13.com", "license": ["GPL-2.0-or-later"],