Skip to content

Commit 6928b5c

Browse files
committed
More migration to DI
1 parent 75e2315 commit 6928b5c

File tree

10 files changed

+28
-90
lines changed

10 files changed

+28
-90
lines changed

Classes/Cache/IdentifierBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class IdentifierBuilder
1616
public function __construct(protected ?EventDispatcherInterface $eventDispatcher = null)
1717
{
1818
if ($this->eventDispatcher === null) {
19-
$this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class);
19+
$this->eventDispatcher = GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
2020
}
2121
}
2222

Classes/Cache/Listener/CachingAllowedListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function __construct(private readonly Typo3Version $typo3Version) {}
1515
public function __invoke(CacheRuleEvent $event): void
1616
{
1717
if ($this->typo3Version->getMajorVersion() >= 13) {
18+
/* @phpstan-ignore-next-line */
1819
if (!$event->getRequest()->getAttribute('frontend.cache.instruction')->isCachingAllowed()) {
1920
$event->addExplanation(__CLASS__, 'No caching via frontend.cache.instruction attribute');
2021
}

Classes/Cache/Listener/EnableListener.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66

77
use SFC\Staticfilecache\Event\CacheRuleEvent;
88
use SFC\Staticfilecache\Service\ConfigurationService;
9-
use TYPO3\CMS\Core\Utility\GeneralUtility;
109

1110
class EnableListener
1211
{
12+
public function __construct(protected readonly ConfigurationService $configurationService) {}
1313
public function __invoke(CacheRuleEvent $event): void
1414
{
15-
/** @var ConfigurationService $configuration */
16-
$configuration = GeneralUtility::makeInstance(ConfigurationService::class);
17-
if ($configuration->isBool('disableCache')) {
15+
if ($this->configurationService->isBool('disableCache')) {
1816
$event->addExplanation(__CLASS__, 'static cache disabled by TypoScript');
1917
}
2018
}

Classes/Cache/Listener/NoUserOrGroupSetListener.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66

77
use SFC\Staticfilecache\Event\CacheRuleEvent;
88
use TYPO3\CMS\Core\Context\Context;
9-
use TYPO3\CMS\Core\Utility\GeneralUtility;
109

1110
class NoUserOrGroupSetListener
1211
{
13-
/**
14-
* Check if no user or group is set.
15-
*/
12+
public function __construct(protected readonly Context $context) {}
13+
1614
public function __invoke(CacheRuleEvent $event): void
1715
{
18-
$context = GeneralUtility::makeInstance(Context::class);
19-
if ($context->getAspect('frontend.user')->isUserOrGroupSet()) {
16+
if ($this->context->getAspect('frontend.user')->isUserOrGroupSet()) {
2017
$event->addExplanation(__CLASS__, 'User or group are set');
2118
}
2219
}

Classes/Middleware/FallbackMiddleware.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function __construct(
2424
protected readonly EventDispatcherInterface $eventDispatcher,
2525
protected readonly ConfigurationService $configurationService,
2626
protected readonly IdentifierBuilder $identifierBuilder,
27+
protected readonly CacheService $cacheService,
2728
) {}
2829

2930
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
@@ -67,7 +68,7 @@ protected function handleViaFallback(ServerRequestInterface $request): ResponseI
6768
throw new Exception('StaticFileCache file not found', 126371823);
6869
}
6970

70-
$cacheDirectory = GeneralUtility::makeInstance(CacheService::class)->getAbsoluteBaseDirectory();
71+
$cacheDirectory = $this->cacheService->getAbsoluteBaseDirectory();
7172
if (!str_starts_with($possibleStaticFile, $cacheDirectory)) {
7273
throw new Exception('The path is not in the cache directory', 348923472);
7374
}

Classes/Middleware/GenerateMiddleware.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public function __construct(
3131
readonly protected EventDispatcherInterface $eventDispatcher,
3232
readonly protected CookieService $cookieService,
3333
readonly protected Typo3Version $typo3Version,
34-
readonly protected CacheService $cacheService
34+
readonly protected CacheService $cacheService,
35+
readonly protected ConfigurationService $configurationService
3536
) {}
3637

3738
/**
@@ -137,7 +138,7 @@ protected function hasValidCacheEntry($uri): bool
137138
*/
138139
protected function removeSfcHeaders(ResponseInterface $response): ResponseInterface
139140
{
140-
$debug = GeneralUtility::makeInstance(ConfigurationService::class)->isBool('debugHeaders');
141+
$debug = $this->configurationService->isBool('debugHeaders');
141142
if (!$debug) {
142143
$response = $response->withoutHeader('X-SFC-Cachable');
143144
$response = $response->withoutHeader('X-SFC-State');

Classes/Middleware/PrepareMiddleware.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
class PrepareMiddleware implements MiddlewareInterface
2121
{
2222
public function __construct(
23-
protected EventDispatcherInterface $eventDispatcher,
24-
protected HttpPushService $httpPushService
23+
protected readonly EventDispatcherInterface $eventDispatcher,
24+
protected readonly HttpPushService $httpPushService,
25+
protected readonly ConfigurationService $configurationService,
26+
protected readonly TypoScriptFrontendService $typoScriptFrontendService,
27+
protected readonly InlineAssetsService $inlineAssetsService
2528
) {}
2629

2730
/**
@@ -42,9 +45,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4245
$this->eventDispatcher->dispatch($event);
4346

4447
if (!$event->isSkipProcessing()) {
45-
$cacheTags = GeneralUtility::makeInstance(TypoScriptFrontendService::class)->getTags();
46-
$configuration = GeneralUtility::makeInstance(ConfigurationService::class);
47-
if (false === (bool) $configuration->get('clearCacheForAllDomains')) {
48+
$cacheTags = $this->typoScriptFrontendService->getTags();
49+
if (false === $this->configurationService->isBool('clearCacheForAllDomains')) {
4850
$cacheTags[] = 'sfc_domain_' . str_replace('.', '_', $event->getRequest()->getUri()->getHost());
4951
}
5052

@@ -63,7 +65,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6365
}
6466
}
6567

66-
$processedHtml = (string) GeneralUtility::makeInstance(InlineAssetsService::class)->replaceInlineContent((string) $response->getBody());
68+
$processedHtml = (string) $this->inlineAssetsService->replaceInlineContent((string) $response->getBody());
6769
$responseBody = new Stream('php://temp', 'rw');
6870
$responseBody->write($processedHtml);
6971
$response = $response->withBody($responseBody);

Classes/Service/TypoScriptFrontendService.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ public function getTags(): array
2121
return array_unique((array) $tsfe->getPageCacheTags());
2222
}
2323

24-
/**
25-
* Get the TSFE.
26-
*
27-
*/
2824
protected function getTsfe(): ?TypoScriptFrontendController
2925
{
3026
return $GLOBALS['TSFE'];

Tests/Unit/Cache/IdentifierBuilderTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace SFC\Staticfilecache\Tests\Unit\Cache;
66

7+
use Psr\EventDispatcher\EventDispatcherInterface;
78
use SFC\Staticfilecache\Cache\IdentifierBuilder;
89
use SFC\Staticfilecache\Tests\Unit\AbstractTest;
10+
use TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher;
911

1012
/**
1113
* IdentifierBuilderTest.
@@ -27,8 +29,7 @@ public function testCheckValidPath(): void
2729
];
2830

2931
foreach ($validUris as $uri) {
30-
$identBuilder = new IdentifierBuilder();
31-
static::assertTrue($identBuilder->isValidEntryIdentifier($uri), 'The URI "' . $uri . '" should be valid!');
32+
static::assertTrue($this->getIdentifierBuilder()->isValidEntryIdentifier($uri), 'The URI "' . $uri . '" should be valid!');
3233
}
3334
}
3435

@@ -42,8 +43,11 @@ public function testCheckInValidPath(): void
4243
];
4344

4445
foreach ($invalidUris as $uri) {
45-
$identBuilder = new IdentifierBuilder();
46-
static::assertFalse($identBuilder->isValidEntryIdentifier($uri), 'The URI "' . $uri . '" should be invalid!');
46+
static::assertFalse($this->getIdentifierBuilder()->isValidEntryIdentifier($uri), 'The URI "' . $uri . '" should be invalid!');
4747
}
4848
}
49+
50+
protected function getIdentifierBuilder(): IdentifierBuilder {
51+
return new IdentifierBuilder(new NoopEventDispatcher());
52+
}
4953
}

Tests/Unit/Cache/UriFrontendTest.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)