@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | CoreInterface $core, |
53 | 53 | ScopeInterface $scope, |
54 | 54 | ResponseFactoryInterface $responseFactory |
55 | - ) { |
|
55 | + ){ |
|
56 | 56 | $this->core = $core; |
57 | 57 | $this->scope = $scope; |
58 | 58 | $this->responseFactory = $responseFactory; |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | */ |
91 | 91 | public function handle(Request $request): Response |
92 | 92 | { |
93 | - if ($this->controller === null) { |
|
93 | + if ($this->controller === null){ |
|
94 | 94 | throw new HandlerException('Controller and action pair is not set'); |
95 | 95 | } |
96 | 96 | |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | $output = $result = null; |
101 | 101 | |
102 | 102 | $response = $this->responseFactory->createResponse(200); |
103 | - try { |
|
103 | + try{ |
|
104 | 104 | // run the core withing PSR-7 Request/Response scope |
105 | 105 | $result = $this->scope->runScope( |
106 | 106 | [ |
@@ -113,29 +113,29 @@ discard block |
||
113 | 113 | $this->parameters |
114 | 114 | ) |
115 | 115 | ); |
116 | - } catch (ControllerException $e) { |
|
116 | + }catch (ControllerException $e){ |
|
117 | 117 | ob_get_clean(); |
118 | 118 | throw $this->mapException($e); |
119 | - } catch (Throwable $e) { |
|
119 | + }catch (Throwable $e){ |
|
120 | 120 | ob_get_clean(); |
121 | 121 | throw $e; |
122 | - } finally { |
|
123 | - while (ob_get_level() > $outputLevel + 1) { |
|
124 | - $output = ob_get_clean() . $output; |
|
122 | + }finally{ |
|
123 | + while (ob_get_level() > $outputLevel + 1){ |
|
124 | + $output = ob_get_clean().$output; |
|
125 | 125 | } |
126 | 126 | } |
127 | 127 | |
128 | 128 | return $this->wrapResponse( |
129 | 129 | $response, |
130 | 130 | $result, |
131 | - ob_get_clean() . $output |
|
131 | + ob_get_clean().$output |
|
132 | 132 | ); |
133 | 133 | } |
134 | 134 | |
135 | 135 | private function getAction(Request $request): string |
136 | 136 | { |
137 | - if ($this->verbActions) { |
|
138 | - return strtolower($request->getMethod()) . ucfirst($this->action); |
|
137 | + if ($this->verbActions){ |
|
138 | + return strtolower($request->getMethod()).ucfirst($this->action); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | return $this->action; |
@@ -150,21 +150,21 @@ discard block |
||
150 | 150 | */ |
151 | 151 | private function wrapResponse(Response $response, $result = null, string $output = ''): Response |
152 | 152 | { |
153 | - if ($result instanceof Response) { |
|
154 | - if ($output !== '' && $result->getBody()->isWritable()) { |
|
153 | + if ($result instanceof Response){ |
|
154 | + if ($output !== '' && $result->getBody()->isWritable()){ |
|
155 | 155 | $result->getBody()->write($output); |
156 | 156 | } |
157 | 157 | |
158 | 158 | return $result; |
159 | 159 | } |
160 | 160 | |
161 | - if ($result instanceof Generator) { |
|
161 | + if ($result instanceof Generator){ |
|
162 | 162 | return $response->withBody(new GeneratorStream($result)); |
163 | 163 | } |
164 | 164 | |
165 | - if (\is_array($result) || $result instanceof JsonSerializable) { |
|
165 | + if (\is_array($result) || $result instanceof JsonSerializable){ |
|
166 | 166 | $response = $this->writeJson($response, $result); |
167 | - } else { |
|
167 | + }else{ |
|
168 | 168 | $response->getBody()->write((string)$result); |
169 | 169 | } |
170 | 170 | |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | */ |
180 | 180 | private function mapException(ControllerException $exception): ClientException |
181 | 181 | { |
182 | - switch ($exception->getCode()) { |
|
182 | + switch ($exception->getCode()){ |
|
183 | 183 | case ControllerException::BAD_ACTION: |
184 | 184 | case ControllerException::NOT_FOUND: |
185 | 185 | return new NotFoundException($exception->getMessage()); |
@@ -90,7 +90,8 @@ discard block |
||
90 | 90 | */ |
91 | 91 | public function handle(Request $request): Response |
92 | 92 | { |
93 | - if ($this->controller === null) { |
|
93 | + if ($this->controller === null) |
|
94 | + { |
|
94 | 95 | throw new HandlerException('Controller and action pair is not set'); |
95 | 96 | } |
96 | 97 | |
@@ -100,7 +101,8 @@ discard block |
||
100 | 101 | $output = $result = null; |
101 | 102 | |
102 | 103 | $response = $this->responseFactory->createResponse(200); |
103 | - try { |
|
104 | + try |
|
105 | + { |
|
104 | 106 | // run the core withing PSR-7 Request/Response scope |
105 | 107 | $result = $this->scope->runScope( |
106 | 108 | [ |
@@ -113,14 +115,21 @@ discard block |
||
113 | 115 | $this->parameters |
114 | 116 | ) |
115 | 117 | ); |
116 | - } catch (ControllerException $e) { |
|
118 | + } |
|
119 | + catch (ControllerException $e) |
|
120 | + { |
|
117 | 121 | ob_get_clean(); |
118 | 122 | throw $this->mapException($e); |
119 | - } catch (Throwable $e) { |
|
123 | + } |
|
124 | + catch (Throwable $e) |
|
125 | + { |
|
120 | 126 | ob_get_clean(); |
121 | 127 | throw $e; |
122 | - } finally { |
|
123 | - while (ob_get_level() > $outputLevel + 1) { |
|
128 | + } |
|
129 | + finally |
|
130 | + { |
|
131 | + while (ob_get_level() > $outputLevel + 1) |
|
132 | + { |
|
124 | 133 | $output = ob_get_clean() . $output; |
125 | 134 | } |
126 | 135 | } |
@@ -134,7 +143,8 @@ discard block |
||
134 | 143 | |
135 | 144 | private function getAction(Request $request): string |
136 | 145 | { |
137 | - if ($this->verbActions) { |
|
146 | + if ($this->verbActions) |
|
147 | + { |
|
138 | 148 | return strtolower($request->getMethod()) . ucfirst($this->action); |
139 | 149 | } |
140 | 150 | |
@@ -150,21 +160,27 @@ discard block |
||
150 | 160 | */ |
151 | 161 | private function wrapResponse(Response $response, $result = null, string $output = ''): Response |
152 | 162 | { |
153 | - if ($result instanceof Response) { |
|
154 | - if ($output !== '' && $result->getBody()->isWritable()) { |
|
163 | + if ($result instanceof Response) |
|
164 | + { |
|
165 | + if ($output !== '' && $result->getBody()->isWritable()) |
|
166 | + { |
|
155 | 167 | $result->getBody()->write($output); |
156 | 168 | } |
157 | 169 | |
158 | 170 | return $result; |
159 | 171 | } |
160 | 172 | |
161 | - if ($result instanceof Generator) { |
|
173 | + if ($result instanceof Generator) |
|
174 | + { |
|
162 | 175 | return $response->withBody(new GeneratorStream($result)); |
163 | 176 | } |
164 | 177 | |
165 | - if (\is_array($result) || $result instanceof JsonSerializable) { |
|
178 | + if (\is_array($result) || $result instanceof JsonSerializable) |
|
179 | + { |
|
166 | 180 | $response = $this->writeJson($response, $result); |
167 | - } else { |
|
181 | + } |
|
182 | + else |
|
183 | + { |
|
168 | 184 | $response->getBody()->write((string)$result); |
169 | 185 | } |
170 | 186 | |
@@ -179,7 +195,8 @@ discard block |
||
179 | 195 | */ |
180 | 196 | private function mapException(ControllerException $exception): ClientException |
181 | 197 | { |
182 | - switch ($exception->getCode()) { |
|
198 | + switch ($exception->getCode()) |
|
199 | + { |
|
183 | 200 | case ControllerException::BAD_ACTION: |
184 | 201 | case ControllerException::NOT_FOUND: |
185 | 202 | return new NotFoundException($exception->getMessage()); |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | 'integer' => '\d+', |
35 | 35 | 'uuid' => '[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}', |
36 | 36 | ]; |
37 | - private const URI_FIXERS = [ |
|
37 | + private const URI_FIXERS = [ |
|
38 | 38 | '[]' => '', |
39 | 39 | '[/]' => '', |
40 | 40 | '[' => '', |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | public function __construct( |
71 | 71 | UriFactoryInterface $uriFactory, |
72 | 72 | SlugifyInterface $slugify = null |
73 | - ) { |
|
73 | + ){ |
|
74 | 74 | $this->uriFactory = $uriFactory; |
75 | 75 | $this->slugify = $slugify ?? new Slugify(); |
76 | 76 | } |
@@ -133,12 +133,12 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public function match(UriInterface $uri, array $defaults): ?array |
135 | 135 | { |
136 | - if (!$this->isCompiled()) { |
|
136 | + if (!$this->isCompiled()){ |
|
137 | 137 | $this->compile(); |
138 | 138 | } |
139 | 139 | |
140 | 140 | $matches = []; |
141 | - if (!preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
141 | + if (!preg_match($this->compiled, $this->fetchTarget($uri), $matches)){ |
|
142 | 142 | return null; |
143 | 143 | } |
144 | 144 | |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | */ |
155 | 155 | public function uri($parameters = [], array $defaults = []): UriInterface |
156 | 156 | { |
157 | - if (!$this->isCompiled()) { |
|
157 | + if (!$this->isCompiled()){ |
|
158 | 158 | $this->compile(); |
159 | 159 | } |
160 | 160 | |
@@ -164,8 +164,8 @@ discard block |
||
164 | 164 | $this->fetchOptions($parameters, $query) |
165 | 165 | ); |
166 | 166 | |
167 | - foreach ($this->constrains as $key => $_) { |
|
168 | - if (empty($parameters[$key])) { |
|
167 | + foreach ($this->constrains as $key => $_){ |
|
168 | + if (empty($parameters[$key])){ |
|
169 | 169 | throw new UriHandlerException("Unable to generate Uri, parameter `{$key}` is missing"); |
170 | 170 | } |
171 | 171 | } |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | $path = $this->interpolate($this->template, $parameters); |
175 | 175 | |
176 | 176 | //Uri with added prefix |
177 | - $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->prefix) . trim($path, '/')); |
|
177 | + $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->prefix).trim($path, '/')); |
|
178 | 178 | |
179 | 179 | return empty($query) ? $uri : $uri->withQuery(http_build_query($query)); |
180 | 180 | } |
@@ -190,18 +190,18 @@ discard block |
||
190 | 190 | $allowed = array_keys($this->options); |
191 | 191 | |
192 | 192 | $result = []; |
193 | - foreach ($parameters as $key => $parameter) { |
|
194 | - if (is_numeric($key) && isset($allowed[$key])) { |
|
193 | + foreach ($parameters as $key => $parameter){ |
|
194 | + if (is_numeric($key) && isset($allowed[$key])){ |
|
195 | 195 | // this segment fetched keys from given parameters either by name or by position |
196 | 196 | $key = $allowed[$key]; |
197 | - } elseif (!array_key_exists($key, $this->options) && is_array($parameters)) { |
|
197 | + } elseif (!array_key_exists($key, $this->options) && is_array($parameters)){ |
|
198 | 198 | // all additional parameters given in array form can be glued to query string |
199 | 199 | $query[$key] = $parameter; |
200 | 200 | continue; |
201 | 201 | } |
202 | 202 | |
203 | 203 | //String must be normalized here |
204 | - if (is_string($parameter) && !preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
204 | + if (is_string($parameter) && !preg_match('/^[a-z\-_0-9]+$/i', $parameter)){ |
|
205 | 205 | $result[$key] = $this->slugify->slugify($parameter); |
206 | 206 | continue; |
207 | 207 | } |
@@ -219,15 +219,15 @@ discard block |
||
219 | 219 | { |
220 | 220 | $path = $uri->getPath(); |
221 | 221 | |
222 | - if (empty($path) || $path[0] !== '/') { |
|
223 | - $path = '/' . $path; |
|
222 | + if (empty($path) || $path[0] !== '/'){ |
|
223 | + $path = '/'.$path; |
|
224 | 224 | } |
225 | 225 | |
226 | - if ($this->matchHost) { |
|
227 | - $uriString = $uri->getHost() . $path; |
|
228 | - } else { |
|
226 | + if ($this->matchHost){ |
|
227 | + $uriString = $uri->getHost().$path; |
|
228 | + }else{ |
|
229 | 229 | $uriString = substr($path, strlen($this->prefix)); |
230 | - if ($uriString === false) { |
|
230 | + if ($uriString === false){ |
|
231 | 231 | $uriString = ''; |
232 | 232 | } |
233 | 233 | } |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | */ |
241 | 241 | private function compile(): void |
242 | 242 | { |
243 | - if ($this->pattern === null) { |
|
243 | + if ($this->pattern === null){ |
|
244 | 244 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
245 | 245 | } |
246 | 246 | |
@@ -248,14 +248,14 @@ discard block |
||
248 | 248 | $pattern = rtrim(ltrim($this->pattern, ':/'), '/'); |
249 | 249 | |
250 | 250 | // correct [/ first occurrence] |
251 | - if (strpos($pattern, '[/') === 0) { |
|
252 | - $pattern = '[' . substr($pattern, 2); |
|
251 | + if (strpos($pattern, '[/') === 0){ |
|
252 | + $pattern = '['.substr($pattern, 2); |
|
253 | 253 | } |
254 | 254 | |
255 | - if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
255 | + if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)){ |
|
256 | 256 | $variables = array_combine($matches[1], $matches[2]); |
257 | 257 | |
258 | - foreach ($variables as $key => $segment) { |
|
258 | + foreach ($variables as $key => $segment){ |
|
259 | 259 | $segment = $this->prepareSegment($key, $segment); |
260 | 260 | $replaces["<$key>"] = "(?P<$key>$segment)"; |
261 | 261 | $options[] = $key; |
@@ -265,13 +265,13 @@ discard block |
||
265 | 265 | $template = preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
266 | 266 | $options = array_fill_keys($options, null); |
267 | 267 | |
268 | - foreach ($this->constrains as $key => $value) { |
|
269 | - if ($value instanceof Autofill) { |
|
268 | + foreach ($this->constrains as $key => $value){ |
|
269 | + if ($value instanceof Autofill){ |
|
270 | 270 | // only forces value replacement, not required to be presented as parameter |
271 | 271 | continue; |
272 | 272 | } |
273 | 273 | |
274 | - if (!array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
274 | + if (!array_key_exists($key, $options) && !isset($this->defaults[$key])){ |
|
275 | 275 | throw new ConstrainException( |
276 | 276 | sprintf( |
277 | 277 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | } |
283 | 283 | } |
284 | 284 | |
285 | - $this->compiled = '/^' . strtr($template, $replaces + self::PATTERN_REPLACES) . '$/iu'; |
|
285 | + $this->compiled = '/^'.strtr($template, $replaces + self::PATTERN_REPLACES).'$/iu'; |
|
286 | 286 | $this->template = stripslashes(str_replace('?', '', $template)); |
287 | 287 | $this->options = $options; |
288 | 288 | } |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | private function interpolate(string $string, array $values): string |
294 | 294 | { |
295 | 295 | $replaces = []; |
296 | - foreach ($values as $key => $value) { |
|
296 | + foreach ($values as $key => $value){ |
|
297 | 297 | $value = (is_array($value) || $value instanceof Closure) ? '' : $value; |
298 | 298 | $replaces["<{$key}>"] = is_object($value) ? (string)$value : $value; |
299 | 299 | } |
@@ -306,15 +306,15 @@ discard block |
||
306 | 306 | */ |
307 | 307 | private function prepareSegment(string $name, string $segment): string |
308 | 308 | { |
309 | - if ($segment !== '') { |
|
309 | + if ($segment !== ''){ |
|
310 | 310 | return self::SEGMENT_TYPES[$segment] ?? $segment; |
311 | 311 | } |
312 | 312 | |
313 | - if (!isset($this->constrains[$name])) { |
|
313 | + if (!isset($this->constrains[$name])){ |
|
314 | 314 | return self::DEFAULT_SEGMENT; |
315 | 315 | } |
316 | 316 | |
317 | - if (is_array($this->constrains[$name])) { |
|
317 | + if (is_array($this->constrains[$name])){ |
|
318 | 318 | $values = array_map([$this, 'filterSegment'], $this->constrains[$name]); |
319 | 319 | |
320 | 320 | return implode('|', $values); |
@@ -133,12 +133,14 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public function match(UriInterface $uri, array $defaults): ?array |
135 | 135 | { |
136 | - if (!$this->isCompiled()) { |
|
136 | + if (!$this->isCompiled()) |
|
137 | + { |
|
137 | 138 | $this->compile(); |
138 | 139 | } |
139 | 140 | |
140 | 141 | $matches = []; |
141 | - if (!preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
142 | + if (!preg_match($this->compiled, $this->fetchTarget($uri), $matches)) |
|
143 | + { |
|
142 | 144 | return null; |
143 | 145 | } |
144 | 146 | |
@@ -154,7 +156,8 @@ discard block |
||
154 | 156 | */ |
155 | 157 | public function uri($parameters = [], array $defaults = []): UriInterface |
156 | 158 | { |
157 | - if (!$this->isCompiled()) { |
|
159 | + if (!$this->isCompiled()) |
|
160 | + { |
|
158 | 161 | $this->compile(); |
159 | 162 | } |
160 | 163 | |
@@ -164,8 +167,10 @@ discard block |
||
164 | 167 | $this->fetchOptions($parameters, $query) |
165 | 168 | ); |
166 | 169 | |
167 | - foreach ($this->constrains as $key => $_) { |
|
168 | - if (empty($parameters[$key])) { |
|
170 | + foreach ($this->constrains as $key => $_) |
|
171 | + { |
|
172 | + if (empty($parameters[$key])) |
|
173 | + { |
|
169 | 174 | throw new UriHandlerException("Unable to generate Uri, parameter `{$key}` is missing"); |
170 | 175 | } |
171 | 176 | } |
@@ -190,18 +195,23 @@ discard block |
||
190 | 195 | $allowed = array_keys($this->options); |
191 | 196 | |
192 | 197 | $result = []; |
193 | - foreach ($parameters as $key => $parameter) { |
|
194 | - if (is_numeric($key) && isset($allowed[$key])) { |
|
198 | + foreach ($parameters as $key => $parameter) |
|
199 | + { |
|
200 | + if (is_numeric($key) && isset($allowed[$key])) |
|
201 | + { |
|
195 | 202 | // this segment fetched keys from given parameters either by name or by position |
196 | 203 | $key = $allowed[$key]; |
197 | - } elseif (!array_key_exists($key, $this->options) && is_array($parameters)) { |
|
204 | + } |
|
205 | + elseif (!array_key_exists($key, $this->options) && is_array($parameters)) |
|
206 | + { |
|
198 | 207 | // all additional parameters given in array form can be glued to query string |
199 | 208 | $query[$key] = $parameter; |
200 | 209 | continue; |
201 | 210 | } |
202 | 211 | |
203 | 212 | //String must be normalized here |
204 | - if (is_string($parameter) && !preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
213 | + if (is_string($parameter) && !preg_match('/^[a-z\-_0-9]+$/i', $parameter)) |
|
214 | + { |
|
205 | 215 | $result[$key] = $this->slugify->slugify($parameter); |
206 | 216 | continue; |
207 | 217 | } |
@@ -219,15 +229,20 @@ discard block |
||
219 | 229 | { |
220 | 230 | $path = $uri->getPath(); |
221 | 231 | |
222 | - if (empty($path) || $path[0] !== '/') { |
|
232 | + if (empty($path) || $path[0] !== '/') |
|
233 | + { |
|
223 | 234 | $path = '/' . $path; |
224 | 235 | } |
225 | 236 | |
226 | - if ($this->matchHost) { |
|
237 | + if ($this->matchHost) |
|
238 | + { |
|
227 | 239 | $uriString = $uri->getHost() . $path; |
228 | - } else { |
|
240 | + } |
|
241 | + else |
|
242 | + { |
|
229 | 243 | $uriString = substr($path, strlen($this->prefix)); |
230 | - if ($uriString === false) { |
|
244 | + if ($uriString === false) |
|
245 | + { |
|
231 | 246 | $uriString = ''; |
232 | 247 | } |
233 | 248 | } |
@@ -240,7 +255,8 @@ discard block |
||
240 | 255 | */ |
241 | 256 | private function compile(): void |
242 | 257 | { |
243 | - if ($this->pattern === null) { |
|
258 | + if ($this->pattern === null) |
|
259 | + { |
|
244 | 260 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
245 | 261 | } |
246 | 262 | |
@@ -248,14 +264,17 @@ discard block |
||
248 | 264 | $pattern = rtrim(ltrim($this->pattern, ':/'), '/'); |
249 | 265 | |
250 | 266 | // correct [/ first occurrence] |
251 | - if (strpos($pattern, '[/') === 0) { |
|
267 | + if (strpos($pattern, '[/') === 0) |
|
268 | + { |
|
252 | 269 | $pattern = '[' . substr($pattern, 2); |
253 | 270 | } |
254 | 271 | |
255 | - if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
272 | + if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) |
|
273 | + { |
|
256 | 274 | $variables = array_combine($matches[1], $matches[2]); |
257 | 275 | |
258 | - foreach ($variables as $key => $segment) { |
|
276 | + foreach ($variables as $key => $segment) |
|
277 | + { |
|
259 | 278 | $segment = $this->prepareSegment($key, $segment); |
260 | 279 | $replaces["<$key>"] = "(?P<$key>$segment)"; |
261 | 280 | $options[] = $key; |
@@ -265,13 +284,16 @@ discard block |
||
265 | 284 | $template = preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
266 | 285 | $options = array_fill_keys($options, null); |
267 | 286 | |
268 | - foreach ($this->constrains as $key => $value) { |
|
269 | - if ($value instanceof Autofill) { |
|
287 | + foreach ($this->constrains as $key => $value) |
|
288 | + { |
|
289 | + if ($value instanceof Autofill) |
|
290 | + { |
|
270 | 291 | // only forces value replacement, not required to be presented as parameter |
271 | 292 | continue; |
272 | 293 | } |
273 | 294 | |
274 | - if (!array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
295 | + if (!array_key_exists($key, $options) && !isset($this->defaults[$key])) |
|
296 | + { |
|
275 | 297 | throw new ConstrainException( |
276 | 298 | sprintf( |
277 | 299 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -293,7 +315,8 @@ discard block |
||
293 | 315 | private function interpolate(string $string, array $values): string |
294 | 316 | { |
295 | 317 | $replaces = []; |
296 | - foreach ($values as $key => $value) { |
|
318 | + foreach ($values as $key => $value) |
|
319 | + { |
|
297 | 320 | $value = (is_array($value) || $value instanceof Closure) ? '' : $value; |
298 | 321 | $replaces["<{$key}>"] = is_object($value) ? (string)$value : $value; |
299 | 322 | } |
@@ -306,15 +329,18 @@ discard block |
||
306 | 329 | */ |
307 | 330 | private function prepareSegment(string $name, string $segment): string |
308 | 331 | { |
309 | - if ($segment !== '') { |
|
332 | + if ($segment !== '') |
|
333 | + { |
|
310 | 334 | return self::SEGMENT_TYPES[$segment] ?? $segment; |
311 | 335 | } |
312 | 336 | |
313 | - if (!isset($this->constrains[$name])) { |
|
337 | + if (!isset($this->constrains[$name])) |
|
338 | + { |
|
314 | 339 | return self::DEFAULT_SEGMENT; |
315 | 340 | } |
316 | 341 | |
317 | - if (is_array($this->constrains[$name])) { |
|
342 | + if (is_array($this->constrains[$name])) |
|
343 | + { |
|
318 | 344 | $values = array_map([$this, 'filterSegment'], $this->constrains[$name]); |
319 | 345 | |
320 | 346 | return implode('|', $values); |
@@ -75,7 +75,7 @@ |
||
75 | 75 | #[ReturnTypeWillChange] |
76 | 76 | public function offsetGet($offset) |
77 | 77 | { |
78 | - if (!$this->offsetExists($offset)) { |
|
78 | + if (!$this->offsetExists($offset)){ |
|
79 | 79 | throw new ConfigException("Undefined configuration key '{$offset}'"); |
80 | 80 | } |
81 | 81 |
@@ -75,7 +75,8 @@ |
||
75 | 75 | #[ReturnTypeWillChange] |
76 | 76 | public function offsetGet($offset) |
77 | 77 | { |
78 | - if (!$this->offsetExists($offset)) { |
|
78 | + if (!$this->offsetExists($offset)) |
|
79 | + { |
|
79 | 80 | throw new ConfigException("Undefined configuration key '{$offset}'"); |
80 | 81 | } |
81 | 82 |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | { |
99 | 99 | $arguments = []; |
100 | 100 | |
101 | - foreach ($reflection->getParameters() as $parameter) { |
|
101 | + foreach ($reflection->getParameters() as $parameter){ |
|
102 | 102 | $type = $parameter->getType(); |
103 | 103 | $name = $parameter->getName(); |
104 | 104 | $class = null; |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | * Container do not currently support union types. In the future, we |
108 | 108 | * can provide the possibility of autowiring based on priorities (TBD). |
109 | 109 | */ |
110 | - if ($type instanceof ReflectionUnionType) { |
|
110 | + if ($type instanceof ReflectionUnionType){ |
|
111 | 111 | $error = 'Parameter $%s in %s contains a union type hint that cannot be inferred unambiguously'; |
112 | 112 | $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection)); |
113 | 113 | |
@@ -117,17 +117,17 @@ discard block |
||
117 | 117 | /** |
118 | 118 | * Container do not currently support intersection types. |
119 | 119 | */ |
120 | - if ($type instanceof ReflectionIntersectionType) { |
|
120 | + if ($type instanceof ReflectionIntersectionType){ |
|
121 | 121 | $error = 'Parameter $%s in %s contains a intersection type hint that cannot be inferred unambiguously'; |
122 | 122 | $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection)); |
123 | 123 | |
124 | 124 | throw new ContainerException($error); |
125 | 125 | } |
126 | 126 | |
127 | - if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) { |
|
128 | - try { |
|
127 | + if ($type instanceof ReflectionNamedType && !$type->isBuiltin()){ |
|
128 | + try{ |
|
129 | 129 | $class = new ReflectionClass($type->getName()); |
130 | - } catch (ReflectionException $e) { |
|
130 | + }catch (ReflectionException $e){ |
|
131 | 131 | $location = $this->getLocationString($reflection); |
132 | 132 | |
133 | 133 | $error = 'Unable to resolve `\$%s` parameter in %s: %s'; |
@@ -137,11 +137,11 @@ discard block |
||
137 | 137 | } |
138 | 138 | } |
139 | 139 | |
140 | - if (isset($parameters[$name]) && is_object($parameters[$name])) { |
|
141 | - if ($parameters[$name] instanceof Autowire) { |
|
140 | + if (isset($parameters[$name]) && is_object($parameters[$name])){ |
|
141 | + if ($parameters[$name] instanceof Autowire){ |
|
142 | 142 | // Supplied by user as late dependency |
143 | 143 | $arguments[] = $parameters[$name]->resolve($this); |
144 | - } else { |
|
144 | + }else{ |
|
145 | 145 | // Supplied by user as object |
146 | 146 | $arguments[] = $parameters[$name]; |
147 | 147 | } |
@@ -149,16 +149,16 @@ discard block |
||
149 | 149 | } |
150 | 150 | |
151 | 151 | // no declared type or scalar type or array |
152 | - if (!isset($class)) { |
|
152 | + if (!isset($class)){ |
|
153 | 153 | // Provided from outside |
154 | - if (\array_key_exists($name, $parameters)) { |
|
154 | + if (\array_key_exists($name, $parameters)){ |
|
155 | 155 | // Make sure it's properly typed |
156 | 156 | $this->assertType($parameter, $reflection, $parameters[$name]); |
157 | 157 | $arguments[] = $parameters[$name]; |
158 | 158 | continue; |
159 | 159 | } |
160 | 160 | |
161 | - if ($parameter->isDefaultValueAvailable()) { |
|
161 | + if ($parameter->isDefaultValueAvailable()){ |
|
162 | 162 | //Default value |
163 | 163 | $arguments[] = $parameter->getDefaultValue(); |
164 | 164 | continue; |
@@ -168,12 +168,12 @@ discard block |
||
168 | 168 | throw new ArgumentException($parameter, $reflection); |
169 | 169 | } |
170 | 170 | |
171 | - try { |
|
171 | + try{ |
|
172 | 172 | //Requesting for contextual dependency |
173 | 173 | $arguments[] = $this->get($class->getName(), $name); |
174 | 174 | continue; |
175 | - } catch (AutowireException $e) { |
|
176 | - if ($parameter->isOptional()) { |
|
175 | + }catch (AutowireException $e){ |
|
176 | + if ($parameter->isOptional()){ |
|
177 | 177 | //This is optional dependency, skip |
178 | 178 | $arguments[] = null; |
179 | 179 | continue; |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | */ |
210 | 210 | public function get($id, string $context = null) |
211 | 211 | { |
212 | - if ($id instanceof Autowire) { |
|
212 | + if ($id instanceof Autowire){ |
|
213 | 213 | return $id->resolve($this); |
214 | 214 | } |
215 | 215 | |
@@ -230,34 +230,34 @@ discard block |
||
230 | 230 | */ |
231 | 231 | public function make(string $alias, array $parameters = [], string $context = null) |
232 | 232 | { |
233 | - if (!isset($this->bindings[$alias])) { |
|
233 | + if (!isset($this->bindings[$alias])){ |
|
234 | 234 | //No direct instructions how to construct class, make is automatically |
235 | 235 | return $this->autowire($alias, $parameters, $context); |
236 | 236 | } |
237 | 237 | |
238 | 238 | $binding = $this->bindings[$alias]; |
239 | - if (\is_object($binding)) { |
|
239 | + if (\is_object($binding)){ |
|
240 | 240 | //When binding is instance, assuming singleton |
241 | 241 | return $binding; |
242 | 242 | } |
243 | 243 | |
244 | - if (\is_string($binding)) { |
|
244 | + if (\is_string($binding)){ |
|
245 | 245 | //Binding is pointing to something else |
246 | 246 | return $this->make($binding, $parameters, $context); |
247 | 247 | } |
248 | 248 | |
249 | 249 | unset($this->bindings[$alias]); |
250 | - try { |
|
251 | - if ($binding[0] === $alias) { |
|
250 | + try{ |
|
251 | + if ($binding[0] === $alias){ |
|
252 | 252 | $instance = $this->autowire($alias, $parameters, $context); |
253 | - } else { |
|
253 | + }else{ |
|
254 | 254 | $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
255 | 255 | } |
256 | - } finally { |
|
256 | + }finally{ |
|
257 | 257 | $this->bindings[$alias] = $binding; |
258 | 258 | } |
259 | 259 | |
260 | - if ($binding[1]) { |
|
260 | + if ($binding[1]){ |
|
261 | 261 | //Indicates singleton |
262 | 262 | $this->bindings[$alias] = $instance; |
263 | 263 | } |
@@ -271,28 +271,28 @@ discard block |
||
271 | 271 | public function runScope(array $bindings, callable $scope) |
272 | 272 | { |
273 | 273 | $cleanup = $previous = []; |
274 | - foreach ($bindings as $alias => $resolver) { |
|
275 | - if (isset($this->bindings[$alias])) { |
|
274 | + foreach ($bindings as $alias => $resolver){ |
|
275 | + if (isset($this->bindings[$alias])){ |
|
276 | 276 | $previous[$alias] = $this->bindings[$alias]; |
277 | - } else { |
|
277 | + }else{ |
|
278 | 278 | $cleanup[] = $alias; |
279 | 279 | } |
280 | 280 | |
281 | 281 | $this->bind($alias, $resolver); |
282 | 282 | } |
283 | 283 | |
284 | - try { |
|
285 | - if (ContainerScope::getContainer() !== $this) { |
|
284 | + try{ |
|
285 | + if (ContainerScope::getContainer() !== $this){ |
|
286 | 286 | return ContainerScope::runScope($this, $scope); |
287 | 287 | } |
288 | 288 | |
289 | 289 | return $scope(); |
290 | - } finally { |
|
291 | - foreach (\array_reverse($previous) as $alias => $resolver) { |
|
290 | + }finally{ |
|
291 | + foreach (\array_reverse($previous) as $alias => $resolver){ |
|
292 | 292 | $this->bindings[$alias] = $resolver; |
293 | 293 | } |
294 | 294 | |
295 | - foreach ($cleanup as $alias) { |
|
295 | + foreach ($cleanup as $alias){ |
|
296 | 296 | unset($this->bindings[$alias]); |
297 | 297 | } |
298 | 298 | } |
@@ -307,7 +307,7 @@ discard block |
||
307 | 307 | */ |
308 | 308 | public function bind(string $alias, $resolver): void |
309 | 309 | { |
310 | - if (\is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire) { |
|
310 | + if (\is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire){ |
|
311 | 311 | // array means = execute me, false = not singleton |
312 | 312 | $this->bindings[$alias] = [$resolver, false]; |
313 | 313 | |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | */ |
326 | 326 | public function bindSingleton(string $alias, $resolver): void |
327 | 327 | { |
328 | - if (\is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire) { |
|
328 | + if (\is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire){ |
|
329 | 329 | // direct binding to an instance |
330 | 330 | $this->bindings[$alias] = $resolver; |
331 | 331 | |
@@ -340,11 +340,11 @@ discard block |
||
340 | 340 | */ |
341 | 341 | public function hasInstance(string $alias): bool |
342 | 342 | { |
343 | - if (!$this->has($alias)) { |
|
343 | + if (!$this->has($alias)){ |
|
344 | 344 | return false; |
345 | 345 | } |
346 | 346 | |
347 | - while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) { |
|
347 | + while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])){ |
|
348 | 348 | //Checking alias tree |
349 | 349 | $alias = $this->bindings[$alias]; |
350 | 350 | } |
@@ -370,18 +370,18 @@ discard block |
||
370 | 370 | */ |
371 | 371 | public function invoke($target, array $parameters = []) |
372 | 372 | { |
373 | - if (\is_array($target) && isset($target[1])) { |
|
373 | + if (\is_array($target) && isset($target[1])){ |
|
374 | 374 | // In a form of resolver and method |
375 | 375 | [$resolver, $method] = $target; |
376 | 376 | |
377 | 377 | // Resolver instance (i.e. [ClassName::class, 'method']) |
378 | - if (\is_string($resolver)) { |
|
378 | + if (\is_string($resolver)){ |
|
379 | 379 | $resolver = $this->get($resolver); |
380 | 380 | } |
381 | 381 | |
382 | - try { |
|
382 | + try{ |
|
383 | 383 | $method = new ReflectionMethod($resolver, $method); |
384 | - } catch (ReflectionException $e) { |
|
384 | + }catch (ReflectionException $e){ |
|
385 | 385 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
386 | 386 | } |
387 | 387 | |
@@ -394,14 +394,14 @@ discard block |
||
394 | 394 | ); |
395 | 395 | } |
396 | 396 | |
397 | - if (\is_string($target) && \is_callable($target)) { |
|
397 | + if (\is_string($target) && \is_callable($target)){ |
|
398 | 398 | $target = Closure::fromCallable($target); |
399 | 399 | } |
400 | 400 | |
401 | - if ($target instanceof Closure) { |
|
402 | - try { |
|
401 | + if ($target instanceof Closure){ |
|
402 | + try{ |
|
403 | 403 | $reflection = new ReflectionFunction($target); |
404 | - } catch (ReflectionException $e) { |
|
404 | + }catch (ReflectionException $e){ |
|
405 | 405 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
406 | 406 | } |
407 | 407 | |
@@ -461,7 +461,7 @@ discard block |
||
461 | 461 | */ |
462 | 462 | protected function autowire(string $class, array $parameters, string $context = null) |
463 | 463 | { |
464 | - if (!\class_exists($class) && !isset($this->injectors[$class])) { |
|
464 | + if (!\class_exists($class) && !isset($this->injectors[$class])){ |
|
465 | 465 | throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class)); |
466 | 466 | } |
467 | 467 | |
@@ -476,7 +476,7 @@ discard block |
||
476 | 476 | { |
477 | 477 | $location = $reflection->getName(); |
478 | 478 | |
479 | - if ($reflection instanceof ReflectionMethod) { |
|
479 | + if ($reflection instanceof ReflectionMethod){ |
|
480 | 480 | return "{$reflection->getDeclaringClass()->getName()}::{$location}()"; |
481 | 481 | } |
482 | 482 | |
@@ -492,8 +492,8 @@ discard block |
||
492 | 492 | */ |
493 | 493 | private function assertType(ReflectionParameter $parameter, ContextFunction $context, $value): void |
494 | 494 | { |
495 | - if ($value === null) { |
|
496 | - if (!$parameter->allowsNull()) { |
|
495 | + if ($value === null){ |
|
496 | + if (!$parameter->allowsNull()){ |
|
497 | 497 | throw new ArgumentException($parameter, $context); |
498 | 498 | } |
499 | 499 | |
@@ -501,20 +501,20 @@ discard block |
||
501 | 501 | } |
502 | 502 | |
503 | 503 | $type = $parameter->getType(); |
504 | - if ($type === null) { |
|
504 | + if ($type === null){ |
|
505 | 505 | return; |
506 | 506 | } |
507 | 507 | |
508 | 508 | $typeName = $type->getName(); |
509 | - if ($typeName === 'array' && !\is_array($value)) { |
|
509 | + if ($typeName === 'array' && !\is_array($value)){ |
|
510 | 510 | throw new ArgumentException($parameter, $context); |
511 | 511 | } |
512 | 512 | |
513 | - if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) { |
|
513 | + if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)){ |
|
514 | 514 | throw new ArgumentException($parameter, $context); |
515 | 515 | } |
516 | 516 | |
517 | - if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) { |
|
517 | + if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)){ |
|
518 | 518 | throw new ArgumentException($parameter, $context); |
519 | 519 | } |
520 | 520 | } |
@@ -531,22 +531,22 @@ discard block |
||
531 | 531 | */ |
532 | 532 | private function createInstance(string $class, array $parameters, string $context = null) |
533 | 533 | { |
534 | - try { |
|
534 | + try{ |
|
535 | 535 | $reflection = new ReflectionClass($class); |
536 | - } catch (ReflectionException $e) { |
|
536 | + }catch (ReflectionException $e){ |
|
537 | 537 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
538 | 538 | } |
539 | 539 | |
540 | 540 | //We have to construct class using external injector when we know exact context |
541 | - if ($parameters === [] && $this->checkInjector($reflection)) { |
|
541 | + if ($parameters === [] && $this->checkInjector($reflection)){ |
|
542 | 542 | $injector = $this->injectors[$reflection->getName()]; |
543 | 543 | |
544 | 544 | $instance = null; |
545 | - try { |
|
545 | + try{ |
|
546 | 546 | /** @var InjectorInterface|mixed $injectorInstance */ |
547 | 547 | $injectorInstance = $this->get($injector); |
548 | 548 | |
549 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
549 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
550 | 550 | throw new InjectionException( |
551 | 551 | \sprintf( |
552 | 552 | "Class '%s' must be an instance of InjectorInterface for '%s'", |
@@ -557,7 +557,7 @@ discard block |
||
557 | 557 | } |
558 | 558 | |
559 | 559 | $instance = $injectorInstance->createInjection($reflection, $context); |
560 | - if (!$reflection->isInstance($instance)) { |
|
560 | + if (!$reflection->isInstance($instance)){ |
|
561 | 561 | throw new InjectionException( |
562 | 562 | \sprintf( |
563 | 563 | "Invalid injection response for '%s'", |
@@ -565,23 +565,23 @@ discard block |
||
565 | 565 | ) |
566 | 566 | ); |
567 | 567 | } |
568 | - } finally { |
|
568 | + }finally{ |
|
569 | 569 | $this->injectors[$reflection->getName()] = $injector; |
570 | 570 | } |
571 | 571 | |
572 | 572 | return $instance; |
573 | 573 | } |
574 | 574 | |
575 | - if (!$reflection->isInstantiable()) { |
|
575 | + if (!$reflection->isInstantiable()){ |
|
576 | 576 | throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class)); |
577 | 577 | } |
578 | 578 | |
579 | 579 | $constructor = $reflection->getConstructor(); |
580 | 580 | |
581 | - if ($constructor !== null) { |
|
581 | + if ($constructor !== null){ |
|
582 | 582 | // Using constructor with resolved arguments |
583 | 583 | $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters)); |
584 | - } else { |
|
584 | + }else{ |
|
585 | 585 | // No constructor specified |
586 | 586 | $instance = $reflection->newInstance(); |
587 | 587 | } |
@@ -595,25 +595,25 @@ discard block |
||
595 | 595 | private function checkInjector(ReflectionClass $reflection): bool |
596 | 596 | { |
597 | 597 | $class = $reflection->getName(); |
598 | - if (\array_key_exists($class, $this->injectors)) { |
|
598 | + if (\array_key_exists($class, $this->injectors)){ |
|
599 | 599 | return $this->injectors[$class] !== null; |
600 | 600 | } |
601 | 601 | |
602 | 602 | if ( |
603 | 603 | $reflection->implementsInterface(InjectableInterface::class) |
604 | 604 | && $reflection->hasConstant('INJECTOR') |
605 | - ) { |
|
605 | + ){ |
|
606 | 606 | $this->injectors[$class] = $reflection->getConstant('INJECTOR'); |
607 | 607 | |
608 | 608 | return true; |
609 | 609 | } |
610 | 610 | |
611 | 611 | // check interfaces |
612 | - foreach ($this->injectors as $target => $injector) { |
|
612 | + foreach ($this->injectors as $target => $injector){ |
|
613 | 613 | if ( |
614 | 614 | \class_exists($target, true) |
615 | 615 | && $reflection->isSubclassOf($target) |
616 | - ) { |
|
616 | + ){ |
|
617 | 617 | $this->injectors[$class] = $injector; |
618 | 618 | |
619 | 619 | return true; |
@@ -622,7 +622,7 @@ discard block |
||
622 | 622 | if ( |
623 | 623 | \interface_exists($target, true) |
624 | 624 | && $reflection->implementsInterface($target) |
625 | - ) { |
|
625 | + ){ |
|
626 | 626 | $this->injectors[$class] = $injector; |
627 | 627 | |
628 | 628 | return true; |
@@ -643,9 +643,9 @@ discard block |
||
643 | 643 | private function registerInstance($instance, array $parameters) |
644 | 644 | { |
645 | 645 | //Declarative singletons (only when class received via direct get) |
646 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
646 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
647 | 647 | $alias = \get_class($instance); |
648 | - if (!isset($this->bindings[$alias])) { |
|
648 | + if (!isset($this->bindings[$alias])){ |
|
649 | 649 | $this->bindings[$alias] = $instance; |
650 | 650 | } |
651 | 651 | } |
@@ -663,18 +663,18 @@ discard block |
||
663 | 663 | */ |
664 | 664 | private function evaluateBinding(string $alias, $target, array $parameters, string $context = null) |
665 | 665 | { |
666 | - if (\is_string($target)) { |
|
666 | + if (\is_string($target)){ |
|
667 | 667 | // Reference |
668 | 668 | return $this->make($target, $parameters, $context); |
669 | 669 | } |
670 | 670 | |
671 | - if ($target instanceof Autowire) { |
|
671 | + if ($target instanceof Autowire){ |
|
672 | 672 | return $target->resolve($this, $parameters); |
673 | 673 | } |
674 | 674 | |
675 | - try { |
|
675 | + try{ |
|
676 | 676 | return $this->invoke($target, $parameters); |
677 | - } catch (NotCallableException $e) { |
|
677 | + }catch (NotCallableException $e){ |
|
678 | 678 | throw new ContainerException(\sprintf("Invalid binding for '%s'", $alias), $e->getCode(), $e); |
679 | 679 | } |
680 | 680 | } |
@@ -98,7 +98,8 @@ discard block |
||
98 | 98 | { |
99 | 99 | $arguments = []; |
100 | 100 | |
101 | - foreach ($reflection->getParameters() as $parameter) { |
|
101 | + foreach ($reflection->getParameters() as $parameter) |
|
102 | + { |
|
102 | 103 | $type = $parameter->getType(); |
103 | 104 | $name = $parameter->getName(); |
104 | 105 | $class = null; |
@@ -107,7 +108,8 @@ discard block |
||
107 | 108 | * Container do not currently support union types. In the future, we |
108 | 109 | * can provide the possibility of autowiring based on priorities (TBD). |
109 | 110 | */ |
110 | - if ($type instanceof ReflectionUnionType) { |
|
111 | + if ($type instanceof ReflectionUnionType) |
|
112 | + { |
|
111 | 113 | $error = 'Parameter $%s in %s contains a union type hint that cannot be inferred unambiguously'; |
112 | 114 | $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection)); |
113 | 115 | |
@@ -117,17 +119,22 @@ discard block |
||
117 | 119 | /** |
118 | 120 | * Container do not currently support intersection types. |
119 | 121 | */ |
120 | - if ($type instanceof ReflectionIntersectionType) { |
|
122 | + if ($type instanceof ReflectionIntersectionType) |
|
123 | + { |
|
121 | 124 | $error = 'Parameter $%s in %s contains a intersection type hint that cannot be inferred unambiguously'; |
122 | 125 | $error = \sprintf($error, $reflection->getName(), $this->getLocationString($reflection)); |
123 | 126 | |
124 | 127 | throw new ContainerException($error); |
125 | 128 | } |
126 | 129 | |
127 | - if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) { |
|
128 | - try { |
|
130 | + if ($type instanceof ReflectionNamedType && !$type->isBuiltin()) |
|
131 | + { |
|
132 | + try |
|
133 | + { |
|
129 | 134 | $class = new ReflectionClass($type->getName()); |
130 | - } catch (ReflectionException $e) { |
|
135 | + } |
|
136 | + catch (ReflectionException $e) |
|
137 | + { |
|
131 | 138 | $location = $this->getLocationString($reflection); |
132 | 139 | |
133 | 140 | $error = 'Unable to resolve `\$%s` parameter in %s: %s'; |
@@ -137,11 +144,15 @@ discard block |
||
137 | 144 | } |
138 | 145 | } |
139 | 146 | |
140 | - if (isset($parameters[$name]) && is_object($parameters[$name])) { |
|
141 | - if ($parameters[$name] instanceof Autowire) { |
|
147 | + if (isset($parameters[$name]) && is_object($parameters[$name])) |
|
148 | + { |
|
149 | + if ($parameters[$name] instanceof Autowire) |
|
150 | + { |
|
142 | 151 | // Supplied by user as late dependency |
143 | 152 | $arguments[] = $parameters[$name]->resolve($this); |
144 | - } else { |
|
153 | + } |
|
154 | + else |
|
155 | + { |
|
145 | 156 | // Supplied by user as object |
146 | 157 | $arguments[] = $parameters[$name]; |
147 | 158 | } |
@@ -149,16 +160,19 @@ discard block |
||
149 | 160 | } |
150 | 161 | |
151 | 162 | // no declared type or scalar type or array |
152 | - if (!isset($class)) { |
|
163 | + if (!isset($class)) |
|
164 | + { |
|
153 | 165 | // Provided from outside |
154 | - if (\array_key_exists($name, $parameters)) { |
|
166 | + if (\array_key_exists($name, $parameters)) |
|
167 | + { |
|
155 | 168 | // Make sure it's properly typed |
156 | 169 | $this->assertType($parameter, $reflection, $parameters[$name]); |
157 | 170 | $arguments[] = $parameters[$name]; |
158 | 171 | continue; |
159 | 172 | } |
160 | 173 | |
161 | - if ($parameter->isDefaultValueAvailable()) { |
|
174 | + if ($parameter->isDefaultValueAvailable()) |
|
175 | + { |
|
162 | 176 | //Default value |
163 | 177 | $arguments[] = $parameter->getDefaultValue(); |
164 | 178 | continue; |
@@ -168,12 +182,16 @@ discard block |
||
168 | 182 | throw new ArgumentException($parameter, $reflection); |
169 | 183 | } |
170 | 184 | |
171 | - try { |
|
185 | + try |
|
186 | + { |
|
172 | 187 | //Requesting for contextual dependency |
173 | 188 | $arguments[] = $this->get($class->getName(), $name); |
174 | 189 | continue; |
175 | - } catch (AutowireException $e) { |
|
176 | - if ($parameter->isOptional()) { |
|
190 | + } |
|
191 | + catch (AutowireException $e) |
|
192 | + { |
|
193 | + if ($parameter->isOptional()) |
|
194 | + { |
|
177 | 195 | //This is optional dependency, skip |
178 | 196 | $arguments[] = null; |
179 | 197 | continue; |
@@ -209,7 +227,8 @@ discard block |
||
209 | 227 | */ |
210 | 228 | public function get($id, string $context = null) |
211 | 229 | { |
212 | - if ($id instanceof Autowire) { |
|
230 | + if ($id instanceof Autowire) |
|
231 | + { |
|
213 | 232 | return $id->resolve($this); |
214 | 233 | } |
215 | 234 | |
@@ -230,34 +249,44 @@ discard block |
||
230 | 249 | */ |
231 | 250 | public function make(string $alias, array $parameters = [], string $context = null) |
232 | 251 | { |
233 | - if (!isset($this->bindings[$alias])) { |
|
252 | + if (!isset($this->bindings[$alias])) |
|
253 | + { |
|
234 | 254 | //No direct instructions how to construct class, make is automatically |
235 | 255 | return $this->autowire($alias, $parameters, $context); |
236 | 256 | } |
237 | 257 | |
238 | 258 | $binding = $this->bindings[$alias]; |
239 | - if (\is_object($binding)) { |
|
259 | + if (\is_object($binding)) |
|
260 | + { |
|
240 | 261 | //When binding is instance, assuming singleton |
241 | 262 | return $binding; |
242 | 263 | } |
243 | 264 | |
244 | - if (\is_string($binding)) { |
|
265 | + if (\is_string($binding)) |
|
266 | + { |
|
245 | 267 | //Binding is pointing to something else |
246 | 268 | return $this->make($binding, $parameters, $context); |
247 | 269 | } |
248 | 270 | |
249 | 271 | unset($this->bindings[$alias]); |
250 | - try { |
|
251 | - if ($binding[0] === $alias) { |
|
272 | + try |
|
273 | + { |
|
274 | + if ($binding[0] === $alias) |
|
275 | + { |
|
252 | 276 | $instance = $this->autowire($alias, $parameters, $context); |
253 | - } else { |
|
277 | + } |
|
278 | + else |
|
279 | + { |
|
254 | 280 | $instance = $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
255 | 281 | } |
256 | - } finally { |
|
282 | + } |
|
283 | + finally |
|
284 | + { |
|
257 | 285 | $this->bindings[$alias] = $binding; |
258 | 286 | } |
259 | 287 | |
260 | - if ($binding[1]) { |
|
288 | + if ($binding[1]) |
|
289 | + { |
|
261 | 290 | //Indicates singleton |
262 | 291 | $this->bindings[$alias] = $instance; |
263 | 292 | } |
@@ -271,28 +300,38 @@ discard block |
||
271 | 300 | public function runScope(array $bindings, callable $scope) |
272 | 301 | { |
273 | 302 | $cleanup = $previous = []; |
274 | - foreach ($bindings as $alias => $resolver) { |
|
275 | - if (isset($this->bindings[$alias])) { |
|
303 | + foreach ($bindings as $alias => $resolver) |
|
304 | + { |
|
305 | + if (isset($this->bindings[$alias])) |
|
306 | + { |
|
276 | 307 | $previous[$alias] = $this->bindings[$alias]; |
277 | - } else { |
|
308 | + } |
|
309 | + else |
|
310 | + { |
|
278 | 311 | $cleanup[] = $alias; |
279 | 312 | } |
280 | 313 | |
281 | 314 | $this->bind($alias, $resolver); |
282 | 315 | } |
283 | 316 | |
284 | - try { |
|
285 | - if (ContainerScope::getContainer() !== $this) { |
|
317 | + try |
|
318 | + { |
|
319 | + if (ContainerScope::getContainer() !== $this) |
|
320 | + { |
|
286 | 321 | return ContainerScope::runScope($this, $scope); |
287 | 322 | } |
288 | 323 | |
289 | 324 | return $scope(); |
290 | - } finally { |
|
291 | - foreach (\array_reverse($previous) as $alias => $resolver) { |
|
325 | + } |
|
326 | + finally |
|
327 | + { |
|
328 | + foreach (\array_reverse($previous) as $alias => $resolver) |
|
329 | + { |
|
292 | 330 | $this->bindings[$alias] = $resolver; |
293 | 331 | } |
294 | 332 | |
295 | - foreach ($cleanup as $alias) { |
|
333 | + foreach ($cleanup as $alias) |
|
334 | + { |
|
296 | 335 | unset($this->bindings[$alias]); |
297 | 336 | } |
298 | 337 | } |
@@ -307,7 +346,8 @@ discard block |
||
307 | 346 | */ |
308 | 347 | public function bind(string $alias, $resolver): void |
309 | 348 | { |
310 | - if (\is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire) { |
|
349 | + if (\is_array($resolver) || $resolver instanceof Closure || $resolver instanceof Autowire) |
|
350 | + { |
|
311 | 351 | // array means = execute me, false = not singleton |
312 | 352 | $this->bindings[$alias] = [$resolver, false]; |
313 | 353 | |
@@ -325,7 +365,8 @@ discard block |
||
325 | 365 | */ |
326 | 366 | public function bindSingleton(string $alias, $resolver): void |
327 | 367 | { |
328 | - if (\is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire) { |
|
368 | + if (\is_object($resolver) && !$resolver instanceof Closure && !$resolver instanceof Autowire) |
|
369 | + { |
|
329 | 370 | // direct binding to an instance |
330 | 371 | $this->bindings[$alias] = $resolver; |
331 | 372 | |
@@ -340,11 +381,13 @@ discard block |
||
340 | 381 | */ |
341 | 382 | public function hasInstance(string $alias): bool |
342 | 383 | { |
343 | - if (!$this->has($alias)) { |
|
384 | + if (!$this->has($alias)) |
|
385 | + { |
|
344 | 386 | return false; |
345 | 387 | } |
346 | 388 | |
347 | - while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) { |
|
389 | + while (isset($this->bindings[$alias]) && \is_string($this->bindings[$alias])) |
|
390 | + { |
|
348 | 391 | //Checking alias tree |
349 | 392 | $alias = $this->bindings[$alias]; |
350 | 393 | } |
@@ -370,18 +413,23 @@ discard block |
||
370 | 413 | */ |
371 | 414 | public function invoke($target, array $parameters = []) |
372 | 415 | { |
373 | - if (\is_array($target) && isset($target[1])) { |
|
416 | + if (\is_array($target) && isset($target[1])) |
|
417 | + { |
|
374 | 418 | // In a form of resolver and method |
375 | 419 | [$resolver, $method] = $target; |
376 | 420 | |
377 | 421 | // Resolver instance (i.e. [ClassName::class, 'method']) |
378 | - if (\is_string($resolver)) { |
|
422 | + if (\is_string($resolver)) |
|
423 | + { |
|
379 | 424 | $resolver = $this->get($resolver); |
380 | 425 | } |
381 | 426 | |
382 | - try { |
|
427 | + try |
|
428 | + { |
|
383 | 429 | $method = new ReflectionMethod($resolver, $method); |
384 | - } catch (ReflectionException $e) { |
|
430 | + } |
|
431 | + catch (ReflectionException $e) |
|
432 | + { |
|
385 | 433 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
386 | 434 | } |
387 | 435 | |
@@ -394,14 +442,19 @@ discard block |
||
394 | 442 | ); |
395 | 443 | } |
396 | 444 | |
397 | - if (\is_string($target) && \is_callable($target)) { |
|
445 | + if (\is_string($target) && \is_callable($target)) |
|
446 | + { |
|
398 | 447 | $target = Closure::fromCallable($target); |
399 | 448 | } |
400 | 449 | |
401 | - if ($target instanceof Closure) { |
|
402 | - try { |
|
450 | + if ($target instanceof Closure) |
|
451 | + { |
|
452 | + try |
|
453 | + { |
|
403 | 454 | $reflection = new ReflectionFunction($target); |
404 | - } catch (ReflectionException $e) { |
|
455 | + } |
|
456 | + catch (ReflectionException $e) |
|
457 | + { |
|
405 | 458 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
406 | 459 | } |
407 | 460 | |
@@ -461,7 +514,8 @@ discard block |
||
461 | 514 | */ |
462 | 515 | protected function autowire(string $class, array $parameters, string $context = null) |
463 | 516 | { |
464 | - if (!\class_exists($class) && !isset($this->injectors[$class])) { |
|
517 | + if (!\class_exists($class) && !isset($this->injectors[$class])) |
|
518 | + { |
|
465 | 519 | throw new NotFoundException(\sprintf("Undefined class or binding '%s'", $class)); |
466 | 520 | } |
467 | 521 | |
@@ -476,7 +530,8 @@ discard block |
||
476 | 530 | { |
477 | 531 | $location = $reflection->getName(); |
478 | 532 | |
479 | - if ($reflection instanceof ReflectionMethod) { |
|
533 | + if ($reflection instanceof ReflectionMethod) |
|
534 | + { |
|
480 | 535 | return "{$reflection->getDeclaringClass()->getName()}::{$location}()"; |
481 | 536 | } |
482 | 537 | |
@@ -492,8 +547,10 @@ discard block |
||
492 | 547 | */ |
493 | 548 | private function assertType(ReflectionParameter $parameter, ContextFunction $context, $value): void |
494 | 549 | { |
495 | - if ($value === null) { |
|
496 | - if (!$parameter->allowsNull()) { |
|
550 | + if ($value === null) |
|
551 | + { |
|
552 | + if (!$parameter->allowsNull()) |
|
553 | + { |
|
497 | 554 | throw new ArgumentException($parameter, $context); |
498 | 555 | } |
499 | 556 | |
@@ -501,20 +558,24 @@ discard block |
||
501 | 558 | } |
502 | 559 | |
503 | 560 | $type = $parameter->getType(); |
504 | - if ($type === null) { |
|
561 | + if ($type === null) |
|
562 | + { |
|
505 | 563 | return; |
506 | 564 | } |
507 | 565 | |
508 | 566 | $typeName = $type->getName(); |
509 | - if ($typeName === 'array' && !\is_array($value)) { |
|
567 | + if ($typeName === 'array' && !\is_array($value)) |
|
568 | + { |
|
510 | 569 | throw new ArgumentException($parameter, $context); |
511 | 570 | } |
512 | 571 | |
513 | - if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) { |
|
572 | + if (($typeName === 'int' || $typeName === 'float') && !\is_numeric($value)) |
|
573 | + { |
|
514 | 574 | throw new ArgumentException($parameter, $context); |
515 | 575 | } |
516 | 576 | |
517 | - if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) { |
|
577 | + if ($typeName === 'bool' && !\is_bool($value) && !\is_numeric($value)) |
|
578 | + { |
|
518 | 579 | throw new ArgumentException($parameter, $context); |
519 | 580 | } |
520 | 581 | } |
@@ -531,22 +592,28 @@ discard block |
||
531 | 592 | */ |
532 | 593 | private function createInstance(string $class, array $parameters, string $context = null) |
533 | 594 | { |
534 | - try { |
|
595 | + try |
|
596 | + { |
|
535 | 597 | $reflection = new ReflectionClass($class); |
536 | - } catch (ReflectionException $e) { |
|
598 | + } |
|
599 | + catch (ReflectionException $e) |
|
600 | + { |
|
537 | 601 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
538 | 602 | } |
539 | 603 | |
540 | 604 | //We have to construct class using external injector when we know exact context |
541 | - if ($parameters === [] && $this->checkInjector($reflection)) { |
|
605 | + if ($parameters === [] && $this->checkInjector($reflection)) |
|
606 | + { |
|
542 | 607 | $injector = $this->injectors[$reflection->getName()]; |
543 | 608 | |
544 | 609 | $instance = null; |
545 | - try { |
|
610 | + try |
|
611 | + { |
|
546 | 612 | /** @var InjectorInterface|mixed $injectorInstance */ |
547 | 613 | $injectorInstance = $this->get($injector); |
548 | 614 | |
549 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
615 | + if (!$injectorInstance instanceof InjectorInterface) |
|
616 | + { |
|
550 | 617 | throw new InjectionException( |
551 | 618 | \sprintf( |
552 | 619 | "Class '%s' must be an instance of InjectorInterface for '%s'", |
@@ -557,7 +624,8 @@ discard block |
||
557 | 624 | } |
558 | 625 | |
559 | 626 | $instance = $injectorInstance->createInjection($reflection, $context); |
560 | - if (!$reflection->isInstance($instance)) { |
|
627 | + if (!$reflection->isInstance($instance)) |
|
628 | + { |
|
561 | 629 | throw new InjectionException( |
562 | 630 | \sprintf( |
563 | 631 | "Invalid injection response for '%s'", |
@@ -565,23 +633,29 @@ discard block |
||
565 | 633 | ) |
566 | 634 | ); |
567 | 635 | } |
568 | - } finally { |
|
636 | + } |
|
637 | + finally |
|
638 | + { |
|
569 | 639 | $this->injectors[$reflection->getName()] = $injector; |
570 | 640 | } |
571 | 641 | |
572 | 642 | return $instance; |
573 | 643 | } |
574 | 644 | |
575 | - if (!$reflection->isInstantiable()) { |
|
645 | + if (!$reflection->isInstantiable()) |
|
646 | + { |
|
576 | 647 | throw new ContainerException(\sprintf("Class '%s' can not be constructed", $class)); |
577 | 648 | } |
578 | 649 | |
579 | 650 | $constructor = $reflection->getConstructor(); |
580 | 651 | |
581 | - if ($constructor !== null) { |
|
652 | + if ($constructor !== null) |
|
653 | + { |
|
582 | 654 | // Using constructor with resolved arguments |
583 | 655 | $instance = $reflection->newInstanceArgs($this->resolveArguments($constructor, $parameters)); |
584 | - } else { |
|
656 | + } |
|
657 | + else |
|
658 | + { |
|
585 | 659 | // No constructor specified |
586 | 660 | $instance = $reflection->newInstance(); |
587 | 661 | } |
@@ -595,7 +669,8 @@ discard block |
||
595 | 669 | private function checkInjector(ReflectionClass $reflection): bool |
596 | 670 | { |
597 | 671 | $class = $reflection->getName(); |
598 | - if (\array_key_exists($class, $this->injectors)) { |
|
672 | + if (\array_key_exists($class, $this->injectors)) |
|
673 | + { |
|
599 | 674 | return $this->injectors[$class] !== null; |
600 | 675 | } |
601 | 676 | |
@@ -609,7 +684,8 @@ discard block |
||
609 | 684 | } |
610 | 685 | |
611 | 686 | // check interfaces |
612 | - foreach ($this->injectors as $target => $injector) { |
|
687 | + foreach ($this->injectors as $target => $injector) |
|
688 | + { |
|
613 | 689 | if ( |
614 | 690 | \class_exists($target, true) |
615 | 691 | && $reflection->isSubclassOf($target) |
@@ -643,9 +719,11 @@ discard block |
||
643 | 719 | private function registerInstance($instance, array $parameters) |
644 | 720 | { |
645 | 721 | //Declarative singletons (only when class received via direct get) |
646 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
722 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
723 | + { |
|
647 | 724 | $alias = \get_class($instance); |
648 | - if (!isset($this->bindings[$alias])) { |
|
725 | + if (!isset($this->bindings[$alias])) |
|
726 | + { |
|
649 | 727 | $this->bindings[$alias] = $instance; |
650 | 728 | } |
651 | 729 | } |
@@ -663,18 +741,23 @@ discard block |
||
663 | 741 | */ |
664 | 742 | private function evaluateBinding(string $alias, $target, array $parameters, string $context = null) |
665 | 743 | { |
666 | - if (\is_string($target)) { |
|
744 | + if (\is_string($target)) |
|
745 | + { |
|
667 | 746 | // Reference |
668 | 747 | return $this->make($target, $parameters, $context); |
669 | 748 | } |
670 | 749 | |
671 | - if ($target instanceof Autowire) { |
|
750 | + if ($target instanceof Autowire) |
|
751 | + { |
|
672 | 752 | return $target->resolve($this, $parameters); |
673 | 753 | } |
674 | 754 | |
675 | - try { |
|
755 | + try |
|
756 | + { |
|
676 | 757 | return $this->invoke($target, $parameters); |
677 | - } catch (NotCallableException $e) { |
|
758 | + } |
|
759 | + catch (NotCallableException $e) |
|
760 | + { |
|
678 | 761 | throw new ContainerException(\sprintf("Invalid binding for '%s'", $alias), $e->getCode(), $e); |
679 | 762 | } |
680 | 763 | } |
@@ -38,11 +38,11 @@ discard block |
||
38 | 38 | */ |
39 | 39 | public function set(string $name, $rule = null): RuleManager |
40 | 40 | { |
41 | - if (empty($rule)) { |
|
41 | + if (empty($rule)){ |
|
42 | 42 | $rule = $name; |
43 | 43 | } |
44 | 44 | |
45 | - if (!$this->validateRule($rule)) { |
|
45 | + if (!$this->validateRule($rule)){ |
|
46 | 46 | throw new RuleException("Unable to set rule '{$name}', invalid rule body"); |
47 | 47 | } |
48 | 48 | |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | */ |
57 | 57 | public function remove(string $name): RuleManager |
58 | 58 | { |
59 | - if (!$this->has($name)) { |
|
59 | + if (!$this->has($name)){ |
|
60 | 60 | throw new RuleException("Undefined rule '{$name}'"); |
61 | 61 | } |
62 | 62 | |
@@ -70,11 +70,11 @@ discard block |
||
70 | 70 | */ |
71 | 71 | public function has(string $name): bool |
72 | 72 | { |
73 | - if (isset($this->rules[$name])) { |
|
73 | + if (isset($this->rules[$name])){ |
|
74 | 74 | return true; |
75 | 75 | } |
76 | 76 | |
77 | - if (class_exists($name)) { |
|
77 | + if (class_exists($name)){ |
|
78 | 78 | //We are allowing to use class names without direct registration |
79 | 79 | return true; |
80 | 80 | } |
@@ -88,26 +88,26 @@ discard block |
||
88 | 88 | */ |
89 | 89 | public function get(string $name): RuleInterface |
90 | 90 | { |
91 | - if (!$this->has($name)) { |
|
91 | + if (!$this->has($name)){ |
|
92 | 92 | throw new RuleException("Undefined rule '{$name}'"); |
93 | 93 | } |
94 | 94 | |
95 | - if (!isset($this->rules[$name])) { |
|
95 | + if (!isset($this->rules[$name])){ |
|
96 | 96 | //Rule represented as class name |
97 | 97 | $rule = $name; |
98 | - } else { |
|
98 | + }else{ |
|
99 | 99 | $rule = $this->rules[$name]; |
100 | 100 | } |
101 | 101 | |
102 | - if ($rule instanceof RuleInterface) { |
|
102 | + if ($rule instanceof RuleInterface){ |
|
103 | 103 | return $rule; |
104 | 104 | } |
105 | 105 | |
106 | - if (is_string($rule)) { |
|
106 | + if (is_string($rule)){ |
|
107 | 107 | //We are expecting that rule points to |
108 | 108 | $rule = $this->container->get($rule); |
109 | 109 | |
110 | - if (!$rule instanceof RuleInterface) { |
|
110 | + if (!$rule instanceof RuleInterface){ |
|
111 | 111 | throw new RuleException(sprintf( |
112 | 112 | "Rule '%s' must point to RuleInterface, '%s' given", |
113 | 113 | $name, |
@@ -129,18 +129,18 @@ discard block |
||
129 | 129 | */ |
130 | 130 | private function validateRule($rule): bool |
131 | 131 | { |
132 | - if ($rule instanceof Closure || $rule instanceof RuleInterface) { |
|
132 | + if ($rule instanceof Closure || $rule instanceof RuleInterface){ |
|
133 | 133 | return true; |
134 | 134 | } |
135 | 135 | |
136 | - if (is_array($rule)) { |
|
136 | + if (is_array($rule)){ |
|
137 | 137 | return is_callable($rule, true); |
138 | 138 | } |
139 | 139 | |
140 | - if (is_string($rule) && class_exists($rule)) { |
|
141 | - try { |
|
140 | + if (is_string($rule) && class_exists($rule)){ |
|
141 | + try{ |
|
142 | 142 | $reflection = new ReflectionClass($rule); |
143 | - } catch (ReflectionException $e) { |
|
143 | + }catch (ReflectionException $e){ |
|
144 | 144 | return false; |
145 | 145 | } |
146 | 146 |
@@ -38,11 +38,13 @@ discard block |
||
38 | 38 | */ |
39 | 39 | public function set(string $name, $rule = null): RuleManager |
40 | 40 | { |
41 | - if (empty($rule)) { |
|
41 | + if (empty($rule)) |
|
42 | + { |
|
42 | 43 | $rule = $name; |
43 | 44 | } |
44 | 45 | |
45 | - if (!$this->validateRule($rule)) { |
|
46 | + if (!$this->validateRule($rule)) |
|
47 | + { |
|
46 | 48 | throw new RuleException("Unable to set rule '{$name}', invalid rule body"); |
47 | 49 | } |
48 | 50 | |
@@ -56,7 +58,8 @@ discard block |
||
56 | 58 | */ |
57 | 59 | public function remove(string $name): RuleManager |
58 | 60 | { |
59 | - if (!$this->has($name)) { |
|
61 | + if (!$this->has($name)) |
|
62 | + { |
|
60 | 63 | throw new RuleException("Undefined rule '{$name}'"); |
61 | 64 | } |
62 | 65 | |
@@ -70,11 +73,13 @@ discard block |
||
70 | 73 | */ |
71 | 74 | public function has(string $name): bool |
72 | 75 | { |
73 | - if (isset($this->rules[$name])) { |
|
76 | + if (isset($this->rules[$name])) |
|
77 | + { |
|
74 | 78 | return true; |
75 | 79 | } |
76 | 80 | |
77 | - if (class_exists($name)) { |
|
81 | + if (class_exists($name)) |
|
82 | + { |
|
78 | 83 | //We are allowing to use class names without direct registration |
79 | 84 | return true; |
80 | 85 | } |
@@ -88,26 +93,33 @@ discard block |
||
88 | 93 | */ |
89 | 94 | public function get(string $name): RuleInterface |
90 | 95 | { |
91 | - if (!$this->has($name)) { |
|
96 | + if (!$this->has($name)) |
|
97 | + { |
|
92 | 98 | throw new RuleException("Undefined rule '{$name}'"); |
93 | 99 | } |
94 | 100 | |
95 | - if (!isset($this->rules[$name])) { |
|
101 | + if (!isset($this->rules[$name])) |
|
102 | + { |
|
96 | 103 | //Rule represented as class name |
97 | 104 | $rule = $name; |
98 | - } else { |
|
105 | + } |
|
106 | + else |
|
107 | + { |
|
99 | 108 | $rule = $this->rules[$name]; |
100 | 109 | } |
101 | 110 | |
102 | - if ($rule instanceof RuleInterface) { |
|
111 | + if ($rule instanceof RuleInterface) |
|
112 | + { |
|
103 | 113 | return $rule; |
104 | 114 | } |
105 | 115 | |
106 | - if (is_string($rule)) { |
|
116 | + if (is_string($rule)) |
|
117 | + { |
|
107 | 118 | //We are expecting that rule points to |
108 | 119 | $rule = $this->container->get($rule); |
109 | 120 | |
110 | - if (!$rule instanceof RuleInterface) { |
|
121 | + if (!$rule instanceof RuleInterface) |
|
122 | + { |
|
111 | 123 | throw new RuleException(sprintf( |
112 | 124 | "Rule '%s' must point to RuleInterface, '%s' given", |
113 | 125 | $name, |
@@ -129,18 +141,24 @@ discard block |
||
129 | 141 | */ |
130 | 142 | private function validateRule($rule): bool |
131 | 143 | { |
132 | - if ($rule instanceof Closure || $rule instanceof RuleInterface) { |
|
144 | + if ($rule instanceof Closure || $rule instanceof RuleInterface) |
|
145 | + { |
|
133 | 146 | return true; |
134 | 147 | } |
135 | 148 | |
136 | - if (is_array($rule)) { |
|
149 | + if (is_array($rule)) |
|
150 | + { |
|
137 | 151 | return is_callable($rule, true); |
138 | 152 | } |
139 | 153 | |
140 | - if (is_string($rule) && class_exists($rule)) { |
|
141 | - try { |
|
154 | + if (is_string($rule) && class_exists($rule)) |
|
155 | + { |
|
156 | + try |
|
157 | + { |
|
142 | 158 | $reflection = new ReflectionClass($rule); |
143 | - } catch (ReflectionException $e) { |
|
159 | + } |
|
160 | + catch (ReflectionException $e) |
|
161 | + { |
|
144 | 162 | return false; |
145 | 163 | } |
146 | 164 |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | public function getGuard(): GuardInterface |
28 | 28 | { |
29 | 29 | $container = ContainerScope::getContainer(); |
30 | - if (empty($container) || !$container->has(GuardInterface::class)) { |
|
30 | + if (empty($container) || !$container->has(GuardInterface::class)){ |
|
31 | 31 | throw new ScopeException( |
32 | 32 | 'Unable to get `GuardInterface`, binding is missing or container scope is not set' |
33 | 33 | ); |
@@ -51,9 +51,9 @@ discard block |
||
51 | 51 | */ |
52 | 52 | protected function resolvePermission(string $permission): string |
53 | 53 | { |
54 | - if (defined('static::GUARD_NAMESPACE')) { |
|
54 | + if (defined('static::GUARD_NAMESPACE')){ |
|
55 | 55 | // Yay! Isolation |
56 | - $permission = constant(static::class . '::' . 'GUARD_NAMESPACE') . '.' . $permission; |
|
56 | + $permission = constant(static::class.'::'.'GUARD_NAMESPACE').'.'.$permission; |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | return $permission; |
@@ -27,7 +27,8 @@ discard block |
||
27 | 27 | public function getGuard(): GuardInterface |
28 | 28 | { |
29 | 29 | $container = ContainerScope::getContainer(); |
30 | - if (empty($container) || !$container->has(GuardInterface::class)) { |
|
30 | + if (empty($container) || !$container->has(GuardInterface::class)) |
|
31 | + { |
|
31 | 32 | throw new ScopeException( |
32 | 33 | 'Unable to get `GuardInterface`, binding is missing or container scope is not set' |
33 | 34 | ); |
@@ -51,7 +52,8 @@ discard block |
||
51 | 52 | */ |
52 | 53 | protected function resolvePermission(string $permission): string |
53 | 54 | { |
54 | - if (defined('static::GUARD_NAMESPACE')) { |
|
55 | + if (defined('static::GUARD_NAMESPACE')) |
|
56 | + { |
|
55 | 57 | // Yay! Isolation |
56 | 58 | $permission = constant(static::class . '::' . 'GUARD_NAMESPACE') . '.' . $permission; |
57 | 59 | } |
@@ -63,20 +63,20 @@ |
||
63 | 63 | $parameters = compact('actor', 'permission', 'context') + $context; |
64 | 64 | |
65 | 65 | //Mounting aliases |
66 | - foreach (static::ALIASES as $target => $alias) { |
|
66 | + foreach (static::ALIASES as $target => $alias){ |
|
67 | 67 | $parameters[$target] = $parameters[$alias]; |
68 | 68 | } |
69 | 69 | |
70 | - try { |
|
70 | + try{ |
|
71 | 71 | $method = new ReflectionMethod($this, static::CHECK_METHOD); |
72 | 72 | $method->setAccessible(true); |
73 | - } catch (ReflectionException $e) { |
|
73 | + }catch (ReflectionException $e){ |
|
74 | 74 | throw new RuleException($e->getMessage(), $e->getCode(), $e); |
75 | 75 | } |
76 | 76 | |
77 | - try { |
|
77 | + try{ |
|
78 | 78 | return $method->invokeArgs($this, $this->resolver->resolveArguments($method, $parameters)); |
79 | - } catch (Throwable $e) { |
|
79 | + }catch (Throwable $e){ |
|
80 | 80 | throw new RuleException(sprintf('[%s] %s', get_class($this), $e->getMessage()), $e->getCode(), $e); |
81 | 81 | } |
82 | 82 | } |
@@ -63,20 +63,27 @@ |
||
63 | 63 | $parameters = compact('actor', 'permission', 'context') + $context; |
64 | 64 | |
65 | 65 | //Mounting aliases |
66 | - foreach (static::ALIASES as $target => $alias) { |
|
66 | + foreach (static::ALIASES as $target => $alias) |
|
67 | + { |
|
67 | 68 | $parameters[$target] = $parameters[$alias]; |
68 | 69 | } |
69 | 70 | |
70 | - try { |
|
71 | + try |
|
72 | + { |
|
71 | 73 | $method = new ReflectionMethod($this, static::CHECK_METHOD); |
72 | 74 | $method->setAccessible(true); |
73 | - } catch (ReflectionException $e) { |
|
75 | + } |
|
76 | + catch (ReflectionException $e) |
|
77 | + { |
|
74 | 78 | throw new RuleException($e->getMessage(), $e->getCode(), $e); |
75 | 79 | } |
76 | 80 | |
77 | - try { |
|
81 | + try |
|
82 | + { |
|
78 | 83 | return $method->invokeArgs($this, $this->resolver->resolveArguments($method, $parameters)); |
79 | - } catch (Throwable $e) { |
|
84 | + } |
|
85 | + catch (Throwable $e) |
|
86 | + { |
|
80 | 87 | throw new RuleException(sprintf('[%s] %s', get_class($this), $e->getMessage()), $e->getCode(), $e); |
81 | 88 | } |
82 | 89 | } |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | { |
74 | 74 | $name ??= $this->default; |
75 | 75 | |
76 | - if (!isset($this->buckets[$name])) { |
|
76 | + if (!isset($this->buckets[$name])){ |
|
77 | 77 | throw new InvalidArgumentException(\sprintf(self::ERROR_NOT_FOUND, $name)); |
78 | 78 | } |
79 | 79 | |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | */ |
96 | 96 | public function add(string $name, BucketInterface $storage, bool $overwrite = false): void |
97 | 97 | { |
98 | - if ($overwrite === false && isset($this->buckets[$name])) { |
|
98 | + if ($overwrite === false && isset($this->buckets[$name])){ |
|
99 | 99 | throw new \InvalidArgumentException(\sprintf(self::ERROR_REDEFINITION, $name)); |
100 | 100 | } |
101 | 101 | |
@@ -128,22 +128,22 @@ discard block |
||
128 | 128 | $uri = $this->uriToString($uri); |
129 | 129 | $result = \parse_url($uri); |
130 | 130 | |
131 | - if ($result === false) { |
|
131 | + if ($result === false){ |
|
132 | 132 | $message = 'URI argument must be a valid URI in "[STORAGE]://[PATH_TO_FILE]" format, but `%s` given'; |
133 | 133 | throw new InvalidArgumentException(\sprintf($message, $uri)); |
134 | 134 | } |
135 | 135 | |
136 | - if (!isset($result['scheme'])) { |
|
136 | + if (!isset($result['scheme'])){ |
|
137 | 137 | $result['scheme'] = $withScheme ? $this->default : null; |
138 | 138 | } |
139 | 139 | |
140 | - if (!isset($result['host'])) { |
|
140 | + if (!isset($result['host'])){ |
|
141 | 141 | $result['host'] = ''; |
142 | 142 | } |
143 | 143 | |
144 | 144 | return [ |
145 | 145 | $result['scheme'] ?? null, |
146 | - $result['host'] . \rtrim($result['path'] ?? '', '/'), |
|
146 | + $result['host'].\rtrim($result['path'] ?? '', '/'), |
|
147 | 147 | ]; |
148 | 148 | } |
149 | 149 | |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | */ |
154 | 154 | private function uriToString($uri): string |
155 | 155 | { |
156 | - switch (true) { |
|
156 | + switch (true){ |
|
157 | 157 | case $uri instanceof UriInterface: |
158 | 158 | case $uri instanceof Stringable: |
159 | 159 | case \is_object($uri) && \method_exists($uri, '__toString'): |
@@ -73,7 +73,8 @@ discard block |
||
73 | 73 | { |
74 | 74 | $name ??= $this->default; |
75 | 75 | |
76 | - if (!isset($this->buckets[$name])) { |
|
76 | + if (!isset($this->buckets[$name])) |
|
77 | + { |
|
77 | 78 | throw new InvalidArgumentException(\sprintf(self::ERROR_NOT_FOUND, $name)); |
78 | 79 | } |
79 | 80 | |
@@ -95,7 +96,8 @@ discard block |
||
95 | 96 | */ |
96 | 97 | public function add(string $name, BucketInterface $storage, bool $overwrite = false): void |
97 | 98 | { |
98 | - if ($overwrite === false && isset($this->buckets[$name])) { |
|
99 | + if ($overwrite === false && isset($this->buckets[$name])) |
|
100 | + { |
|
99 | 101 | throw new \InvalidArgumentException(\sprintf(self::ERROR_REDEFINITION, $name)); |
100 | 102 | } |
101 | 103 | |
@@ -128,16 +130,19 @@ discard block |
||
128 | 130 | $uri = $this->uriToString($uri); |
129 | 131 | $result = \parse_url($uri); |
130 | 132 | |
131 | - if ($result === false) { |
|
133 | + if ($result === false) |
|
134 | + { |
|
132 | 135 | $message = 'URI argument must be a valid URI in "[STORAGE]://[PATH_TO_FILE]" format, but `%s` given'; |
133 | 136 | throw new InvalidArgumentException(\sprintf($message, $uri)); |
134 | 137 | } |
135 | 138 | |
136 | - if (!isset($result['scheme'])) { |
|
139 | + if (!isset($result['scheme'])) |
|
140 | + { |
|
137 | 141 | $result['scheme'] = $withScheme ? $this->default : null; |
138 | 142 | } |
139 | 143 | |
140 | - if (!isset($result['host'])) { |
|
144 | + if (!isset($result['host'])) |
|
145 | + { |
|
141 | 146 | $result['host'] = ''; |
142 | 147 | } |
143 | 148 | |
@@ -153,7 +158,8 @@ discard block |
||
153 | 158 | */ |
154 | 159 | private function uriToString($uri): string |
155 | 160 | { |
156 | - switch (true) { |
|
161 | + switch (true) |
|
162 | + { |
|
157 | 163 | case $uri instanceof UriInterface: |
158 | 164 | case $uri instanceof Stringable: |
159 | 165 | case \is_object($uri) && \method_exists($uri, '__toString'): |
@@ -32,7 +32,7 @@ |
||
32 | 32 | { |
33 | 33 | $resolver = $this->getResolver(); |
34 | 34 | |
35 | - if ($resolver === null) { |
|
35 | + if ($resolver === null){ |
|
36 | 36 | throw new LogicException('Can not generate public url: File not accessible by HTTP'); |
37 | 37 | } |
38 | 38 |
@@ -32,7 +32,8 @@ |
||
32 | 32 | { |
33 | 33 | $resolver = $this->getResolver(); |
34 | 34 | |
35 | - if ($resolver === null) { |
|
35 | + if ($resolver === null) |
|
36 | + { |
|
36 | 37 | throw new LogicException('Can not generate public url: File not accessible by HTTP'); |
37 | 38 | } |
38 | 39 |