Each middleware can be associated with a particular path; if that
path is matched when that middleware is invoked, it will be processed;
otherwise it is skipped.
No path means it should be executed every request cycle.
A handler CAN implement MiddlewareInterface, but MUST be callable.
Handlers with arity >= 4 or those implementing ErrorMiddlewareInterface
are considered error handlers, and will be executed when a handler calls
$next with an error or raises an exception.
<?php use Phly\Conduit\MiddlewarePipe; use Application\NotFound; return call_user_func(function () { $services = (include 'config/services.php'); $app = new MiddlewarePipe(); // basics eg.redirect // site 1 $app->pipe($services->get('Sample\\Module')); // errors $app->pipe(new NotFound()); // authentication // error handler $app->pipe($services->get('ErrorHandler')); return $app; });
/** * @group matching * @group nesting * @dataProvider nestedPaths */ public function testNestedMiddlewareMatchesOnlyAtPathBoundaries($topPath, $nestedPath, $fullPath, $assertion) { $middleware = $this->middleware; $nest = new MiddlewarePipe(); $nest->pipe($nestedPath, function ($req, $res) use($nestedPath) { return $res->withHeader('X-Found', 'true'); }); $middleware->pipe($topPath, function ($req, $res, $next = null) use($topPath, $nest) { $result = $nest($req, $res, $next); return $result; }); $uri = (new Uri())->withPath($fullPath); $request = (new Request())->withUri($uri); $response = $middleware($request, $this->response); $this->{$assertion}($response->hasHeader('X-Found'), sprintf("%s failed with full path %s against top pipe '%s' and nested pipe '%s'\n", $assertion, $fullPath, $topPath, $nestedPath)); }