-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Separate cache relevant methods #11
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
namespace Slim\Middleware\HttpCache; | ||
|
||
use InvalidArgumentException; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class CacheHelper | ||
{ | ||
/** | ||
* Enable client-side HTTP caching | ||
* | ||
* @param ResponseInterface $response PSR7 response object | ||
* @param string $type Cache-Control type: "private" or "public" | ||
* @param null|int|string $maxAge Maximum cache age (integer timestamp or datetime string) | ||
* | ||
* @return ResponseInterface A new PSR7 response object with `Cache-Control` header | ||
* @throws InvalidArgumentException if the cache-control type is invalid | ||
*/ | ||
public function allowCache(ResponseInterface $response, $type = 'private', $maxAge = null) | ||
{ | ||
if (!in_array($type, ['private', 'public'])) { | ||
throw new InvalidArgumentException('Invalid Cache-Control type. Must be "public" or "private".'); | ||
} | ||
$headerValue = $type; | ||
if ($maxAge) { | ||
if (!is_integer($maxAge)) { | ||
$maxAge = strtotime($maxAge); | ||
} | ||
$headerValue = $headerValue . ', max-age=' . $maxAge; | ||
} | ||
|
||
return $response->withHeader('Cache-Control', $headerValue); | ||
} | ||
|
||
/** | ||
* Disable client-side HTTP caching | ||
* | ||
* @param ResponseInterface $response PSR7 response object | ||
* | ||
* @return ResponseInterface A new PSR7 response object with `Cache-Control` header | ||
*/ | ||
public function denyCache(ResponseInterface $response) | ||
{ | ||
return $response->withHeader('Cache-Control', 'no-store,no-cache'); | ||
} | ||
|
||
/** | ||
* Add `Expires` header to PSR7 response object | ||
* | ||
* @param ResponseInterface $response A PSR7 response object | ||
* @param int|string $time A UNIX timestamp or a valid `strtotime()` string | ||
* | ||
* @return ResponseInterface A new PSR7 response object with `Expires` header | ||
* @throws InvalidArgumentException if the expiration date cannot be parsed | ||
*/ | ||
public function withExpires(ResponseInterface $response, $time) | ||
{ | ||
if (!is_integer($time)) { | ||
$time = strtotime($time); | ||
if ($time === false) { | ||
throw new InvalidArgumentException('Expiration value could not be parsed with `strtotime()`.'); | ||
} | ||
} | ||
|
||
return $response->withHeader('Expires', gmdate('D, d M Y H:i:s T', $time)); | ||
} | ||
|
||
/** | ||
* Add `ETag` header to PSR7 response object | ||
* | ||
* @param ResponseInterface $response A PSR7 response object | ||
* @param string $value The ETag value | ||
* @param string $type ETag type: "strong" or "weak" | ||
* | ||
* @return ResponseInterface A new PSR7 response object with `ETag` header | ||
* @throws InvalidArgumentException if the etag type is invalid | ||
*/ | ||
public function withEtag(ResponseInterface $response, $value, $type = 'strong') | ||
{ | ||
if (!in_array($type, ['strong', 'weak'])) { | ||
throw new InvalidArgumentException('Invalid etag type. Must be "strong" or "weak".'); | ||
} | ||
$value = '"' . $value . '"'; | ||
if ($type === 'weak') { | ||
$value = 'W/' . $value; | ||
} | ||
|
||
return $response->withHeader('ETag', $value); | ||
} | ||
|
||
/** | ||
* Add `Last-Modified` header to PSR7 response object | ||
* | ||
* @param ResponseInterface $response A PSR7 response object | ||
* @param int|string $time A UNIX timestamp or a valid `strtotime()` string | ||
* | ||
* @return ResponseInterface A new PSR7 response object with `Last-Modified` header | ||
* @throws InvalidArgumentException if the last modified date cannot be parsed | ||
*/ | ||
public function withLastModified(ResponseInterface $response, $time) | ||
{ | ||
if (!is_integer($time)) { | ||
$time = strtotime($time); | ||
if ($time === false) { | ||
throw new InvalidArgumentException('Last Modified value could not be parsed with `strtotime()`.'); | ||
} | ||
} | ||
|
||
return $response->withHeader('Last-Modified', gmdate('D, d M Y H:i:s T', $time)); | ||
} | ||
|
||
/** | ||
* Compares the request and the response to determine if the client still has a valid copy. | ||
* | ||
* @param RequestInterface $request | ||
* @param ResponseInterface $response | ||
* @return bool | ||
*/ | ||
public function isStillValid(RequestInterface $request, ResponseInterface $response) | ||
{ | ||
// Last-Modified header and conditional GET check | ||
$lastModified = $response->getHeaderLine('Last-Modified'); | ||
|
||
if ($lastModified) { | ||
if (!is_integer($lastModified)) { | ||
$lastModified = strtotime($lastModified); | ||
} | ||
|
||
$ifModifiedSince = $request->getHeaderLine('If-Modified-Since'); | ||
|
||
if ($ifModifiedSince && $lastModified <= strtotime($ifModifiedSince)) { | ||
return true; | ||
} | ||
} | ||
|
||
// ETag header and conditional GET check | ||
$etag = $response->getHeaderLine('ETag'); | ||
|
||
if ($etag) { | ||
$ifNoneMatch = $request->getHeaderLine('If-None-Match'); | ||
|
||
if ($ifNoneMatch) { | ||
$etagList = preg_split('@\s*,\s*@', $ifNoneMatch); | ||
if (in_array(str_replace('"', '', $etag), $etagList) || in_array('*', $etagList)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$cache
can also benull
:@param CacheHelper|null $cache The cache object to use.