Skip to content

Commit

Permalink
Merge pull request #1 from theUniC/patch-1
Browse files Browse the repository at this point in the history
Implemented TerminableInterface
  • Loading branch information
moufmouf committed Jul 10, 2015
2 parents cea7a71 + bfc0c1f commit 702fc0e
Showing 1 changed file with 53 additions and 41 deletions.
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);
}
}

0 comments on commit 702fc0e

Please sign in to comment.