Commit 01daa0c0 authored by Fabien Potencier's avatar Fabien Potencier

feature #808 changed the default controller to be unique for the whole app (fabpot)

This PR was merged into the master branch.

Discussion
----------

changed the default controller to be unique for the whole app

Commits
-------

9c68da8d changed the default controller to be unique for the whole app
parents 2b966973 9c68da8d
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
namespace Silex; namespace Silex;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\HttpFoundation\Request;
use Silex\Controller; use Silex\Controller;
/** /**
...@@ -28,6 +29,7 @@ class ControllerCollection ...@@ -28,6 +29,7 @@ class ControllerCollection
{ {
protected $controllers = array(); protected $controllers = array();
protected $defaultRoute; protected $defaultRoute;
protected $defaultController;
/** /**
* Constructor. * Constructor.
...@@ -35,6 +37,9 @@ class ControllerCollection ...@@ -35,6 +37,9 @@ class ControllerCollection
public function __construct(Route $defaultRoute) public function __construct(Route $defaultRoute)
{ {
$this->defaultRoute = $defaultRoute; $this->defaultRoute = $defaultRoute;
$this->defaultController = function (Request $request) {
throw new \LogicException(sprintf('The "%s" route must have code to run when it matches.', $request->attributes->get('_route')));
};
} }
/** /**
...@@ -52,13 +57,7 @@ class ControllerCollection ...@@ -52,13 +57,7 @@ class ControllerCollection
$route = clone $this->defaultRoute; $route = clone $this->defaultRoute;
$route->setPath($pattern); $route->setPath($pattern);
$this->controllers[] = $controller = new Controller($route); $this->controllers[] = $controller = new Controller($route);
$route->setDefault('_controller', null === $to ? $this->defaultController : $to);
if (null === $to) {
$to = function () use ($controller) {
throw new \LogicException(sprintf('The "%s" route must have code to run when it matches.', $controller->getRouteName()));
};
}
$route->setDefault('_controller', $to);
return $controller; return $controller;
} }
......
...@@ -502,6 +502,18 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase ...@@ -502,6 +502,18 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
} }
} }
/**
* @expectedException \LogicException
* @expectedExceptionMessage The "homepage" route must have code to run when it matches.
*/
public function testGetRouteCollectionWithRouteWithoutController()
{
$app = new Application();
$app['exception_handler']->disable();
$app->match('/')->bind('homepage');
$app->handle(Request::create('/'));
}
public function testRedirectDoesNotRaisePHPNoticesWhenMonologIsRegistered() public function testRedirectDoesNotRaisePHPNoticesWhenMonologIsRegistered()
{ {
$app = new Application(); $app = new Application();
......
...@@ -30,19 +30,6 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase ...@@ -30,19 +30,6 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(0, count($routes->all())); $this->assertEquals(0, count($routes->all()));
} }
/**
* @expectedException \LogicException
* @expectedExceptionMessage The "foo" route must have code to run when it matches.
*/
public function testGetRouteCollectionWithRouteWithoutController()
{
$controllers = new ControllerCollection(new Route());
$controllers->match('/foo')->bind('foo');
$routes = $controllers->flush();
call_user_func($routes->get('foo')->getDefault('_controller'));
}
public function testGetRouteCollectionWithRoutes() public function testGetRouteCollectionWithRoutes()
{ {
$controllers = new ControllerCollection(new Route()); $controllers = new ControllerCollection(new Route());
......
...@@ -55,7 +55,7 @@ class RememberMeServiceProviderTest extends WebTestCase ...@@ -55,7 +55,7 @@ class RememberMeServiceProviderTest extends WebTestCase
$app = new Application(); $app = new Application();
$app['debug'] = true; $app['debug'] = true;
unset($app['exception_handler']); $app['exception_handler']->disable();
$app->register(new SessionServiceProvider(), array( $app->register(new SessionServiceProvider(), array(
'session.test' => true, 'session.test' => true,
......
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