|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class QueueRequestHandler |
| 4 | + * |
| 5 | + * @link https://github.com/libreworks/caridea-dispatch |
| 6 | + * |
| 7 | + * @filesource QueueRequestHandler.php |
| 8 | + * @created 08.03.2019 |
| 9 | + * @package chillerlan\HTTP\Psr15 |
| 10 | + * @author smiley <smiley@chillerlan.net> |
| 11 | + * @copyright 2019 smiley |
| 12 | + * @license MIT |
| 13 | + */ |
| 14 | + |
| 15 | +namespace chillerlan\HTTP\Psr15; |
| 16 | + |
| 17 | +use chillerlan\HTTP\Psr15\Middleware\MiddlewareException; |
| 18 | +use chillerlan\HTTP\Psr17\ResponseFactory; |
| 19 | +use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; |
| 20 | +use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface}; |
| 21 | + |
| 22 | +class QueueRequestHandler implements MiddlewareInterface, RequestHandlerInterface{ |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \Psr\Http\Server\MiddlewareInterface[] |
| 26 | + */ |
| 27 | + protected $middlewareStack = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var \Psr\Http\Server\RequestHandlerInterface |
| 31 | + */ |
| 32 | + protected $fallbackHandler; |
| 33 | + |
| 34 | + /** |
| 35 | + * QueueRequestHandler constructor. |
| 36 | + * |
| 37 | + * @param iterable|null $middlewareStack |
| 38 | + * @param \Psr\Http\Server\RequestHandlerInterface|null $fallbackHandler |
| 39 | + */ |
| 40 | + public function __construct(iterable $middlewareStack = null, RequestHandlerInterface $fallbackHandler = null){ |
| 41 | + $this->fallbackHandler = $fallbackHandler ?? new EmptyResponseHandler(new ResponseFactory, 500); |
| 42 | + |
| 43 | + $this->addStack($middlewareStack ?? []); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @param iterable|\Psr\Http\Server\MiddlewareInterface[] $middlewareStack |
| 48 | + * |
| 49 | + * @return \chillerlan\HTTP\Psr15\QueueRequestHandler |
| 50 | + * @throws \Exception |
| 51 | + */ |
| 52 | + public function addStack(iterable $middlewareStack):QueueRequestHandler{ |
| 53 | + |
| 54 | + foreach($middlewareStack as $middleware){ |
| 55 | + |
| 56 | + if(!$middleware instanceof MiddlewareInterface){ |
| 57 | + throw new MiddlewareException('invalid middleware'); |
| 58 | + } |
| 59 | + |
| 60 | + $this->middlewareStack[] = $middleware; |
| 61 | + } |
| 62 | + |
| 63 | + return $this; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param \Psr\Http\Server\MiddlewareInterface $middleware |
| 68 | + * |
| 69 | + * @return \chillerlan\HTTP\Psr15\QueueRequestHandler |
| 70 | + */ |
| 71 | + public function add(MiddlewareInterface $middleware):QueueRequestHandler{ |
| 72 | + $this->middlewareStack[] = $middleware; |
| 73 | + |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @inheritDoc |
| 79 | + */ |
| 80 | + public function handle(ServerRequestInterface $request):ResponseInterface{ |
| 81 | + return (new QueueRunner($this->middlewareStack, $this->fallbackHandler))->handle($request); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @inheritDoc |
| 86 | + */ |
| 87 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler):ResponseInterface{ |
| 88 | + return (new QueueRunner($this->middlewareStack, $handler))->handle($request); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments