11<?php
2+
23declare (strict_types=1 );
34
45namespace Flowpack \FullPageCache \Middleware ;
56
7+ use Flowpack \FullPageCache \Domain \Dto \CacheEntry ;
68use Neos \Flow \Annotations as Flow ;
79use Neos \Cache \Frontend \VariableFrontend ;
810use Psr \Http \Message \ResponseInterface ;
911use Psr \Http \Message \ServerRequestInterface ;
1012use Psr \Http \Server \MiddlewareInterface ;
1113use Psr \Http \Server \RequestHandlerInterface ;
1214use GuzzleHttp \Psr7 \Message ;
13- use function GuzzleHttp \Psr7 \str ;
1415
16+ /**
17+ * @phpstan-type CacheEntryShape array{timestamp:int, response:string}
18+ */
1519class RequestCacheMiddleware implements MiddlewareInterface
1620{
1721 public const HEADER_ENABLED = 'X-FullPageCache-Enabled ' ;
1822
1923 public const HEADER_INFO = 'X-FullPageCache-Info ' ;
2024
25+ // @deprecated use HEADER_LIFETIME instead
2126 public const HEADER_LIFTIME = 'X-FullPageCache-Lifetime ' ;
27+ public const HEADER_LIFETIME = 'X-FullPageCache-Lifetime ' ;
2228
2329 public const HEADER_TAGS = 'X-FullPageCache-Tags ' ;
2430
@@ -35,29 +41,35 @@ class RequestCacheMiddleware implements MiddlewareInterface
3541 protected $ cacheFrontend ;
3642
3743 /**
38- * @var array
44+ * @var string[]
3945 * @Flow\InjectConfiguration(path="request.queryParams.allow")
4046 */
4147 protected $ allowedQueryParams ;
4248
4349 /**
44- * @var array
50+ * @var string[]
4551 * @Flow\InjectConfiguration(path="request.queryParams.ignore")
4652 */
4753 protected $ ignoredQueryParams ;
4854
4955 /**
50- * @var array
56+ * @var string[]
5157 * @Flow\InjectConfiguration(path="request.cookieParams.ignore")
5258 */
5359 protected $ ignoredCookieParams ;
5460
5561 /**
56- * @var boolean
62+ * @var int
5763 * @Flow\InjectConfiguration(path="maxPublicCacheTime")
5864 */
5965 protected $ maxPublicCacheTime ;
6066
67+ /**
68+ * @var int
69+ * @Flow\InjectConfiguration(path="maxSharedCacheTime")
70+ */
71+ protected $ maxSharedCacheTime ;
72+
6173 public function process (ServerRequestInterface $ request , RequestHandlerInterface $ next ): ResponseInterface
6274 {
6375 if (!$ this ->enabled ) {
@@ -70,22 +82,24 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
7082 return $ next ->handle ($ request )->withHeader (self ::HEADER_INFO , 'SKIP ' );
7183 }
7284
73- if ($ cacheEntry = $ this ->cacheFrontend ->get ($ entryIdentifier )) {
85+ /** @var ?CacheEntryShape $cacheEntry */
86+ $ cacheEntry = $ this ->cacheFrontend ->get ($ entryIdentifier );
87+ if ($ cacheEntry ) {
7488 $ age = time () - $ cacheEntry ['timestamp ' ];
7589 $ response = Message ::parseResponse ($ cacheEntry ['response ' ]);
7690 return $ response
77- ->withHeader ('Age ' , $ age )
91+ ->withHeader ('Age ' , ( string ) $ age )
7892 ->withHeader (self ::HEADER_INFO , 'HIT: ' . $ entryIdentifier );
7993 }
8094
8195 $ response = $ next ->handle ($ request ->withHeader (self ::HEADER_ENABLED , '' ));
8296
8397 if ($ response ->hasHeader (self ::HEADER_ENABLED )) {
84- $ lifetime = $ response ->hasHeader (self ::HEADER_LIFTIME ) ? (int )$ response ->getHeaderLine (self ::HEADER_LIFTIME ) : null ;
98+ $ lifetime = $ response ->hasHeader (self ::HEADER_LIFETIME ) ? (int )$ response ->getHeaderLine (self ::HEADER_LIFETIME ) : null ;
8599 $ tags = $ response ->hasHeader (self ::HEADER_TAGS ) ? $ response ->getHeader (self ::HEADER_TAGS ) : [];
86100 $ response = $ response
87101 ->withoutHeader (self ::HEADER_ENABLED )
88- ->withoutHeader (self ::HEADER_LIFTIME )
102+ ->withoutHeader (self ::HEADER_LIFETIME )
89103 ->withoutHeader (self ::HEADER_TAGS );
90104
91105 $ publicLifetime = 0 ;
@@ -105,8 +119,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
105119 ->withHeader ('Cache-Control ' , 'public, max-age= ' . $ publicLifetime );
106120 }
107121
108- $ this ->cacheFrontend ->set ($ entryIdentifier ,[ 'timestamp ' => time (), 'response ' => Message ::toString ($ response ) ], $ tags , $ lifetime );
109- $ response ->getBody ()->rewind ();
122+ /** @var CacheEntryShape $cacheEntry */
123+ $ cacheEntry = [ 'timestamp ' => time (), 'response ' => Message ::toString ($ response ) ];
124+ $ this ->cacheFrontend ->set ($ entryIdentifier , $ cacheEntry , $ tags , $ lifetime );
110125 return $ response ->withHeader (self ::HEADER_INFO , 'MISS: ' . $ entryIdentifier );
111126 }
112127
0 commit comments