Skip to content

Commit a10baca

Browse files
Make cache helper methods static
1 parent 2f635c5 commit a10baca

2 files changed

Lines changed: 30 additions & 30 deletions

File tree

src/AbstractCachePlugin.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function __construct(CacheItemPoolInterface $pool, StreamFactoryInterface
7272
}
7373

7474
$optionsResolver = new OptionsResolver();
75-
$this->configureOptions($optionsResolver);
75+
self::configureOptions($optionsResolver);
7676
$this->config = $optionsResolver->resolve($config);
7777

7878
if (null === $this->config['cache_key_generator']) {
@@ -123,21 +123,21 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
123123
if ($cacheItem->isHit()) {
124124
$data = $cacheItem->get();
125125
if (is_array($data)) {
126-
if ($this->shouldUseCachedResponse($data)) {
126+
if (static::shouldUseCachedResponse($data)) {
127127
// This item is still valid according to previous cache headers
128128
$response = $this->createResponseFromCacheItem($cacheItem);
129129
$response = $this->handleCacheListeners($request, $response, true, $cacheItem);
130130

131131
return new FulfilledPromise($response);
132132
}
133133

134-
$request = $this->withCacheValidationHeaders($request, $cacheItem);
134+
$request = static::withCacheValidationHeaders($request, $cacheItem);
135135
}
136136
}
137137

138138
return $next($request)->then(function (ResponseInterface $response) use ($request, $cacheItem) {
139139
if (304 === $response->getStatusCode()) {
140-
if (!$this->canUseCacheItemForNotModifiedResponse($cacheItem)) {
140+
if (!static::canUseCacheItemForNotModifiedResponse($cacheItem)) {
141141
/*
142142
* We do not have the item in cache. This plugin did not add If-Modified-Since
143143
* or If-None-Match headers. Return the response from server.
@@ -148,7 +148,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
148148
// The cached response we have is still valid
149149
$data = $cacheItem->get();
150150
$maxAge = $this->getMaxAge($response);
151-
$data['expiresAt'] = $this->calculateResponseExpiresAt($maxAge);
151+
$data['expiresAt'] = static::calculateResponseExpiresAt($maxAge);
152152
$cacheItem->set($data)->expiresAfter($this->calculateCacheItemExpiresAfter($maxAge));
153153
$this->pool->save($cacheItem);
154154

@@ -170,7 +170,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
170170
->set([
171171
'response' => $response,
172172
'body' => $body,
173-
'expiresAt' => $this->calculateResponseExpiresAt($maxAge),
173+
'expiresAt' => static::calculateResponseExpiresAt($maxAge),
174174
'createdAt' => time(),
175175
'etag' => $response->getHeader('ETag'),
176176
]);
@@ -209,7 +209,7 @@ private function calculateCacheItemExpiresAfter(?int $maxAge): ?int
209209
*
210210
* @return int|null Unix system time. A null value means that the response expires when the cache item expires
211211
*/
212-
protected function calculateResponseExpiresAt(?int $maxAge)
212+
protected static function calculateResponseExpiresAt(?int $maxAge)
213213
{
214214
if (null === $maxAge) {
215215
return null;
@@ -231,7 +231,7 @@ protected function isCacheable(ResponseInterface $response)
231231

232232
$nocacheDirectives = array_intersect($this->config['respect_response_cache_directives'], $this->noCacheFlags);
233233
foreach ($nocacheDirectives as $nocacheDirective) {
234-
if ($this->getCacheControlDirective($response, $nocacheDirective)) {
234+
if (self::getCacheControlDirective($response, $nocacheDirective)) {
235235
return false;
236236
}
237237
}
@@ -261,7 +261,7 @@ private function isCacheableRequest(RequestInterface $request): bool
261261
*
262262
* @return bool|string The value of the directive, true if directive without value, false if directive not present
263263
*/
264-
private function getCacheControlDirective(ResponseInterface $response, string $name)
264+
private static function getCacheControlDirective(ResponseInterface $response, string $name)
265265
{
266266
$headers = $response->getHeader('Cache-Control');
267267
foreach ($headers as $header) {
@@ -297,7 +297,7 @@ private function getMaxAge(ResponseInterface $response): ?int
297297
}
298298

299299
// check for max age in the Cache-Control header
300-
$maxAge = $this->getCacheControlDirective($response, 'max-age');
300+
$maxAge = self::getCacheControlDirective($response, 'max-age');
301301
if (!is_bool($maxAge)) {
302302
$ageHeaders = $response->getHeader('Age');
303303
foreach ($ageHeaders as $age) {
@@ -319,7 +319,7 @@ private function getMaxAge(ResponseInterface $response): ?int
319319
/**
320320
* Configure an options resolver.
321321
*/
322-
private function configureOptions(OptionsResolver $resolver): void
322+
private static function configureOptions(OptionsResolver $resolver): void
323323
{
324324
$resolver->setDefaults([
325325
'cache_lifetime' => 86400 * 30, // 30 days
@@ -351,7 +351,7 @@ private function configureOptions(OptionsResolver $resolver): void
351351

352352
$resolver->setNormalizer('respect_cache_headers', function (Options $options, $value) {
353353
if (null !== $value) {
354-
@trigger_error('The option "respect_cache_headers" is deprecated since version 1.3 and will be removed in 2.0. Use "respect_response_cache_directives" instead.', E_USER_DEPRECATED);
354+
@trigger_error('The option "respect_cache_headers" is deprecated since version 1.3 and will be removed in 3.0. Use "respect_response_cache_directives" instead.', E_USER_DEPRECATED);
355355
}
356356

357357
return null === $value ? true : $value;
@@ -383,7 +383,7 @@ private function createResponseFromCacheItem(CacheItemInterface $cacheItem): Res
383383
return $response->withBody($stream);
384384
}
385385

386-
protected function responseHasETag(ResponseInterface $response): bool
386+
protected static function responseHasETag(ResponseInterface $response): bool
387387
{
388388
foreach ($response->getHeader('ETag') as $etag) {
389389
if ('' !== trim($etag)) {
@@ -397,10 +397,10 @@ protected function responseHasETag(ResponseInterface $response): bool
397397
/**
398398
* Get the value for the "If-Modified-Since" header.
399399
*/
400-
private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem): ?string
400+
private static function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem): ?string
401401
{
402402
$data = $cacheItem->get();
403-
// The isset() is to be removed in 2.0.
403+
// The isset() is to be removed in 3.0.
404404
if (!isset($data['createdAt'])) {
405405
return null;
406406
}
@@ -414,10 +414,10 @@ private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem): ?st
414414
/**
415415
* Get the ETag from the cached response.
416416
*/
417-
protected function getETag(CacheItemInterface $cacheItem): ?string
417+
protected static function getETag(CacheItemInterface $cacheItem): ?string
418418
{
419419
$data = $cacheItem->get();
420-
// The isset() is to be removed in 2.0.
420+
// The isset() is to be removed in 3.0.
421421
if (!isset($data['etag'])) {
422422
return null;
423423
}
@@ -446,27 +446,27 @@ private function handleCacheListeners(RequestInterface $request, ResponseInterfa
446446
/**
447447
* @param mixed[] $data
448448
*/
449-
protected function shouldUseCachedResponse(array $data): bool
449+
protected static function shouldUseCachedResponse(array $data): bool
450450
{
451-
// The array_key_exists() is to be removed in 2.0.
451+
// The array_key_exists() is to be removed in 3.0.
452452
return array_key_exists('expiresAt', $data) && (null === $data['expiresAt'] || time() < $data['expiresAt']);
453453
}
454454

455-
protected function withCacheValidationHeaders(RequestInterface $request, CacheItemInterface $cacheItem): RequestInterface
455+
protected static function withCacheValidationHeaders(RequestInterface $request, CacheItemInterface $cacheItem): RequestInterface
456456
{
457457
// Add headers to ask the server if this cache is still valid
458-
if ($modifiedSinceValue = $this->getModifiedSinceHeaderValue($cacheItem)) {
458+
if ($modifiedSinceValue = self::getModifiedSinceHeaderValue($cacheItem)) {
459459
$request = $request->withHeader('If-Modified-Since', $modifiedSinceValue);
460460
}
461461

462-
if ($etag = $this->getETag($cacheItem)) {
462+
if ($etag = static::getETag($cacheItem)) {
463463
$request = $request->withHeader('If-None-Match', $etag);
464464
}
465465

466466
return $request;
467467
}
468468

469-
protected function canUseCacheItemForNotModifiedResponse(CacheItemInterface $cacheItem): bool
469+
protected static function canUseCacheItemForNotModifiedResponse(CacheItemInterface $cacheItem): bool
470470
{
471471
return $cacheItem->isHit();
472472
}

src/EtagCachePlugin.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,35 @@ public static function serverCache(CacheItemPoolInterface $pool, StreamFactoryIn
4444
/**
4545
* @return int
4646
*/
47-
protected function calculateResponseExpiresAt(?int $maxAge)
47+
protected static function calculateResponseExpiresAt(?int $maxAge)
4848
{
4949
return 0;
5050
}
5151

5252
protected function isCacheable(ResponseInterface $response)
5353
{
54-
return parent::isCacheable($response) && $this->responseHasETag($response);
54+
return parent::isCacheable($response) && self::responseHasETag($response);
5555
}
5656

5757
/**
5858
* @param mixed[] $data
5959
*/
60-
protected function shouldUseCachedResponse(array $data): bool
60+
protected static function shouldUseCachedResponse(array $data): bool
6161
{
6262
return false;
6363
}
6464

65-
protected function withCacheValidationHeaders(RequestInterface $request, CacheItemInterface $cacheItem): RequestInterface
65+
protected static function withCacheValidationHeaders(RequestInterface $request, CacheItemInterface $cacheItem): RequestInterface
6666
{
67-
if ($etag = $this->getETag($cacheItem)) {
67+
if ($etag = self::getETag($cacheItem)) {
6868
$request = $request->withHeader('If-None-Match', $etag);
6969
}
7070

7171
return $request;
7272
}
7373

74-
protected function canUseCacheItemForNotModifiedResponse(CacheItemInterface $cacheItem): bool
74+
protected static function canUseCacheItemForNotModifiedResponse(CacheItemInterface $cacheItem): bool
7575
{
76-
return $cacheItem->isHit() && null !== $this->getETag($cacheItem);
76+
return $cacheItem->isHit() && null !== self::getETag($cacheItem);
7777
}
7878
}

0 commit comments

Comments
 (0)