Create "Resource not found" event

This commit is contained in:
Dmitriy Simushev 2014-10-17 13:47:57 +00:00
parent 5c52ec61da
commit 1814298f4f
2 changed files with 44 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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';
}