Middleware.php 692 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace Qiniu\Http\Middleware;
  3. use Qiniu\Http\Request;
  4. use Qiniu\Http\Response;
  5. interface Middleware
  6. {
  7. /**
  8. * @param Request $request
  9. * @param callable(Request): Response $next
  10. * @return Response
  11. */
  12. public function send($request, $next);
  13. }
  14. /**
  15. * @param array<Middleware> $middlewares
  16. * @param callable(Request): Response $handler
  17. * @return callable(Request): Response
  18. */
  19. function compose($middlewares, $handler)
  20. {
  21. $next = $handler;
  22. foreach (array_reverse($middlewares) as $middleware) {
  23. $next = function ($request) use ($middleware, $next) {
  24. return $middleware->send($request, $next);
  25. };
  26. }
  27. return $next;
  28. }