Browse Source

refs #10778 remove config keys using constructor param for cache config name

Jorge González 8 years ago
parent
commit
2f9aa0db3b

+ 13 - 12
src/Routing/Middleware/RoutingMiddleware.php

@@ -33,11 +33,6 @@ use Zend\Diactoros\Response\RedirectResponse;
 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
      */
     const ROUTE_COLLECTION_CACHE_KEY = 'routeCollection';
@@ -50,13 +45,23 @@ 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 @@ class RoutingMiddleware
      */
     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();

+ 15 - 16
tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php

@@ -465,26 +465,26 @@ class RoutingMiddlewareTest extends TestCase
      */
     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 @@ class RoutingMiddlewareTest extends TestCase
      */
     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 @@ class RoutingMiddlewareTest extends TestCase
         $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 @@ class RoutingMiddlewareTest extends TestCase
             return $res;
         };
         $app = new Application(CONFIG);
-        $middleware = new RoutingMiddleware($app);
+        $middleware = new RoutingMiddleware($app, 'notfound');
         $middleware($request, $response, $next);
 
         Cache::drop('_cake_router_');