Commit 24e01cf2 authored by Fabien Potencier's avatar Fabien Potencier

made the flush mechanism more flexible

parent 089c84b5
...@@ -69,7 +69,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -69,7 +69,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
}); });
$this['controllers'] = $this->share(function () use ($app) { $this['controllers'] = $this->share(function () use ($app) {
return new ControllerCollection($app['routes']); return new ControllerCollection();
}); });
$this['exception_handler'] = $this->share(function () { $this['exception_handler'] = $this->share(function () {
...@@ -267,10 +267,12 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -267,10 +267,12 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
/** /**
* Flushes the controller collection. * Flushes the controller collection.
*
* @param string $prefix The route prefix
*/ */
public function flush() public function flush($prefix = '')
{ {
$this['controllers']->flush(); $this['routes']->addCollection($this['controllers']->flush(), $prefix);
} }
/** /**
...@@ -311,9 +313,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -311,9 +313,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$app = $app(); $app = $app();
} }
foreach ($app['controllers']->all() as $controller) { $app->flush($prefix);
$controller->getRoute()->setPattern($prefix.$controller->getRoute()->getPattern());
}
return $app->handle($request); return $app->handle($request);
}; };
...@@ -359,7 +359,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -359,7 +359,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this['request']->isSecure() ? $this['request']->getPort() : $this['request.https_port'] $this['request']->isSecure() ? $this['request']->getPort() : $this['request.https_port']
); );
$this['controllers']->flush(); $this->flush();
$matcher = new RedirectableUrlMatcher($this['routes'], $this['request_context']); $matcher = new RedirectableUrlMatcher($this['routes'], $this['request_context']);
......
...@@ -18,19 +18,13 @@ use Symfony\Component\Routing\RouteCollection; ...@@ -18,19 +18,13 @@ use Symfony\Component\Routing\RouteCollection;
* *
* It acts as a staging area for routes. You are able to set the route name * It acts as a staging area for routes. You are able to set the route name
* until flush() is called, at which point all controllers are frozen and * until flush() is called, at which point all controllers are frozen and
* added to the RouteCollection. * converted to a RouteCollection.
* *
* @author Igor Wiedler <igor@wiedler.ch> * @author Igor Wiedler <igor@wiedler.ch>
*/ */
class ControllerCollection class ControllerCollection
{ {
private $controllers = array(); private $controllers = array();
private $routes;
public function __construct(RouteCollection $routes)
{
$this->routes = $routes;
}
/** /**
* Adds a controller to the staging area. * Adds a controller to the staging area.
...@@ -44,27 +38,23 @@ class ControllerCollection ...@@ -44,27 +38,23 @@ class ControllerCollection
/** /**
* Persists and freezes staged controllers. * Persists and freezes staged controllers.
*
* @return RouteCollection A RouteCollection instance
*/ */
public function flush() public function flush()
{ {
$routes = new RouteCollection();
foreach ($this->controllers as $controller) { foreach ($this->controllers as $controller) {
if (!$controller->getRouteName()) { if (!$controller->getRouteName()) {
$controller->bindDefaultRouteName(); $controller->bindDefaultRouteName();
} }
$this->routes->add($controller->getRouteName(), $controller->getRoute()); $routes->add($controller->getRouteName(), $controller->getRoute());
$controller->freeze(); $controller->freeze();
} }
$this->controllers = array(); $this->controllers = array();
}
/** return $routes;
* Gets all controllers.
*
* @return array An array of Controllers
*/
public function all()
{
return $this->controllers;
} }
} }
...@@ -29,39 +29,24 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase ...@@ -29,39 +29,24 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
{ {
public function testGetRouteCollectionWithNoRoutes() public function testGetRouteCollectionWithNoRoutes()
{ {
$routes = new RouteCollection(); $controllers = new ControllerCollection();
$controllers = new ControllerCollection($routes); $routes = $controllers->flush();
$this->assertEquals(0, count($routes->all()));
$controllers->flush();
$this->assertEquals(0, count($routes->all())); $this->assertEquals(0, count($routes->all()));
} }
public function testGetRouteCollectionWithRoutes() public function testGetRouteCollectionWithRoutes()
{ {
$routes = new RouteCollection(); $controllers = new ControllerCollection();
$controllers = new ControllerCollection($routes);
$controllers->add(new Controller(new Route('/foo'))); $controllers->add(new Controller(new Route('/foo')));
$controllers->add(new Controller(new Route('/bar'))); $controllers->add(new Controller(new Route('/bar')));
$this->assertEquals(0, count($routes->all())); $routes = $controllers->flush();
$controllers->flush();
$this->assertEquals(2, count($routes->all())); $this->assertEquals(2, count($routes->all()));
} }
public function testAll()
{
$controllers = new ControllerCollection(new RouteCollection());
$controllers->add($c1 = new Controller(new Route('/foo')));
$controllers->add($c2 = new Controller(new Route('/bar')));
$this->assertEquals(array($c1, $c2), $controllers->all());
}
public function testControllerFreezing() public function testControllerFreezing()
{ {
$routes = new RouteCollection(); $controllers = new ControllerCollection();
$controllers = new ControllerCollection($routes);
$fooController = new Controller(new Route('/foo')); $fooController = new Controller(new Route('/foo'));
$fooController->bind('foo'); $fooController->bind('foo');
......
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