-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from theUniC/patch-1
Implemented TerminableInterface
- Loading branch information
Showing
1 changed file
with
53 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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); | ||
} | ||
} |