Skip to content

Commit

Permalink
First working implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Jan 28, 2015
1 parent 3ef9d0e commit cea7a71
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
Silex middleware for StackPHP
=============================
Symfony 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.
This package contains a [StackPHP middleware](http://stackphp.com/) that enables you to push a Symfony
application (actually a `Kernel`) directly on the middleware stack.
The Symfony application will try to handle requests but instead of sending a 404 response if no route 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).
Through [Composer](https://getcomposer.org/) as [mouf/symfony-middleware](https://packagist.org/packages/mouf/symfony-middleware).

Usage
-----

Simply use the `SilexMiddleWare` class in your middleware stack:
Simply use the `SymfonyMiddleWare` class in your middleware stack:

```php
use Mouf\StackPhp\SilexMiddleware;
use Silex\Application;
use Mouf\StackPhp\SymfonyMiddleware;
use My\Symfony\Application;
use Stack\Builder;

$app = ...

$silex = new Silex\Application();
$silex->get('/hello', function(Request $request) {
return 'Hello World!';
});
$symfonyApplication = new Application(...);

$stack = (new Stack\Builder())
->push(SilexMiddleWare::class, $silex);
->push(SymfonyMiddleware::class, $symfonyApplication);

$app = $stack->resolve($app);
```

Why?
----

Why would I want to make a Symfony app a middleware?
Because if every app becomes a middleware, we can easily chain middlewares together, and therefore, chain many
frameworks in the same application... and this is cool :)
44 changes: 29 additions & 15 deletions src/SymfonyMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;
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;

/**
* This StackPHP middleware creates a middleware from a Symfony application.
Expand All @@ -16,35 +22,43 @@
*/
class SymfonyMiddleware implements HttpKernelInterface
{
private $request;
private $type;
private $catch;
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, HttpKernel $symfonyApp) {
public function __construct(HttpKernelInterface $app, KernelInterface $symfonyApp) {
$this->app = $app;
$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;
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;
}

return $this->symfonyApp->handle($request, $type, $catch);
}
Expand Down

0 comments on commit cea7a71

Please sign in to comment.