1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Philae; |
4
|
|
|
|
5
|
|
|
use FastRoute\DataGenerator\GroupCountBased as GroupCountBasedDataGenerator; |
6
|
|
|
use FastRoute\RouteCollector; |
7
|
|
|
use FastRoute\RouteParser\Std as StdRouteParser; |
8
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
9
|
|
|
use Middlewares\Utils\CallableResolver\ReflectionResolver; |
10
|
|
|
use Middlewares\Utils\Dispatcher; |
11
|
|
|
use Philae\Utils\CallableResolver; |
12
|
|
|
use Philae\Utils\UriResolver; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
use Zend\Diactoros\Response; |
16
|
|
|
use Zend\Diactoros\Response\SapiEmitter; |
17
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Service provider that registers the default core dependencies in a container |
21
|
|
|
*/ |
22
|
|
|
class DefaultServiceProvider extends AbstractServiceProvider |
23
|
|
|
{ |
24
|
|
|
const SERVICE_REQUEST = 'request'; |
25
|
|
|
const SERVICE_ORIGINAL_REQUEST = 'original_request'; |
26
|
|
|
const SERVICE_URI_RESOLVER = 'uri_resolver'; |
27
|
|
|
const SERVICE_RESPONSE = 'response'; |
28
|
|
|
const SERVICE_RESPONSE_EMITTER = 'response_emitter'; |
29
|
|
|
const SERVICE_ROUTE_COLLECTOR = 'route_collector'; |
30
|
|
|
const SERVICE_MIDDLEWARE_DISPATCHER = 'middleware_dispatcher'; |
31
|
|
|
const SERVICE_CALLABLE_RESOLVER = 'callable_resolver'; |
32
|
|
|
|
33
|
21 |
|
public function __construct() |
34
|
|
|
{ |
35
|
21 |
|
$this->provides = array_values((new \ReflectionClass(static::class))->getConstants()); |
36
|
21 |
|
} |
37
|
|
|
|
38
|
21 |
|
public function register() |
39
|
|
|
{ |
40
|
|
|
$this |
41
|
21 |
|
->addCallableResolver() |
42
|
21 |
|
->addRouteCollector() |
43
|
21 |
|
->addOriginalRequest() |
44
|
21 |
|
->addRequest() |
45
|
21 |
|
->addResponse() |
46
|
21 |
|
->addResponseEmitter() |
47
|
21 |
|
->addMiddlewareDispatcher() |
48
|
21 |
|
->addUriResolver(); |
49
|
21 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
21 |
|
protected function addCallableResolver() |
55
|
|
|
{ |
56
|
21 |
|
$this->getContainer()->share( |
57
|
21 |
|
self::SERVICE_CALLABLE_RESOLVER, |
58
|
|
|
function () { |
59
|
15 |
|
$callableResolver = new CallableResolver(new ReflectionResolver()); |
60
|
15 |
|
$callableResolver->setContainer($this->getContainer()); |
61
|
|
|
|
62
|
15 |
|
return $callableResolver; |
63
|
21 |
|
} |
64
|
|
|
); |
65
|
|
|
|
66
|
21 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
21 |
|
protected function addRouteCollector() |
73
|
|
|
{ |
74
|
21 |
|
$this->getContainer()->share( |
75
|
21 |
|
self::SERVICE_ROUTE_COLLECTOR, |
76
|
|
|
function () { |
77
|
21 |
|
return new RouteCollector( |
78
|
21 |
|
new StdRouteParser(), |
79
|
21 |
|
new GroupCountBasedDataGenerator() |
80
|
|
|
); |
81
|
21 |
|
} |
82
|
|
|
); |
83
|
|
|
|
84
|
21 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
21 |
|
protected function addOriginalRequest() |
91
|
|
|
{ |
92
|
21 |
|
$this->getContainer()->share( |
93
|
21 |
|
self::SERVICE_ORIGINAL_REQUEST, |
94
|
|
|
function () { |
95
|
|
|
return ServerRequestFactory::fromGlobals(); |
96
|
21 |
|
} |
97
|
|
|
); |
98
|
|
|
|
99
|
21 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
21 |
|
protected function addRequest() |
106
|
|
|
{ |
107
|
21 |
|
$this->getContainer()->share( |
108
|
21 |
|
self::SERVICE_REQUEST, |
109
|
|
|
function (ServerRequestInterface $originalRequest = null) { |
110
|
|
|
return $originalRequest ?: ServerRequestFactory::fromGlobals(); |
111
|
21 |
|
} |
112
|
21 |
|
)->withArgument(self::SERVICE_ORIGINAL_REQUEST); |
113
|
|
|
|
114
|
21 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
21 |
|
protected function addUriResolver() |
121
|
|
|
{ |
122
|
21 |
|
$this->getContainer()->share( |
123
|
21 |
|
self::SERVICE_URI_RESOLVER, |
124
|
|
|
function (ServerRequestInterface $request) { |
125
|
3 |
|
return new UriResolver($request); |
126
|
21 |
|
} |
127
|
21 |
|
)->withArgument(self::SERVICE_REQUEST); |
128
|
|
|
|
129
|
21 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
21 |
|
protected function addResponse() |
136
|
|
|
{ |
137
|
21 |
|
$this->getContainer()->add( |
138
|
21 |
|
self::SERVICE_RESPONSE, |
139
|
|
|
function () { |
140
|
3 |
|
return new Response(); |
141
|
21 |
|
} |
142
|
|
|
); |
143
|
|
|
|
144
|
21 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Should return a callable that accepts a ServerRequestInterface as a parameter |
149
|
|
|
* |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
21 |
|
protected function addMiddlewareDispatcher() |
153
|
|
|
{ |
154
|
21 |
|
$this->getContainer()->add( |
155
|
21 |
|
self::SERVICE_MIDDLEWARE_DISPATCHER, |
156
|
|
|
function (array $middlewares) { |
157
|
18 |
|
$dispatcher = (new Dispatcher($middlewares)); |
158
|
|
|
|
159
|
|
|
return function (ServerRequestInterface $request) use ($dispatcher) { |
160
|
18 |
|
return $dispatcher->dispatch($request); |
161
|
18 |
|
}; |
162
|
21 |
|
} |
163
|
|
|
); |
164
|
|
|
|
165
|
21 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Should return a callable that accepts a ResponseInterface as a parameter |
170
|
|
|
* and returns a ResponseInterface as a result. |
171
|
|
|
* |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
21 |
|
protected function addResponseEmitter() |
175
|
|
|
{ |
176
|
21 |
|
$this->getContainer()->share( |
177
|
21 |
|
self::SERVICE_RESPONSE_EMITTER, |
178
|
|
|
function () { |
179
|
|
|
$emitter = new SapiEmitter(); |
180
|
|
|
|
181
|
|
|
return function (ResponseInterface $response) use ($emitter) { |
182
|
|
|
$maxBufferLevel = 10; |
183
|
|
|
$emitter->emit($response, $maxBufferLevel); |
184
|
|
|
}; |
185
|
21 |
|
} |
186
|
|
|
); |
187
|
|
|
|
188
|
21 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|