-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
b32446b
commit 8589ed4
Showing
6 changed files
with
260 additions
and
5 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
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
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,104 @@ | ||
# Application middleware | ||
|
||
Slim implements a version of the Rack protocol, therefore it can have middleware | ||
that may inspect, or modify the application environment, request, and response | ||
before and/or after the Slim application is invoked. | ||
|
||
You can register all your application middleware in a single place. You only | ||
need to extend the class `ComPHPPuebla\Slim\MiddlewareLayers` and add all your | ||
middleware in the `init` method by calling the method `add`. | ||
|
||
Suppose you have a middleware that logs the requests, matched routes and responses | ||
of your application. You would have to register it the following way. | ||
|
||
```php | ||
namespace Application; | ||
|
||
use Slim\Helper\Set; | ||
use ComPHPPuebla\MiddlewareLayers; | ||
|
||
class ApplicationMiddleware extends MiddlewareLayers | ||
{ | ||
/** | ||
* Add the middleware for your application here | ||
*/ | ||
protected function init(Set $container) | ||
{ | ||
$this | ||
->add(new RequestLoggingMiddleware()) | ||
; | ||
} | ||
} | ||
``` | ||
|
||
## Using the container | ||
|
||
Suppose you want to use dependency injection for your middleware in order to | ||
control what logger your application should use, instead of simply using Slim's | ||
logger. | ||
|
||
You could register a [Monolog][1] logger in a service provider. | ||
|
||
```php | ||
namespace Application; | ||
|
||
use ComPHPPuebla\Slim\Resolver; | ||
use ComPHPPuebla\Slim\ServiceProvider; | ||
use Monolog\Handler\StreamHandler; | ||
use Monolog\Logger; | ||
use Slim\Slim; | ||
|
||
class MonologServiceProvider implements ServiceProvider | ||
{ | ||
public function configure(Slim $app, Resolver $resolver, array $options = []) | ||
{ | ||
$app->container->singleton( | ||
'logger', | ||
function () use ($app, $options) { | ||
$logger = new Logger($options['monolog']['channel']); | ||
$logger->pushHandler(new StreamHandler( | ||
$options['monolog']['path'], Logger::DEBUG | ||
)); | ||
|
||
return $logger; | ||
} | ||
); | ||
} | ||
} | ||
``` | ||
|
||
And then retrieve the object inside the `init` method through the container. | ||
|
||
```php | ||
protected function init(Set $container) | ||
{ | ||
$this | ||
->add(new RequestLoggingMiddleware($container->get('logger')) | ||
; | ||
} | ||
``` | ||
|
||
Alternatively, you could register the middleware class inside a service provider | ||
and simply retrieve it from the container. | ||
|
||
```php | ||
protected function init(Set $container) | ||
{ | ||
$this | ||
->add($container->get('slim.middleware.request_logger') | ||
; | ||
} | ||
``` | ||
|
||
Then your `index.php` file would only need: | ||
|
||
```php | ||
$app = new Slim\Slim(); | ||
|
||
$middleware = new Application\ApplicationMiddleware(); | ||
$middleware->configure($app); | ||
|
||
$app->run(); | ||
``` | ||
|
||
[1]: https://github.com/Seldaek/monolog |
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
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,56 @@ | ||
<?php | ||
/** | ||
* PHP version 5.5 | ||
* | ||
* This source file is subject to the license that is bundled with this package in the file LICENSE. | ||
* | ||
* @copyright Comunidad PHP Puebla 2015 (http://www.comunidadphppuebla.com) | ||
*/ | ||
namespace ComPHPPuebla\Slim; | ||
|
||
use Slim\Helper\Set; | ||
use Slim\Middleware; | ||
use Slim\Slim; | ||
|
||
class MiddlewareLayers | ||
{ | ||
/** @var array */ | ||
private $middleware = []; | ||
|
||
/** | ||
* Override this in subclasses to add your Slim middleware classes | ||
* | ||
* This method will be called at the beginning of ComPHPPuebla\Slim\MiddlewareLayers::configure | ||
* | ||
* @param Set $container | ||
*/ | ||
protected function init(Set $container) | ||
{ | ||
} | ||
|
||
/** | ||
* @param Middleware $middleware | ||
* @return MiddlewareLayers | ||
*/ | ||
public function add(Middleware $middleware) | ||
{ | ||
$this->middleware[] = $middleware; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Configure the middleware layers for your application | ||
* | ||
* @param Slim $app | ||
*/ | ||
public function configure(Slim $app) | ||
{ | ||
$this->init($app->container); | ||
|
||
/** @var MiddlewareProvider $middleware */ | ||
foreach ($this->middleware as $middleware) { | ||
$app->add($middleware); | ||
} | ||
} | ||
} |
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,91 @@ | ||
<?php | ||
/** | ||
* PHP version 5.5 | ||
* | ||
* This source file is subject to the license that is bundled with this package in the file LICENSE. | ||
* | ||
* @copyright Comunidad PHP Puebla 2015 (http://www.comunidadphppuebla.com) | ||
*/ | ||
namespace ComPHPPuebla\Slim; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Slim\Helper\Set; | ||
use Slim\Middleware; | ||
use Slim\Slim; | ||
use stdClass; | ||
|
||
class FakeMiddleware extends Middleware | ||
{ | ||
private $logger; | ||
|
||
public function __construct($logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public function call() | ||
{ | ||
} | ||
} | ||
|
||
class FakeMiddlewareLayers extends MiddlewareLayers | ||
{ | ||
public function init(Set $container) | ||
{ | ||
$this->add(new FakeMiddleware($container->get('logger'))); | ||
} | ||
} | ||
|
||
class MiddlewareLayersTest extends TestCase | ||
{ | ||
/** @test */ | ||
function it_should_register_middleware_to_the_given_slim_application() | ||
{ | ||
$middleware1 = $this->getMock(Middleware::class); | ||
$middleware2 = $this->getMock(Middleware::class); | ||
|
||
$builder = $this->getMockBuilder(Slim::class); | ||
$app = $builder->setMethods(['add'])->getMock(); | ||
|
||
$app | ||
->expects($spy = $this->exactly(2)) | ||
->method('add') | ||
->with($this->isInstanceOf(Middleware::class)) | ||
; | ||
|
||
$middleware = new MiddlewareLayers(); | ||
$middleware | ||
->add($middleware1) | ||
->add($middleware2) | ||
; | ||
|
||
$middleware->configure($app); | ||
|
||
$this->assertEquals( | ||
2, $spy->getInvocationCount(), 'It should have registered 2 middleware layers' | ||
); | ||
} | ||
|
||
/** @test */ | ||
function it_should_be_able_to_use_slim_container_to_build_middleware() | ||
{ | ||
$app = new Slim(); | ||
$container = $this->getMock(Set::class); | ||
$container | ||
->expects($spy = $this->once()) | ||
->method('get') | ||
->with('logger') | ||
->willReturn($this->returnValue(new stdClass())) | ||
; | ||
|
||
$app->container = $container; | ||
|
||
$middleware = new FakeMiddlewareLayers(); | ||
|
||
$middleware->configure($app); | ||
|
||
$this->assertEquals( | ||
1, $spy->getInvocationCount(), 'It should have retrieved the logger from the container' | ||
); | ||
} | ||
} |