Skip to content
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
5 changes: 3 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/Build/ export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/Tests export-ignore
/.github export-ignore

52 changes: 52 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: CI

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
TYPO3: [ '14' ]
php: [ '8.2', '8.5']

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up PHP Version
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2

- name: Start MySQL
run: sudo /etc/init.d/mysql start

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.composer/cache
key: dependencies-composer-${{ hashFiles('composer.json') }}

- name: Install composer dependencies
run: |
composer install --no-progress --no-interaction
- name: Phpstan
run: vendor/bin/phpstan analyze -c Build/phpstan.neon
- name: Phpcsfix
run: vendor/bin/php-cs-fixer fix --config=Build/php-cs-fixer.php --dry-run --stop-on-violation --using-cache=no
- name: Unit Tests
run: |
vendor/bin/phpunit -c Build/phpunit/UnitTests.xml Tests/Unit
- name: Functional Tests
run: |
export typo3DatabaseName="typo3";
export typo3DatabaseHost="127.0.0.1";
export typo3DatabaseUsername="root";
export typo3DatabasePassword="root";
vendor/bin/phpunit -c Build/phpunit/FunctionalTests.xml Tests/Functional
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Build
composer.lock
Build/phpunit/.phpunit.result.cache
vendor
public
.phpunit.result.cache
.php-cs-fixer.cache
11 changes: 11 additions & 0 deletions Build/php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$config = \TYPO3\CodingStandards\CsFixerConfig::create();
$config->getFinder()->exclude(['var', 'public', 'vendor'])->in(__DIR__ . '/..');
$config->addRules([
'nullable_type_declaration' => [
'syntax' => 'question_mark',
],
'nullable_type_declaration_for_default_null_value' => true,
]);
return $config;
5 changes: 5 additions & 0 deletions Build/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 5
paths:
- %currentWorkingDirectory%/Classes
- %currentWorkingDirectory%/Tests
24 changes: 24 additions & 0 deletions Build/phpunit/FunctionalTests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="true"
bootstrap="FunctionalTestsBootstrap.php"
colors="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
beStrictAboutTestsThatDoNotTestAnything="false"
failOnWarning="true"
failOnDeprecation="true"
>
<testsuites>
<testsuite name="Functional tests">
<directory>../../Tests/Functional/</directory>
</testsuite>
</testsuites>
<php>
<ini name="display_errors" value="1" />
<env name="TYPO3_CONTEXT" value="Testing" />
</php>
</phpunit>
21 changes: 21 additions & 0 deletions Build/phpunit/FunctionalTestsBootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

call_user_func(function () {
$testbase = new \TYPO3\TestingFramework\Core\Testbase();
$testbase->defineOriginalRootPath();
$testbase->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/tests');
$testbase->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/transient');
});
24 changes: 24 additions & 0 deletions Build/phpunit/UnitTests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="true"
bootstrap="UnitTestsBootstrap.php"
colors="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
beStrictAboutTestsThatDoNotTestAnything="false"
failOnWarning="true"
>
<testsuites>
<testsuite name="Unit tests">
<directory>../../Tests/Unit/</directory>
</testsuite>
</testsuites>
<php>
<ini name="display_errors" value="1" />
<env name="TYPO3_CONTEXT" value="Testing" />
</php>
</phpunit>
68 changes: 68 additions & 0 deletions Build/phpunit/UnitTestsBootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

/**
* This file is defined in UnitTests.xml and called by phpunit
* before instantiating the test suites.
*/
(static function () {
$testbase = new \TYPO3\TestingFramework\Core\Testbase();

// These if's are for core testing (package typo3/cms) only. cms-composer-installer does
// not create the autoload-include.php file that sets these env vars and sets composer
// mode to true. testing-framework can not be used without composer anyway, so it is safe
// to do this here. This way it does not matter if 'bin/phpunit' or 'vendor/phpunit/phpunit/phpunit'
// is called to run the tests since the 'relative to entry script' path calculation within
// SystemEnvironmentBuilder is not used. However, the binary must be called from the document
// root since getWebRoot() uses 'getcwd()'.
if (!getenv('TYPO3_PATH_ROOT')) {
putenv('TYPO3_PATH_ROOT=' . rtrim($testbase->getWebRoot(), '/'));
}
if (!getenv('TYPO3_PATH_WEB')) {
putenv('TYPO3_PATH_WEB=' . rtrim($testbase->getWebRoot(), '/'));
}

$testbase->defineSitePath();

$requestType = \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::REQUESTTYPE_BE | \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::REQUESTTYPE_CLI;
\TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::run(0, $requestType);

$testbase->createDirectory(\TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/typo3conf/ext');
$testbase->createDirectory(\TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/typo3temp/assets');
$testbase->createDirectory(\TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/typo3temp/var/tests');
$testbase->createDirectory(\TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/typo3temp/var/transient');

// Retrieve an instance of class loader and inject to core bootstrap
$classLoader = require $testbase->getPackagesPath() . '/autoload.php';
\TYPO3\CMS\Core\Core\Bootstrap::initializeClassLoader($classLoader);

// Initialize default TYPO3_CONF_VARS
$configurationManager = new \TYPO3\CMS\Core\Configuration\ConfigurationManager();
$GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration();

$cache = new \TYPO3\CMS\Core\Cache\Frontend\PhpFrontend(
'core',
new \TYPO3\CMS\Core\Cache\Backend\NullBackend([])
);
// Set all packages to active
$packageManager = \TYPO3\CMS\Core\Core\Bootstrap::createPackageManager(\TYPO3\CMS\Core\Package\UnitTestPackageManager::class, \TYPO3\CMS\Core\Core\Bootstrap::createPackageCache($cache));

\TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Package\PackageManager::class, $packageManager);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::setPackageManager($packageManager);

$testbase->dumpClassLoadingInformation();

\TYPO3\CMS\Core\Utility\GeneralUtility::purgeInstances();
})();
20 changes: 20 additions & 0 deletions Build/sites/main/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
base: 'http://localhost/'
baseVariants: { }
errorHandling: { }
languages:
-
title: english
enabled: true
base: /
typo3Language: default
locale: en_US.UTF-8
iso-639-1: en
navigationTitle: ''
hreflang: ''
direction: ''
flag: global
languageId: '0'
websiteTitle: ''
rootPageId: 1
routes: { }
websiteTitle: 'Testing'
24 changes: 19 additions & 5 deletions Classes/Http/ResourcePusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use TYPO3\CMS\Core\Cache\CacheDataCollector;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Takes existing accumulated resources and pushes them as HTTP2 <link> headers as middleware.
Expand All @@ -30,15 +32,27 @@
*/
class ResourcePusher implements MiddlewareInterface
{
public function __construct(
#[Autowire(service: 'cache.tx_http2')]
private readonly FrontendInterface $cache
) {}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
/** @var TypoScriptFrontendController $frontendController */
$frontendController = $request->getAttribute('frontend.controller');
$resources = $frontendController->config['b13/http2'] ?? null;
$response = $response->withHeader('foo', 'bar');

/** @var CacheDataCollector $cacheDataCollector */
$cacheDataCollector = $request->getAttribute('frontend.cache.collector');
$identifier = $cacheDataCollector->getPageCacheIdentifier();
$resources = [];
if ($this->cache->has($identifier)) {
$resources = $this->cache->get($identifier);
}

/** @var NormalizedParams $normalizedParams */
$normalizedParams = $request->getAttribute('normalizedParams');
if (is_array($resources) && $normalizedParams->isHttps()) {
if (!empty($resources) && $normalizedParams->isHttps()) {
foreach ($resources['scripts'] ?? [] as $resource) {
$response = $this->addPreloadHeaderToResponse($response, $resource, 'script');
}
Expand Down
46 changes: 36 additions & 10 deletions Classes/PageRendererHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@
*/

use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use TYPO3\CMS\Core\Cache\CacheDataCollector;
use TYPO3\CMS\Core\Cache\CacheTag;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Hooks into PageRenderer before the rendering is taken care of, and remember the files
* that can be sent as resources.
*
* This considers that everything is required, thus is marked as "preload", not via "prefetch".
*/
#[Autoconfigure(public: true)]
class PageRendererHook
{
public function __construct(protected ResourceMatcher $matcher) {}
public function __construct(
#[Autowire(service: 'cache.tx_http2')]
private readonly FrontendInterface $cache,
protected ResourceMatcher $matcher
) {}

/**
* @param array $params
Expand All @@ -40,10 +49,8 @@ public function accumulateResources(array $params, PageRenderer $pageRenderer)
return;
}
// If this is a second run (non-cached cObjects adding more data), then the existing cached data is fetched
if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) {
/** @var TypoScriptFrontendController $frontendController */
$frontendController = $request->getAttribute('frontend.controller');
$allResources = $frontendController->config['b13/http2'] ?? [];
if (ApplicationType::fromRequest($request)->isFrontend()) {
$allResources = $this->getFromCached($request);
} else {
$allResources = [];
}
Expand Down Expand Up @@ -74,16 +81,35 @@ public function accumulateResources(array $params, PageRenderer $pageRenderer)
*/
protected function process(array $allResources, ServerRequestInterface $request): void
{
if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) {
/** @var TypoScriptFrontendController $frontendController */
$frontendController = $request->getAttribute('frontend.controller');
$frontendController->config['b13/http2'] = $allResources;
if (ApplicationType::fromRequest($request)->isFrontend()) {
$this->addToCached($request, $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 getFromCached(ServerRequestInterface $request): array
{
/** @var CacheDataCollector $cacheDataCollector */
$cacheDataCollector = $request->getAttribute('frontend.cache.collector');
$identifier = $cacheDataCollector->getPageCacheIdentifier();
if ($this->cache->has($identifier)) {
return $this->cache->get($identifier);
}
return [];
}

protected function addToCached(ServerRequestInterface $request, array $data): void
{
/** @var CacheDataCollector $cacheDataCollector */
$cacheDataCollector = $request->getAttribute('frontend.cache.collector');
$identifier = $cacheDataCollector->getPageCacheIdentifier();
$cacheTags = array_map(fn(CacheTag $cacheTag) => $cacheTag->name, $cacheDataCollector->getCacheTags());
$cacheTimeout = $cacheDataCollector->resolveLifetime();
$this->cache->set($identifier, $data, $cacheTags, $cacheTimeout);
}

protected function getRequest(): ?ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'] ?? null;
Expand Down
6 changes: 4 additions & 2 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ services:
B13\Http2\:
resource: '../Classes/*'

B13\Http2\PageRendererHook:
public: true
cache.tx_http2:
class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
arguments: ['tx_http2']
Loading
Loading