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