From 6e03488795281b6ff5a771b6cda83f637708f94e Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 7 Jan 2018 00:40:24 +0100 Subject: [PATCH 1/4] Made adding middleware immutable --- src/GroupedPSR15Middleware.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/GroupedPSR15Middleware.php b/src/GroupedPSR15Middleware.php index 0210661..aec64b7 100644 --- a/src/GroupedPSR15Middleware.php +++ b/src/GroupedPSR15Middleware.php @@ -27,7 +27,7 @@ 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) { @@ -35,9 +35,10 @@ public function add(string $middleware, array $arguments = [], callable $func = }; } - $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 From 5b552b08428c225f3d1e1c6dd64e0fc04ba81939 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 7 Jan 2018 01:29:14 +0100 Subject: [PATCH 2/4] Grouped Usage example --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 716284e..560ab62 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,32 @@ $server = new Server(new MiddlewareRunner([ ])); ``` +# 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(new MiddlewareRunner([ + /** 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 This adapter rewrite the code of the PSR-15 middleware during the constructor phase, wrapping all `$delegate->process($request)` From 00efeb85719c2f30dbb735b63eff34fbe0470d46 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 7 Jan 2018 01:31:33 +0100 Subject: [PATCH 3/4] Removed MiddlewareRunner from examples --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 560ab62..1fab0b2 100644 --- a/README.md +++ b/README.md @@ -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) @@ -36,7 +36,7 @@ $server = new Server(new MiddlewareRunner([ } ), /** Other middleware */ -])); +]); ``` # Grouped Usage @@ -46,7 +46,7 @@ following example where we add [`middlewares/cache`](https://github.com/middlewa ```php $loop = Factory::create(); -$server = new Server(new MiddlewareRunner([ +$server = new Server([ /** Other middleware */ (new GroupedPSR15Middleware($loop)->withMiddleware( Redirect::class, @@ -62,7 +62,7 @@ $server = new Server(new MiddlewareRunner([ } )->withMiddleware(Expires::class), /** Other middleware */ -])); +]); ``` # Warning From 196156dd2c4b1f8692a3ad792f6c5c26f891e694 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 7 Jan 2018 01:36:34 +0100 Subject: [PATCH 4/4] Missing parentheses for Grouped example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1fab0b2..d5a4038 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ following example where we add [`middlewares/cache`](https://github.com/middlewa $loop = Factory::create(); $server = new Server([ /** Other middleware */ - (new GroupedPSR15Middleware($loop)->withMiddleware( + (new GroupedPSR15Middleware($loop))->withMiddleware( Redirect::class, [ ['/old-url' => '/new-url']