diff --git a/core/lib/Drupal/Core/DrupalApp.php b/core/lib/Drupal/Core/DrupalApp.php index e9fae2f335a..36f1ad0ed6a 100644 --- a/core/lib/Drupal/Core/DrupalApp.php +++ b/core/lib/Drupal/Core/DrupalApp.php @@ -14,6 +14,8 @@ use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; +use Drupal\Core\EventSubscriber\HtmlSubscriber; + use Exception; /** @@ -32,21 +34,8 @@ class DrupalApp { $dispatcher = new EventDispatcher(); - // Quick and dirty attempt at wrapping our rendering logic as is. - $dispatcher->addListener(KernelEvents::VIEW, function(Event $event) { - $page_callback_result = $event->getControllerResult(); - $event->setResponse(new Response(drupal_render_page($page_callback_result))); - }); - $dispatcher->addListener(KernelEvents::EXCEPTION, function(Event $event) use ($request) { - debug($request->getAcceptableContentTypes()); - - if (in_array('text/html', $request->getAcceptableContentTypes())) { - if ($event->getException() instanceof ResourceNotFoundException) { - $event->setResponse(new Response('Not Found', 404)); - } - } - }); - + // @todo Make this extensible rather than just hard coding some. + $dispatcher->addSubscriber(new HtmlSubscriber()); // Resolve a routing context(path, etc) using the routes object to a // Set a routing context to translate. @@ -65,7 +54,7 @@ class DrupalApp { $response = $kernel->handle($request); } catch (Exception $e) { - $error_event = new GetResponseForExceptionEvent($this, $request, $this->type, $e); + $error_event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $e); $dispatcher->dispatch(KernelEvents::EXCEPTION, $error_event); if ($error_event->hasResponse()) { $response = $error_event->getResponse(); diff --git a/core/lib/Drupal/Core/EventSubscriber/HtmlSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/HtmlSubscriber.php new file mode 100644 index 00000000000..ed4048f5244 --- /dev/null +++ b/core/lib/Drupal/Core/EventSubscriber/HtmlSubscriber.php @@ -0,0 +1,52 @@ +getRequest()->getAcceptableContentTypes()); + } + + public function onResourceNotFoundException(GetResponseEvent $event) { + if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof ResourceNotFoundException) { + $event->setResponse(new Response('Not Found', 404)); + } + } + + public function onView(GetResponseEvent $event) { + if ($this->isHtmlRequestEvent($event)) { + $page_callback_result = $event->getControllerResult(); + $event->setResponse(new Response(drupal_render_page($page_callback_result))); + } + } + + public function onAccessDeniedException(Event $event) { + + } + + static function getSubscribedEvents() { + $events[KernelEvents::EXCEPTION][] = array('onResourceNotFoundException'); + $events[KernelEvents::EXCEPTION][] = array('onAccessDeniedException'); + + $events[KernelEvents::VIEW][] = array('onView'); + + return $events; + } +}