Skip to content

Commit d7475c8

Browse files
committed
TASK: Update code for PHP 8
1 parent 0a9f741 commit d7475c8

4 files changed

Lines changed: 51 additions & 103 deletions

File tree

Classes/Aspects/ContentCacheAspect.php

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,31 @@
44
use Neos\Cache\Frontend\StringFrontend;
55
use Neos\Flow\Annotations as Flow;
66
use Neos\Flow\Aop\JoinPointInterface;
7+
use Neos\Utility\Exception\PropertyNotAccessibleException;
78
use Neos\Utility\ObjectAccess;
89

9-
/**
10-
* @Flow\Aspect
11-
* @Flow\Scope("singleton")
12-
*/
10+
#[Flow\Aspect]
11+
#[Flow\Scope("singleton")]
1312
class ContentCacheAspect
1413
{
15-
private $hadUncachedSegments = false;
14+
private bool $hadUncachedSegments = false;
1615

17-
private $cacheTags = [];
18-
19-
/**
20-
* @var null|int
21-
*/
22-
private $shortestLifetime = null;
23-
24-
/**
25-
* @Flow\Inject
26-
* @var StringFrontend
27-
*/
28-
protected $cacheFrontend;
16+
#[Flow\Inject]
17+
protected StringFrontend $cacheFrontend;
2918

3019
/**
3120
* @Flow\Before("method(Neos\Fusion\Core\Cache\ContentCache->(createUncachedSegment)())")
3221
*/
33-
public function grabUncachedSegment(JoinPointInterface $joinPoint)
22+
public function grabUncachedSegment(JoinPointInterface $joinPoint): void
3423
{
3524
$this->hadUncachedSegments = true;
3625
}
3726

3827
/**
39-
* @Flow\Before("method(Neos\Neos\Fusion\Cache\ContentCacheFlusher->shutdownObject())")
40-
* @param JoinPointInterface $joinPoint
41-
*
42-
* @throws \Neos\Utility\Exception\PropertyNotAccessibleException
28+
* @throws PropertyNotAccessibleException
4329
*/
44-
public function interceptNodeCacheFlush(JoinPointInterface $joinPoint)
30+
#[Flow\Before("method(Neos\Neos\Fusion\Cache\ContentCacheFlusher->shutdownObject())")]
31+
public function interceptNodeCacheFlush(JoinPointInterface $joinPoint): void
4532
{
4633
$object = $joinPoint->getProxy();
4734

@@ -50,9 +37,6 @@ public function interceptNodeCacheFlush(JoinPointInterface $joinPoint)
5037
$this->cacheFrontend->flushByTags($tags);
5138
}
5239

53-
/**
54-
* @return bool
55-
*/
5640
public function hasUncachedSegments(): bool
5741
{
5842
return $this->hadUncachedSegments;
@@ -64,7 +48,7 @@ public function hasUncachedSegments(): bool
6448
* @param string $tag A tag which possibly contains non-allowed characters, for example "NodeType_Acme.Com:Page"
6549
* @return string A cleaned up tag, for example "NodeType_Acme_Com-Page"
6650
*/
67-
protected function sanitizeTag($tag)
51+
protected function sanitizeTag(string $tag): string
6852
{
6953
return strtr($tag, '.:', '_-');
7054
}

Classes/Cache/MetadataAwareStringFrontend.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,35 @@
1313
*/
1414
class MetadataAwareStringFrontend extends StringFrontend
1515
{
16-
const SEPARATOR = '|';
16+
protected const SEPARATOR = '|';
1717

1818
/**
1919
* Store metadata of all loaded cache entries indexed by identifier
20-
*
21-
* @var array
2220
*/
23-
protected $metadata = [];
21+
protected array $metadata = [];
2422

25-
/**
26-
* @Flow\Inject
27-
* @var Environment
28-
*/
29-
protected $environment;
23+
#[Flow\Inject]
24+
protected Environment $environment;
3025

31-
/**
32-
* @Flow\Inject
33-
* @var LoggerInterface
34-
*/
35-
protected $logger;
26+
#[Flow\Inject]
27+
protected LoggerInterface $logger;
3628

3729
/**
3830
* Set a cache entry and store additional metadata (tags and lifetime)
3931
*
4032
* {@inheritdoc}
4133
*/
42-
public function set(string $entryIdentifier, $content, array $tags = [], int $lifetime = null)
34+
public function set(string $entryIdentifier, $string, array $tags = [], int $lifetime = null): void
4335
{
44-
$content = $this->insertMetadata($content, $entryIdentifier, $tags, $lifetime);
36+
$content = $this->insertMetadata($string, $entryIdentifier, $tags, $lifetime);
4537
parent::set($entryIdentifier, $content, $tags, $lifetime);
4638
}
4739

4840
/**
4941
* {@inheritdoc}
42+
* @throws InvalidDataTypeException
5043
*/
51-
public function get(string $entryIdentifier)
44+
public function get(string $entryIdentifier): bool|string
5245
{
5346
$content = parent::get($entryIdentifier);
5447
if ($content !== false) {
@@ -60,6 +53,7 @@ public function get(string $entryIdentifier)
6053

6154
/**
6255
* {@inheritdoc}
56+
* @throws InvalidDataTypeException
6357
*/
6458
public function getByTag(string $tag): array
6559
{
@@ -74,18 +68,13 @@ public function getByTag(string $tag): array
7468
/**
7569
* Insert metadata into the content
7670
*
77-
* @param string $content
7871
* @param string $entryIdentifier The identifier metadata
7972
* @param array $tags The tags metadata
80-
* @param integer $lifetime The lifetime metadata
73+
* @param integer|null $lifetime The lifetime metadata
8174
* @return string The content including the serialized metadata
82-
* @throws InvalidDataTypeException
8375
*/
84-
protected function insertMetadata($content, $entryIdentifier, array $tags, $lifetime)
76+
protected function insertMetadata(string $content, string $entryIdentifier, array $tags, int $lifetime = null): string
8577
{
86-
if (!is_string($content)) {
87-
throw new InvalidDataTypeException('Given data is of type "' . gettype($content) . '", but a string is expected for string cache.', 1433155737);
88-
}
8978
$metadata = [
9079
'identifier' => $entryIdentifier,
9180
'tags' => $tags,
@@ -105,7 +94,7 @@ protected function insertMetadata($content, $entryIdentifier, array $tags, $life
10594
* @return string The content without metadata
10695
* @throws InvalidDataTypeException
10796
*/
108-
protected function extractMetadata($entryIdentifier, $content)
97+
protected function extractMetadata(string $entryIdentifier, string $content): string
10998
{
11099
$separatorIndex = strpos($content, self::SEPARATOR);
111100
if ($separatorIndex === false) {
@@ -136,7 +125,7 @@ protected function extractMetadata($entryIdentifier, $content)
136125
/**
137126
* @return array Metadata of all loaded entries (indexed by identifier)
138127
*/
139-
public function getAllMetadata()
128+
public function getAllMetadata(): array
140129
{
141130
return $this->metadata;
142131
}

Classes/Middleware/FusionAutoconfigurationMiddleware.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,19 @@ class FusionAutoconfigurationMiddleware implements MiddlewareInterface
1616
public const HEADER_ENABLED = 'X-FullPageCache-EnableFusionAutoconfiguration';
1717

1818
/**
19-
* @Flow\Inject
2019
* @var MetadataAwareStringFrontend
2120
*/
21+
#[Flow\Inject]
2222
protected $contentCache;
2323

2424
/**
25-
* @Flow\Inject
2625
* @var ContentCacheAspect
2726
*/
27+
#[Flow\Inject]
2828
protected $contentCacheAspect;
2929

30-
/**
31-
* @var boolean
32-
* @Flow\InjectConfiguration(path="enabled")
33-
*/
34-
protected $enabled;
30+
#[Flow\InjectConfiguration('enabled')]
31+
protected bool $enabled;
3532

3633
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
3734
{
@@ -43,13 +40,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4340

4441
if (!$response->hasHeader(self::HEADER_ENABLED)) {
4542
return $response;
46-
} else {
47-
$response = $response->withoutHeader(self::HEADER_ENABLED);
4843
}
4944

50-
list($hasUncachedSegments, $tags, $lifetime) = $this->getFusionCacheInformations();
45+
$response = $response->withoutHeader(self::HEADER_ENABLED);
46+
47+
[$hasUncachedSegments, $tags, $lifetime] = $this->getFusionCacheInformations();
5148

52-
if ($response->hasHeader('Set-Cookie') || $hasUncachedSegments) {
49+
if ($hasUncachedSegments || $response->hasHeader('Set-Cookie')) {
5350
return $response;
5451
}
5552

@@ -63,7 +60,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6360

6461
if ($lifetime) {
6562
$response = $response
66-
->withHeader(RequestCacheMiddleware::HEADER_LIFTIME, $lifetime);
63+
->withHeader(RequestCacheMiddleware::HEADER_LIFETIME, $lifetime);
6764
}
6865

6966
return $response;
@@ -80,8 +77,8 @@ public function getFusionCacheInformations(): array
8077
$tags = [];
8178
$entriesMetadata = $this->contentCache->getAllMetadata();
8279
foreach ($entriesMetadata as $identifier => $metadata) {
83-
$entryTags = isset($metadata['tags']) ? $metadata['tags'] : [];
84-
$entryLifetime = isset($metadata['lifetime']) ? $metadata['lifetime'] : null;
80+
$entryTags = $metadata['tags'] ?? [];
81+
$entryLifetime = $metadata['lifetime'] ?? null;
8582
if ($entryLifetime !== null) {
8683
if ($lifetime === null) {
8784
$lifetime = $entryLifetime;

Classes/Middleware/RequestCacheMiddleware.php

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,30 @@ class RequestCacheMiddleware implements MiddlewareInterface
1818

1919
public const HEADER_INFO = 'X-FullPageCache-Info';
2020

21-
public const HEADER_LIFTIME = 'X-FullPageCache-Lifetime';
21+
public const HEADER_LIFETIME = 'X-FullPageCache-Lifetime';
2222

2323
public const HEADER_TAGS = 'X-FullPageCache-Tags';
2424

25-
/**
26-
* @var boolean
27-
* @Flow\InjectConfiguration(path="enabled")
28-
*/
29-
protected $enabled;
25+
#[Flow\InjectConfiguration('enabled')]
26+
protected bool $enabled;
3027

3128
/**
32-
* @Flow\Inject
3329
* @var VariableFrontend
3430
*/
31+
#[Flow\Inject]
3532
protected $cacheFrontend;
3633

37-
/**
38-
* @var array
39-
* @Flow\InjectConfiguration(path="request.queryParams.allow")
40-
*/
41-
protected $allowedQueryParams;
34+
#[Flow\InjectConfiguration('request.queryParams.allow')]
35+
protected array $allowedQueryParams;
4236

43-
/**
44-
* @var array
45-
* @Flow\InjectConfiguration(path="request.queryParams.ignore")
46-
*/
47-
protected $ignoredQueryParams;
37+
#[Flow\InjectConfiguration('request.queryParams.ignore')]
38+
protected array $ignoredQueryParams;
4839

49-
/**
50-
* @var array
51-
* @Flow\InjectConfiguration(path="request.cookieParams.ignore")
52-
*/
53-
protected $ignoredCookieParams;
40+
#[Flow\InjectConfiguration('request.cookieParams.ignore')]
41+
protected array $ignoredCookieParams;
5442

55-
/**
56-
* @var boolean
57-
* @Flow\InjectConfiguration(path="maxPublicCacheTime")
58-
*/
59-
protected $maxPublicCacheTime;
43+
#[Flow\InjectConfiguration('maxPublicCacheTime')]
44+
protected int $maxPublicCacheTime;
6045

6146
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
6247
{
@@ -81,11 +66,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8166
$response = $next->handle($request->withHeader(self::HEADER_ENABLED, ''));
8267

8368
if ($response->hasHeader(self::HEADER_ENABLED)) {
84-
$lifetime = $response->hasHeader(self::HEADER_LIFTIME) ? (int)$response->getHeaderLine(self::HEADER_LIFTIME) : null;
69+
$lifetime = $response->hasHeader(self::HEADER_LIFETIME) ? (int)$response->getHeaderLine(self::HEADER_LIFETIME) : null;
8570
$tags = $response->hasHeader(self::HEADER_TAGS) ? $response->getHeader(self::HEADER_TAGS) : [];
8671
$response = $response
8772
->withoutHeader(self::HEADER_ENABLED)
88-
->withoutHeader(self::HEADER_LIFTIME)
73+
->withoutHeader(self::HEADER_LIFETIME)
8974
->withoutHeader(self::HEADER_TAGS);
9075

9176
$publicLifetime = 0;
@@ -113,11 +98,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
11398
return $response;
11499
}
115100

116-
117-
/**
118-
* @param ServerRequestInterface $request
119-
* @return string|null
120-
*/
121101
protected function getCacheIdentifierForRequestIfCacheable(ServerRequestInterface $request): ?string
122102
{
123103
if (!in_array(strtoupper($request->getMethod()), ['GET', 'HEAD'])) {
@@ -126,15 +106,13 @@ protected function getCacheIdentifierForRequestIfCacheable(ServerRequestInterfac
126106

127107
$requestQueryParams = $request->getQueryParams();
128108
$allowedQueryParams = [];
129-
$ignoredQueryParams = [];
130109
$disallowedQueryParams = [];
131110
foreach ($requestQueryParams as $key => $value) {
132111
switch (true) {
133112
case (in_array($key, $this->allowedQueryParams)):
134113
$allowedQueryParams[$key] = $value;
135114
break;
136115
case (in_array($key, $this->ignoredQueryParams)):
137-
$ignoredQueryParams[$key] = $value;
138116
break;
139117
default:
140118
$disallowedQueryParams[$key] = $value;

0 commit comments

Comments
 (0)