Skip to content

Commit

Permalink
refs #10778 remove config keys using constructor param for cache conf…
Browse files Browse the repository at this point in the history
…ig name
  • Loading branch information
steinkel committed Mar 6, 2018
1 parent 228207c commit 2f9aa0d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
25 changes: 13 additions & 12 deletions src/Routing/Middleware/RoutingMiddleware.php
Expand Up @@ -32,11 +32,6 @@
*/
class RoutingMiddleware
{
/**
* Name of the default cache configuration name used to store routes collection
*/
const DEFAULT_ROUTER_CACHE_CONFIG = '_cake_router_';

/**
* Key used to store the route collection in the cache engine
*/
Expand All @@ -49,14 +44,24 @@ class RoutingMiddleware
*/
protected $app;

/**
* The cache configuration name to use for route collection caching,
* null to disable caching
*
* @var string
*/
protected $cacheConfig;

/**
* Constructor
*
* @param \Cake\Http\BaseApplication $app The application instance that routes are defined on.
* @param string|null $cacheConfig The cache config name to use or null to disable routes cache
*/
public function __construct(BaseApplication $app = null)
public function __construct(BaseApplication $app = null, $cacheConfig = null)
{
$this->app = $app;
$this->cacheConfig = $cacheConfig;
}

/**
Expand Down Expand Up @@ -85,14 +90,10 @@ protected function loadRoutes()
*/
protected function buildRouteCollection()
{
$isRouterCacheEnabled = Configure::read('Router.cache');
if (Cache::enabled() && $isRouterCacheEnabled) {
$routesCacheConfig = Configure::read('Router.cacheConfig', static::DEFAULT_ROUTER_CACHE_CONFIG);

if (Cache::enabled() && $this->cacheConfig !== null) {
return Cache::remember(static::ROUTE_COLLECTION_CACHE_KEY, function () {

return $this->prepareRouteCollection();
}, $routesCacheConfig);
}, $this->cacheConfig);
}

return $this->prepareRouteCollection();
Expand Down
31 changes: 15 additions & 16 deletions tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php
Expand Up @@ -465,26 +465,26 @@ public function scopedMiddlewareUrlProvider()
*/
public function testCacheRoutes()
{
Configure::write('Router.cache', true);
Cache::setConfig('_cake_router_', [
$cacheConfigName = '_cake_router_';
Cache::setConfig($cacheConfigName, [
'engine' => 'File',
'path' => TMP,
]);
Router::$initialized = false;
$request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
$response = new Response();
$next = function ($req, $res) {
$routeCollection = Cache::read('routeCollection', '_cake_router_');
$next = function ($req, $res) use ($cacheConfigName) {
$routeCollection = Cache::read('routeCollection', $cacheConfigName);
$this->assertInstanceOf(RouteCollection::class, $routeCollection);

return $res;
};
$app = new Application(CONFIG);
$middleware = new RoutingMiddleware($app);
$middleware = new RoutingMiddleware($app, $cacheConfigName);
$middleware($request, $response, $next);

Cache::clear(false, '_cake_router_');
Cache::drop('_cake_router_');
Cache::clear(false, $cacheConfigName);
Cache::drop($cacheConfigName);
}

/**
Expand All @@ -494,26 +494,27 @@ public function testCacheRoutes()
*/
public function testCacheNotUsedIfCacheDisabled()
{
Configure::write('Router.cache', true);
$cacheConfigName = '_cake_router_';
Cache::disable();
Cache::setConfig('_cake_router_', [
Cache::setConfig($cacheConfigName, [
'engine' => 'File',
'path' => TMP,
]);
Router::$initialized = false;
$request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
$response = new Response();
$next = function ($req, $res) {
$routeCollection = Cache::read('routeCollection', '_cake_router_');
$next = function ($req, $res) use ($cacheConfigName) {
$routeCollection = Cache::read('routeCollection', $cacheConfigName);
$this->assertFalse($routeCollection);

return $res;
};
$app = new Application(CONFIG);
$middleware = new RoutingMiddleware($app);
$middleware = new RoutingMiddleware($app, $cacheConfigName);
$middleware($request, $response, $next);

Cache::drop('_cake_router_');
Cache::clear(false, $cacheConfigName);
Cache::drop($cacheConfigName);
Cache::enable();
}

Expand All @@ -527,8 +528,6 @@ public function testCacheConfigNotFound()
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "notfound" cache configuration does not exist');

Configure::write('Router.cache', true);
Configure::write('Router.cacheConfig', 'notfound');
Cache::setConfig('_cake_router_', [
'engine' => 'File',
'path' => TMP,
Expand All @@ -540,7 +539,7 @@ public function testCacheConfigNotFound()
return $res;
};
$app = new Application(CONFIG);
$middleware = new RoutingMiddleware($app);
$middleware = new RoutingMiddleware($app, 'notfound');
$middleware($request, $response, $next);

Cache::drop('_cake_router_');
Expand Down

0 comments on commit 2f9aa0d

Please sign in to comment.