Browse Source

Remove deprecated routes caching.

ADmad 4 years ago
parent
commit
85db41af61

+ 0 - 26
src/Routing/Exception/FailedRouteCacheException.php

@@ -1,26 +0,0 @@
-<?php
-declare(strict_types=1);
-
-/**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link          https://cakephp.org CakePHP(tm) Project
- * @since         4.4.0
- * @license       https://opensource.org/licenses/mit-license.php MIT License
- */
-namespace Cake\Routing\Exception;
-
-use Cake\Core\Exception\CakeException;
-
-/**
- * Thrown when unable to cache route collection.
- */
-class FailedRouteCacheException extends CakeException
-{
-}

+ 2 - 70
src/Routing/Middleware/RoutingMiddleware.php

@@ -16,17 +16,12 @@ declare(strict_types=1);
  */
 namespace Cake\Routing\Middleware;
 
-use Cake\Cache\Cache;
-use Cake\Cache\InvalidArgumentException;
 use Cake\Core\PluginApplicationInterface;
 use Cake\Http\Exception\RedirectException;
 use Cake\Http\MiddlewareQueue;
 use Cake\Http\Runner;
-use Cake\Routing\Exception\FailedRouteCacheException;
-use Cake\Routing\RouteCollection;
 use Cake\Routing\Router;
 use Cake\Routing\RoutingApplicationInterface;
-use Exception;
 use Laminas\Diactoros\Response\RedirectResponse;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
@@ -54,90 +49,27 @@ class RoutingMiddleware implements MiddlewareInterface
     protected RoutingApplicationInterface $app;
 
     /**
-     * The cache configuration name to use for route collection caching,
-     * null to disable caching
-     *
-     * @var string|null
-     */
-    protected ?string $cacheConfig = null;
-
-    /**
      * Constructor
      *
      * @param \Cake\Routing\RoutingApplicationInterface $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(RoutingApplicationInterface $app, ?string $cacheConfig = null)
+    public function __construct(RoutingApplicationInterface $app)
     {
-        if ($cacheConfig !== null) {
-            deprecationWarning(
-                '4.4',
-                'Use of routing cache is deprecated and will be removed in 5.0. ' .
-                'Upgrade to the new `CakeDC/CachedRouting` plugin. ' .
-                'See https://github.com/CakeDC/cakephp-cached-routing'
-            );
-        }
         $this->app = $app;
-        $this->cacheConfig = $cacheConfig;
     }
 
     /**
-     * Trigger the application's routes() hook if the application exists and Router isn't initialized.
-     * Uses the routes cache if enabled via configuration param "Router.cache"
-     *
-     * If the middleware is created without an Application, routes will be
-     * loaded via the automatic route loading that pre-dates the routes() hook.
+     * Trigger the application's and plugin's routes() hook.
      *
      * @return void
      */
     protected function loadRoutes(): void
     {
-        $routeCollection = $this->buildRouteCollection();
-        Router::setRouteCollection($routeCollection);
-    }
-
-    /**
-     * Check if route cache is enabled and use the configured Cache to 'remember' the route collection
-     *
-     * @return \Cake\Routing\RouteCollection
-     */
-    protected function buildRouteCollection(): RouteCollection
-    {
-        if (Cache::enabled() && $this->cacheConfig !== null) {
-            try {
-                return Cache::remember(static::ROUTE_COLLECTION_CACHE_KEY, function () {
-                    return $this->prepareRouteCollection();
-                }, $this->cacheConfig);
-            } catch (InvalidArgumentException $e) {
-                throw $e;
-            } catch (Exception $e) {
-                throw new FailedRouteCacheException(
-                    'Unable to cache route collection. Cached routes must be serializable. Check for route-specific
-                    middleware or other unserializable settings in your routes. The original exception message can
-                    show what type of object failed to serialize.',
-                    null,
-                    $e
-                );
-            }
-        }
-
-        return $this->prepareRouteCollection();
-    }
-
-    /**
-     * Generate the route collection using the builder
-     *
-     * @return \Cake\Routing\RouteCollection
-     */
-    protected function prepareRouteCollection(): RouteCollection
-    {
         $builder = Router::createRouteBuilder('/');
         $this->app->routes($builder);
         if ($this->app instanceof PluginApplicationInterface) {
             $this->app->pluginRoutes($builder);
         }
-
-        return Router::getRouteCollection();
     }
 
     /**

+ 0 - 135
tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php

@@ -16,24 +16,19 @@ declare(strict_types=1);
  */
 namespace Cake\Test\TestCase\Routing\Middleware;
 
-use Cake\Cache\Cache;
-use Cake\Cache\InvalidArgumentException as CacheInvalidArgumentException;
 use Cake\Core\Configure;
 use Cake\Core\HttpApplicationInterface;
 use Cake\Http\ServerRequestFactory;
-use Cake\Routing\Exception\FailedRouteCacheException;
 use Cake\Routing\Exception\MissingRouteException;
 use Cake\Routing\Middleware\RoutingMiddleware;
 use Cake\Routing\Route\Route;
 use Cake\Routing\RouteBuilder;
-use Cake\Routing\RouteCollection;
 use Cake\Routing\Router;
 use Cake\TestSuite\TestCase;
 use Laminas\Diactoros\Response;
 use TestApp\Application;
 use TestApp\Http\TestRequestHandler;
 use TestApp\Middleware\DumbMiddleware;
-use TestApp\Middleware\UnserializableMiddleware;
 
 /**
  * Test for RoutingMiddleware
@@ -62,17 +57,6 @@ class RoutingMiddlewareTest extends TestCase
         Configure::write('App.base', '');
     }
 
-    public function tearDown(): void
-    {
-        parent::tearDown();
-
-        Cache::enable();
-        if (in_array('_cake_router_', Cache::configured(), true)) {
-            Cache::clear('_cake_router_');
-        }
-        Cache::drop('_cake_router_');
-    }
-
     /**
      * Test redirect responses from redirect routes
      */
@@ -457,125 +441,6 @@ class RoutingMiddlewareTest extends TestCase
     }
 
     /**
-     * Test we store route collection in cache.
-     */
-    public function testCacheRoutes(): void
-    {
-        Configure::write('Error.ignoredDeprecationPaths', [
-            'tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php',
-        ]);
-        $cacheConfigName = '_cake_router_';
-        Cache::setConfig($cacheConfigName, [
-            'engine' => 'File',
-            'path' => CACHE,
-        ]);
-        $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
-        $handler = new TestRequestHandler(function ($req) use ($cacheConfigName) {
-            $routeCollection = Cache::read('routeCollection', $cacheConfigName);
-            $this->assertInstanceOf(RouteCollection::class, $routeCollection);
-
-            return new Response();
-        });
-        $app = new Application(CONFIG);
-        $middleware = new RoutingMiddleware($app, $cacheConfigName);
-        $middleware->process($request, $handler);
-        Configure::delete('Error.ignoredDeprecationPaths');
-    }
-
-    /**
-     * Test we don't cache routes if cache is disabled.
-     */
-    public function testCacheNotUsedIfCacheDisabled(): void
-    {
-        Configure::write('Error.ignoredDeprecationPaths', [
-            'tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php',
-        ]);
-        $cacheConfigName = '_cake_router_';
-        Cache::drop($cacheConfigName);
-        Cache::disable();
-        Cache::setConfig($cacheConfigName, [
-            'engine' => 'File',
-            'path' => CACHE,
-        ]);
-        $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
-        $handler = new TestRequestHandler(function ($req) use ($cacheConfigName) {
-            $routeCollection = Cache::read('routeCollection', $cacheConfigName);
-            $this->assertNull($routeCollection);
-
-            return new Response();
-        });
-        $app = new Application(CONFIG);
-        $middleware = new RoutingMiddleware($app, $cacheConfigName);
-        $middleware->process($request, $handler);
-        Configure::delete('Error.ignoredDeprecationPaths');
-    }
-
-    /**
-     * Test cache name is used
-     */
-    public function testCacheConfigNotFound(): void
-    {
-        Configure::write('Error.ignoredDeprecationPaths', [
-            'tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php',
-        ]);
-        $this->expectException(CacheInvalidArgumentException::class);
-        $this->expectExceptionMessage('The "notfound" cache configuration does not exist.');
-
-        Cache::setConfig('_cake_router_', [
-            'engine' => 'File',
-            'path' => CACHE,
-        ]);
-        $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
-        $app = new Application(CONFIG);
-        $middleware = new RoutingMiddleware($app, 'notfound');
-        $middleware->process($request, new TestRequestHandler());
-        Configure::delete('Error.ignoredDeprecationPaths');
-    }
-
-    public function testFailedRouteCache(): void
-    {
-        Cache::setConfig('_cake_router_', [
-            'engine' => 'File',
-            'path' => CACHE,
-        ]);
-
-        $app = $this->createMock(Application::class);
-        $this->expectDeprecation();
-        $this->expectDeprecationMessage(
-            'Use of routing cache is deprecated and will be removed in 5.0. ' .
-            'Upgrade to the new `CakeDC/CachedRouting` plugin. ' .
-            'See https://github.com/CakeDC/cakephp-cached-routing'
-        );
-        new RoutingMiddleware($app, '_cake_router_');
-    }
-
-    public function testDeprecatedRouteCache(): void
-    {
-        Configure::write('Error.ignoredDeprecationPaths', [
-            'tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php',
-        ]);
-        Cache::setConfig('_cake_router_', [
-            'engine' => 'File',
-            'path' => CACHE,
-        ]);
-
-        $app = $this->createMock(Application::class);
-        $app
-            ->method('routes')
-            ->will($this->returnCallback(function (RouteBuilder $routes) use ($app) {
-                return $routes->registerMiddleware('should fail', new UnserializableMiddleware($app));
-            }));
-
-        $middleware = new RoutingMiddleware($app, '_cake_router_');
-        $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']);
-
-        $this->expectException(FailedRouteCacheException::class);
-        $this->expectExceptionMessage('Unable to cache route collection.');
-        $middleware->process($request, new TestRequestHandler());
-        Configure::delete('Error.ignoredDeprecationPaths');
-    }
-
-    /**
      * Create a stub application for testing.
      *
      * @param callable|null $handleCallback Callback for "handle" method.

+ 0 - 41
tests/test_app/TestApp/Middleware/UnserializableMiddleware.php

@@ -1,41 +0,0 @@
-<?php
-declare(strict_types=1);
-
-/**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link          https://cakephp.org CakePHP(tm) Project
- * @since         3.3.0
- * @license       https://opensource.org/licenses/mit-license.php MIT License
- */
-namespace TestApp\Middleware;
-
-use Cake\Core\HttpApplicationInterface;
-use Psr\Http\Message\ResponseInterface;
-use Psr\Http\Message\ServerRequestInterface;
-use Psr\Http\Server\MiddlewareInterface;
-use Psr\Http\Server\RequestHandlerInterface;
-
-/**
- * Testing stub for middleware tests.
- */
-class UnserializableMiddleware implements MiddlewareInterface
-{
-    protected HttpApplicationInterface $app;
-
-    public function __construct(HttpApplicationInterface $app)
-    {
-        $this->app = $app;
-    }
-
-    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
-    {
-        return $request;
-    }
-}