1 | <?php |
||
17 | class Rack implements MiddlewareInterface, RequestHandlerInterface |
||
18 | { |
||
19 | /** @var \SplQueue */ |
||
20 | protected $workQueue; |
||
21 | |||
22 | /** |
||
23 | * Rack constructor. |
||
24 | */ |
||
25 | 15 | public function __construct() |
|
29 | |||
30 | /** |
||
31 | * Add ServerMiddleware to the queue |
||
32 | * |
||
33 | * @param MiddlewareInterface $middleware |
||
34 | * |
||
35 | * @return self |
||
36 | * |
||
37 | * @throws InvalidHandlerException |
||
38 | */ |
||
39 | 10 | public function add(MiddlewareInterface $middleware): Rack |
|
47 | |||
48 | /** |
||
49 | * Handle the request and return a response |
||
50 | * |
||
51 | * @param ServerRequestInterface $request an Http Request |
||
52 | * |
||
53 | * @return ResponseInterface |
||
54 | * |
||
55 | * @throws InvalidHandlerException |
||
56 | * @throws RackExhaustedException |
||
57 | */ |
||
58 | 13 | public function handle(ServerRequestInterface $request): ResponseInterface |
|
70 | |||
71 | /** |
||
72 | * Process an incoming server request and return a response, optionally delegating |
||
73 | * response creation to a handler. |
||
74 | * |
||
75 | * Here, we will use our own workQueue to try to handle the request, allowing stacking of |
||
76 | * Rack instances in a middleware queue (i.e a Rack of Racks) |
||
77 | * |
||
78 | * @param ServerRequestInterface $request an Http Request |
||
79 | * @param RequestHandlerInterface $handler |
||
80 | * |
||
81 | * @return ResponseInterface |
||
82 | * |
||
83 | * @throws InvalidHandlerException |
||
84 | */ |
||
85 | 4 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
94 | |||
95 | /** |
||
96 | * Process a request using the middleware that has been add()ed to the Rack |
||
97 | * |
||
98 | * @param ServerRequestInterface $request |
||
99 | * |
||
100 | * @return ResponseInterface |
||
101 | * |
||
102 | * @throws InvalidHandlerException |
||
103 | * @throws RackExhaustedException |
||
104 | */ |
||
105 | 11 | public function run(ServerRequestInterface $request): ResponseInterface |
|
109 | |||
110 | /** |
||
111 | * __invoke() is an alias for run() |
||
112 | * |
||
113 | * @param ServerRequestInterface $request |
||
114 | * |
||
115 | * @return ResponseInterface |
||
116 | * |
||
117 | * @throws InvalidHandlerException |
||
118 | * @throws RackExhaustedException |
||
119 | */ |
||
120 | 3 | public function __invoke(ServerRequestInterface $request): ResponseInterface |
|
124 | } |
||
125 |