From 4498394e1f8147f54bb456e4038e0ccf5277384f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Mon, 23 Oct 2017 10:53:00 +0200 Subject: [PATCH] Move `Router::$initialized` from `RoutingMiddleware` to `BaseApplication`. Refs #11357 --- src/Http/BaseApplication.php | 7 ++++++- src/Routing/Middleware/RoutingMiddleware.php | 4 +--- .../TestCase/Routing/Middleware/RoutingMiddlewareTest.php | 3 ++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Http/BaseApplication.php b/src/Http/BaseApplication.php index 31fb5ce2973..56962dab809 100644 --- a/src/Http/BaseApplication.php +++ b/src/Http/BaseApplication.php @@ -17,6 +17,7 @@ use Cake\Core\ConsoleApplicationInterface; use Cake\Core\HttpApplicationInterface; use Cake\Routing\DispatcherFactory; +use Cake\Routing\Router; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -69,7 +70,11 @@ public function bootstrap() */ public function routes($routes) { - require $this->configDir . '/routes.php'; + if (!Router::$initialized) { + require $this->configDir . '/routes.php'; + // Prevent routes from being loaded again + Router::$initialized = true; + } } /** diff --git a/src/Routing/Middleware/RoutingMiddleware.php b/src/Routing/Middleware/RoutingMiddleware.php index bba3d17fca2..fe384265d9b 100644 --- a/src/Routing/Middleware/RoutingMiddleware.php +++ b/src/Routing/Middleware/RoutingMiddleware.php @@ -56,11 +56,9 @@ public function __construct(BaseApplication $app = null) */ protected function loadRoutes() { - if ($this->app && !Router::$initialized) { + if ($this->app) { $builder = Router::createRouteBuilder('/'); $this->app->routes($builder); - // Prevent routes from being loaded again - Router::$initialized = true; } } diff --git a/tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php b/tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php index 9321a3988e3..ff16bb23899 100644 --- a/tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php +++ b/tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php @@ -152,7 +152,8 @@ public function testRoutesHookInvokedOnApp() ]; $this->assertEquals($expected, $req->getAttribute('params')); $this->assertTrue(Router::$initialized, 'Router state should indicate routes loaded'); - $this->assertCount(1, Router::routes()); + $this->assertNotEmpty(Router::routes()); + $this->assertEquals('/app/articles', Router::routes()[0]->template); }; $app = new Application(CONFIG); $middleware = new RoutingMiddleware($app);