Move the HTML request type handling to a subscriber class so that it's nicely encapsulated.
parent
501f6d5af6
commit
fd02c84604
|
@ -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();
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Core\EventSubscriber;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Description goes here.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Description of HtmlSubscriber
|
||||
*/
|
||||
class HtmlSubscriber implements EventSubscriberInterface {
|
||||
|
||||
protected function isHtmlRequestEvent(GetResponseEvent $event) {
|
||||
return in_array('text/html', $event->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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue