Commit 52b5ab1b authored by Fabien Potencier's avatar Fabien Potencier

bug #813 fixed order of route registration for mounted controllers (fabpot)

This PR was merged into the master branch.

Discussion
----------

fixed order of route registration for mounted controllers

simpler alternative for #736 in order to fix #716

Commits
-------

4e6031e2 fixed order of route registration for mounted controllers
parents bc673b47 4e6031e2
...@@ -4,6 +4,7 @@ Changelog ...@@ -4,6 +4,7 @@ Changelog
1.2.0 (2013-XX-XX) 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 * Added run() on Route to be able to define the controller code
* Deprecated TwigCoreExtension (register the new HttpFragmentServiceProvider instead) * Deprecated TwigCoreExtension (register the new HttpFragmentServiceProvider instead)
* Added HttpFragmentServiceProvider * Added HttpFragmentServiceProvider
......
...@@ -473,7 +473,7 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte ...@@ -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.'); 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; return $this;
} }
......
...@@ -30,6 +30,7 @@ class ControllerCollection ...@@ -30,6 +30,7 @@ class ControllerCollection
protected $controllers = array(); protected $controllers = array();
protected $defaultRoute; protected $defaultRoute;
protected $defaultController; protected $defaultController;
protected $prefix;
/** /**
* Constructor. * Constructor.
...@@ -42,6 +43,19 @@ class ControllerCollection ...@@ -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. * Maps a pattern to a callable.
* *
...@@ -123,7 +137,9 @@ class ControllerCollection ...@@ -123,7 +137,9 @@ class ControllerCollection
call_user_func_array(array($this->defaultRoute, $method), $arguments); call_user_func_array(array($this->defaultRoute, $method), $arguments);
foreach ($this->controllers as $controller) { 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; return $this;
...@@ -141,15 +157,19 @@ class ControllerCollection ...@@ -141,15 +157,19 @@ class ControllerCollection
$routes = new RouteCollection(); $routes = new RouteCollection();
foreach ($this->controllers as $controller) { foreach ($this->controllers as $controller) {
if (!$name = $controller->getRouteName()) { if ($controller instanceof Controller) {
$name = $controller->generateRouteName($prefix); if (!$name = $controller->getRouteName()) {
while ($routes->get($name)) { $name = $controller->generateRouteName($prefix);
$name .= '_'; 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); $routes->addPrefix($prefix);
......
...@@ -489,6 +489,20 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase ...@@ -489,6 +489,20 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertSame($app, $app->mount('/hello', $mounted)); $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() public function testSendFile()
{ {
$app = new Application(); $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