Skip to content

Commit

Permalink
changed the main parameter of the kernel handle() method to type with…
Browse files Browse the repository at this point in the history
… 3 different values
  • Loading branch information
fabpot committed May 13, 2010
1 parent bb77e9a commit 2c16569
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 59 deletions.
97 changes: 65 additions & 32 deletions src/Symfony/Components/HttpKernel/HttpKernel.php
Expand Up @@ -38,7 +38,7 @@ public function __construct(EventDispatcher $dispatcher)
}

/**
* Gets the Request instance associated with the main request.
* Gets the Request instance associated with the master request.
*
* @return Request A Request instance
*/
Expand All @@ -48,42 +48,45 @@ public function getRequest()
}

/**
* Handles a request to convert it to a response.
* Handles a Request to convert it to a Response.
*
* All exceptions are caught, and a core.exception event is notified
* for user management.
*
* @param Request $request A Request instance
* @param Boolean $main Whether this is the main request or not
* @param Boolean $raw Whether to catch exceptions or not
* @param Request $request A Request instance
* @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST, HttpKernelInterface::FORWARDED_REQUEST, or HttpKernelInterface::EMBEDDED_REQUEST)
* @param Boolean $raw Whether to catch exceptions or not
*
* @return Response $response A Response instance
* @return Response A Response instance
*
* @throws \Exception When Exception couldn't be caught by event processing
* @throws \Exception When an Exception occurs during processing
* and couldn't be caught by event processing or $raw is true
*/
public function handle(Request $request = null, $main = true, $raw = false)
public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false)
{
$main = (Boolean) $main;
if (HttpKernelInterface::EMBEDDED_REQUEST === $type) {
return $this->handleEmbedded($request, $raw);
}

if (null === $request) {
$request = new Request();
}

if (true === $main) {
if (HttpKernelInterface::MASTER_REQUEST === $type) {
$this->request = $request;
}

try {
return $this->handleRaw($request, $main);
return $this->handleRaw($request, $type);
} catch (\Exception $e) {
if (true === $raw) {
throw $e;
}

// exception
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.exception', array('main_request' => $main, 'request' => $request, 'exception' => $e)));
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e)));
if ($event->isProcessed()) {
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.exception" listener returned a non response object.', $main);
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.exception" listener returned a non response object.', $type);
}

throw $e;
Expand All @@ -95,26 +98,24 @@ public function handle(Request $request = null, $main = true, $raw = false)
*
* Exceptions are not caught.
*
* @param Request $request A Request instance
* @param Boolean $main Whether this is the main request or not
* @param Request $request A Request instance
* @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST, HttpKernelInterface::FORWARDED_REQUEST, or HttpKernelInterface::EMBEDDED_REQUEST)
*
* @return Response $response A Response instance
* @return Response A Response instance
*
* @throws \LogicException If one of the listener does not behave as expected
* @throws NotFoundHttpException When controller cannot be found
*/
protected function handleRaw(Request $request, $main = true)
protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
{
$main = (Boolean) $main;

// request
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.request', array('main_request' => $main, 'request' => $request)));
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.request', array('request_type' => $type, 'request' => $request)));
if ($event->isProcessed()) {
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.request" listener returned a non response object.', $main);
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.request" listener returned a non response object.', $type);
}

// load controller
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.load_controller', array('main_request' => $main, 'request' => $request)));
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.load_controller', array('request_type' => $type, 'request' => $request)));
if (!$event->isProcessed()) {
throw new NotFoundHttpException('Unable to find the controller.');
}
Expand All @@ -127,10 +128,10 @@ protected function handleRaw(Request $request, $main = true)
}

// controller
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.controller', array('main_request' => $main, 'request' => $request, 'controller' => &$controller, 'arguments' => &$arguments)));
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request, 'controller' => &$controller, 'arguments' => &$arguments)));
if ($event->isProcessed()) {
try {
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.controller" listener returned a non response object.', $main);
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.controller" listener returned a non response object.', $type);
} catch (\Exception $e) {
$retval = $event->getReturnValue();
}
Expand All @@ -140,28 +141,60 @@ protected function handleRaw(Request $request, $main = true)
}

// view
$event = $this->dispatcher->filter(new Event($this, 'core.view', array('main_request' => $main, 'request' => $request)), $retval);
$event = $this->dispatcher->filter(new Event($this, 'core.view', array('request_type' => $type, 'request' => $request)), $retval);

return $this->filterResponse($event->getReturnValue(), $request, sprintf('The controller must return a response (instead of %s).', is_object($event->getReturnValue()) ? 'an object of class '.get_class($event->getReturnValue()) : str_replace("\n", '', var_export($event->getReturnValue(), true))), $type);
}

/**
* Handles a request that need to be embedded.
*
* @param Request $request A Request instance
* @param Boolean $raw Whether to catch exceptions or not
*
* @return string|false The Response content or false if there is a problem
*
* @throws \RuntimeException When an Exception occurs during processing
* and couldn't be caught by event processing or $raw is true
*/
protected function handleEmbedded(Request $request, $raw = false)
{
try {
$response = $this->handleRaw($request, HttpKernelInterface::EMBEDDED_REQUEST);

if (200 != $response->getStatusCode()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
}

return $this->filterResponse($event->getReturnValue(), $request, sprintf('The controller must return a response (instead of %s).', is_object($event->getReturnValue()) ? 'an object of class '.get_class($event->getReturnValue()) : str_replace("\n", '', var_export($event->getReturnValue(), true))), $main);
return $response->getContent();
} catch (\Exception $e) {
if (true === $raw)
{
throw $e;
}

return false;
}
}

/**
* Filters a response object.
*
* @param Object $response A Response instance
* @param string $message A error message in case the response is not a Response object
* @param Response $response A Response instance
* @param string $message A error message in case the response is not a Response object
* @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST, HttpKernelInterface::FORWARDED_REQUEST, or HttpKernelInterface::EMBEDDED_REQUEST)
*
* @param Object $response The filtered Response instance
* @return Response The filtered Response instance
*
* @throws \RuntimeException if the response object does not implement the send() method
* @throws \RuntimeException if the passed object is not a Response instance
*/
protected function filterResponse($response, $request, $message, $main)
protected function filterResponse($response, $request, $message, $type)
{
if (!$response instanceof Response) {
throw new \RuntimeException($message);
}

$event = $this->dispatcher->filter(new Event($this, 'core.response', array('main_request' => $main, 'request' => $request)), $response);
$event = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response);
$response = $event->getReturnValue();

if (!$response instanceof Response) {
Expand Down
10 changes: 7 additions & 3 deletions src/Symfony/Components/HttpKernel/HttpKernelInterface.php
Expand Up @@ -20,19 +20,23 @@
*/
interface HttpKernelInterface
{
const MASTER_REQUEST = 1;
const FORWARDED_REQUEST = 2;
const EMBEDDED_REQUEST = 3;

/**
* Handles a request to convert it to a response.
*
* @param Request $request A Request instance
* @param Boolean $main Whether this is the main request or not
* @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST, HttpKernelInterface::FORWARDED_REQUEST, or HttpKernelInterface::EMBEDDED_REQUEST)
* @param Boolean $raw Whether to catch exceptions or not
*
* @return Response $response A Response instance
*/
public function handle(Request $request = null, $main = true, $raw = false);
public function handle(Request $request = null, $type = self::MASTER_REQUEST, $raw = false);

/**
* Gets the Request instance associated with the main request.
* Gets the Request instance associated with the master request.
*
* @return Request A Request instance
*/
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Foundation/Kernel.php
Expand Up @@ -151,7 +151,7 @@ public function reboot()
}

/**
* Gets the Request instance associated with the main request.
* Gets the Request instance associated with the master request.
*
* @return Request A Request instance
*/
Expand All @@ -164,12 +164,12 @@ public function getRequest()
* Handles a request to convert it to a response by calling the HttpKernel service.
*
* @param Request $request A Request instance
* @param Boolean $main Whether this is the main request or not
* @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST, HttpKernelInterface::FORWARDED_REQUEST, or HttpKernelInterface::EMBEDDED_REQUEST)
* @param Boolean $raw Whether to catch exceptions or not
*
* @return Response $response A Response instance
*/
public function handle(Request $request = null, $main = true, $raw = false)
public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false)
{
if (false === $this->booted) {
$this->boot();
Expand All @@ -179,13 +179,13 @@ public function handle(Request $request = null, $main = true, $raw = false)
$request = $this->container->getRequestService();
}

if (true === $main) {
if (HttpKernelInterface::MASTER_REQUEST === $type) {
$this->request = $request;
}

$this->container->setService('request', $request);

$response = $this->container->getHttpKernelService()->handle($request, $main, $raw);
$response = $this->container->getHttpKernelService()->handle($request, $type, $raw);

$this->container->setService('request', $this->request);

Expand Down
Expand Up @@ -5,6 +5,7 @@
use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\EventDispatcher\Event;
use Symfony\Components\HttpKernel\Response;
use Symfony\Components\HttpKernel\HttpKernelInterface;
use Symfony\Framework\ProfilerBundle\ProfilerStorage;
use Symfony\Foundation\LoggerInterface;

Expand Down Expand Up @@ -49,7 +50,7 @@ public function register()

public function handle(Event $event, Response $response)
{
if (!$event->getParameter('main_request')) {
if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type')) {
return $response;
}

Expand Down
Expand Up @@ -5,6 +5,7 @@
use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\EventDispatcher\Event;
use Symfony\Components\HttpKernel\Response;
use Symfony\Components\HttpKernel\HttpKernelInterface;
use Symfony\Framework\ProfilerBundle\DataCollector\DataCollectorManager;

/*
Expand Down Expand Up @@ -41,7 +42,7 @@ public function register()

public function handle(Event $event, Response $response)
{
if (!$event->getParameter('main_request')) {
if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type')) {
return $response;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Framework/WebBundle/Controller.php
Expand Up @@ -5,6 +5,7 @@
use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\HttpKernel\Request;
use Symfony\Components\HttpKernel\Response;
use Symfony\Components\HttpKernel\HttpKernelInterface;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -96,7 +97,7 @@ public function forward($controller, array $path = array(), array $query = array
$path['_controller'] = $controller;
$subRequest = $this->getRequest()->duplicate($query, null, $path);

return $this->container->getKernelService()->handle($subRequest, false, true);
return $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::FORWARDED_REQUEST, true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Framework/WebBundle/Helper/ActionsHelper.php
Expand Up @@ -62,7 +62,7 @@ public function render($controller, array $path = array(), array $query = array(
$path['_controller'] = $controller;
$subRequest = $this->container->getRequestService()->duplicate($query, null, Escaper::unescape($path));

return $this->container->getKernelService()->handle($subRequest, false, true);
return $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::EMBEDDED_REQUEST, true);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Framework/WebBundle/Listener/ExceptionHandler.php
Expand Up @@ -5,6 +5,7 @@
use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\EventDispatcher\Event;
use Symfony\Foundation\LoggerInterface;
use Symfony\Components\HttpKernel\HttpKernelInterface;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -43,7 +44,7 @@ public function register()

public function handle(Event $event)
{
if (!$event->getParameter('main_request')) {
if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type')) {
return false;
}

Expand All @@ -63,7 +64,7 @@ public function handle(Event $event)
$request = $event->getParameter('request')->duplicate(null, null, $parameters);

try {
$response = $event->getSubject()->handle($request, false, true);
$response = $event->getSubject()->handle($request, HttpKernelInterface::FORWARDED_REQUEST, true);

error_log(sprintf('%s: %s', get_class($exception), $exception->getMessage()));
} catch (\Exception $e) {
Expand Down
23 changes: 11 additions & 12 deletions src/Symfony/Framework/WebBundle/Listener/RequestParser.php
Expand Up @@ -6,6 +6,7 @@
use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\EventDispatcher\Event;
use Symfony\Components\Routing\RouterInterface;
use Symfony\Components\HttpKernel\HttpKernelInterface;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -45,20 +46,18 @@ public function resolve(Event $event)
{
$request = $event->getParameter('request');

if (!$event->getParameter('main_request')) {
return;
if (HttpKernelInterface::MASTER_REQUEST === $event->getParameter('request_type')) {
// set the context even if the parsing does not need to be done
// to have correct link generation
$this->router->setContext(array(
'base_url' => $request->getBaseUrl(),
'method' => $request->getMethod(),
'host' => $request->getHost(),
'is_secure' => $request->isSecure(),
));
$this->container->setParameter('request.base_path', $request->getBasePath());
}

// set the context even if the parsing does not need to be done
// to have correct link generation
$this->router->setContext(array(
'base_url' => $request->getBaseUrl(),
'method' => $request->getMethod(),
'host' => $request->getHost(),
'is_secure' => $request->isSecure(),
));
$this->container->setParameter('request.base_path', $request->getBasePath());

if ($request->path->has('_controller')) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Framework/WebBundle/Listener/ResponseFilter.php
Expand Up @@ -6,6 +6,7 @@
use Symfony\Components\EventDispatcher\Event;
use Symfony\Components\HttpKernel\Request;
use Symfony\Components\HttpKernel\Response;
use Symfony\Components\HttpKernel\HttpKernelInterface;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -39,7 +40,7 @@ public function register()

public function filter(Event $event, Response $response)
{
if (!$event->getParameter('main_request') || $response->headers->has('Content-Type')) {
if (HttpKernelInterface::MASTER_REQUEST !== $event->getParameter('request_type') || $response->headers->has('Content-Type')) {
return $response;
}

Expand Down

0 comments on commit 2c16569

Please sign in to comment.