From 1814298f4f1d2e27b389021f7a841b1fb7500dab Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Fri, 17 Oct 2014 13:47:57 +0000 Subject: [PATCH] Create "Resource not found" event --- src/mibew/libs/classes/Mibew/Application.php | 30 +++++++++++++++++++ .../classes/Mibew/EventDispatcher/Events.php | 14 +++++++++ 2 files changed, 44 insertions(+) diff --git a/src/mibew/libs/classes/Mibew/Application.php b/src/mibew/libs/classes/Mibew/Application.php index 89cba39f..3a5ac1de 100644 --- a/src/mibew/libs/classes/Mibew/Application.php +++ b/src/mibew/libs/classes/Mibew/Application.php @@ -137,6 +137,8 @@ class Application implements RouterAwareInterface, AuthenticationManagerAwareInt $response = call_user_func($controller, $request); } catch (AccessDeniedHttpException $e) { $response = $this->buildAccessDeniedResponse($request); + } catch (NotFoundHttpException $e) { + $response = $this->buildNotFoundResponse($request); } catch (HttpException $e) { // Build response based on status code which is stored in exception // instance. @@ -291,4 +293,32 @@ class Application implements RouterAwareInterface, AuthenticationManagerAwareInt return $response; } + + /** + * Builds response for a not found page. + * + * Triggers {@link \Mibew\EventDispatcher\Events::RESOURCE_NOT_FOUND} + * event. + * + * @param Request $request Incoming request + * @return Response + */ + protected function buildNotFoundResponse(Request $request) + { + // Trigger fail + $args = array( + 'request' => $request, + 'response' => false, + ); + $dispatcher = EventDispatcher::getInstance(); + $dispatcher->triggerEvent(Events::RESOURCE_NOT_FOUND, $args); + + if ($args['response'] && ($args['response'] instanceof Response)) { + // If one of event listeners returned the response object send it + // to the client. + return $args['response']; + } + + return new Response('Not Found', 404); + } } diff --git a/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php b/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php index dddd4a8d..9f99b1f0 100644 --- a/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php +++ b/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php @@ -141,4 +141,18 @@ final class Events * a response object to this field. */ const RESOURCE_ACCESS_DENIED = 'resourceAccessDenied'; + + /** + * Resource is not found. + * + * This event is triggered if a resource is not found. An + * associative array with the following items is passed to the event + * handlers: + * - "request": {@link Symfony\Component\HttpFoundation\Request}, incoming + * request object. + * - "response": {@link Symfony\Component\HttpFoundation\Response}, if a + * plugin wants to send a custom response to the client it should attach + * a response object to this field. + */ + const RESOURCE_NOT_FOUND = 'resourceNotFound'; }