Skip to content

Commit

Permalink
added support for container-interop
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jan 9, 2017
1 parent 3547563 commit 454daf5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 0.2.0 - 2017-01-09

### Added

* Support for [container-interop](https://github.com/container-interop/container-interop), to create middleware components on demand

First version

## 0.1.0 - 2017-01-09

First version
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@

Simple (but powerful) PSR-15 middleware dispatcher:

Example:
## Requirements

* PHP 7
* A [PSR-7 Message implementation](http://www.php-fig.org/psr/psr-7/), for example [zend-diactoros](https://github.com/zendframework/zend-diactoros)
* Optionally, a [container-interop](https://github.com/container-interop/container-interop) implementation to create the middleware components on demand.

## Example

```php
use Middleland\Dispatcher;

$dispatcher = new Dispatcher([
$middleware = [
new Middleware1(),
new Middleware2(),
new Middleware3(),
Expand All @@ -29,6 +35,9 @@ $dispatcher = new Dispatcher([
return $response->withHeader('X-Foo', 'Bar');
},

//Or use a string to create the middleware on demand using container-interop
'middleware6'

//USE AN ARRAY TO ADD CONDITIONS:

//This middleware is processed only in paths starting by "/admin"
Expand All @@ -42,7 +51,9 @@ $dispatcher = new Dispatcher([

//And use several for each middleware component
[ENV === 'DEV', new RequestIsHttps(), new MiddlewareHttps()],
]);
];

$dispatcher = new Dispatcher($middleware, new ContainerInterop());

$response = $dispatcher->dispatch(new Request());
```
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"require": {
"php": "^7.0",
"http-interop/http-middleware": "^0.4"
"http-interop/http-middleware": "^0.4",
"container-interop/container-interop": "^1.1"
},
"require-dev": {
"zendframework/zend-diactoros": "^1.3",
Expand Down
21 changes: 18 additions & 3 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psr\Http\Message\ResponseInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Container\ContainerInterface;
use InvalidArgumentException;
use LogicException;
use Closure;
Expand All @@ -17,16 +18,22 @@ class Dispatcher implements MiddlewareInterface
*/
private $middleware;

/**
* @var ContainerInterface|null
*/
private $container;

/**
* @param MiddlewareInterface[] $middleware
*/
public function __construct(array $middleware)
public function __construct(array $middleware, ContainerInterface $container = null)
{
if (empty($middleware)) {
throw new LogicException('Empty middleware queue');
}

$this->middleware = $middleware;
$this->container = $container;
}

/**
Expand Down Expand Up @@ -75,12 +82,20 @@ private function get(ServerRequestInterface $request)
}
}

if (is_string($frame)) {
if ($this->container === null) {
throw new InvalidArgumentException(sprintf('No valid middleware provided (%s)', $frame));
}

$frame = $this->container->get($frame);
}

if ($frame === false) {
return $frame;
}

if ($frame instanceof Closure) {
return $this->createMiddleware($frame);
return $this->createMiddlewareFromClosure($frame);
}

if ($frame instanceof MiddlewareInterface) {
Expand Down Expand Up @@ -164,7 +179,7 @@ public function process(ServerRequestInterface $request)
*
* @return MiddlewareInterface
*/
private function createMiddleware(Closure $handler): MiddlewareInterface
private function createMiddlewareFromClosure(Closure $handler): MiddlewareInterface
{
return new class($handler) implements MiddlewareInterface {
private $handler;
Expand Down
16 changes: 16 additions & 0 deletions tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,20 @@ public function testMatchers()
$this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response);
$this->assertEquals('431', (string) $response->getBody());
}

public function testContainer()
{
$dispatcher = new Dispatcher([
'1',
'2',
[false, '3'],
[true, '4'],
new FakeEndPointMiddleware(),
], new FakeContainer());

$response = $dispatcher->dispatch(new ServerRequest());

$this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response);
$this->assertEquals('421', (string) $response->getBody());
}
}
18 changes: 18 additions & 0 deletions tests/FakeContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Middleland\Tests;

use Interop\Container\ContainerInterface;

class FakeContainer implements ContainerInterface
{
public function has($id)
{
return true;
}

public function get($id)
{
return new FakeMiddleware($id);
}
}

0 comments on commit 454daf5

Please sign in to comment.