Skip to content

Implemented TerminableInterface #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 10, 2015
Merged
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
94 changes: 53 additions & 41 deletions src/SymfonyMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace Mouf\StackPhp;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Expand All @@ -20,46 +19,59 @@
*
* @author David Négrier <[email protected]>
*/
class SymfonyMiddleware implements HttpKernelInterface
class SymfonyMiddleware implements HttpKernelInterface, TerminableInterface
{
private $app;
private $symfonyApp;
private $initDone = false;
private $app;
private $symfonyApp;
private $initDone = false;

/**
*
* @param HttpKernelInterface $app The next application the request will be forwarded to if not handled by Symfony
* @param HttpKernel $symfonyApp The Symfony application that will try catching requests
*/
public function __construct(HttpKernelInterface $app, KernelInterface $symfonyApp) {
$this->app = $app;
$this->symfonyApp = $symfonyApp;
}
/**
*
* @param HttpKernelInterface $app The next application the request will be forwarded to if not handled by Symfony
* @param HttpKernel $symfonyApp The Symfony application that will try catching requests
*/
public function __construct(HttpKernelInterface $app, KernelInterface $symfonyApp)
{
$this->app = $app;
$this->symfonyApp = $symfonyApp;
}

/* (non-PHPdoc)
* @see \Symfony\Component\HttpKernel\HttpKernelInterface::handle()
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) {
if (!$this->initDone) {
$this->symfonyApp->boot();
$dispatcher = $this->symfonyApp->getContainer()->get('event_dispatcher');
/* @var $dispatcher EventDispatcherInterface */
$dispatcher->addListener('kernel.exception', function (Event $event) use ($request, $type, $catch) {
/* @var $event GetResponseForExceptionEvent */

if ($event->getException() instanceof NotFoundHttpException) {
$response = $this->app->handle($request, $type, $catch);

// Let's force the return code of the response into HttpKernel:
$response->headers->set('X-Status-Code', $response->getStatusCode());

$event->setResponse($response);
}
});

$this->initDone = true;
}
/**
* (non-PHPdoc)
* @see \Symfony\Component\HttpKernel\HttpKernelInterface::handle()
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
if (!$this->initDone) {
$this->symfonyApp->boot();
$dispatcher = $this->symfonyApp->getContainer()->get('event_dispatcher');
$dispatcher->addListener('kernel.exception', function(Event $event) use ($request, $type, $catch) {
if ($event->getException() instanceof NotFoundHttpException) {
$response = $this->app->handle($request, $type, $catch);
// Let's force the return code of the response into HttpKernel:
$response->headers->set('X-Status-Code', $response->getStatusCode());
$event->setResponse($response);
}
});

return $this->symfonyApp->handle($request, $type, $catch);
}
$this->initDone = true;
}

return $this->symfonyApp->handle($request, $type, $catch);
}

/**
* Terminates a request/response cycle.
*
* Should be called after sending the response and before shutting down the kernel.
*
* @param Request $request A Request instance
* @param \Symfony\Component\HttpFoundation\Response $response A Response instance
*
* @api
*/
public function terminate(Request $request, Response $response)
{
$this->symfonyApp->terminate($request, $response);
}
}