Commit 251d38bd authored by Crell's avatar Crell
Browse files

Lots of documentation and cleanup.

parent de96603c
......@@ -5,7 +5,6 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
......@@ -30,6 +29,13 @@
*/
class DrupalApp {
/**
*
* @param Request $request
* The request to process.
* @return Response
* The response object to return to the requesting user agent.
*/
function execute(Request $request) {
try {
......@@ -38,25 +44,34 @@ function execute(Request $request) {
$matcher = $this->getMatcher($request);
$dispatcher->addSubscriber(new RouterListener($matcher));
$resolver = $this->getControllerResolver($request);
$resolver = new ControllerResolver();
$kernel = new HttpKernel($dispatcher, $resolver);
$response = $kernel->handle($request);
}
catch (Exception $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();
}
else {
$response = new Response('An error occurred in the wrong place', 500);
}
// Some other form of error occured that wasn't handled by another kernel
// listener. That could mean that it's a method/mime-type/error
// combination that is not accounted for, or some other type of error.
// Either way, treat it as a server-level error and return an HTTP 500.
// By default, this will be an HTML-type response because that's a decent
// best guess if we don't know otherwise.
$response = new Response('An error occurred in the wrong place', 500);
}
return $response;
}
/**
* Returns an EventDispatcher for the Kernel to use.
*
* The EventDispatcher is pre-wired with some event listeners/subscribers.
*
* @todo Make the listeners that get attached extensible, but without using
* hooks.
*
* @return EventDispatcher
*/
protected function getDispatcher() {
$dispatcher = new EventDispatcher();
......@@ -68,6 +83,13 @@ protected function getDispatcher() {
return $dispatcher;
}
/**
* Returns a UrlMatcher object for the specified request.
*
* @param Request $request
* The request object for this matcher to use.
* @return UrlMatcher
*/
protected function getMatcher(Request $request) {
// Resolve a routing context(path, etc) using the routes object to a
// Set a routing context to translate.
......@@ -77,10 +99,4 @@ protected function getMatcher(Request $request) {
return $matcher;
}
protected function getControllerResolver() {
$resolver = new ControllerResolver();
return $resolver;
}
}
......@@ -4,41 +4,84 @@
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;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @file
*
* Description goes here.
* Definition of Drupal\Core\EventSubscriber\HtmlSubscriber;
*/
/**
* Description of HtmlSubscriber
* Main subscriber for HTML-type HTTP responses.
*/
class HtmlSubscriber implements EventSubscriberInterface {
/**
* Determines if we are dealing with an HTML-style response.
*
* @param GetResponseEvent $event
* The Event to process.
* @return boolean
* True if it is an event we should process as HTML, False otherwise.
*/
protected function isHtmlRequestEvent(GetResponseEvent $event) {
return in_array('text/html', $event->getRequest()->getAcceptableContentTypes());
}
/**
* Processes an AccessDenied exception into an HTTP 403 response.
*
* @param GetResponseEvent $event
* The Event to process.
*/
public function onAccessDeniedException(Event $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof AccessDeniedHttpException) {
$event->setResponse(new Response('Access Denied', 403));
}
}
/**
* Processes a NotFound exception into an HTTP 404 response.
*
* @param GetResponseEvent $event
* The Event to process.
*/
public function onNotFoundHttpException(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof NotFoundHttpException) {
$event->setResponse(new Response('Not Found', 404));
}
}
/**
* Processes a MethodNotAllowed exception into an HTTP 405 response.
*
* @param GetResponseEvent $event
* The Event to process.
*/
public function onMethodAllowedException(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof MethodNotAllowedException) {
$event->setResponse(new Response('Method Not Allowed', 405));
}
}
/**
* Processes a successful controller into an HTTP 200 response.
*
* Some controllers may not return a response object but simply the body of
* one. The VIEW event is called in that case, to allow us to mutate that
* body into a Response object. In particular we assume that the return
* from an HTML-type response is a render array from a legacy page callback
* and render it.
*
* @param GetResponseEvent $event
* The Event to process.
*/
public function onView(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event)) {
$page_callback_result = $event->getControllerResult();
......@@ -46,12 +89,12 @@ public function onView(GetResponseEvent $event) {
}
}
public function onAccessDeniedException(Event $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof AccessDeniedHttpException) {
$event->setResponse(new Response('Access Denied', 403));
}
}
/**
* Registers the methods in this class that should be listeners.
*
* @return array
* An array of event listener definitions.
*/
static function getSubscribedEvents() {
$events[KernelEvents::EXCEPTION][] = array('onNotFoundHttpException');
$events[KernelEvents::EXCEPTION][] = array('onAccessDeniedException');
......
......@@ -19,8 +19,8 @@ class UrlMatcher extends SymfonyUrlMatcher {
/**
* Constructor.
*
* @param RouteCollection $routes A RouteCollection instance
* @param RequestContext $context The context
* @param RequestContext $context
* The request context object.
*/
public function __construct(RequestContext $context) {
$this->context = $context;
......@@ -71,8 +71,11 @@ public function match($pathinfo) {
/**
* Get a drupal menu item.
*
* @todo Make this return multiple possible candidates for the resolver to
* consider.
*
* @param string $path
* The path being looked up by
* The path being looked up by
*/
protected function matchDrupalItem($path) {
// For now we can just proxy our procedural method. At some point this will
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment