Move the HTML request type handling to a subscriber class so that it's nicely encapsulated.

8.0.x
Larry Garfield 2012-02-23 00:58:41 -06:00
parent 501f6d5af6
commit fd02c84604
2 changed files with 57 additions and 16 deletions

View File

@ -14,6 +14,8 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Drupal\Core\EventSubscriber\HtmlSubscriber;
use Exception; use Exception;
/** /**
@ -32,21 +34,8 @@ class DrupalApp {
$dispatcher = new EventDispatcher(); $dispatcher = new EventDispatcher();
// Quick and dirty attempt at wrapping our rendering logic as is. // @todo Make this extensible rather than just hard coding some.
$dispatcher->addListener(KernelEvents::VIEW, function(Event $event) { $dispatcher->addSubscriber(new HtmlSubscriber());
$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));
}
}
});
// Resolve a routing context(path, etc) using the routes object to a // Resolve a routing context(path, etc) using the routes object to a
// Set a routing context to translate. // Set a routing context to translate.
@ -65,7 +54,7 @@ class DrupalApp {
$response = $kernel->handle($request); $response = $kernel->handle($request);
} }
catch (Exception $e) { 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); $dispatcher->dispatch(KernelEvents::EXCEPTION, $error_event);
if ($error_event->hasResponse()) { if ($error_event->hasResponse()) {
$response = $error_event->getResponse(); $response = $error_event->getResponse();

View File

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