diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..344ab03 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*~ +vendor/* +.*~ +.buildpath +.project +.settings +/vendor/ +composer.lock diff --git a/README.md b/README.md new file mode 100644 index 0000000..66f32cd --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +Silex middleware for StackPHP +============================= + +This package contains a [StackPHP middleware](http://stackphp.com/) that unables you to push a Silex +application directly on the middleware stack. The Silex application will try to handle requests but +instead of sending a 404 response if nothing is found, the next middleware on the stack will be called. + +Installation +------------ + +Through [Composer](https://getcomposer.org/) as [mouf/silex-middleware](https://packagist.org/packages/mouf/silex-middleware). + +Usage +----- + +Simply use the `SilexMiddleWare` class in your middleware stack: + +```php +use Mouf\StackPhp\SilexMiddleware; +use Silex\Application; +use Stack\Builder; + +$app = ... + +$silex = new Silex\Application(); +$silex->get('/hello', function(Request $request) { + return 'Hello World!'; +}); + +$stack = (new Stack\Builder()) + ->push(SilexMiddleWare::class, $silex); + +$app = $stack->resolve($app); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c947a1a --- /dev/null +++ b/composer.json @@ -0,0 +1,32 @@ +{ + "name" : "mouf/symfony-middleware", + "description" : "This package provides a StackPHP middleware that can be used to use a Symfony application as a middleware (instead of an app)", + "type" : "library", + "authors" : [{ + "name" : "David NĂ©grier", + "email" : "d.negrier@thecodingmachine.com", + "homepage" : "http://mouf-php.com" + } + ], + "keywords" : [ + "stackphp", + "stack", + "silex" + ], + "homepage" : "https://github.com/thecodingmachine/symfony-middleware", + "license" : "MIT", + "require" : { + "php" : ">=5.4.0", + "symfony/http-kernel" : "~2.0" + }, + "autoload" : { + "psr-4" : { + "Mouf\\StackPhp\\" : "src/" + } + }, + "extra" : { + "mouf" : { + "logo" : "logo.png" + } + } +} diff --git a/logo.png b/logo.png new file mode 100644 index 0000000..2ba2f9a Binary files /dev/null and b/logo.png differ diff --git a/src/SymfonyMiddleware.php b/src/SymfonyMiddleware.php new file mode 100644 index 0000000..6052fca --- /dev/null +++ b/src/SymfonyMiddleware.php @@ -0,0 +1,51 @@ + + */ +class SymfonyMiddleware implements HttpKernelInterface +{ + private $request; + private $type; + private $catch; + private $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, HttpKernel $symfonyApp) { + $this->symfonyApp = $symfonyApp; + // TODO + $this->symfonyApp->error(function(\Exception $e, $code) use ($app) { + if ($code == 404) { + return $app->handle($this->request, $this->type, $this->catch); + } else { + return; + } + }); + } + + /* (non-PHPdoc) + * @see \Symfony\Component\HttpKernel\HttpKernelInterface::handle() + */ + public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { + $this->request = $request; + $this->type = $type; + $this->catch = $catch; + + return $this->symfonyApp->handle($request, $type, $catch); + } +}