Prechádzať zdrojové kódy

simplified HttpKernel types of request

Fabien Potencier 14 rokov pred
rodič
commit
ef0347c1b9

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Controller.php

@@ -96,7 +96,7 @@ class Controller
         $path['_controller'] = $controller;
         $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);
     }
 
     /**

+ 7 - 1
src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php

@@ -127,7 +127,13 @@ class ControllerResolver extends BaseControllerResolver
         }
 
         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) {
             if ($options['alt']) {
                 $alt = $options['alt'];

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionListener.php

@@ -68,7 +68,7 @@ class ExceptionListener
         $request = $event->getParameter('request')->duplicate(null, null, $parameters);
 
         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()));
         } catch (\Exception $e) {

+ 1 - 1
src/Symfony/Components/HttpKernel/Cache/Cache.php

@@ -131,7 +131,7 @@ class Cache implements HttpKernelInterface
      * Handles a Request.
      *
      * @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)
      *
      * @return Symfony\Components\HttpFoundation\Response A Response instance

+ 7 - 1
src/Symfony/Components/HttpKernel/Cache/Esi.php

@@ -173,7 +173,13 @@ class Esi
         $subRequest = Request::create($uri, 'get', array(), $cache->getRequest()->cookies->all(), array(), $cache->getRequest()->server->all());
 
         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) {
             if ($alt) {
                 return $this->handle($cache, $alt, '', $ignoreErrors);

+ 3 - 38
src/Symfony/Components/HttpKernel/HttpKernel.php

@@ -58,7 +58,7 @@ class HttpKernel implements HttpKernelInterface
      * for user management.
      *
      * @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
      *
      * @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)
     {
-        if (HttpKernelInterface::EMBEDDED_REQUEST === $type) {
-            return $this->handleEmbedded($request, $raw);
-        }
-
         if (null === $request) {
             $request = new Request();
         }
@@ -103,7 +99,7 @@ class HttpKernel implements HttpKernelInterface
      * Exceptions are not caught.
      *
      * @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
      *
@@ -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);
     }
 
-    /**
-     * 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.
      *
      * @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 integer  $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
      *
      * @return Response The filtered Response instance
      *

+ 2 - 3
src/Symfony/Components/HttpKernel/HttpKernelInterface.php

@@ -21,14 +21,13 @@ use Symfony\Components\HttpFoundation\Request;
 interface HttpKernelInterface
 {
     const MASTER_REQUEST = 1;
-    const FORWARDED_REQUEST = 2;
-    const EMBEDDED_REQUEST = 3;
+    const SUB_REQUEST = 2;
 
     /**
      * Handles a request to convert it to a response.
      *
      * @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
      *
      * @return Response $response A Response instance

+ 1 - 1
src/Symfony/Framework/Kernel.php

@@ -184,7 +184,7 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
      * Handles a request to convert it to a response by calling the HttpKernel service.
      *
      * @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
      *
      * @return Response $response A Response instance