-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFusionAutoconfigurationMiddleware.php
More file actions
95 lines (77 loc) · 2.96 KB
/
FusionAutoconfigurationMiddleware.php
File metadata and controls
95 lines (77 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace Flowpack\FullPageCache\Middleware;
use Neos\Flow\Annotations as Flow;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Flowpack\FullPageCache\Aspects\ContentCacheAspect;
use Flowpack\FullPageCache\Cache\MetadataAwareStringFrontend;
class FusionAutoconfigurationMiddleware implements MiddlewareInterface
{
public const HEADER_ENABLED = 'X-FullPageCache-EnableFusionAutoconfiguration';
/**
* @var MetadataAwareStringFrontend
*/
#[Flow\Inject]
protected $contentCache;
/**
* @var ContentCacheAspect
*/
#[Flow\Inject]
protected $contentCacheAspect;
#[Flow\InjectConfiguration('enabled')]
protected bool $enabled;
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (!$this->enabled || !$request->hasHeader(RequestCacheMiddleware::HEADER_ENABLED)) {
return $handler->handle($request)->withoutHeader(self::HEADER_ENABLED);
}
$response = $handler->handle($request);
if (!$response->hasHeader(self::HEADER_ENABLED)) {
return $response;
}
$response = $response->withoutHeader(self::HEADER_ENABLED);
[$hasUncachedSegments, $tags, $lifetime] = $this->getFusionCacheInformation();
if ($hasUncachedSegments || $response->hasHeader('Set-Cookie')) {
return $response;
}
$response = $response
->withHeader(RequestCacheMiddleware::HEADER_ENABLED, "");
if ($tags) {
$response = $response
->withHeader(RequestCacheMiddleware::HEADER_TAGS, $tags);
}
if ($lifetime) {
$response = $response
->withHeader(RequestCacheMiddleware::HEADER_LIFETIME, $lifetime);
}
return $response;
}
/**
* Get cache tags and lifetime from the cache metadata that was extracted by the special cache frontend for content cache
*
* @return array with first "hasUncachedSegments", "tags" and "lifetime"
*/
public function getFusionCacheInformation(): array
{
$lifetime = null;
$tags = [];
$entriesMetadata = $this->contentCache->getAllMetadata();
foreach ($entriesMetadata as $metadata) {
$entryTags = $metadata['tags'] ?? [];
$entryLifetime = $metadata['lifetime'] ?? null;
if ($entryLifetime !== null) {
if ($lifetime === null) {
$lifetime = $entryLifetime;
} else {
$lifetime = min($lifetime, $entryLifetime);
}
}
$tags = array_unique(array_merge($tags, $entryTags));
}
$hasUncachedSegments = $this->contentCacheAspect->hasUncachedSegments();
return [$hasUncachedSegments, $tags, $lifetime];
}
}