├── .gitignore ├── README.md ├── composer.json ├── logo.png └── src └── SymfonyMiddleware.php /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | vendor/* 3 | .*~ 4 | .buildpath 5 | .project 6 | .settings 7 | /vendor/ 8 | composer.lock 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Symfony middleware for StackPHP 2 | =============================== 3 | 4 | This package contains a [StackPHP middleware](http://stackphp.com/) that enables you to push a Symfony 5 | application (actually a `Kernel`) directly on the middleware stack. 6 | The Symfony application will try to handle requests but instead of sending a 404 response if no route is found, 7 | the next middleware on the stack will be called. 8 | 9 | Installation 10 | ------------ 11 | 12 | Through [Composer](https://getcomposer.org/) as [mouf/symfony-middleware](https://packagist.org/packages/mouf/symfony-middleware). 13 | 14 | Usage 15 | ----- 16 | 17 | Simply use the `SymfonyMiddleWare` class in your middleware stack: 18 | 19 | ```php 20 | use Mouf\StackPhp\SymfonyMiddleware; 21 | use My\Symfony\Application; 22 | use Stack\Builder; 23 | 24 | $app = ... 25 | 26 | $symfonyApplication = new Application(...); 27 | 28 | $stack = (new Stack\Builder()) 29 | ->push(SymfonyMiddleware::class, $symfonyApplication); 30 | 31 | $app = $stack->resolve($app); 32 | ``` 33 | 34 | Why? 35 | ---- 36 | 37 | Why would I want to make a Symfony app a middleware? 38 | Because if every app becomes a middleware, we can easily chain middlewares together, and therefore, chain many 39 | frameworks in the same application... and this is cool :) 40 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "mouf/symfony-middleware", 3 | "description" : "This package provides a StackPHP middleware that can be used to use a Symfony application as a middleware (instead of an app)", 4 | "type" : "library", 5 | "authors" : [{ 6 | "name" : "David Négrier", 7 | "email" : "d.negrier@thecodingmachine.com", 8 | "homepage" : "http://mouf-php.com" 9 | } 10 | ], 11 | "keywords" : [ 12 | "stackphp", 13 | "stack", 14 | "silex" 15 | ], 16 | "homepage" : "https://github.com/thecodingmachine/symfony-middleware", 17 | "license" : "MIT", 18 | "require" : { 19 | "php" : ">=5.4.0", 20 | "symfony/http-kernel" : "~2.0 | ~3.0" 21 | }, 22 | "autoload" : { 23 | "psr-4" : { 24 | "Mouf\\StackPhp\\" : "src/" 25 | } 26 | }, 27 | "extra" : { 28 | "mouf" : { 29 | "logo" : "logo.png" 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thecodingmachine/symfony-middleware/4e6c46577534521b700ccb14005f799a8e22f950/logo.png -------------------------------------------------------------------------------- /src/SymfonyMiddleware.php: -------------------------------------------------------------------------------- 1 | 21 | */ 22 | class SymfonyMiddleware implements HttpKernelInterface, TerminableInterface 23 | { 24 | private $app; 25 | private $symfonyApp; 26 | private $initDone = false; 27 | 28 | /** 29 | * 30 | * @param HttpKernelInterface $app The next application the request will be forwarded to if not handled by Symfony 31 | * @param HttpKernel $symfonyApp The Symfony application that will try catching requests 32 | */ 33 | public function __construct(HttpKernelInterface $app, KernelInterface $symfonyApp) 34 | { 35 | $this->app = $app; 36 | $this->symfonyApp = $symfonyApp; 37 | } 38 | 39 | /** 40 | * (non-PHPdoc) 41 | * @see \Symfony\Component\HttpKernel\HttpKernelInterface::handle() 42 | */ 43 | public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) 44 | { 45 | if (!$this->initDone) { 46 | $this->symfonyApp->boot(); 47 | $dispatcher = $this->symfonyApp->getContainer()->get('event_dispatcher'); 48 | $dispatcher->addListener('kernel.exception', function(Event $event) use ($request, $type, $catch) { 49 | if ($event->getException() instanceof NotFoundHttpException) { 50 | $response = $this->app->handle($request, $type, $catch); 51 | // Let's force the return code of the response into HttpKernel: 52 | $response->headers->set('X-Status-Code', $response->getStatusCode()); 53 | $event->setResponse($response); 54 | } 55 | }); 56 | 57 | $this->initDone = true; 58 | } 59 | 60 | return $this->symfonyApp->handle($request, $type, $catch); 61 | } 62 | 63 | /** 64 | * Terminates a request/response cycle. 65 | * 66 | * Should be called after sending the response and before shutting down the kernel. 67 | * 68 | * @param Request $request A Request instance 69 | * @param \Symfony\Component\HttpFoundation\Response $response A Response instance 70 | * 71 | * @api 72 | */ 73 | public function terminate(Request $request, Response $response) 74 | { 75 | $this->symfonyApp->terminate($request, $response); 76 | $this->symfonyApp->shutdown(); 77 | $this->initDone = false; 78 | } 79 | } 80 | --------------------------------------------------------------------------------