Browse Source

[HttpKernel] tweaked HttpKernelInterface

Fabien Potencier 14 years ago
parent
commit
a471f65759

+ 3 - 34
src/Symfony/Component/HttpKernel/BaseHttpKernel.php

@@ -27,7 +27,6 @@ class BaseHttpKernel implements HttpKernelInterface
 {
     protected $dispatcher;
     protected $resolver;
-    protected $request;
 
     /**
      * Constructor
@@ -42,44 +41,14 @@ class BaseHttpKernel implements HttpKernelInterface
     }
 
     /**
-     * Gets the Request instance associated with the master request.
-     *
-     * @return Request A Request instance
+     * {@inheritdoc}
      */
-    public function getRequest()
+    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
     {
-        return $this->request;
-    }
-
-    /**
-     * 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 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
-     *
-     * @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, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false)
-    {
-        if (null === $request) {
-            $request = new Request();
-        }
-
-        if (HttpKernelInterface::MASTER_REQUEST === $type) {
-            $this->request = $request;
-        }
-
         try {
             return $this->handleRaw($request, $type);
         } catch (\Exception $e) {
-            if (true === $raw) {
+            if (false === $catch) {
                 throw $e;
             }
 

+ 5 - 15
src/Symfony/Component/HttpKernel/Cache/Cache.php

@@ -128,21 +128,11 @@ 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 or HttpKernelInterface::SUB_REQUEST)
-     * @param Boolean $raw     Whether to catch exceptions or not (this is NOT used in this context)
-     *
-     * @return Symfony\Component\HttpFoundation\Response A Response instance
+     * {@inheritdoc}
      */
-    public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false)
+    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
     {
         // FIXME: catch exceptions and implement a 500 error page here? -> in Varnish, there is a built-in error page mechanism
-        if (null === $request) {
-            $request = new Request();
-        }
-
         if (HttpKernelInterface::MASTER_REQUEST === $type) {
             $this->traces = array();
             $this->request = $request;
@@ -370,19 +360,19 @@ class Cache implements HttpKernelInterface
      * Forwards the Request to the backend and returns the Response.
      *
      * @param Request  $request  A Request instance
-     * @param Boolean  $raw      Whether to catch exceptions or not
+     * @param Boolean  $catch    Whether to catch exceptions or not
      * @param Response $response A Response instance (the stale entry if present, null otherwise)
      *
      * @return Response A Response instance
      */
-    protected function forward(Request $request, $raw = false, Response $entry = null)
+    protected function forward(Request $request, $catch = false, Response $entry = null)
     {
         if ($this->esi) {
             $this->esi->addSurrogateEsiCapability($request);
         }
 
         // always a "master" request (as the real master request can be in cache)
-        $response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $raw);
+        $response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch);
         // FIXME: we probably need to also catch exceptions if raw === true
 
         // we don't implement the stale-if-error on Requests, which is nonetheless part of the RFC

+ 9 - 10
src/Symfony/Component/HttpKernel/HttpKernel.php

@@ -43,21 +43,20 @@ class HttpKernel extends BaseHttpKernel
     /**
      * {@inheritdoc}
      */
-    public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false)
+    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
     {
-        if (null === $request) {
-            $request = $this->container->get('request');
-        } else {
-            $this->container->set('request', $request);
-        }
-
+        $currentRequest = null;
         if (HttpKernelInterface::MASTER_REQUEST === $type) {
-            $this->request = $request;
+            $currentRequest = $this->container->get('request');
         }
 
-        $response = parent::handle($request, $type, $raw);
+        $this->container->set('request', $request);
 
-        $this->container->set('request', $this->request);
+        $response = parent::handle($request, $type, $catch);
+
+        if (null !== $currentRequest) {
+            $this->container->set('request', $currentRequest);
+        }
 
         return $response;
     }

+ 11 - 12
src/Symfony/Component/HttpKernel/HttpKernelInterface.php

@@ -14,7 +14,7 @@ use Symfony\Component\HttpFoundation\Request;
  */
 
 /**
- * HttpKernelInterface.
+ * HttpKernelInterface handles a Request to convert it to a Response.
  *
  * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  */
@@ -24,20 +24,19 @@ interface HttpKernelInterface
     const SUB_REQUEST = 2;
 
     /**
-     * Handles a request to convert it to a response.
+     * Handles a Request to convert it to a Response.
+     *
+     * When $catch is true, the implementation must catch all exceptions
+     * and do its best to convert them to a Response instance.
      *
      * @param  Request $request A Request instance
-     * @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  integer $type    The type of the request
+     *                          (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
+     * @param  Boolean $catch   Whether to catch exceptions or not
      *
-     * @return Response $response A Response instance
-     */
-    function handle(Request $request = null, $type = self::MASTER_REQUEST, $raw = false);
-
-    /**
-     * Gets the Request instance associated with the master request.
+     * @return Response A Response instance
      *
-     * @return Request A Request instance
+     * @throws \Exception When an Exception occurs during processing
      */
-    function getRequest();
+    function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true);
 }

+ 3 - 21
src/Symfony/Component/HttpKernel/Kernel.php

@@ -45,7 +45,6 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
     protected $booted;
     protected $name;
     protected $startTime;
-    protected $request;
 
     const VERSION = '2.0.0-DEV';
 
@@ -81,7 +80,6 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
 
         $this->booted = false;
         $this->container = null;
-        $this->request = null;
     }
 
     abstract public function registerRootDir();
@@ -172,31 +170,15 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
     }
 
     /**
-     * Gets the Request instance associated with the master request.
-     *
-     * @return Request A Request instance
-     */
-    public function getRequest()
-    {
-        return $this->request;
-    }
-
-    /**
-     * 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 or HttpKernelInterface::SUB_REQUEST)
-     * @param  Boolean $raw     Whether to catch exceptions or not
-     *
-     * @return Response $response A Response instance
+     * {@inheritdoc}
      */
-    public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false)
+    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
     {
         if (false === $this->booted) {
             $this->boot();
         }
 
-        return $this->container->getHttpKernelService()->handle($request, $type, $raw);
+        return $this->container->getHttpKernelService()->handle($request, $type, $catch);
     }
 
     /**

+ 11 - 27
tests/Symfony/Tests/Component/HttpKernel/BaseHttpKernelTest.php

@@ -19,22 +19,6 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
 
 class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
 {
-    public function testHandleChangingMasterRequest()
-    {
-        $kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver());
-
-        $kernel->handle();
-        $this->assertInstanceof('Symfony\Component\HttpFoundation\Request', $kernel->getRequest());
-
-        $request = Request::create('/');
-        $kernel->handle($request);
-        $this->assertSame($request, $kernel->getRequest());
-
-        $subRequest = Request::create('/');
-        $kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
-        $this->assertSame($request, $kernel->getRequest());
-    }
-
     /**
      * @expectedException RuntimeException
      */
@@ -42,7 +26,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
     {
         $kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
 
-        $kernel->handle(null, HttpKernelInterface::MASTER_REQUEST, true);
+        $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
     }
 
     /**
@@ -52,7 +36,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
     {
         $kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
 
-        $kernel->handle(null, HttpKernelInterface::MASTER_REQUEST, false);
+        $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
     }
 
     public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalse()
@@ -67,7 +51,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
 
         $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); }));
 
-        $this->assertEquals('foo', $kernel->handle()->getContent());
+        $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
     }
 
     public function testHandleWhenAListenerReturnsAResponse()
@@ -82,7 +66,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
 
         $kernel = new BaseHttpKernel($dispatcher, $this->getResolver());
 
-        $this->assertEquals('hello', $kernel->handle()->getContent());
+        $this->assertEquals('hello', $kernel->handle(new Request())->getContent());
     }
 
     /**
@@ -93,7 +77,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
         $dispatcher = new EventDispatcher();
         $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(false));
 
-        $kernel->handle();
+        $kernel->handle(new Request());
     }
 
     /**
@@ -104,7 +88,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
         $dispatcher = new EventDispatcher();
         $kernel = new BaseHttpKernel($dispatcher, $this->getResolver('foobar'));
 
-        $kernel->handle();
+        $kernel->handle(new Request());
     }
 
     /**
@@ -115,7 +99,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
         $dispatcher = new EventDispatcher();
         $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
 
-        $kernel->handle();
+        $kernel->handle(new Request());
     }
 
     public function testHandleWhenControllerDoesNotReturnAResponseButAViewIsRegistered()
@@ -127,7 +111,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
         });
         $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
 
-        $this->assertEquals('foo', $kernel->handle()->getContent());
+        $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
     }
 
     /**
@@ -142,7 +126,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
         });
         $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
 
-        $kernel->handle();
+        $kernel->handle(new Request());
     }
 
     /**
@@ -157,7 +141,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
         });
         $kernel = new BaseHttpKernel($dispatcher, $this->getResolver());
 
-        $kernel->handle();
+        $kernel->handle(new Request());
     }
 
     public function testHandleWithAResponseListener()
@@ -169,7 +153,7 @@ class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase
         });
         $kernel = new BaseHttpKernel($dispatcher, $this->getResolver());
 
-        $this->assertEquals('foo', $kernel->handle()->getContent());
+        $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
     }
 
     protected function getResolver($controller = null)

+ 2 - 18
tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php

@@ -19,27 +19,11 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
 
 class HttpKernelTest extends \PHPUnit_Framework_TestCase
 {
-    public function testHandleGetsTheRequestFromTheContainer()
+    public function testHandleSetsTheRequest()
     {
         $request = Request::create('/');
         $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
-        $container->expects($this->any())
-                  ->method('get')
-                  ->will($this->returnValue($request))
-        ;
-
-        $kernel = new HttpKernel($container, new EventDispatcher(), $this->getResolver());
-
-        $kernel->handle();
-
-        $this->assertEquals($request, $kernel->getRequest());
-    }
-
-    public function testHandleSetsTheRequestIfPassed()
-    {
-        $request = Request::create('/');
-        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
-        $container->expects($this->exactly(2))
+        $container->expects($this->exactly(1))
                   ->method('set')
                   ->with('request', $request)
         ;