diff --git a/src/Stack/CallableHttpKernel.php b/src/Stack/CallableHttpKernel.php index 6e08dc8..45ff16d 100644 --- a/src/Stack/CallableHttpKernel.php +++ b/src/Stack/CallableHttpKernel.php @@ -5,23 +5,30 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\TerminableInterface; -class CallableHttpKernel implements HttpKernelInterface +class CallableHttpKernel implements HttpKernelInterface, TerminableInterface { - private $callable; + private $handleCallable; + private $terminateCallable; - public function __construct($callable) + public function __construct($handleCallable, $terminateCallable = null) { - if (!is_callable($callable)) { - throw new \InvalidArgumentException('Invalid callable passed to CallableHttpKernel::__construct().'); + if (!is_callable($handleCallable)) { + throw new \InvalidArgumentException('Invalid handleCallable passed to CallableHttpKernel::__construct().'); } - $this->callable = $callable; + if ($terminateCallable && !is_callable($terminateCallable)) { + throw new \InvalidArgumentException('Invalid terminateCallable passed to CallableHttpKernel::__construct().'); + } + + $this->handleCallable = $handleCallable; + $this->terminateCallable = $terminateCallable; } public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { - $response = call_user_func($this->callable, $request, $type, $catch); + $response = call_user_func($this->handleCallable, $request, $type, $catch); if (!$response instanceof Response) { throw new \UnexpectedValueException('Kernel function did not return an object of type Response'); @@ -29,4 +36,13 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ return $response; } + + public function terminate(Request $request, Response $response) + { + if (!$this->terminateCallable) { + return; + } + + call_user_func($this->terminateCallable, $request, $response); + } } diff --git a/tests/unit/Stack/CallableHttpKernelTest.php b/tests/unit/Stack/CallableHttpKernelTest.php index 503afa9..904f8af 100644 --- a/tests/unit/Stack/CallableHttpKernelTest.php +++ b/tests/unit/Stack/CallableHttpKernelTest.php @@ -9,36 +9,45 @@ class CallableHttpKernelTest extends \PHPUnit_Framework_TestCase { /** @test */ - public function handleShouldDelegateToCallable() + public function handleAndTerminateShouldDelegateToCallable() { - $spy = new SpyCallable(new Response('ok')); - $kernel = new CallableHttpKernel($spy); + $handleSpy = new SpyCallable(new Response('ok')); + $terminateSpy = new SpyCallable(null); + $kernel = new CallableHttpKernel($handleSpy, $terminateSpy); $request = Request::create('/'); $response = $kernel->handle($request); $this->assertSame('ok', $response->getContent()); - $this->assertSame(1, $spy->getCount()); + $this->assertSame(1, $handleSpy->getCount()); $this->assertSame( array($request, HttpKernelInterface::MASTER_REQUEST, true), - $spy->getCall(0) + $handleSpy->getCall(0) + ); + + $kernel->terminate($request, $response); + + $this->assertSame(1, $terminateSpy->getCount()); + $this->assertSame( + array($request, $response), + $terminateSpy->getCall(0) ); } /** @test */ public function handleShouldDelegateAllArgs() { - $spy = new SpyCallable(new Response('ok')); - $kernel = new CallableHttpKernel($spy); + $handleSpy = new SpyCallable(new Response('ok')); + $kernel = new CallableHttpKernel($handleSpy); $request = Request::create('/'); $response = $kernel->handle($request, HttpKernelInterface::SUB_REQUEST, false); $this->assertSame('ok', $response->getContent()); - $this->assertSame(1, $spy->getCount()); + $this->assertSame(1, $handleSpy->getCount()); $this->assertSame( array($request, HttpKernelInterface::SUB_REQUEST, false), - $spy->getCall(0) + $handleSpy->getCall(0) ); } }