From b2a8bbdbe9215b20eee0d06da4993aecbb0d588e Mon Sep 17 00:00:00 2001 From: Achim Fritz Date: Fri, 28 Jun 2024 15:15:40 +0200 Subject: [PATCH] !!! [TASK] change requirements * drop TYPO3 < 11 Support * add TYPO3 13 Support * add ci workflow * add BeforeMiddlewareIsAppliedEvent Fixes: #5 Fixes: #11 --- .editorconfig | 2 +- .github/workflows/ci.yml | 48 +++++++++++++ .gitignore | 2 +- Configuration/Services.yaml | 8 +++ .../SiteConfiguration/Overrides/sites.php | 2 +- README.md | 5 ++ composer.json | 67 ++++++++++--------- .php_cs => php-cs-fixer.php | 2 +- phpstan.neon | 6 ++ .../BeforeMiddlewareIsAppliedEvent.php | 41 ++++++++++++ src/Middleware/LowerCaseUri.php | 19 +++++- 11 files changed, 164 insertions(+), 38 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 Configuration/Services.yaml rename .php_cs => php-cs-fixer.php (55%) create mode 100644 phpstan.neon create mode 100644 src/Middleware/BeforeMiddlewareIsAppliedEvent.php diff --git a/.editorconfig b/.editorconfig index 44fa4e3..3683123 100644 --- a/.editorconfig +++ b/.editorconfig @@ -18,7 +18,7 @@ indent_size = 2 # JSON-Files [*.json] -indent_style = tab +indent_size = 2 # ReST-Files [*.rst] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..e7b4305 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,48 @@ +name: CI + +on: [push, pull_request] + +jobs: + testsuite: + + runs-on: ubuntu-latest + strategy: + matrix: + TYPO3: ['11' , '12', '13'] + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set up PHP Version + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + tools: composer:v2 + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Cache dependencies + uses: actions/cache@v1 + with: + path: ~/.composer/cache + key: dependencies-composer-${{ hashFiles('composer.json') }} + + - name: Install composer dependencies TYPO3 13 + if: matrix.TYPO3 == '13' + run: | + composer install --no-progress --no-interaction + + - name: Install composer dependencies TYPO3 12 + if: matrix.TYPO3 == '12' + run: | + composer require typo3/cms-core:^12.4 --no-progress --no-interaction --dev -W + - name: Install composer dependencies TYPO3 11 + if: matrix.TYPO3 == '11' + run: | + composer require typo3/cms-core:^11.5 --no-progress --no-interaction --dev -W + - name: Phpstan + run: ./vendor/bin/phpstan analyze -c phpstan.neon + - name: Phpcsfix + run: ./vendor/bin/php-cs-fixer fix --config=php-cs-fixer.php --dry-run --stop-on-violation --using-cache=no diff --git a/.gitignore b/.gitignore index ebee64a..796aece 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /composer.lock /public/ /vendor/ -/.php_cs.cache +/.php-cs-fixer.cache diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml new file mode 100644 index 0000000..2ca5807 --- /dev/null +++ b/Configuration/Services.yaml @@ -0,0 +1,8 @@ +services: + _defaults: + autowire: true + autoconfigure: true + public: false + + B13\JustInCase\: + resource: '../src/*' diff --git a/Configuration/SiteConfiguration/Overrides/sites.php b/Configuration/SiteConfiguration/Overrides/sites.php index d11d7e4..d846b3d 100644 --- a/Configuration/SiteConfiguration/Overrides/sites.php +++ b/Configuration/SiteConfiguration/Overrides/sites.php @@ -37,5 +37,5 @@ $GLOBALS['SiteConfiguration']['site_language']['types']['1']['showitem'] = str_replace( 'flag', 'flag, ,--palette--;;justincase', - (string) $GLOBALS['SiteConfiguration']['site_language']['types']['1']['showitem'] + (string)$GLOBALS['SiteConfiguration']['site_language']['types']['1']['showitem'] ); diff --git a/README.md b/README.md index 939f2b7..f3aade2 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,11 @@ If you wish to enable redirect for all languages, add these lines at the bottom Please note that this option only works for GET or HEAD requests. +## Events + +a ```BeforeMiddlewareIsAppliedEvent``` is fired before LowerCaseUri Middleware is applied. +you can prevent applying Middleware by calling `event->doNotApply` + ## Caveats If specific route enhancers check on camel-case (e.g. `{order}/paymentForm/`) this might lead to unexpected behaviours diff --git a/composer.json b/composer.json index 7f05d1e..aef502a 100644 --- a/composer.json +++ b/composer.json @@ -1,33 +1,38 @@ { - "name": "b13/justincase", - "type": "typo3-cms-extension", - "description": "With incoming URLs, it does not matter if they are upper/lowercase, they just work.", - "license": "GPL-2.0-or-later", - "require": { - "php": "^7.2 || ^8.0", - "typo3/cms-core": "^9.5 || ^10.0 || ^11.0 || ^12.0" - }, - "replace": { - "typo3-ter/justincase": "self.version" - }, - "extra": { - "typo3/cms": { - "extension-key": "justincase" - } - }, - "autoload": { - "psr-4": { - "B13\\JustInCase\\": "src/" - } - }, - "require-dev": { - "typo3/coding-standards": "^0.2.0", - "typo3/tailor": "^1.1" - }, - "config": { - "sort-packages": true - }, - "scripts": { - "php:cs": "@composer exec php-cs-fixer fix" - } + "name": "b13/justincase", + "type": "typo3-cms-extension", + "description": "With incoming URLs, it does not matter if they are upper/lowercase, they just work.", + "license": "GPL-2.0-or-later", + "require": { + "typo3/cms-core": "^11.5 || ^12.4 || ^13.1" + }, + "replace": { + "typo3-ter/justincase": "self.version" + }, + "extra": { + "typo3/cms": { + "extension-key": "justincase" + } + }, + "autoload": { + "psr-4": { + "B13\\JustInCase\\": "src/" + } + }, + "require-dev": { + "typo3/coding-standards": "^0.5.5", + "saschaegerer/phpstan-typo3": "^1.8", + "typo3/tailor": "^1.1" + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "typo3/cms-composer-installers": true, + "typo3/class-alias-loader": true + } + }, + "scripts": { + "php:cs": "@composer exec 'php-cs-fixer fix --config=php-cs-fixer.php'", + "php:phpstan": "@composer exec 'phpstan analyse -c phpstan.neon'" + } } diff --git a/.php_cs b/php-cs-fixer.php similarity index 55% rename from .php_cs rename to php-cs-fixer.php index cc0061d..1f8d70e 100644 --- a/.php_cs +++ b/php-cs-fixer.php @@ -1,5 +1,5 @@ getFinder()->in(__DIR__); +$config->getFinder()->exclude(['public', 'vendor'])->in(__DIR__); return $config; diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..72cba8d --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,6 @@ +parameters: + level: 5 + + paths: + - %currentWorkingDirectory%/src + diff --git a/src/Middleware/BeforeMiddlewareIsAppliedEvent.php b/src/Middleware/BeforeMiddlewareIsAppliedEvent.php new file mode 100644 index 0000000..2c7430d --- /dev/null +++ b/src/Middleware/BeforeMiddlewareIsAppliedEvent.php @@ -0,0 +1,41 @@ +serverRequest = $serverRequest; + } + + public function doNotApply(): void + { + $this->shouldBeApplied = false; + } + + public function getServerRequest(): ServerRequestInterface + { + return $this->serverRequest; + } + + public function shouldBeApplied(): bool + { + return $this->shouldBeApplied; + } +} diff --git a/src/Middleware/LowerCaseUri.php b/src/Middleware/LowerCaseUri.php index f60bd29..306eb72 100644 --- a/src/Middleware/LowerCaseUri.php +++ b/src/Middleware/LowerCaseUri.php @@ -16,6 +16,7 @@ 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\RedirectResponse; use TYPO3\CMS\Core\Routing\RouteNotFoundException; use TYPO3\CMS\Core\Routing\SiteRouteResult; @@ -30,8 +31,20 @@ */ class LowerCaseUri implements MiddlewareInterface { + protected EventDispatcher $eventDispatcher; + + public function __construct(EventDispatcher $eventDispatcher) + { + $this->eventDispatcher = $eventDispatcher; + } + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { + $beforeMiddlewareIsAppliedEvent = new BeforeMiddlewareIsAppliedEvent($request); + $this->eventDispatcher->dispatch($beforeMiddlewareIsAppliedEvent); + if ($beforeMiddlewareIsAppliedEvent->shouldBeApplied() === false) { + return $handler->handle($request); + } /** @var Site $site */ $site = $request->getAttribute('site'); @@ -60,8 +73,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $redirectStatusCode = 307; if ($site instanceof Site) { $siteLanguage = $request->getAttribute('language')->toArray(); - $doRedirect = (bool) ($site->getConfiguration()['settings']['redirectOnUpperCase'] ?? $siteLanguage['redirectOnUpperCase'] ?? false); - $redirectStatusCode = (int) ($site->getConfiguration()['settings']['redirectStatusCode'] ?? $siteLanguage['redirectStatusCode'] ?? 307); + $doRedirect = (bool)($site->getConfiguration()['settings']['redirectOnUpperCase'] ?? $siteLanguage['redirectOnUpperCase'] ?? false); + $redirectStatusCode = (int)($site->getConfiguration()['settings']['redirectStatusCode'] ?? $siteLanguage['redirectStatusCode'] ?? 307); } // Redirects only work on GET and HEAD requests if ($doRedirect && in_array($request->getMethod(), ['GET', 'HEAD'])) { @@ -76,7 +89,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $updatedUri, $routeResult->getSite(), $routeResult->getLanguage(), - mb_strtolower((string) $routeResult->getTail()) + mb_strtolower((string)$routeResult->getTail()) ); $request = $request->withAttribute('routing', $routeResult); }