Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Add LazyTerminableHttpKernel class to handle kernel termination. #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Stack/LazyHttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class LazyHttpKernel implements HttpKernelInterface
{
private $factory;
private $app;
protected $app;

public function __construct(callable $factory)
{
Expand Down
18 changes: 18 additions & 0 deletions src/Stack/LazyTerminableHttpKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Stack;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\TerminableInterface;

class LazyTerminableHttpKernel extends LazyHttpKernel implements TerminableInterface
{
public function terminate(Request $request, Response $response)
{
// don't instantiate the wrapped lazy kernel but do terminate it if it's been instantiated already
if ($this->app instanceof TerminableInterface) {
return $this->app->terminate($request, $response);
}
}
}
4 changes: 2 additions & 2 deletions src/Stack/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Stack;

function lazy(callable $factory)
function lazy(callable $factory, $terminable = false)
{
return new LazyHttpKernel($factory);
return $terminable ? new LazyTerminableHttpKernel($factory) : new LazyHttpKernel($factory);
}
77 changes: 77 additions & 0 deletions tests/unit/Stack/LazyTerminableHttpKernelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Stack;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\TerminableInterface;

/*
* Extends the LazyHttpKernelTest to perform the base tests as well, this is for BC.
*
* The new tests checks:
* - if the wrapped kernel gets terminated once the lazy wrapper is.
* - if the shortcut function returns the correct class when the second argument is provided
*/
class LazyTerminableHttpKernelTest extends LazyHttpKernelTest
{
public function testTermination()
{
$app = null;

/* @var \Stack\LazyTerminableHttpKernel $kernel */
$kernel = lazy(function () use (&$app) {
return $app = $this->createHelloKernel();
}, true);

// if the lazy kernel did not handle a request it should not be instantiated nor terminated
$request = Request::create('/');
$kernel->terminate($request, Response::create('fake response'));
$this->assertNull($app);

// if the kernel handled a request and the underlying instance should be instantiated and terminated
$response = $kernel->handle($request);
$this->assertInstanceOf('Stack\CallableTerminableHttpKernel', $app);
$this->assertFalse($app->isTerminated());
$kernel->terminate($request, $response);
$this->assertTrue($app->isTerminated());
}


public function testShortcutFunction()
{
$kernel = lazy(function () {
return $this->createHelloKernel();
}, true);

$this->assertInstanceOf('Stack\LazyTerminableHttpKernel', $kernel);
$this->assertSame('Hello World!', $kernel->handle(Request::create('/'))->getContent());
}

private function createHelloKernel()
{
return $this->createTerminableKernel('Hello World!');
}

private function createTerminableKernel($body)
{
return new CallableTerminableHttpKernel(function (Request $request) use ($body) {
return new Response($body);
});
}
}

class CallableTerminableHttpKernel extends CallableHttpKernel implements TerminableInterface
{
private $terminated = false;

public function terminate(Request $request, Response $response)
{
$this->terminated = true;
}

public function isTerminated()
{
return $this->terminated;
}
}