From 18f253f20c9fb58b67f875492830690ace413767 Mon Sep 17 00:00:00 2001 From: lschricke Date: Fri, 12 Jun 2015 02:05:11 -0400 Subject: [PATCH] Added factory for RouteCollection, makes subclassing of RouteCollection possible. --- src/Silex/Application.php | 7 ++++-- src/Silex/ControllerCollection.php | 10 ++++++-- tests/Silex/Tests/ApplicationTest.php | 20 +++++++++++++++ .../Silex/Tests/ControllerCollectionTest.php | 25 +++++++++++++++++++ 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/Silex/Application.php b/src/Silex/Application.php index c07e9bc75..ca3883fc7 100644 --- a/src/Silex/Application.php +++ b/src/Silex/Application.php @@ -64,8 +64,11 @@ public function __construct(array $values = array()) $app = $this; - $this['routes'] = function () { + $this['routes_factory'] = $this->factory(function () { return new RouteCollection(); + }); + $this['routes'] = function () use ($app) { + return $app['routes_factory']; }; $this['controllers'] = function () use ($app) { @@ -73,7 +76,7 @@ public function __construct(array $values = array()) }; $this['controllers_factory'] = $this->factory(function () use ($app) { - return new ControllerCollection($app['route_factory']); + return new ControllerCollection($app['route_factory'], $app['routes_factory']); }); $this['route_class'] = 'Silex\\Route'; diff --git a/src/Silex/ControllerCollection.php b/src/Silex/ControllerCollection.php index 17921b222..1c06c0469 100644 --- a/src/Silex/ControllerCollection.php +++ b/src/Silex/ControllerCollection.php @@ -42,10 +42,12 @@ class ControllerCollection protected $defaultRoute; protected $defaultController; protected $prefix; + protected $routesFactory; - public function __construct(Route $defaultRoute) + public function __construct(Route $defaultRoute, $routesFactory = null) { $this->defaultRoute = $defaultRoute; + $this->routesFactory = $routesFactory; $this->defaultController = function (Request $request) { throw new \LogicException(sprintf('The "%s" route must have code to run when it matches.', $request->attributes->get('_route'))); }; @@ -186,7 +188,11 @@ public function __call($method, $arguments) */ public function flush($prefix = '') { - $routes = new RouteCollection(); + if (null === $this->routesFactory) { + $routes = new RouteCollection(); + } else { + $routes = $this->routesFactory; + } foreach ($this->controllers as $controller) { if ($controller instanceof Controller) { diff --git a/tests/Silex/Tests/ApplicationTest.php b/tests/Silex/Tests/ApplicationTest.php index 8e8229009..da83a4151 100644 --- a/tests/Silex/Tests/ApplicationTest.php +++ b/tests/Silex/Tests/ApplicationTest.php @@ -23,6 +23,7 @@ use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\Routing\RouteCollection; /** * Application test cases. @@ -635,6 +636,21 @@ public function testViewListenersResponsesAreNotUsedIfNull() $this->assertEquals('Hello view listener', $response->getContent()); } + + public function testDefaultRoutesFactory() + { + $app = new Application(); + $this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $app['routes']); + } + + public function testOverriddenRoutesFactory() + { + $app = new Application(); + $app['routes_factory'] = $app->factory(function () { + return new RouteCollectionSubClass(); + }); + $this->assertInstanceOf('Silex\Tests\RouteCollectionSubClass', $app['routes']); + } } class FooController @@ -652,3 +668,7 @@ public function connect(Application $app) return; } } + +class RouteCollectionSubClass extends RouteCollection +{ +} diff --git a/tests/Silex/Tests/ControllerCollectionTest.php b/tests/Silex/Tests/ControllerCollectionTest.php index 80e7f0eb6..ecba58f3c 100644 --- a/tests/Silex/Tests/ControllerCollectionTest.php +++ b/tests/Silex/Tests/ControllerCollectionTest.php @@ -11,10 +11,12 @@ namespace Silex\Tests; +use Silex\Application; use Silex\Controller; use Silex\ControllerCollection; use Silex\Exception\ControllerFrozenException; use Silex\Route; +use Symfony\Component\Routing\RouteCollection; /** * ControllerCollection test cases. @@ -194,6 +196,25 @@ public function testNestedCollectionRouteCallbacks() $this->assertEquals(array('before'), $c2->getRoute()->getOption('_before_middlewares')); $this->assertEquals(array('before'), $c3->getRoute()->getOption('_before_middlewares')); } + + public function testRoutesFactoryOmitted() + { + $controllers = new ControllerCollection(new Route()); + $routes = $controllers->flush(); + $this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $routes); + } + + public function testRoutesFactoryInConstructor() + { + $app = new Application(); + $app['routes_factory'] = $app->factory(function () { + return new RouteCollectionSubClass2(); + }); + + $controllers = new ControllerCollection(new Route(), $app['routes_factory']); + $routes = $controllers->flush(); + $this->assertInstanceOf('Silex\Tests\RouteCollectionSubClass2', $routes); + } } class MyRoute1 extends Route @@ -205,3 +226,7 @@ public function foo($value) $this->foo = $value; } } + +class RouteCollectionSubClass2 extends RouteCollection +{ +}