Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Jan 7, 2018
2 parents b220d41 + 196156d commit 78aa807
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ and using the callback to call several methods on the redirect middleware to cha

```php
$loop = Factory::create();
$server = new Server(new MiddlewareRunner([
$server = new Server([
/** Other middleware */
new PSR15Middleware(
$loop, // The react/event-loop (required)
Expand All @@ -36,7 +36,33 @@ $server = new Server(new MiddlewareRunner([
}
),
/** Other middleware */
]));
]);
```

# Grouped Usage

When using more then one PSR-15 in a row the `GroupedPSR15Middleware` is more performing than using multiple `PSR15Middleware`. Consider the
following example where we add [`middlewares/cache`](https://github.com/middlewares/cache) for expires headers:

```php
$loop = Factory::create();
$server = new Server([
/** Other middleware */
(new GroupedPSR15Middleware($loop))->withMiddleware(
Redirect::class,
[
['/old-url' => '/new-url']
],
function ($redirectMiddleware) {
return $redirectMiddleware
->permanent(false)
->query(false)
->method(['GET', 'POST'])
;
}
)->withMiddleware(Expires::class),
/** Other middleware */
]);
```

# Warning
Expand Down
7 changes: 4 additions & 3 deletions src/GroupedPSR15Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ public function __construct(LoopInterface $loop)
$this->kernel = ReactKernel::create($loop);
}

public function add(string $middleware, array $arguments = [], callable $func = null)
public function withMiddleware(string $middleware, array $arguments = [], callable $func = null)
{
if ($func === null) {
$func = function ($middleware) {
return $middleware;
};
}

$this->middleware[] = $func(YieldingMiddlewareFactory::construct($middleware, $arguments));
$clone = clone $this;
$clone->middleware[] = $func(YieldingMiddlewareFactory::construct($middleware, $arguments));

return $this;
return $clone;
}

public function __invoke(ServerRequestInterface $request, callable $next): Promise\PromiseInterface
Expand Down

0 comments on commit 78aa807

Please sign in to comment.