Commit 4e6031e2 authored by Fabien Potencier's avatar Fabien Potencier

fixed order of route registration for mounted controllers

parent bc673b47
......@@ -4,6 +4,7 @@ Changelog
1.2.0 (2013-XX-XX)
------------------
* [BC BREAK] Routes are now always added in the order of their registration (even for mounted routes)
* Added run() on Route to be able to define the controller code
* Deprecated TwigCoreExtension (register the new HttpFragmentServiceProvider instead)
* Added HttpFragmentServiceProvider
......
......@@ -473,7 +473,7 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
throw new \LogicException('The "mount" method takes either a ControllerCollection or a ControllerProviderInterface instance.');
}
$this['routes']->addCollection($controllers->flush($prefix));
$this['controllers']->mount($prefix, $controllers);
return $this;
}
......
......@@ -30,6 +30,7 @@ class ControllerCollection
protected $controllers = array();
protected $defaultRoute;
protected $defaultController;
protected $prefix;
/**
* Constructor.
......@@ -42,6 +43,19 @@ class ControllerCollection
};
}
/**
* Mounts controllers under the given route prefix.
*
* @param string $prefix The route prefix
* @param ControllerCollection $controllers A ControllerCollection instance
*/
public function mount($prefix, ControllerCollection $controllers)
{
$controllers->prefix = $prefix;
$this->controllers[] = $controllers;
}
/**
* Maps a pattern to a callable.
*
......@@ -123,7 +137,9 @@ class ControllerCollection
call_user_func_array(array($this->defaultRoute, $method), $arguments);
foreach ($this->controllers as $controller) {
call_user_func_array(array($controller, $method), $arguments);
if ($controller instanceof Controller) {
call_user_func_array(array($controller, $method), $arguments);
}
}
return $this;
......@@ -141,15 +157,19 @@ class ControllerCollection
$routes = new RouteCollection();
foreach ($this->controllers as $controller) {
if (!$name = $controller->getRouteName()) {
$name = $controller->generateRouteName($prefix);
while ($routes->get($name)) {
$name .= '_';
if ($controller instanceof Controller) {
if (!$name = $controller->getRouteName()) {
$name = $controller->generateRouteName($prefix);
while ($routes->get($name)) {
$name .= '_';
}
$controller->bind($name);
}
$controller->bind($name);
$routes->add($name, $controller->getRoute());
$controller->freeze();
} else {
$routes->addCollection($controller->flush($controller->prefix));
}
$routes->add($name, $controller->getRoute());
$controller->freeze();
}
$routes->addPrefix($prefix);
......
......@@ -489,6 +489,20 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertSame($app, $app->mount('/hello', $mounted));
}
public function testMountPreservesOrder()
{
$app = new Application();
$mounted = new ControllerCollection(new Route());
$mounted->get('/mounted')->bind('second');
$app->get('/before')->bind('first');
$app->mount('/', $mounted);
$app->get('/after')->bind('third');
$app->flush();
$this->assertEquals(array('first', 'second', 'third'), array_keys(iterator_to_array($app['routes'])));
}
public function testSendFile()
{
$app = new Application();
......
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