Commit 18f253f2 authored by lschricke's avatar lschricke

Added factory for RouteCollection, makes subclassing of RouteCollection possible.

parent 5b7f26c2
...@@ -64,8 +64,11 @@ class Application extends Container implements HttpKernelInterface, TerminableIn ...@@ -64,8 +64,11 @@ class Application extends Container implements HttpKernelInterface, TerminableIn
$app = $this; $app = $this;
$this['routes'] = function () { $this['routes_factory'] = $this->factory(function () {
return new RouteCollection(); return new RouteCollection();
});
$this['routes'] = function () use ($app) {
return $app['routes_factory'];
}; };
$this['controllers'] = function () use ($app) { $this['controllers'] = function () use ($app) {
...@@ -73,7 +76,7 @@ class Application extends Container implements HttpKernelInterface, TerminableIn ...@@ -73,7 +76,7 @@ class Application extends Container implements HttpKernelInterface, TerminableIn
}; };
$this['controllers_factory'] = $this->factory(function () use ($app) { $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'; $this['route_class'] = 'Silex\\Route';
......
...@@ -42,10 +42,12 @@ class ControllerCollection ...@@ -42,10 +42,12 @@ class ControllerCollection
protected $defaultRoute; protected $defaultRoute;
protected $defaultController; protected $defaultController;
protected $prefix; protected $prefix;
protected $routesFactory;
public function __construct(Route $defaultRoute) public function __construct(Route $defaultRoute, $routesFactory = null)
{ {
$this->defaultRoute = $defaultRoute; $this->defaultRoute = $defaultRoute;
$this->routesFactory = $routesFactory;
$this->defaultController = function (Request $request) { $this->defaultController = function (Request $request) {
throw new \LogicException(sprintf('The "%s" route must have code to run when it matches.', $request->attributes->get('_route'))); throw new \LogicException(sprintf('The "%s" route must have code to run when it matches.', $request->attributes->get('_route')));
}; };
...@@ -186,7 +188,11 @@ class ControllerCollection ...@@ -186,7 +188,11 @@ class ControllerCollection
*/ */
public function flush($prefix = '') public function flush($prefix = '')
{ {
$routes = new RouteCollection(); if (null === $this->routesFactory) {
$routes = new RouteCollection();
} else {
$routes = $this->routesFactory;
}
foreach ($this->controllers as $controller) { foreach ($this->controllers as $controller) {
if ($controller instanceof Controller) { if ($controller instanceof Controller) {
......
...@@ -23,6 +23,7 @@ use Symfony\Component\HttpFoundation\Response; ...@@ -23,6 +23,7 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Routing\RouteCollection;
/** /**
* Application test cases. * Application test cases.
...@@ -635,6 +636,21 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase ...@@ -635,6 +636,21 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Hello view listener', $response->getContent()); $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 class FooController
...@@ -652,3 +668,7 @@ class IncorrectControllerCollection implements ControllerProviderInterface ...@@ -652,3 +668,7 @@ class IncorrectControllerCollection implements ControllerProviderInterface
return; return;
} }
} }
class RouteCollectionSubClass extends RouteCollection
{
}
...@@ -11,10 +11,12 @@ ...@@ -11,10 +11,12 @@
namespace Silex\Tests; namespace Silex\Tests;
use Silex\Application;
use Silex\Controller; use Silex\Controller;
use Silex\ControllerCollection; use Silex\ControllerCollection;
use Silex\Exception\ControllerFrozenException; use Silex\Exception\ControllerFrozenException;
use Silex\Route; use Silex\Route;
use Symfony\Component\Routing\RouteCollection;
/** /**
* ControllerCollection test cases. * ControllerCollection test cases.
...@@ -194,6 +196,25 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase ...@@ -194,6 +196,25 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('before'), $c2->getRoute()->getOption('_before_middlewares')); $this->assertEquals(array('before'), $c2->getRoute()->getOption('_before_middlewares'));
$this->assertEquals(array('before'), $c3->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 class MyRoute1 extends Route
...@@ -205,3 +226,7 @@ class MyRoute1 extends Route ...@@ -205,3 +226,7 @@ class MyRoute1 extends Route
$this->foo = $value; $this->foo = $value;
} }
} }
class RouteCollectionSubClass2 extends RouteCollection
{
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment