1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Philae; |
4
|
|
|
|
5
|
|
|
use FastRoute\RouteCollector; |
6
|
|
|
use Interop\Http\ServerMiddleware\MiddlewareInterface; |
7
|
|
|
use League\Container\Container; |
8
|
|
|
use League\Container\ContainerAwareInterface; |
9
|
|
|
use Philae\Exceptions\InvalidCallableServiceException; |
10
|
|
|
use Philae\Utils\UriResolverInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
13
|
|
|
|
14
|
|
|
class Application extends Container |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var MiddlewareInterface[] |
18
|
|
|
*/ |
19
|
|
|
protected $middlewares; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return MiddlewareInterface[] |
23
|
|
|
*/ |
24
|
21 |
|
public function getMiddlewares(): array |
25
|
|
|
{ |
26
|
21 |
|
return $this->middlewares; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param MiddlewareInterface[] $middlewares |
31
|
|
|
* |
32
|
|
|
* @return self|$this |
33
|
|
|
*/ |
34
|
21 |
|
public function setMiddlewares(array $middlewares): Application |
35
|
|
|
{ |
36
|
21 |
|
foreach ($middlewares as $i => $middleware) { |
37
|
21 |
|
if ($middleware instanceof ContainerAwareInterface) { |
38
|
21 |
|
$middleware->setContainer($this); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
21 |
|
$this->middlewares = $middlewares; |
43
|
|
|
|
44
|
21 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Dispatches the request and sends the response |
49
|
|
|
* |
50
|
|
|
* @param ServerRequestInterface|null $request |
51
|
|
|
* @return self|$this |
52
|
|
|
* @throws InvalidCallableServiceException |
53
|
|
|
*/ |
54
|
3 |
|
public function execute(ServerRequestInterface $request = null): Application |
55
|
|
|
{ |
56
|
3 |
|
$response = $this->dispatch($request); |
57
|
3 |
|
$emitter = $this->get(DefaultServiceProvider::SERVICE_RESPONSE_EMITTER); |
58
|
|
|
|
59
|
3 |
|
if (!is_callable($emitter)) { |
60
|
3 |
|
throw new InvalidCallableServiceException(DefaultServiceProvider::SERVICE_RESPONSE_EMITTER); |
61
|
|
|
} |
62
|
|
|
call_user_func($emitter, $response); |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Dispatches the request and returns the response |
69
|
|
|
* |
70
|
|
|
* @param ServerRequestInterface|null $request |
71
|
|
|
* @return ResponseInterface |
72
|
|
|
* @throws InvalidCallableServiceException |
73
|
|
|
*/ |
74
|
21 |
|
public function dispatch(ServerRequestInterface $request = null): ResponseInterface |
75
|
|
|
{ |
76
|
21 |
|
$request = $request ?: $this->getRequest(); |
77
|
21 |
|
$this->share(DefaultServiceProvider::SERVICE_REQUEST, $request); |
78
|
|
|
|
79
|
21 |
|
$dispatcher = $this->get( |
80
|
21 |
|
DefaultServiceProvider::SERVICE_MIDDLEWARE_DISPATCHER, |
81
|
21 |
|
[$this->getMiddlewares()] |
82
|
|
|
); |
83
|
|
|
|
84
|
21 |
|
if (!is_callable($dispatcher)) { |
85
|
3 |
|
throw new InvalidCallableServiceException(DefaultServiceProvider::SERVICE_MIDDLEWARE_DISPATCHER); |
86
|
|
|
} |
87
|
|
|
|
88
|
18 |
|
return call_user_func($dispatcher, $request); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return RouteCollector |
93
|
|
|
*/ |
94
|
18 |
|
public function getRouter(): RouteCollector |
95
|
|
|
{ |
96
|
18 |
|
return $this->get(DefaultServiceProvider::SERVICE_ROUTE_COLLECTOR); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return ServerRequestInterface |
101
|
|
|
*/ |
102
|
|
|
public function getRequest(): ServerRequestInterface |
103
|
|
|
{ |
104
|
|
|
return $this->get(DefaultServiceProvider::SERVICE_REQUEST); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return UriResolverInterface |
109
|
|
|
*/ |
110
|
3 |
|
public function getUriResolver(): UriResolverInterface |
111
|
|
|
{ |
112
|
3 |
|
return $this->get(DefaultServiceProvider::SERVICE_URI_RESOLVER); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return ResponseInterface |
117
|
|
|
*/ |
118
|
3 |
|
public function getResponse(): ResponseInterface |
119
|
|
|
{ |
120
|
3 |
|
return $this->get(DefaultServiceProvider::SERVICE_RESPONSE); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|