Skip to content

Commit

Permalink
Reference Middleware and Strategy Headers via static keyword instead …
Browse files Browse the repository at this point in the history
…of self (#189)
  • Loading branch information
jblz authored Aug 28, 2024
1 parent 7f25853 commit c80bed9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
24 changes: 12 additions & 12 deletions src/CacheMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ function (ResponseInterface $response) use ($request) {
$response = $this->invalidateCache($request, $response);
}

return $response->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_MISS);
return $response->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_MISS);
}
);
}

if ($request->hasHeader(self::HEADER_RE_VALIDATION)) {
if ($request->hasHeader(static::HEADER_RE_VALIDATION)) {
// It's a re-validation request, so bypass the cache!
return $handler($request->withoutHeader(self::HEADER_RE_VALIDATION), $options);
return $handler($request->withoutHeader(static::HEADER_RE_VALIDATION), $options);
}

// Retrieve information from request (Cache-Control)
Expand All @@ -162,14 +162,14 @@ function (ResponseInterface $response) use ($request) {
) {
// Cache HIT!
return new FulfilledPromise(
$cacheEntry->getResponse()->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_HIT)
$cacheEntry->getResponse()->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_HIT)
);
} elseif ($staleResponse
|| ($maxStaleCache !== null && $cacheEntry->getStaleAge() <= $maxStaleCache)
) {
// Staled cache!
return new FulfilledPromise(
$cacheEntry->getResponse()->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_HIT)
$cacheEntry->getResponse()->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_HIT)
);
} elseif ($cacheEntry->hasValidationInformation() && !$onlyFromCache) {
// Re-validation header
Expand All @@ -180,7 +180,7 @@ function (ResponseInterface $response) use ($request) {

return new FulfilledPromise(
$cacheEntry->getResponse()
->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_STALE)
->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_STALE)
);
}
}
Expand Down Expand Up @@ -215,7 +215,7 @@ function (ResponseInterface $response) use ($request, $cacheEntry) {
/** @var ResponseInterface $response */
$response = $response
->withStatus($cacheEntry->getResponse()->getStatusCode())
->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_HIT);
->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_HIT);
$response = $response->withBody($cacheEntry->getResponse()->getBody());

// Merge headers of the "304 Not Modified" and the cache entry
Expand All @@ -224,14 +224,14 @@ function (ResponseInterface $response) use ($request, $cacheEntry) {
* @var string[] $headerValue
*/
foreach ($cacheEntry->getOriginalResponse()->getHeaders() as $headerName => $headerValue) {
if (!$response->hasHeader($headerName) && $headerName !== self::HEADER_CACHE_INFO) {
if (!$response->hasHeader($headerName) && $headerName !== static::HEADER_CACHE_INFO) {
$response = $response->withHeader($headerName, $headerValue);
}
}

$update = true;
} else {
$response = $response->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_MISS);
$response = $response->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_MISS);
}

return static::addToCache($this->cacheStorage, $request, $response, $update);
Expand Down Expand Up @@ -299,7 +299,7 @@ protected function addReValidationRequest(
// Add the promise for revalidate
if ($this->client !== null) {
/** @var RequestInterface $request */
$request = $request->withHeader(self::HEADER_RE_VALIDATION, '1');
$request = $request->withHeader(static::HEADER_RE_VALIDATION, '1');
$this->waitingRevalidate[] = $this->client
->sendAsync($request)
->then(function (ResponseInterface $response) use ($request, &$cacheStorage, $cacheEntry) {
Expand Down Expand Up @@ -340,7 +340,7 @@ protected static function getStaleResponse(CacheEntry $cacheEntry = null)
// Return staled cache entry if we can
if ($cacheEntry instanceof CacheEntry && $cacheEntry->serveStaleIfError()) {
return $cacheEntry->getResponse()
->withHeader(self::HEADER_CACHE_INFO, self::HEADER_CACHE_STALE);
->withHeader(static::HEADER_CACHE_INFO, static::HEADER_CACHE_STALE);
}

return;
Expand Down Expand Up @@ -395,6 +395,6 @@ private function invalidateCache(RequestInterface $request, ResponseInterface $r
$this->cacheStorage->delete($request->withMethod($method));
}

return $response->withHeader(self::HEADER_INVALIDATION, true);
return $response->withHeader(static::HEADER_INVALIDATION, true);
}
}
6 changes: 3 additions & 3 deletions src/Strategy/GreedyCacheStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ protected function getCacheObject(RequestInterface $request, ResponseInterface $
$response = $response->withoutHeader('Etag')->withoutHeader('Last-Modified');

$ttl = $this->defaultTtl;
if ($request->hasHeader(self::HEADER_TTL)) {
$ttlHeaderValues = $request->getHeader(self::HEADER_TTL);
if ($request->hasHeader(static::HEADER_TTL)) {
$ttlHeaderValues = $request->getHeader(static::HEADER_TTL);
$ttl = (int)reset($ttlHeaderValues);
}

return new CacheEntry($request->withoutHeader(self::HEADER_TTL), $response, new \DateTime(sprintf('+%d seconds', $ttl)));
return new CacheEntry($request->withoutHeader(static::HEADER_TTL), $response, new \DateTime(sprintf('+%d seconds', $ttl)));
}

public function fetch(RequestInterface $request)
Expand Down

0 comments on commit c80bed9

Please sign in to comment.