Commit 9c68da8d authored by Fabien Potencier's avatar Fabien Potencier

changed the default controller to be unique for the whole app

parent 2b966973
......@@ -12,6 +12,7 @@
namespace Silex;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\HttpFoundation\Request;
use Silex\Controller;
/**
......@@ -28,6 +29,7 @@ class ControllerCollection
{
protected $controllers = array();
protected $defaultRoute;
protected $defaultController;
/**
* Constructor.
......@@ -35,6 +37,9 @@ class ControllerCollection
public function __construct(Route $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
$route = clone $this->defaultRoute;
$route->setPath($pattern);
$this->controllers[] = $controller = new Controller($route);
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);
$route->setDefault('_controller', null === $to ? $this->defaultController : $to);
return $controller;
}
......
......@@ -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()
{
$app = new Application();
......
......@@ -30,19 +30,6 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
$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()
{
$controllers = new ControllerCollection(new Route());
......
......@@ -55,7 +55,7 @@ class RememberMeServiceProviderTest extends WebTestCase
$app = new Application();
$app['debug'] = true;
unset($app['exception_handler']);
$app['exception_handler']->disable();
$app->register(new SessionServiceProvider(), array(
'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