1313
1414namespace Toflar \Psr6HttpCacheStore ;
1515
16- use Psr \Cache \CacheItemInterface ;
1716use Psr \Cache \InvalidArgumentException as CacheInvalidArgumentException ;
1817use Symfony \Component \Cache \Adapter \AdapterInterface ;
1918use Symfony \Component \Cache \Adapter \FilesystemTagAwareAdapter ;
2019use Symfony \Component \Cache \Adapter \TagAwareAdapterInterface ;
20+ use Symfony \Component \Cache \CacheItem ;
2121use Symfony \Component \Cache \PruneableInterface ;
2222use Symfony \Component \HttpFoundation \BinaryFileResponse ;
2323use Symfony \Component \HttpFoundation \File \Exception \FileNotFoundException ;
4444 */
4545class Psr6Store implements Psr6StoreInterface, ClearableInterface
4646{
47- const NON_VARYING_KEY = 'non-varying ' ;
48- const COUNTER_KEY = 'write-operations-counter ' ;
49- const CLEANUP_LOCK_KEY = 'cleanup-lock ' ;
47+ public const NON_VARYING_KEY = 'non-varying ' ;
48+ public const COUNTER_KEY = 'write-operations-counter ' ;
49+ public const CLEANUP_LOCK_KEY = 'cleanup-lock ' ;
5050
51- /**
52- * @var array
53- */
54- private $ options ;
55-
56- /**
57- * @var TagAwareAdapterInterface
58- */
59- private $ cache ;
60-
61- /**
62- * @var LockFactory
63- */
64- private $ lockFactory ;
51+ private array $ options ;
52+ private AdapterInterface $ cache ;
53+ private LockFactory $ lockFactory ;
6554
6655 /**
6756 * @var LockInterface[]
6857 */
69- private $ locks = [];
58+ private array $ locks = [];
7059
7160 /**
7261 * When creating a Psr6Store you can configure a number options.
@@ -111,17 +100,11 @@ public function __construct(array $options = [])
111100 $ this ->lockFactory = $ this ->options ['lock_factory ' ];
112101 }
113102
114- /**
115- * Locates a cached Response for the Request provided.
116- *
117- * @param Request $request A Request instance
118- *
119- * @return Response|null A Response instance, or null if no cache entry was found
120- */
121103 public function lookup (Request $ request ): ?Response
122104 {
123105 $ cacheKey = $ this ->getCacheKey ($ request );
124106
107+ /** @var CacheItem $item */
125108 $ item = $ this ->cache ->getItem ($ cacheKey );
126109
127110 if (!$ item ->isHit ()) {
@@ -150,17 +133,6 @@ public function lookup(Request $request): ?Response
150133 return null ;
151134 }
152135
153- /**
154- * Writes a cache entry to the store for the given Request and Response.
155- *
156- * Existing entries are read and any that match the response are removed. This
157- * method calls write with the new list of cache entries.
158- *
159- * @param Request $request A Request instance
160- * @param Response $response A Response instance
161- *
162- * @return string The key under which the response is stored
163- */
164136 public function write (Request $ request , Response $ response ): string
165137 {
166138 if (null === $ response ->getMaxAge ()) {
@@ -174,6 +146,7 @@ public function write(Request $request, Response $response): string
174146 $ headers = $ response ->headers ->all ();
175147 unset($ headers ['age ' ]);
176148
149+ /** @var CacheItem $item */
177150 $ item = $ this ->cache ->getItem ($ cacheKey );
178151
179152 if (!$ item ->isHit ()) {
@@ -220,26 +193,14 @@ public function write(Request $request, Response $response): string
220193 return $ cacheKey ;
221194 }
222195
223- /**
224- * Invalidates all cache entries that match the request.
225- *
226- * @param Request $request A Request instance
227- */
228196 public function invalidate (Request $ request ): void
229197 {
230198 $ cacheKey = $ this ->getCacheKey ($ request );
231199
232200 $ this ->cache ->deleteItem ($ cacheKey );
233201 }
234202
235- /**
236- * Locks the cache for a given Request.
237- *
238- * @param Request $request A Request instance
239- *
240- * @return bool|string true if the lock is acquired, the path to the current lock otherwise
241- */
242- public function lock (Request $ request )
203+ public function lock (Request $ request ): bool |string
243204 {
244205 $ cacheKey = $ this ->getCacheKey ($ request );
245206
@@ -253,13 +214,6 @@ public function lock(Request $request)
253214 return $ this ->locks [$ cacheKey ]->acquire ();
254215 }
255216
256- /**
257- * Releases the lock for the given Request.
258- *
259- * @param Request $request A Request instance
260- *
261- * @return bool False if the lock file does not exist or cannot be unlocked, true otherwise
262- */
263217 public function unlock (Request $ request ): bool
264218 {
265219 $ cacheKey = $ this ->getCacheKey ($ request );
@@ -279,13 +233,6 @@ public function unlock(Request $request): bool
279233 return true ;
280234 }
281235
282- /**
283- * Returns whether or not a lock exists.
284- *
285- * @param Request $request A Request instance
286- *
287- * @return bool true if lock exists, false otherwise
288- */
289236 public function isLocked (Request $ request ): bool
290237 {
291238 $ cacheKey = $ this ->getCacheKey ($ request );
@@ -297,25 +244,13 @@ public function isLocked(Request $request): bool
297244 return $ this ->locks [$ cacheKey ]->isAcquired ();
298245 }
299246
300- /**
301- * Purges data for the given URL.
302- *
303- * @param string $url A URL
304- *
305- * @return bool true if the URL exists and has been purged, false otherwise
306- */
307- public function purge ($ url ): bool
247+ public function purge (string $ url ): bool
308248 {
309249 $ cacheKey = $ this ->getCacheKey (Request::create ($ url ));
310250
311251 return $ this ->cache ->deleteItem ($ cacheKey );
312252 }
313253
314- /**
315- * Release all locks.
316- *
317- * {@inheritdoc}
318- */
319254 public function cleanup (): void
320255 {
321256 try {
@@ -485,7 +420,7 @@ private function saveContentDigest(Response $response): void
485420
486421 // Make sure the content-length header is present
487422 if (!$ response ->headers ->has ('Transfer-Encoding ' )) {
488- $ response ->headers ->set ('Content-Length ' , \strlen ((string ) $ response ->getContent ()));
423+ $ response ->headers ->set ('Content-Length ' , ( string ) \strlen ((string ) $ response ->getContent ()));
489424 }
490425 }
491426
@@ -526,16 +461,12 @@ private function autoPruneExpiredEntries(): void
526461 $ this ->cache ->saveDeferred ($ item );
527462 }
528463
529- /**
530- * @param int $expiresAfter
531- * @param array $tags
532- */
533- private function saveDeferred (CacheItemInterface $ item , $ data , $ expiresAfter = null , $ tags = []): bool
464+ private function saveDeferred (CacheItem $ item , $ data , ?int $ expiresAfter = null , array $ tags = []): bool
534465 {
535466 $ item ->set ($ data );
536467 $ item ->expiresAfter ($ expiresAfter );
537468
538- if (0 !== \count ($ tags ) && method_exists ( $ item , ' tag ' ) ) {
469+ if (0 !== \count ($ tags )) {
539470 $ item ->tag ($ tags );
540471 }
541472
0 commit comments