simplified HttpKernel types of request

This commit is contained in:
Fabien Potencier 2010-08-14 18:29:27 +02:00
parent c139fd64d1
commit ef0347c1b9
8 changed files with 23 additions and 47 deletions

View File

@ -96,7 +96,7 @@ class Controller
$path['_controller'] = $controller; $path['_controller'] = $controller;
$subRequest = $this->getRequest()->duplicate($query, null, $path); $subRequest = $this->getRequest()->duplicate($query, null, $path);
return $this->container->get('kernel')->handle($subRequest, HttpKernelInterface::FORWARDED_REQUEST, true); return $this->container->get('kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
} }
/** /**

View File

@ -127,7 +127,13 @@ class ControllerResolver extends BaseControllerResolver
} }
try { try {
return $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::EMBEDDED_REQUEST, true); $response = $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
if (200 != $response->getStatusCode()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
}
return $response->getContent();
} catch (\Exception $e) { } catch (\Exception $e) {
if ($options['alt']) { if ($options['alt']) {
$alt = $options['alt']; $alt = $options['alt'];

View File

@ -68,7 +68,7 @@ class ExceptionListener
$request = $event->getParameter('request')->duplicate(null, null, $parameters); $request = $event->getParameter('request')->duplicate(null, null, $parameters);
try { try {
$response = $event->getSubject()->handle($request, HttpKernelInterface::FORWARDED_REQUEST, true); $response = $event->getSubject()->handle($request, HttpKernelInterface::SUB_REQUEST, true);
error_log(sprintf('%s: %s', get_class($exception), $exception->getMessage())); error_log(sprintf('%s: %s', get_class($exception), $exception->getMessage()));
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@ -131,7 +131,7 @@ class Cache implements HttpKernelInterface
* Handles a Request. * Handles a Request.
* *
* @param Request $request A Request instance * @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 integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
* @param Boolean $raw Whether to catch exceptions or not (this is NOT used in this context) * @param Boolean $raw Whether to catch exceptions or not (this is NOT used in this context)
* *
* @return Symfony\Components\HttpFoundation\Response A Response instance * @return Symfony\Components\HttpFoundation\Response A Response instance

View File

@ -173,7 +173,13 @@ class Esi
$subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all()); $subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
try { try {
return $cache->handle($subRequest, HttpKernelInterface::EMBEDDED_REQUEST, true); $response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
if (200 != $response->getStatusCode()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
}
return $response->getContent();
} catch (\Exception $e) { } catch (\Exception $e) {
if ($alt) { if ($alt) {
return $this->handle($cache, $alt, '', $ignoreErrors); return $this->handle($cache, $alt, '', $ignoreErrors);

View File

@ -58,7 +58,7 @@ class HttpKernel implements HttpKernelInterface
* for user management. * for user management.
* *
* @param Request $request A Request instance * @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 integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
* @param Boolean $raw Whether to catch exceptions or not * @param Boolean $raw Whether to catch exceptions or not
* *
* @return Response A Response instance * @return Response A Response instance
@ -68,10 +68,6 @@ class HttpKernel implements HttpKernelInterface
*/ */
public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false) public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false)
{ {
if (HttpKernelInterface::EMBEDDED_REQUEST === $type) {
return $this->handleEmbedded($request, $raw);
}
if (null === $request) { if (null === $request) {
$request = new Request(); $request = new Request();
} }
@ -103,7 +99,7 @@ class HttpKernel implements HttpKernelInterface
* Exceptions are not caught. * Exceptions are not caught.
* *
* @param Request $request A Request instance * @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 integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
* *
* @return Response A Response instance * @return Response A Response instance
* *
@ -143,43 +139,12 @@ class HttpKernel implements HttpKernelInterface
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()) : is_array($event->getReturnValue()) ? 'an array' : str_replace("\n", '', var_export($event->getReturnValue(), true))), $type); 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()) : is_array($event->getReturnValue()) ? 'an array' : 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 $response->getContent();
} catch (\Exception $e) {
if (true === $raw)
{
throw $e;
}
return false;
}
}
/** /**
* Filters a response object. * Filters a response object.
* *
* @param Response $response A Response instance * @param Response $response A Response instance
* @param string $message A error message in case the response is not a Response object * @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 integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
* *
* @return Response The filtered Response instance * @return Response The filtered Response instance
* *

View File

@ -21,14 +21,13 @@ use Symfony\Components\HttpFoundation\Request;
interface HttpKernelInterface interface HttpKernelInterface
{ {
const MASTER_REQUEST = 1; const MASTER_REQUEST = 1;
const FORWARDED_REQUEST = 2; const SUB_REQUEST = 2;
const EMBEDDED_REQUEST = 3;
/** /**
* Handles a request to convert it to a response. * Handles a request to convert it to a response.
* *
* @param Request $request A Request instance * @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 integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
* @param Boolean $raw Whether to catch exceptions or not * @param Boolean $raw Whether to catch exceptions or not
* *
* @return Response $response A Response instance * @return Response $response A Response instance

View File

@ -184,7 +184,7 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
* Handles a request to convert it to a response by calling the HttpKernel service. * Handles a request to convert it to a response by calling the HttpKernel service.
* *
* @param Request $request A Request instance * @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 integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
* @param Boolean $raw Whether to catch exceptions or not * @param Boolean $raw Whether to catch exceptions or not
* *
* @return Response $response A Response instance * @return Response $response A Response instance