-
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.
- Loading branch information
0 parents
commit 3ef9d0e
Showing
5 changed files
with
125 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
*~ | ||
vendor/* | ||
.*~ | ||
.buildpath | ||
.project | ||
.settings | ||
/vendor/ | ||
composer.lock |
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 |
---|---|---|
@@ -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); | ||
``` |
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 |
---|---|---|
@@ -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" : "[email protected]", | ||
"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" | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Mouf\StackPhp; | ||
|
||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\HttpKernel; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* This StackPHP middleware creates a middleware from a Symfony application. | ||
* Basically, the middleware will use the Symfony application to catch any request. | ||
* If no request is found, instead of returning a 404 page, control is passed | ||
* to the next middleware. | ||
* | ||
* @author David Négrier <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} |