Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Jan 28, 2015
0 parents commit 3ef9d0e
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*~
vendor/*
.*~
.buildpath
.project
.settings
/vendor/
composer.lock
34 changes: 34 additions & 0 deletions README.md
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);
```
32 changes: 32 additions & 0 deletions composer.json
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"
}
}
}
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions src/SymfonyMiddleware.php
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);
}
}

0 comments on commit 3ef9d0e

Please sign in to comment.