diff --git a/src/Dispatcher/AbstractDispatchableController.php b/src/Dispatcher/AbstractDispatchableController.php index f1dd7fa..140b755 100644 --- a/src/Dispatcher/AbstractDispatchableController.php +++ b/src/Dispatcher/AbstractDispatchableController.php @@ -3,6 +3,7 @@ namespace Chevron\Kernel\Dispatcher; use Psr\Log; +use Chevron\Kernel\Router\RouteInterface; abstract class AbstractDispatchableController implements DispatchableInterface { diff --git a/src/Dispatcher/ActionNotFoundException.php b/src/Dispatcher/ActionNotFoundException.php index a97208f..f509354 100644 --- a/src/Dispatcher/ActionNotFoundException.php +++ b/src/Dispatcher/ActionNotFoundException.php @@ -2,4 +2,4 @@ namespace Chevron\Kernel\Dispatcher; -class ActionNotFoundException extends \Exception {} \ No newline at end of file +class ActionNotFoundException extends DispatcherException {} \ No newline at end of file diff --git a/src/Dispatcher/DispatchableInterface.php b/src/Dispatcher/DispatchableInterface.php index afd2624..435d38d 100644 --- a/src/Dispatcher/DispatchableInterface.php +++ b/src/Dispatcher/DispatchableInterface.php @@ -2,7 +2,7 @@ namespace Chevron\Kernel\Dispatcher; -use Chevron\Kernel\Router\Interfaces\RouteInterface; +use Chevron\Kernel\Router\RouteInterface; /** * our dispatcher is very simple diff --git a/src/Dispatcher/Dispatcher.php b/src/Dispatcher/Dispatcher.php index 9163675..2545e08 100644 --- a/src/Dispatcher/Dispatcher.php +++ b/src/Dispatcher/Dispatcher.php @@ -65,11 +65,15 @@ function dispatch( RouteInterface $route ){ $obj = $instance->newInstance($this->di, $route); - return function($method = "", array $args = []) use ($obj){ + $that = $this; + return function($method = "", array $args = []) use ($obj, $that, $route){ call_user_func([$obj, "init"]); if($method){ + if(!method_exists($obj, $method)){ + $this->logException(new ActionNotFoundException(sprintf("Request: %s::%s", get_class($obj), $method)), $route); + } $obj = [$obj, $method]; } @@ -78,7 +82,7 @@ function dispatch( RouteInterface $route ){ } - protected function logException(DispatcherException $e, RouteInterface $route){ + protected function logException(DispatcherException $e, RouteInterface $route = null){ if($this->logger InstanceOf Log\LoggerInterface){ $this->logger->error(get_class($e), [ "e.type" => get_class($e), diff --git a/tests/PHPUnit/Dispatcher/DispatcherTest.php b/tests/PHPUnit/Dispatcher/DispatcherTest.php index c18871e..612870c 100644 --- a/tests/PHPUnit/Dispatcher/DispatcherTest.php +++ b/tests/PHPUnit/Dispatcher/DispatcherTest.php @@ -71,7 +71,6 @@ function test_dispatch(){ // invoked itself. } - /** * @expectedException \Chevron\Kernel\Dispatcher\ControllerNotFoundException */ @@ -87,6 +86,22 @@ function test_ControllerNotFoundException(){ } + /** + * @expectedException \Chevron\Kernel\Dispatcher\ActionNotFoundException + */ + function test_ActionNotFoundException(){ + + $di = $this->getTestDi(); + $route = $this->getTestRoute("BasicController"); + + $dispatcher = new Dispatcher($di, "Chevron\\Kernel\\"); + + $controller = $dispatcher->dispatch($route); + + call_user_func($controller, "NotAMethod"); + + } + function test_ControllerNotFoundExceptionLogging(){ $di = $this->getTestDi();