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
});
$this['controllers'] = $this->share(function () use ($app) {
return new ControllerCollection($app['routes']);
return new ControllerCollection();
});
$this['exception_handler'] = $this->share(function () {
......@@ -267,10 +267,12 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
/**
* 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
$app = $app();
}
foreach ($app['controllers']->all() as $controller) {
$controller->getRoute()->setPattern($prefix.$controller->getRoute()->getPattern());
}
$app->flush($prefix);
return $app->handle($request);
};
......@@ -359,7 +359,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this['request']->isSecure() ? $this['request']->getPort() : $this['request.https_port']
);
$this['controllers']->flush();
$this->flush();
$matcher = new RedirectableUrlMatcher($this['routes'], $this['request_context']);
......
......@@ -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
* 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>
*/
class ControllerCollection
{
private $controllers = array();
private $routes;
public function __construct(RouteCollection $routes)
{
$this->routes = $routes;
}
/**
* Adds a controller to the staging area.
......@@ -44,27 +38,23 @@ class ControllerCollection
/**
* Persists and freezes staged controllers.
*
* @return RouteCollection A RouteCollection instance
*/
public function flush()
{
$routes = new RouteCollection();
foreach ($this->controllers as $controller) {
if (!$controller->getRouteName()) {
$controller->bindDefaultRouteName();
}
$this->routes->add($controller->getRouteName(), $controller->getRoute());
$routes->add($controller->getRouteName(), $controller->getRoute());
$controller->freeze();
}
$this->controllers = array();
}
/**
* Gets all controllers.
*
* @return array An array of Controllers
*/
public function all()
{
return $this->controllers;
return $routes;
}
}
......@@ -29,39 +29,24 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
{
public function testGetRouteCollectionWithNoRoutes()
{
$routes = new RouteCollection();
$controllers = new ControllerCollection($routes);
$this->assertEquals(0, count($routes->all()));
$controllers->flush();
$controllers = new ControllerCollection();
$routes = $controllers->flush();
$this->assertEquals(0, count($routes->all()));
}
public function testGetRouteCollectionWithRoutes()
{
$routes = new RouteCollection();
$controllers = new ControllerCollection($routes);
$controllers = new ControllerCollection();
$controllers->add(new Controller(new Route('/foo')));
$controllers->add(new Controller(new Route('/bar')));
$this->assertEquals(0, count($routes->all()));
$controllers->flush();
$routes = $controllers->flush();
$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()
{
$routes = new RouteCollection();
$controllers = new ControllerCollection($routes);
$controllers = new ControllerCollection();
$fooController = new Controller(new Route('/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