protected function getResponse(Request $request, Route $route, array $params)
{
// convert route params into container params
$params = $this->getContainerParams($route, $params, $request);
$this->currentRoute = $route;
if ($this->eventDispatcher !== null) {
$event = new Events\BeforeEvent($request, $route);
$this->eventDispatcher->dispatch("route.before", $event);
foreach ($route->getBeforeHooks() as $hook) {
$this->eventDispatcher->dispatch("route.before.{$hook}", $event);
}
}
// if the event has been dispatched, check if the event has a response
// that should override the route's response. if the event doesn't have
// a response, check if the event has a controller that should override
// the route's controller
if (isset($event)) {
$response = $event->getResponse();
if (!$response) {
$callable = $event->getController() ?: $route->getController();
}
} else {
$callable = $route->getController();
}
// if the event hasn't been dispatched, or the event hasn't had a
// response set onto it, invoke the controller
if (!isset($response) || !$response) {
$constructorArgs = $route->getOption('constructor_params');
$response = $this->invoker->invoke($callable, $params, $constructorArgs);
}
if (!$response instanceof Response) {
if (is_array($response) || $response instanceof \stdClass) {
$response = new JsonResponse($response);
} else {
$response = new Response($response);
}
}
if ($this->eventDispatcher !== null) {
$event = new Events\AfterEvent($request, $route, $response);
$this->eventDispatcher->dispatch("route.after", $event);
foreach ($route->getAfterHooks() as $hook) {
$this->eventDispatcher->dispatch("route.after.{$hook}", $event);
}
if ($event->getResponse() !== $response) {
$response = $event->getResponse();
}
}
$this->currentRoute = null;
return $response;
}