This class implements a pipe-line of middleware, which can be attached using the pipe() method, and is itself middleware. The request and response objects are decorated using the Phly\Conduit\Http variants in this package, ensuring that the request may store arbitrary properties, and the response exposes the convenience write(), end(), and isComplete() methods. It creates an instance of Next internally, invoking it with the provided request and response instances; if no $out argument is provided, it will create a FinalHandler instance and pass that to Next as well. Inspired by Sencha Connect.
See also: https://github.com/sencha/connect
Inheritance: implements Phly\Conduit\MiddlewareInterface
<?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;
});
Exemple #2
0
 /**
  * @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));
 }