Skip to content

Commit

Permalink
Migration from Rule structure to PSR-14 Event Listener
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Oct 6, 2024
1 parent f5ab7e4 commit 117848c
Show file tree
Hide file tree
Showing 27 changed files with 234 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@

declare(strict_types=1);

namespace SFC\Staticfilecache\Cache\Rule;
namespace SFC\Staticfilecache\Cache\Listener;

use Psr\Http\Message\ServerRequestInterface;
use SFC\Staticfilecache\Event\CacheRuleEvent;
use SFC\Staticfilecache\Service\ConfigurationService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Enable.
*/
class Enable extends AbstractRule
class EnableListener
{
/**
* Enable.
*/
public function checkRule(ServerRequestInterface $request, array &$explanation, bool &$skipProcessing): void
public function __invoke(CacheRuleEvent $event): void
{
/** @var ConfigurationService $configuration */
$configuration = GeneralUtility::makeInstance(ConfigurationService::class);
if ($configuration->isBool('disableCache')) {
$explanation[__CLASS__] = 'static cache disabled by TypoScript';
$event->addExplanation(__CLASS__, 'static cache disabled by TypoScript');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

declare(strict_types=1);

namespace SFC\Staticfilecache\Cache\Rule;
namespace SFC\Staticfilecache\Cache\Listener;

use Psr\Http\Message\ServerRequestInterface;
use SFC\Staticfilecache\Event\CacheRuleEvent;

/**
* No fake frontend.
*/
class NoFakeFrontend extends AbstractRule
class NoFakeFrontendListener
{
/**
* No fake frontend.
*/
public function checkRule(ServerRequestInterface $request, array &$explanation, bool &$skipProcessing): void
public function __invoke(CacheRuleEvent $event): void
{
$ignorePaths = [
// Solr extension
Expand All @@ -24,17 +18,16 @@ public function checkRule(ServerRequestInterface $request, array &$explanation,
foreach ($ignorePaths as $ignorePath) {
foreach ($this->getCallPaths() as $path) {
if (str_ends_with($path, $ignorePath)) {
$skipProcessing = true;
$explanation[__CLASS__] = 'Fake frontend';

$event->setSkipProcessing(true);
$event->addExplanation(__CLASS__, 'Fake frontend');
return;
}
}
}

if ($request->hasHeader('x-yoast-page-request')) {
$skipProcessing = true;
$explanation[__CLASS__] = 'Yoast SEO page request';
if ($event->getRequest()->hasHeader('x-yoast-page-request')) {
$event->setSkipProcessing(true);
$event->addExplanation(__CLASS__, 'Yoast SEO page request');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@

declare(strict_types=1);

namespace SFC\Staticfilecache\Cache\Rule;
namespace SFC\Staticfilecache\Cache\Listener;

use Psr\Http\Message\ServerRequestInterface;
use SFC\Staticfilecache\Event\CacheRuleEvent;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* No _INT scripts.
*/
class NoIntScripts extends AbstractRule
class NoIntScriptsListener
{
/**
* Check if there are no _INT scripts.
*/
public function checkRule(ServerRequestInterface $request, array &$explanation, bool &$skipProcessing): void
public function __invoke(CacheRuleEvent $event): void
{
$tsfe = $GLOBALS['TSFE'] ?? null;
if ($tsfe instanceof TypoScriptFrontendController && $tsfe->isINTincScript()) {
foreach ((array) $tsfe->config['INTincScript'] as $key => $configuration) {
$explanation[__CLASS__ . ':' . $key] = 'The page has a INTincScript: ' . implode(', ', $this->getInformation($configuration));
$event->addExplanation(__CLASS__ . ':' . $key, 'The page has a INTincScript: ' . implode(', ', $this->getInformation($configuration)));
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions Classes/Cache/Listener/NoLongPathSegmentListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace SFC\Staticfilecache\Cache\Listener;

use SFC\Staticfilecache\Event\CacheRuleEvent;

/**
* Check if there is no path segment that is to long.
*/
class NoLongPathSegmentListener
{
/**
* Check if there is no path segment that is to long.
*/
public function __invoke(CacheRuleEvent $event): void
{
$uri = (string) $event->getRequest()->getUri();
$path = (string) parse_url($uri, PHP_URL_PATH);
$segments = explode('/', $path);

foreach ($segments as $segment) {
if (\strlen($segment) > 255) {
$event->addExplanation(__CLASS__, 'The URI seegment of the URI is to long to create a folder based on tthis segment: ' . $segment);
$event->setSkipProcessing(true);

return;
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

declare(strict_types=1);

namespace SFC\Staticfilecache\Cache\Rule;
namespace SFC\Staticfilecache\Cache\Listener;

use Psr\Http\Message\ServerRequestInterface;
use SFC\Staticfilecache\Event\CacheRuleEvent;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* No no_cache.
*/
class NoNoCache extends AbstractRule
class NoNoCacheListener
{
/**
* No no_cache.
*/
public function checkRule(ServerRequestInterface $request, array &$explanation, bool &$skipProcessing): void
public function __invoke(CacheRuleEvent $event): void
{
$tsfe = $GLOBALS['TSFE'] ?? null;
/* @phpstan-ignore-next-line */
if ($tsfe instanceof TypoScriptFrontendController && $tsfe->no_cache) {
$explanation[__CLASS__] = 'config.no_cache is true';
$event->addExplanation(__CLASS__, 'config.no_cache is true');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@

declare(strict_types=1);

namespace SFC\Staticfilecache\Cache\Rule;
namespace SFC\Staticfilecache\Cache\Listener;

use Psr\Http\Message\ServerRequestInterface;
use SFC\Staticfilecache\Event\CacheRuleEvent;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* NoUserOrGroupSet.
*/
class NoUserOrGroupSet extends AbstractRule
class NoUserOrGroupSetListener
{
/**
* Check if no user or group is set.
*/
public function checkRule(ServerRequestInterface $request, array &$explanation, bool &$skipProcessing): void
public function __invoke(CacheRuleEvent $event): void
{
if ($this->isUserOrGroupSet()) {
$explanation[__CLASS__] = 'User or group are set';
$event->addExplanation(__CLASS__, 'User or group are set');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

declare(strict_types=1);

namespace SFC\Staticfilecache\Cache\Rule;
namespace SFC\Staticfilecache\Cache\Listener;

use Psr\Http\Message\ServerRequestInterface;
use SFC\Staticfilecache\Event\CacheRuleEvent;
use TYPO3\CMS\Core\Context\Context;

/**
* No workspace preview.
*/
class NoWorkspacePreview extends AbstractRule
class NoWorkspacePreviewListener
{
public function __construct(
private readonly Context $context,
Expand All @@ -19,10 +17,10 @@ public function __construct(
/**
* Check if it is no workspace preview.
*/
public function checkRule(ServerRequestInterface $request, array &$explanation, bool &$skipProcessing): void
public function __invoke(CacheRuleEvent $event): void
{
if ($this->context->getPropertyFromAspect('workspace', 'isOffline', false)) {
$explanation[__CLASS__] = 'The page is in workspace preview mode';
$event->addExplanation(__CLASS__, 'The page is in workspace preview mode');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@

declare(strict_types=1);

namespace SFC\Staticfilecache\Cache\Rule;
namespace SFC\Staticfilecache\Cache\Listener;

use Psr\Http\Message\ServerRequestInterface;
use SFC\Staticfilecache\Event\CacheRuleEvent;
use TYPO3\CMS\Core\Site\Entity\Site;

/**
* Check if the current site is static cacheable.
*/
class SiteCacheable extends AbstractRule
class SiteCacheableListener
{
/**
* Check if the current site is static cacheable.
*/
public function checkRule(ServerRequestInterface $request, array &$explanation, bool &$skipProcessing): void
public function __invoke(CacheRuleEvent $event): void
{
$site = $request->getAttribute('site');
$site = $event->getRequest()->getAttribute('site');
if (!($site instanceof Site)) {
return;
}
$config = $site->getConfiguration();
if (isset($config['disableStaticFileCache']) && $config['disableStaticFileCache']) {
$explanation[__CLASS__] = 'static cache disabled on site configuration: ' . $site->getIdentifier();
$event->addExplanation(__CLASS__, 'static cache disabled on site configuration: ' . $site->getIdentifier());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@

declare(strict_types=1);

namespace SFC\Staticfilecache\Cache\Rule;
namespace SFC\Staticfilecache\Cache\Listener;

use Psr\Http\Message\ServerRequestInterface;
use SFC\Staticfilecache\Event\CacheRuleEvent;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Check if the doktype is valid.
*/
class ValidDoktype extends AbstractRule
class ValidDoktypeListener
{
/**
* Check if the URI is valid.
* Check if the doktype is valid.
*/
public function checkRule(ServerRequestInterface $request, array &$explanation, bool &$skipProcessing): void
public function __invoke(CacheRuleEvent $event): void
{
$tsfe = $GLOBALS['TSFE'] ?? null;
if (!($tsfe instanceof TypoScriptFrontendController) || !isset($GLOBALS['TSFE']->page)) {
$explanation[__CLASS__] = 'There is no valid page in the frontendController object';
$skipProcessing = true;
$event->addExplanation(__CLASS__, 'There is no valid page in the frontendController object');
$event->setSkipProcessing(true);

return;
}
Expand All @@ -33,11 +31,12 @@ public function checkRule(ServerRequestInterface $request, array &$explanation,

$currentType = (int) ($GLOBALS['TSFE']->page['doktype'] ?? 1);
if (\in_array($currentType, $ignoreTypes, true)) {
$explanation[__CLASS__] = 'The Page doktype ' . $currentType . ' is one of the following not allowed numbers: ' . implode(

$event->addExplanation(__CLASS__, 'The Page doktype ' . $currentType . ' is one of the following not allowed numbers: ' . implode(
', ',
$ignoreTypes
);
$skipProcessing = true;
));
$event->setSkipProcessing(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@

declare(strict_types=1);

namespace SFC\Staticfilecache\Cache\Rule;
namespace SFC\Staticfilecache\Cache\Listener;

use Psr\Http\Message\ServerRequestInterface;
use SFC\Staticfilecache\Event\CacheRuleEvent;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* ValidPageInformation.
*
* @see https://github.com/lochmueller/staticfilecache/issues/150
*/
class ValidPageInformation extends AbstractRule
class ValidPageInformationListener
{
/**
* ValidPageInformation.
*/
public function checkRule(ServerRequestInterface $request, array &$explanation, bool &$skipProcessing): void
public function __invoke(CacheRuleEvent $event): void
{
$tsfe = $GLOBALS['TSFE'] ?? null;
if (!$tsfe instanceof TypoScriptFrontendController || !\is_array($tsfe->page) || !$tsfe->page['uid']) {
$skipProcessing = true;
$explanation[__CLASS__] = 'There is no valid page in the TSFE';
$event->setSkipProcessing(true);
$event->addExplanation(__CLASS__, 'There is no valid page in the TSFE');
}
}
}
19 changes: 0 additions & 19 deletions Classes/Cache/Rule/AbstractRule.php

This file was deleted.

Loading

0 comments on commit 117848c

Please sign in to comment.