-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 68528d1
Showing
11 changed files
with
474 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor | ||
composer.lock |
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,19 @@ | ||
# How to Contribute | ||
|
||
## Pull Requests | ||
|
||
1. Fork this repository | ||
2. Create a new branch for each feature or improvement | ||
3. Send a pull request from each feature branch | ||
|
||
It is very important to separate new features or improvements into separate feature branches, and to send a pull request for each branch. This allows me to review and pull in new features or improvements individually. | ||
|
||
## Style Guide | ||
|
||
All pull requests must adhere to the [PSR-2 standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md). | ||
|
||
## Unit Testing | ||
|
||
All pull requests must be accompanied by passing unit tests and complete code coverage. The Slim Framework uses phpunit for testing. | ||
|
||
[Learn about PHPUnit](https://github.com/sebastianbergmann/phpunit/) |
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,19 @@ | ||
Copyright (c) 2012-2015 Josh Lockhart | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,57 @@ | ||
# Slim Framework HTTP Cache | ||
|
||
This repository contains a Slim Framework HTTP cache middleware and service provider. | ||
|
||
## Install | ||
|
||
Via Composer | ||
|
||
``` bash | ||
$ composer require slim/httpcache | ||
``` | ||
|
||
Requires Slim 3.0.0 or newer. | ||
|
||
## Usage | ||
|
||
```php | ||
$app = new \Slim\App(); | ||
|
||
// Register middleware | ||
$app->add(new \Slim\HttpCache\Cache('public', 86400)); | ||
|
||
// Register service provider | ||
$app->register(new \Slim\HttpCache\CacheProvider); | ||
|
||
// Example route with ETag header | ||
$app->get('/foo', function ($req, $res, $args) { | ||
$resWithEtag = $this['cache']->withEtag($res, 'abc'); | ||
|
||
return $resWithEtag; | ||
}); | ||
|
||
$app->run(); | ||
``` | ||
|
||
## Testing | ||
|
||
``` bash | ||
$ phpunit | ||
``` | ||
|
||
## Contributing | ||
|
||
Please see [CONTRIBUTING](CONTRIBUTING.md) for details. | ||
|
||
## Security | ||
|
||
If you discover any security related issues, please email security@slimframework.com instead of using the issue tracker. | ||
|
||
## Credits | ||
|
||
- [Josh Lockhart](https://github.com/codeguy) | ||
- [All Contributors](../../contributors) | ||
|
||
## License | ||
|
||
The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
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,33 @@ | ||
{ | ||
"name": "slim/http-cache", | ||
"type": "library", | ||
"description": "Slim Framework HTTP cache middleware and service provider", | ||
"keywords": ["slim","framework","middleware","cache"], | ||
"homepage": "http://slimframework.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Josh Lockhart", | ||
"email": "hello@joshlockhart.com", | ||
"homepage": "http://joshlockhart.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.4.0", | ||
"pimple/pimple": "~3.0", | ||
"psr/http-message": "~0.9" | ||
}, | ||
"require-dev": { | ||
"slim/slim": "dev-develop" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Slim\\HttpCache\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Slim\\HttpCache\\Tests\\": "tests" | ||
} | ||
} | ||
} |
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,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="tests/bootstrap.php" | ||
> | ||
<testsuites> | ||
<testsuite name="Slim Test Suite"> | ||
<directory>tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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,83 @@ | ||
<?php | ||
namespace Slim\HttpCache; | ||
|
||
use Pimple\Container; | ||
use Pimple\ServiceProviderInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class Cache | ||
{ | ||
/** | ||
* Cache-Control type (public or private) | ||
* | ||
* @var string | ||
*/ | ||
protected $type; | ||
|
||
/** | ||
* Cache-Control max age in seconds | ||
* | ||
* @var int | ||
*/ | ||
protected $maxAge; | ||
|
||
/** | ||
* Create new HTTP cache | ||
* | ||
* @param string $type The cache type: "public" or "private" | ||
* @param int $maxAge The maximum age of client-side cache | ||
*/ | ||
public function __construct($type = 'private', $maxAge = 86400) | ||
{ | ||
$this->type = $type; | ||
$this->maxAge = $maxAge; | ||
} | ||
|
||
/** | ||
* Invoke cache middleware | ||
* | ||
* @param RequestInterface $request A PSR7 request object | ||
* @param ResponseInterface $response A PSR7 response object | ||
* @param callable $next The next middleware callable | ||
* | ||
* @return ResponseInterface A PSR7 response object | ||
*/ | ||
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) | ||
{ | ||
$response = $next($request, $response); | ||
|
||
// Cache-Control header | ||
$response = $response->withHeader('Cache-Control', sprintf( | ||
'%s, max-age=%s', | ||
$this->type, | ||
$this->maxAge | ||
)); | ||
|
||
// Last-Modified header and conditional GET check | ||
$lastModified = $response->getHeader('Last-Modified'); | ||
if ($lastModified) { | ||
if (!is_integer($lastModified)) { | ||
$lastModified = strtotime($lastModified); | ||
} | ||
$ifModifiedSince = $request->getHeader('If-Modified-Since'); | ||
if ($ifModifiedSince && $lastModified === strtotime($ifModifiedSince)) { | ||
return $response->withStatus(304); | ||
} | ||
} | ||
|
||
// ETag header and conditional GET check | ||
$etag = $response->getHeader('ETag'); | ||
if ($etag) { | ||
$ifNoneMatch = $request->getHeader('If-None-Match'); | ||
if ($ifNoneMatch) { | ||
$etagList = preg_split('@\s*,\s*@', $ifNoneMatch); | ||
if (in_array($etag, $etagList) || in_array('*', $etagList)) { | ||
return $response->withStatus(304); | ||
} | ||
} | ||
} | ||
|
||
return $response; | ||
} | ||
} |
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,81 @@ | ||
<?php | ||
namespace Slim\HttpCache; | ||
|
||
use Pimple\Container; | ||
use Pimple\ServiceProviderInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class CacheProvider implements ServiceProviderInterface | ||
{ | ||
/** | ||
* Register this cache provider with a Pimple container | ||
* | ||
* @param Container $container | ||
*/ | ||
public function register(Container $container) | ||
{ | ||
$container['cache'] = $this; | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
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 | ||
*/ | ||
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 | ||
*/ | ||
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)); | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
namespace Slim\HttpCache\Tests; | ||
|
||
use Slim\HttpCache\CacheProvider; | ||
use Slim\Http\Response; | ||
|
||
class CacheProviderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testWithExpires() | ||
{ | ||
$now = time(); | ||
$cacheProvider = new CacheProvider(); | ||
$res = $cacheProvider->withExpires(new Response(), $now); | ||
|
||
$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $res->getHeader('Expires')); | ||
} | ||
|
||
public function testWithETag() | ||
{ | ||
$etag = 'abc'; | ||
$cacheProvider = new CacheProvider(); | ||
$res = $cacheProvider->withEtag(new Response(), $etag); | ||
|
||
$this->assertEquals('"' . $etag . '"', $res->getHeader('ETag')); | ||
} | ||
|
||
public function testWithETagWeak() | ||
{ | ||
$etag = 'abc'; | ||
$cacheProvider = new CacheProvider(); | ||
$res = $cacheProvider->withEtag(new Response(), $etag, 'weak'); | ||
|
||
$this->assertEquals('W/"' . $etag . '"', $res->getHeader('ETag')); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
public function testWithETagInvalidType() | ||
{ | ||
$etag = 'abc'; | ||
$cacheProvider = new CacheProvider(); | ||
$cacheProvider->withEtag(new Response(), $etag, 'bork'); | ||
} | ||
|
||
public function testWithLastModified() | ||
{ | ||
$now = time(); | ||
$cacheProvider = new CacheProvider(); | ||
$res = $cacheProvider->withLastModified(new Response(), $now); | ||
|
||
$this->assertEquals(gmdate('D, d M Y H:i:s T', $now), $res->getHeader('Last-Modified')); | ||
} | ||
} |
Oops, something went wrong.