From 4810518c7d2c199eac38d1da4da804f7ce443397 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 30 Sep 2011 22:40:30 -0400 Subject: [PATCH] Fix issue where abstract or interface controllers would be constructed by Dispatcher. Tests added. Fixes #2048 --- lib/Cake/Routing/Dispatcher.php | 6 ++- lib/Cake/Test/Case/Routing/DispatcherTest.php | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Routing/Dispatcher.php b/lib/Cake/Routing/Dispatcher.php index 3acfc71bff1..58f093691b6 100644 --- a/lib/Cake/Routing/Dispatcher.php +++ b/lib/Cake/Routing/Dispatcher.php @@ -159,7 +159,11 @@ protected function _getController($request, $response) { if (!$ctrlClass) { return false; } - return new $ctrlClass($request, $response); + $reflection = new ReflectionClass($ctrlClass); + if ($reflection->isAbstract() || $reflection->isInterface()) { + return false; + } + return $reflection->newInstance($request, $response); } /** diff --git a/lib/Cake/Test/Case/Routing/DispatcherTest.php b/lib/Cake/Test/Case/Routing/DispatcherTest.php index d5f4f1d5b0f..8602909cfa1 100644 --- a/lib/Cake/Test/Case/Routing/DispatcherTest.php +++ b/lib/Cake/Test/Case/Routing/DispatcherTest.php @@ -64,6 +64,14 @@ protected function _invoke(Controller $controller, CakeRequest $request, CakeRes class MyPluginAppController extends AppController { } +abstract class DispatcherTestAbstractController extends Controller { + abstract public function index(); +} + +interface DispatcherTestInterfaceController { + public function index(); +} + /** * MyPluginController class * @@ -680,6 +688,41 @@ public function testMissingController() { $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); } +/** + * testMissingControllerInterface method + * + * @expectedException MissingControllerException + * @expectedExceptionMessage Controller class DispatcherTestInterfaceController could not be found. + * @return void + */ + public function testMissingControllerInterface() { + Router::connect('/:controller/:action/*'); + + $Dispatcher = new TestDispatcher(); + Configure::write('App.baseUrl', '/index.php'); + $url = new CakeRequest('dispatcher_test_interface/index'); + $response = $this->getMock('CakeResponse'); + + $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + } + +/** + * testMissingControllerInterface method + * + * @expectedException MissingControllerException + * @expectedExceptionMessage Controller class DispatcherTestAbstractController could not be found. + * @return void + */ + public function testMissingControllerAbstract() { + Router::connect('/:controller/:action/*'); + + $Dispatcher = new TestDispatcher(); + Configure::write('App.baseUrl', '/index.php'); + $url = new CakeRequest('dispatcher_test_abstract/index'); + $response = $this->getMock('CakeResponse'); + + $controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); + } /** * testDispatch method *