public function testPrepending() { $queue = new Queue(); $queue->append(function ($req, $res, $next) use($queue) { $queue->prepend(function ($req, $res, $next) { $res->getBody()->write('Second!'); return $next($req, $res); }); $res->getBody()->write('First!'); return $next($req, $res); }); $queue->append(function ($req, $res, $next) use($queue) { $queue->append(function ($req, $res, $next) { $res->getBody()->write('Sixth!'); return $next($req, $res); }); $res->getBody()->write('Third!'); return $next($req, $res); }); $queue->append(function ($req, $res, $next) use($queue) { $queue->prepend(function ($req, $res, $next) { $res->getBody()->write('Fifth!'); return $next($req, $res); }); $res->getBody()->write('Fourth!'); return $next($req, $res); }); $this->assertEquals('First!Second!Third!Fourth!Fifth!Sixth!', (string) Runtime::run($queue)->getBody(), 'first time'); $this->assertEquals('First!Second!Third!Fourth!Fifth!Sixth!', (string) Runtime::run($queue)->getBody(), 'second time'); $this->assertEquals('First!Second!Third!Fourth!Fifth!Sixth!', (string) Runtime::run($queue)->getBody(), 'third time'); }
/** * @param callable $middleware * * @return $this */ public function prepend($middleware) { $this->queue->prepend($this->prepareMiddleware($middleware)); return $this; }
public static function run(Queue $queue, ServerRequestInterface $request = null, ResponseInterface $response = null) { return $queue->run($request ?: Http::getServerRequest(), $response ?: new Response()); }