Skip to content

Commit

Permalink
load settings on invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Sep 16, 2016
1 parent 85e05f8 commit be8541f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -72,6 +72,8 @@ $cacheMiddleware = new CacheWare([

Selects cache limiter type. It's values can be `public`, `private`, `private_no_expire` or `nocache`. If not provided value defined in ini_set `session.cache_limiter` will be automatically used (normally 'nocache').

Cacheware class has CACHE_* constants for convenience.

If you want to completely disable cache headers give limiter a value of `null`.

#### expire
Expand Down
66 changes: 40 additions & 26 deletions src/CacheWare.php
Expand Up @@ -17,6 +17,11 @@
*/
class CacheWare
{
const CACHE_PUBLIC = 'limit';
const CACHE_PRIVATE = 'private';
const CACHE_PRIVATE_NO_EXPIRE = 'private_no_expire';
const CACHE_NOCACHE = 'nocache';

const CACHE_EXPIRED = 'Thu, 19 Nov 1981 08:52:00 GMT';

/**
Expand All @@ -31,23 +36,7 @@ class CacheWare
*/
public function __construct(array $settings = [])
{
$this->settings = array_merge(
$this->getCacheParams(),
$settings
);
}

/**
* Retrieve default cache parameters.
*
* @return array
*/
protected function getCacheParams()
{
return [
'limiter' => session_cache_limiter(),
'expire' => session_cache_expire() ?: 180,
];
$this->settings = $settings;
}

/**
Expand All @@ -71,7 +60,24 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
ini_set('session.use_strict_mode', false);
ini_set('session.cache_limiter', '');

return $next($request, $this->respondWithCacheHeaders($this->settings, $response));
$cacheSettings = array_merge($this->getCacheSettings(), $this->settings);

$response = $next($request, $response);

return $this->respondWithCacheHeaders($cacheSettings, $response);
}

/**
* Retrieve default cache settings.
*
* @return array
*/
protected function getCacheSettings()
{
return [
'limiter' => session_cache_limiter(),
'expire' => session_cache_expire() ?: 180,
];
}

/**
Expand All @@ -80,23 +86,23 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
* @param array $settings
* @param ResponseInterface $response
*
* @throws \InvalidArgumentException
*
* @return ResponseInterface
*/
protected function respondWithCacheHeaders(array $settings, ResponseInterface $response)
{
switch ($settings['limiter']) {
case 'public':
case static::CACHE_PUBLIC:
return $this->respondWithPublicCache($settings, $response);

case 'private':
case static::CACHE_PRIVATE:
return $this->respondWithPrivateCache($settings, $response);

case 'private_no_expire':
case 'private-no-expire':
case static::CACHE_PRIVATE_NO_EXPIRE:
return $this->respondWithPrivateNoExpireCache($settings, $response);

case 'nocache':
case 'no-cache':
case static::CACHE_NOCACHE:
return $this->respondWithNoCacheCache($response);
}

Expand All @@ -109,6 +115,8 @@ protected function respondWithCacheHeaders(array $settings, ResponseInterface $r
* @param array $settings
* @param ResponseInterface $response
*
* @throws \InvalidArgumentException
*
* @return ResponseInterface
*/
protected function respondWithPublicCache(array $settings, ResponseInterface $response)
Expand All @@ -134,13 +142,15 @@ protected function respondWithPublicCache(array $settings, ResponseInterface $re
* @param array $settings
* @param ResponseInterface $response
*
* @throws \InvalidArgumentException
*
* @return ResponseInterface
*/
protected function respondWithPrivateCache(array $settings, ResponseInterface $response)
{
return $this->respondWithPrivateNoExpireCache(
$settings,
$response->withAddedHeader('Expires', self::CACHE_EXPIRED)
$response->withAddedHeader('Expires', static::CACHE_EXPIRED)
);
}

Expand All @@ -150,6 +160,8 @@ protected function respondWithPrivateCache(array $settings, ResponseInterface $r
* @param array $settings
* @param ResponseInterface $response
*
* @throws \InvalidArgumentException
*
* @return ResponseInterface
*/
protected function respondWithPrivateNoExpireCache(array $settings, ResponseInterface $response)
Expand All @@ -167,12 +179,14 @@ protected function respondWithPrivateNoExpireCache(array $settings, ResponseInte
*
* @param ResponseInterface $response
*
* @throws \InvalidArgumentException
*
* @return ResponseInterface
*/
protected function respondWithNoCacheCache(ResponseInterface $response)
{
return $response
->withAddedHeader('Expires', self::CACHE_EXPIRED)
->withAddedHeader('Expires', static::CACHE_EXPIRED)
->withAddedHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0')
->withAddedHeader('Pragma', 'no-cache');
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Middleware/CacheWareTest.php
Expand Up @@ -47,7 +47,7 @@ public function setUp()

public function testPublicCache()
{
$middleware = new CacheWare(['limiter' => 'public']);
$middleware = new CacheWare(['limiter' => CacheWare::CACHE_PUBLIC]);

/* @var Response $response */
$response = $middleware($this->request, $this->response, $this->callback);
Expand All @@ -60,7 +60,7 @@ public function testPublicCache()

public function testPrivateCache()
{
$middleware = new CacheWare(['limiter' => 'private']);
$middleware = new CacheWare(['limiter' => CacheWare::CACHE_PRIVATE]);

/* @var Response $response */
$response = $middleware($this->request, $this->response, $this->callback);
Expand All @@ -74,7 +74,7 @@ public function testPrivateCache()

public function testPrivateNoExpireCache()
{
$middleware = new CacheWare(['limiter' => 'private_no_expire']);
$middleware = new CacheWare(['limiter' => CacheWare::CACHE_PRIVATE_NO_EXPIRE]);

/* @var Response $response */
$response = $middleware($this->request, $this->response, $this->callback);
Expand All @@ -87,7 +87,7 @@ public function testPrivateNoExpireCache()

public function testNoCacheCache()
{
$middleware = new CacheWare(['limiter' => 'nocache']);
$middleware = new CacheWare(['limiter' => CacheWare::CACHE_NOCACHE]);

/* @var Response $response */
$response = $middleware($this->request, $this->response, $this->callback);
Expand Down

0 comments on commit be8541f

Please sign in to comment.