Skip to content

Commit

Permalink
[HttpKernel] tweaked HttpKernelInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Nov 6, 2010
1 parent bf5b8d8 commit a471f65
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 137 deletions.
37 changes: 3 additions & 34 deletions src/Symfony/Component/HttpKernel/BaseHttpKernel.php
Expand Up @@ -27,7 +27,6 @@ class BaseHttpKernel implements HttpKernelInterface
{
protected $dispatcher;
protected $resolver;
protected $request;

/**
* Constructor
Expand All @@ -42,44 +41,14 @@ public function __construct(EventDispatcher $dispatcher, ControllerResolverInter
}

/**
* 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;
}

Expand Down
20 changes: 5 additions & 15 deletions src/Symfony/Component/HttpKernel/Cache/Cache.php
Expand Up @@ -128,21 +128,11 @@ public function getRequest()
}

/**
* 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;
Expand Down Expand Up @@ -370,19 +360,19 @@ protected function fetch(Request $request)
* 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
Expand Down
19 changes: 9 additions & 10 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Expand Up @@ -43,21 +43,20 @@ public function __construct(ContainerInterface $container, EventDispatcher $disp
/**
* {@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;
}
Expand Down
23 changes: 11 additions & 12 deletions src/Symfony/Component/HttpKernel/HttpKernelInterface.php
Expand Up @@ -14,7 +14,7 @@
*/

/**
* HttpKernelInterface.
* HttpKernelInterface handles a Request to convert it to a Response.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
Expand All @@ -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);
}
24 changes: 3 additions & 21 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -45,7 +45,6 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
protected $booted;
protected $name;
protected $startTime;
protected $request;

const VERSION = '2.0.0-DEV';

Expand Down Expand Up @@ -81,7 +80,6 @@ public function __clone()

$this->booted = false;
$this->container = null;
$this->request = null;
}

abstract public function registerRootDir();
Expand Down Expand Up @@ -172,31 +170,15 @@ public function reboot()
}

/**
* 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);
}

/**
Expand Down
38 changes: 11 additions & 27 deletions tests/Symfony/Tests/Component/HttpKernel/BaseHttpKernelTest.php
Expand Up @@ -19,30 +19,14 @@

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
*/
public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue()
{
$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);
}

/**
Expand All @@ -52,7 +36,7 @@ public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalseAndNoListe
{
$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()
Expand All @@ -67,7 +51,7 @@ public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalse()

$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()
Expand All @@ -82,7 +66,7 @@ public function testHandleWhenAListenerReturnsAResponse()

$kernel = new BaseHttpKernel($dispatcher, $this->getResolver());

$this->assertEquals('hello', $kernel->handle()->getContent());
$this->assertEquals('hello', $kernel->handle(new Request())->getContent());
}

/**
Expand All @@ -93,7 +77,7 @@ public function testHandleWhenNoControllerIsFound()
$dispatcher = new EventDispatcher();
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver(false));

$kernel->handle();
$kernel->handle(new Request());
}

/**
Expand All @@ -104,7 +88,7 @@ public function testHandleWhenNoControllerIsNotACallable()
$dispatcher = new EventDispatcher();
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver('foobar'));

$kernel->handle();
$kernel->handle(new Request());
}

/**
Expand All @@ -115,7 +99,7 @@ public function testHandleWhenControllerDoesNotReturnAResponse()
$dispatcher = new EventDispatcher();
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));

$kernel->handle();
$kernel->handle(new Request());
}

public function testHandleWhenControllerDoesNotReturnAResponseButAViewIsRegistered()
Expand All @@ -127,7 +111,7 @@ public function testHandleWhenControllerDoesNotReturnAResponseButAViewIsRegister
});
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));

$this->assertEquals('foo', $kernel->handle()->getContent());
$this->assertEquals('foo', $kernel->handle(new Request())->getContent());
}

/**
Expand All @@ -142,7 +126,7 @@ public function testHandleWhenAViewDoesNotReturnAResponse()
});
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));

$kernel->handle();
$kernel->handle(new Request());
}

/**
Expand All @@ -157,7 +141,7 @@ public function testHandleWhenAResponseListenerDoesNotReturnAResponse()
});
$kernel = new BaseHttpKernel($dispatcher, $this->getResolver());

$kernel->handle();
$kernel->handle(new Request());
}

public function testHandleWithAResponseListener()
Expand All @@ -169,7 +153,7 @@ public function testHandleWithAResponseListener()
});
$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)
Expand Down
20 changes: 2 additions & 18 deletions tests/Symfony/Tests/Component/HttpKernel/HttpKernelTest.php
Expand Up @@ -19,27 +19,11 @@

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)
;
Expand Down

0 comments on commit a471f65

Please sign in to comment.