Skip to content
Permalink
Browse files

refs #10778 remove config keys using constructor param for cache conf…

…ig name
  • Loading branch information...
steinkel committed Mar 6, 2018
1 parent 228207c commit 2f9aa0db3b3f20ce2a269be9280be100fdfe615d
@@ -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
*/
@@ -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;
}
/**
@@ -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();
@@ -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);
}
/**
@@ -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();
}
@@ -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,
@@ -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_');

0 comments on commit 2f9aa0d

Please sign in to comment.
You can’t perform that action at this time.