From 01b6eacc738cd894b49095accf44725aae9b2cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?t=CE=BBeUniC?= Date: Thu, 9 Jul 2015 19:16:28 +0200 Subject: [PATCH 1/3] Implemented TerminableInterface In order to allow internal Symfony apps to properly terminate --- src/SymfonyMiddleware.php | 92 +++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 38 deletions(-) diff --git a/src/SymfonyMiddleware.php b/src/SymfonyMiddleware.php index 92fef4e..2bcb9ce 100644 --- a/src/SymfonyMiddleware.php +++ b/src/SymfonyMiddleware.php @@ -3,14 +3,17 @@ 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\HttpFoundation\Response; use Symfony\Component\HttpKernel\Kernel; 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; +use Symfony\Component\HttpKernel\TerminableInterface; /** * This StackPHP middleware creates a middleware from a Symfony application. @@ -20,46 +23,59 @@ * * @author David NĂ©grier */ -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); + } } From a5cfd8ac51a768e14b170637f63594f9cfe0f592 Mon Sep 17 00:00:00 2001 From: Christian Soronellas Date: Thu, 9 Jul 2015 19:20:13 +0200 Subject: [PATCH 2/3] Cleanup --- src/SymfonyMiddleware.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/SymfonyMiddleware.php b/src/SymfonyMiddleware.php index 2bcb9ce..d7b4e7d 100644 --- a/src/SymfonyMiddleware.php +++ b/src/SymfonyMiddleware.php @@ -7,10 +7,7 @@ use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Kernel; 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; use Symfony\Component\HttpKernel\TerminableInterface; From bfc0c1ff4cf217309706fb0f64a765f5b50badc3 Mon Sep 17 00:00:00 2001 From: Christian Soronellas Date: Thu, 9 Jul 2015 19:23:42 +0200 Subject: [PATCH 3/3] Fixed a typo --- src/SymfonyMiddleware.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/SymfonyMiddleware.php b/src/SymfonyMiddleware.php index d7b4e7d..1c4a7c1 100644 --- a/src/SymfonyMiddleware.php +++ b/src/SymfonyMiddleware.php @@ -10,7 +10,6 @@ use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -use Symfony\Component\HttpKernel\TerminableInterface; /** * This StackPHP middleware creates a middleware from a Symfony application.