|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Import classes |
|
16
|
|
|
*/ |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
19
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
20
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
21
|
|
|
use Sunrise\Http\Message\ResponseFactory; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* RequestHandler |
|
25
|
|
|
* |
|
26
|
|
|
* @link https://www.php-fig.org/psr/psr-15/ |
|
27
|
|
|
* @link https://www.php-fig.org/psr/psr-15/meta/ |
|
28
|
|
|
*/ |
|
29
|
|
|
class RequestHandler implements RequestHandlerInterface |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The request handler middleware queue |
|
34
|
|
|
* |
|
35
|
|
|
* @var \SplQueue |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $middlewareQueue; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Constructor of the class |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->middlewareQueue = new \SplQueue(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Adds the given middleware to the request handler middleware queue |
|
49
|
|
|
* |
|
50
|
|
|
* @param MiddlewareInterface $middleware |
|
51
|
|
|
* |
|
52
|
|
|
* @return RequestHandlerInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
public function add(MiddlewareInterface $middleware) : RequestHandlerInterface |
|
55
|
|
|
{ |
|
56
|
|
|
$this->middlewareQueue->enqueue($middleware); |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritDoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function handle(ServerRequestInterface $request) : ResponseInterface |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->middlewareQueue->isEmpty()) |
|
67
|
|
|
{ |
|
68
|
|
|
return (new ResponseFactory)->createResponse(200); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$middleware = $this->middlewareQueue->dequeue(); |
|
72
|
|
|
|
|
73
|
|
|
return $middleware->process($request, $this); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|