├── .gitignore ├── README.md ├── composer.json ├── logo.png └── src └── SilexMiddleware.php /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | vendor/* 3 | .*~ 4 | .buildpath 5 | .project 6 | .settings 7 | /vendor/ 8 | composer.lock 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Silex middleware for StackPHP 2 | ============================= 3 | 4 | This package contains a [StackPHP middleware](http://stackphp.com/) that unables you to push a Silex 5 | application directly on the middleware stack. The Silex application will try to handle requests but 6 | instead of sending a 404 response if nothing is found, the next middleware on the stack will be called. 7 | 8 | Installation 9 | ------------ 10 | 11 | Through [Composer](https://getcomposer.org/) as [mouf/silex-middleware](https://packagist.org/packages/mouf/silex-middleware). 12 | 13 | Usage 14 | ----- 15 | 16 | Simply use the `SilexMiddleWare` class in your middleware stack: 17 | 18 | ```php 19 | use Mouf\StackPhp\SilexMiddleware; 20 | use Silex\Application; 21 | use Stack\Builder; 22 | 23 | $app = ... 24 | 25 | $silex = new Silex\Application(); 26 | $silex->get('/hello', function(Request $request) { 27 | return 'Hello World!'; 28 | }); 29 | 30 | $stack = (new Stack\Builder()) 31 | ->push(SilexMiddleWare::class, $silex); 32 | 33 | $app = $stack->resolve($app); 34 | ``` 35 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "mouf/silex-middleware", 3 | "description" : "This package provides a StackPHP middleware that can be used to plug a Silex application", 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/silex-middleware", 17 | "license" : "MIT", 18 | "require" : { 19 | "php" : ">=5.4.0", 20 | "symfony/http-kernel" : "~2.0", 21 | "silex/silex" : "~1.0" 22 | }, 23 | "autoload" : { 24 | "psr-4" : { 25 | "Mouf\\StackPhp\\" : "src/" 26 | } 27 | }, 28 | "extra" : { 29 | "mouf" : { 30 | "logo" : "logo.png" 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thecodingmachine/silex-middleware/4187035a591661b3e2467deed56a3aa1c493b86e/logo.png -------------------------------------------------------------------------------- /src/SilexMiddleware.php: -------------------------------------------------------------------------------- 1 | 16 | */ 17 | class SilexMiddleware implements HttpKernelInterface 18 | { 19 | private $request; 20 | private $type; 21 | private $catch; 22 | private $silex; 23 | 24 | /** 25 | * 26 | * @param HttpKernelInterface $app The next application the request will be forwarded to if not handled by Silex 27 | * @param Application $silex The Silex application that will try catching requests 28 | */ 29 | public function __construct(HttpKernelInterface $app, Application $silex) { 30 | $this->silex = $silex; 31 | $this->silex->error(function(\Exception $e, $code) use ($app) { 32 | if ($code == 404) { 33 | $response = $app->handle($this->request, $this->type, $this->catch); 34 | // Let's force the return code of the response into HttpKernel: 35 | $response->headers->set('X-Status-Code', $response->getStatusCode()); 36 | return $response; 37 | } else { 38 | if (!$this->catch) { 39 | // If we are not to catch the exception, let's rethrow it. 40 | throw $e; 41 | } 42 | return; 43 | } 44 | }); 45 | } 46 | 47 | /* (non-PHPdoc) 48 | * @see \Symfony\Component\HttpKernel\HttpKernelInterface::handle() 49 | */ 50 | public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { 51 | $this->request = $request; 52 | $this->type = $type; 53 | $this->catch = $catch; 54 | 55 | return $this->silex->handle($request, $type, true); 56 | } 57 | } 58 | --------------------------------------------------------------------------------