/**
  * @param \Spekkoek\MiddlewareStack $middleware The middleware stack to set in your App Class
  * @return \Spekkoek\MiddlewareStack
  */
 public function middleware($middleware)
 {
     $middleware->push(function ($req, $res, $next) {
         return 'Not a response';
     });
     return $middleware;
 }
Ejemplo n.º 2
0
 /**
  * @param \Psr\Http\Message\ServerRequestInterface $request  The server request
  * @param \Psr\Http\Message\ResponseInterface $response The response object
  * @return \Psr\Http\Message\ResponseInterface An updated response
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     $next = $this->middleware->get($this->index);
     if ($next) {
         $this->index++;
         return $next($request, $response, $this);
     }
     // End of the stack
     return $response;
 }
 /**
  * @param \Spekkoek\MiddlewareStack $middleware The middleware stack to set in your App Class
  * @return \Spekkoek\MiddlewareStack
  */
 public function middleware($middleware)
 {
     $middleware->push(function ($req, $res, $next) {
         $res = $res->withHeader('X-First', 'first');
         return $next($req, $res);
     })->push(function ($req, $res, $next) {
         $res = $res->withHeader('X-Second', 'second');
         return $next($req, $res);
     })->push(function ($req, $res, $next) {
         if ($req->hasHeader('X-pass')) {
             $res = $res->withHeader('X-pass', $req->getHeaderLine('X-pass'));
         }
         $res = $res->withHeader('X-Second', 'second');
         return $next($req, $res);
     });
     return $middleware;
 }
 public function testInsertAfterInvalid()
 {
     $one = new SampleMiddleware();
     $two = function () {
     };
     $three = function () {
     };
     $stack = new MiddlewareStack();
     $stack->push($one)->push($two)->insertAfter('InvalidClass', $three);
     $this->assertCount(3, $stack);
     $this->assertSame($one, $stack->get(0));
     $this->assertSame($two, $stack->get(1));
     $this->assertSame($three, $stack->get(2));
 }