@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | string $postfix = 'Controller', |
| 41 | 41 | int $options = 0, |
| 42 | 42 | string $defaultAction = 'index' |
| 43 | - ) { |
|
| 43 | + ){ |
|
| 44 | 44 | $this->namespace = rtrim($namespace, '\\'); |
| 45 | 45 | $this->postfix = ucfirst($postfix); |
| 46 | 46 | |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | */ |
| 59 | 59 | protected function resolveController(array $matches): string |
| 60 | 60 | { |
| 61 | - if (preg_match('/[^a-z_0-9\-]/i', $matches['controller'])) { |
|
| 61 | + if (preg_match('/[^a-z_0-9\-]/i', $matches['controller'])){ |
|
| 62 | 62 | throw new TargetException('Invalid namespace target, controller name not allowed.'); |
| 63 | 63 | } |
| 64 | 64 | |
@@ -58,7 +58,8 @@ |
||
| 58 | 58 | */ |
| 59 | 59 | protected function resolveController(array $matches): string |
| 60 | 60 | { |
| 61 | - if (preg_match('/[^a-z_0-9\-]/i', $matches['controller'])) { |
|
| 61 | + if (preg_match('/[^a-z_0-9\-]/i', $matches['controller'])) |
|
| 62 | + { |
|
| 62 | 63 | throw new TargetException('Invalid namespace target, controller name not allowed.'); |
| 63 | 64 | } |
| 64 | 65 | |
@@ -105,11 +105,11 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | protected function coreHandler(ContainerInterface $container): CoreHandler |
| 107 | 107 | { |
| 108 | - if ($this->handler !== null) { |
|
| 108 | + if ($this->handler !== null){ |
|
| 109 | 109 | return $this->handler; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | - try { |
|
| 112 | + try{ |
|
| 113 | 113 | // construct on demand |
| 114 | 114 | $this->handler = new CoreHandler( |
| 115 | 115 | $this->core ?? $container->get(CoreInterface::class), |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | ); |
| 119 | 119 | |
| 120 | 120 | return $this->handler; |
| 121 | - } catch (ContainerExceptionInterface $e) { |
|
| 121 | + }catch (ContainerExceptionInterface $e){ |
|
| 122 | 122 | throw new TargetException($e->getMessage(), $e->getCode(), $e); |
| 123 | 123 | } |
| 124 | 124 | } |
@@ -105,11 +105,13 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | protected function coreHandler(ContainerInterface $container): CoreHandler |
| 107 | 107 | { |
| 108 | - if ($this->handler !== null) { |
|
| 108 | + if ($this->handler !== null) |
|
| 109 | + { |
|
| 109 | 110 | return $this->handler; |
| 110 | 111 | } |
| 111 | 112 | |
| 112 | - try { |
|
| 113 | + try |
|
| 114 | + { |
|
| 113 | 115 | // construct on demand |
| 114 | 116 | $this->handler = new CoreHandler( |
| 115 | 117 | $this->core ?? $container->get(CoreInterface::class), |
@@ -118,7 +120,9 @@ discard block |
||
| 118 | 120 | ); |
| 119 | 121 | |
| 120 | 122 | return $this->handler; |
| 121 | - } catch (ContainerExceptionInterface $e) { |
|
| 123 | + } |
|
| 124 | + catch (ContainerExceptionInterface $e) |
|
| 125 | + { |
|
| 122 | 126 | throw new TargetException($e->getMessage(), $e->getCode(), $e); |
| 123 | 127 | } |
| 124 | 128 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | public function __construct(string $basePath, UriHandler $uriHandler, ContainerInterface $container) |
| 58 | 58 | { |
| 59 | - $this->basePath = '/' . ltrim($basePath, '/'); |
|
| 59 | + $this->basePath = '/'.ltrim($basePath, '/'); |
|
| 60 | 60 | $this->uriHandler = $uriHandler; |
| 61 | 61 | $this->container = $container; |
| 62 | 62 | } |
@@ -69,13 +69,13 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public function handle(ServerRequestInterface $request): ResponseInterface |
| 71 | 71 | { |
| 72 | - try { |
|
| 72 | + try{ |
|
| 73 | 73 | $route = $this->matchRoute($request, $routeName); |
| 74 | - } catch (RouteException $e) { |
|
| 74 | + }catch (RouteException $e){ |
|
| 75 | 75 | throw new RouterException('Invalid route definition', $e->getCode(), $e); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - if ($route === null) { |
|
| 78 | + if ($route === null){ |
|
| 79 | 79 | throw new RouteNotFoundException($request->getUri()); |
| 80 | 80 | } |
| 81 | 81 | |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | */ |
| 121 | 121 | public function getRoute(string $name): RouteInterface |
| 122 | 122 | { |
| 123 | - if (isset($this->routes[$name])) { |
|
| 123 | + if (isset($this->routes[$name])){ |
|
| 124 | 124 | return $this->routes[$name]; |
| 125 | 125 | } |
| 126 | 126 | |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | */ |
| 133 | 133 | public function getRoutes(): array |
| 134 | 134 | { |
| 135 | - if (!empty($this->default)) { |
|
| 135 | + if (!empty($this->default)){ |
|
| 136 | 136 | return $this->routes + [null => $this->default]; |
| 137 | 137 | } |
| 138 | 138 | |
@@ -144,9 +144,9 @@ discard block |
||
| 144 | 144 | */ |
| 145 | 145 | public function uri(string $route, $parameters = []): UriInterface |
| 146 | 146 | { |
| 147 | - try { |
|
| 147 | + try{ |
|
| 148 | 148 | return $this->getRoute($route)->uri($parameters); |
| 149 | - } catch (UndefinedRouteException $e) { |
|
| 149 | + }catch (UndefinedRouteException $e){ |
|
| 150 | 150 | //In some cases route name can be provided as controller:action pair, we can try to |
| 151 | 151 | //generate such route automatically based on our default/fallback route |
| 152 | 152 | return $this->castRoute($route)->uri($parameters); |
@@ -161,17 +161,17 @@ discard block |
||
| 161 | 161 | */ |
| 162 | 162 | protected function matchRoute(ServerRequestInterface $request, string &$routeName = null): ?RouteInterface |
| 163 | 163 | { |
| 164 | - foreach ($this->routes as $name => $route) { |
|
| 164 | + foreach ($this->routes as $name => $route){ |
|
| 165 | 165 | // Matched route will return new route instance with matched parameters |
| 166 | 166 | $matched = $route->match($request); |
| 167 | 167 | |
| 168 | - if ($matched !== null) { |
|
| 168 | + if ($matched !== null){ |
|
| 169 | 169 | $routeName = $name; |
| 170 | 170 | return $matched; |
| 171 | 171 | } |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - if ($this->default !== null) { |
|
| 174 | + if ($this->default !== null){ |
|
| 175 | 175 | return $this->default->match($request); |
| 176 | 176 | } |
| 177 | 177 | |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | */ |
| 188 | 188 | protected function configure(RouteInterface $route): RouteInterface |
| 189 | 189 | { |
| 190 | - if ($route instanceof ContainerizedInterface && !$route->hasContainer()) { |
|
| 190 | + if ($route instanceof ContainerizedInterface && !$route->hasContainer()){ |
|
| 191 | 191 | // isolating route in a given container |
| 192 | 192 | $route = $route->withContainer($this->container); |
| 193 | 193 | } |
@@ -214,17 +214,17 @@ discard block |
||
| 214 | 214 | $route, |
| 215 | 215 | $matches |
| 216 | 216 | ) |
| 217 | - ) { |
|
| 217 | + ){ |
|
| 218 | 218 | throw new UndefinedRouteException( |
| 219 | 219 | "Unable to locate route or use default route with 'name/controller:action' pattern" |
| 220 | 220 | ); |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - if (!empty($matches['name'])) { |
|
| 223 | + if (!empty($matches['name'])){ |
|
| 224 | 224 | $routeObject = $this->getRoute($matches['name']); |
| 225 | - } elseif ($this->default !== null) { |
|
| 225 | + } elseif ($this->default !== null){ |
|
| 226 | 226 | $routeObject = $this->default; |
| 227 | - } else { |
|
| 227 | + }else{ |
|
| 228 | 228 | throw new UndefinedRouteException("Unable to locate route candidate for `{$route}`"); |
| 229 | 229 | } |
| 230 | 230 | |
@@ -69,13 +69,17 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public function handle(ServerRequestInterface $request): ResponseInterface |
| 71 | 71 | { |
| 72 | - try { |
|
| 72 | + try |
|
| 73 | + { |
|
| 73 | 74 | $route = $this->matchRoute($request, $routeName); |
| 74 | - } catch (RouteException $e) { |
|
| 75 | + } |
|
| 76 | + catch (RouteException $e) |
|
| 77 | + { |
|
| 75 | 78 | throw new RouterException('Invalid route definition', $e->getCode(), $e); |
| 76 | 79 | } |
| 77 | 80 | |
| 78 | - if ($route === null) { |
|
| 81 | + if ($route === null) |
|
| 82 | + { |
|
| 79 | 83 | throw new RouteNotFoundException($request->getUri()); |
| 80 | 84 | } |
| 81 | 85 | |
@@ -120,7 +124,8 @@ discard block |
||
| 120 | 124 | */ |
| 121 | 125 | public function getRoute(string $name): RouteInterface |
| 122 | 126 | { |
| 123 | - if (isset($this->routes[$name])) { |
|
| 127 | + if (isset($this->routes[$name])) |
|
| 128 | + { |
|
| 124 | 129 | return $this->routes[$name]; |
| 125 | 130 | } |
| 126 | 131 | |
@@ -132,7 +137,8 @@ discard block |
||
| 132 | 137 | */ |
| 133 | 138 | public function getRoutes(): array |
| 134 | 139 | { |
| 135 | - if (!empty($this->default)) { |
|
| 140 | + if (!empty($this->default)) |
|
| 141 | + { |
|
| 136 | 142 | return $this->routes + [null => $this->default]; |
| 137 | 143 | } |
| 138 | 144 | |
@@ -144,9 +150,12 @@ discard block |
||
| 144 | 150 | */ |
| 145 | 151 | public function uri(string $route, $parameters = []): UriInterface |
| 146 | 152 | { |
| 147 | - try { |
|
| 153 | + try |
|
| 154 | + { |
|
| 148 | 155 | return $this->getRoute($route)->uri($parameters); |
| 149 | - } catch (UndefinedRouteException $e) { |
|
| 156 | + } |
|
| 157 | + catch (UndefinedRouteException $e) |
|
| 158 | + { |
|
| 150 | 159 | //In some cases route name can be provided as controller:action pair, we can try to |
| 151 | 160 | //generate such route automatically based on our default/fallback route |
| 152 | 161 | return $this->castRoute($route)->uri($parameters); |
@@ -161,17 +170,20 @@ discard block |
||
| 161 | 170 | */ |
| 162 | 171 | protected function matchRoute(ServerRequestInterface $request, string &$routeName = null): ?RouteInterface |
| 163 | 172 | { |
| 164 | - foreach ($this->routes as $name => $route) { |
|
| 173 | + foreach ($this->routes as $name => $route) |
|
| 174 | + { |
|
| 165 | 175 | // Matched route will return new route instance with matched parameters |
| 166 | 176 | $matched = $route->match($request); |
| 167 | 177 | |
| 168 | - if ($matched !== null) { |
|
| 178 | + if ($matched !== null) |
|
| 179 | + { |
|
| 169 | 180 | $routeName = $name; |
| 170 | 181 | return $matched; |
| 171 | 182 | } |
| 172 | 183 | } |
| 173 | 184 | |
| 174 | - if ($this->default !== null) { |
|
| 185 | + if ($this->default !== null) |
|
| 186 | + { |
|
| 175 | 187 | return $this->default->match($request); |
| 176 | 188 | } |
| 177 | 189 | |
@@ -187,7 +199,8 @@ discard block |
||
| 187 | 199 | */ |
| 188 | 200 | protected function configure(RouteInterface $route): RouteInterface |
| 189 | 201 | { |
| 190 | - if ($route instanceof ContainerizedInterface && !$route->hasContainer()) { |
|
| 202 | + if ($route instanceof ContainerizedInterface && !$route->hasContainer()) |
|
| 203 | + { |
|
| 191 | 204 | // isolating route in a given container |
| 192 | 205 | $route = $route->withContainer($this->container); |
| 193 | 206 | } |
@@ -220,11 +233,16 @@ discard block |
||
| 220 | 233 | ); |
| 221 | 234 | } |
| 222 | 235 | |
| 223 | - if (!empty($matches['name'])) { |
|
| 236 | + if (!empty($matches['name'])) |
|
| 237 | + { |
|
| 224 | 238 | $routeObject = $this->getRoute($matches['name']); |
| 225 | - } elseif ($this->default !== null) { |
|
| 239 | + } |
|
| 240 | + elseif ($this->default !== null) |
|
| 241 | + { |
|
| 226 | 242 | $routeObject = $this->default; |
| 227 | - } else { |
|
| 243 | + } |
|
| 244 | + else |
|
| 245 | + { |
|
| 228 | 246 | throw new UndefinedRouteException("Unable to locate route candidate for `{$route}`"); |
| 229 | 247 | } |
| 230 | 248 | |
@@ -46,15 +46,15 @@ discard block |
||
| 46 | 46 | $route = clone $this; |
| 47 | 47 | |
| 48 | 48 | // array fallback |
| 49 | - if (count($middleware) === 1 && is_array($middleware[0])) { |
|
| 49 | + if (count($middleware) === 1 && is_array($middleware[0])){ |
|
| 50 | 50 | $middleware = $middleware[0]; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - foreach ($middleware as $item) { |
|
| 54 | - if (!is_string($item) && !$item instanceof MiddlewareInterface) { |
|
| 55 | - if (is_object($item)) { |
|
| 53 | + foreach ($middleware as $item){ |
|
| 54 | + if (!is_string($item) && !$item instanceof MiddlewareInterface){ |
|
| 55 | + if (is_object($item)){ |
|
| 56 | 56 | $name = get_class($item); |
| 57 | - } else { |
|
| 57 | + }else{ |
|
| 58 | 58 | $name = gettype($item); |
| 59 | 59 | } |
| 60 | 60 | |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | $route->middleware[] = $item; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - if ($route->pipeline !== null) { |
|
| 67 | + if ($route->pipeline !== null){ |
|
| 68 | 68 | $route->pipeline = $route->makePipeline(); |
| 69 | 69 | } |
| 70 | 70 | |
@@ -81,22 +81,22 @@ discard block |
||
| 81 | 81 | protected function makePipeline(): Pipeline |
| 82 | 82 | { |
| 83 | 83 | // pre-aggregated |
| 84 | - if (count($this->middleware) === 1 && $this->middleware[0] instanceof Pipeline) { |
|
| 84 | + if (count($this->middleware) === 1 && $this->middleware[0] instanceof Pipeline){ |
|
| 85 | 85 | return $this->middleware[0]; |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - try { |
|
| 88 | + try{ |
|
| 89 | 89 | $pipeline = $this->container->get(Pipeline::class); |
| 90 | 90 | |
| 91 | - foreach ($this->middleware as $middleware) { |
|
| 92 | - if ($middleware instanceof MiddlewareInterface) { |
|
| 91 | + foreach ($this->middleware as $middleware){ |
|
| 92 | + if ($middleware instanceof MiddlewareInterface){ |
|
| 93 | 93 | $pipeline->pushMiddleware($middleware); |
| 94 | - } else { |
|
| 94 | + }else{ |
|
| 95 | 95 | // dynamically resolved |
| 96 | 96 | $pipeline->pushMiddleware($this->container->get($middleware)); |
| 97 | 97 | } |
| 98 | 98 | } |
| 99 | - } catch (ContainerExceptionInterface $e) { |
|
| 99 | + }catch (ContainerExceptionInterface $e){ |
|
| 100 | 100 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
| 101 | 101 | } |
| 102 | 102 | |
@@ -46,15 +46,21 @@ discard block |
||
| 46 | 46 | $route = clone $this; |
| 47 | 47 | |
| 48 | 48 | // array fallback |
| 49 | - if (count($middleware) === 1 && is_array($middleware[0])) { |
|
| 49 | + if (count($middleware) === 1 && is_array($middleware[0])) |
|
| 50 | + { |
|
| 50 | 51 | $middleware = $middleware[0]; |
| 51 | 52 | } |
| 52 | 53 | |
| 53 | - foreach ($middleware as $item) { |
|
| 54 | - if (!is_string($item) && !$item instanceof MiddlewareInterface) { |
|
| 55 | - if (is_object($item)) { |
|
| 54 | + foreach ($middleware as $item) |
|
| 55 | + { |
|
| 56 | + if (!is_string($item) && !$item instanceof MiddlewareInterface) |
|
| 57 | + { |
|
| 58 | + if (is_object($item)) |
|
| 59 | + { |
|
| 56 | 60 | $name = get_class($item); |
| 57 | - } else { |
|
| 61 | + } |
|
| 62 | + else |
|
| 63 | + { |
|
| 58 | 64 | $name = gettype($item); |
| 59 | 65 | } |
| 60 | 66 | |
@@ -64,7 +70,8 @@ discard block |
||
| 64 | 70 | $route->middleware[] = $item; |
| 65 | 71 | } |
| 66 | 72 | |
| 67 | - if ($route->pipeline !== null) { |
|
| 73 | + if ($route->pipeline !== null) |
|
| 74 | + { |
|
| 68 | 75 | $route->pipeline = $route->makePipeline(); |
| 69 | 76 | } |
| 70 | 77 | |
@@ -81,22 +88,30 @@ discard block |
||
| 81 | 88 | protected function makePipeline(): Pipeline |
| 82 | 89 | { |
| 83 | 90 | // pre-aggregated |
| 84 | - if (count($this->middleware) === 1 && $this->middleware[0] instanceof Pipeline) { |
|
| 91 | + if (count($this->middleware) === 1 && $this->middleware[0] instanceof Pipeline) |
|
| 92 | + { |
|
| 85 | 93 | return $this->middleware[0]; |
| 86 | 94 | } |
| 87 | 95 | |
| 88 | - try { |
|
| 96 | + try |
|
| 97 | + { |
|
| 89 | 98 | $pipeline = $this->container->get(Pipeline::class); |
| 90 | 99 | |
| 91 | - foreach ($this->middleware as $middleware) { |
|
| 92 | - if ($middleware instanceof MiddlewareInterface) { |
|
| 100 | + foreach ($this->middleware as $middleware) |
|
| 101 | + { |
|
| 102 | + if ($middleware instanceof MiddlewareInterface) |
|
| 103 | + { |
|
| 93 | 104 | $pipeline->pushMiddleware($middleware); |
| 94 | - } else { |
|
| 105 | + } |
|
| 106 | + else |
|
| 107 | + { |
|
| 95 | 108 | // dynamically resolved |
| 96 | 109 | $pipeline->pushMiddleware($this->container->get($middleware)); |
| 97 | 110 | } |
| 98 | 111 | } |
| 99 | - } catch (ContainerExceptionInterface $e) { |
|
| 112 | + } |
|
| 113 | + catch (ContainerExceptionInterface $e) |
|
| 114 | + { |
|
| 100 | 115 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
| 101 | 116 | } |
| 102 | 117 | |
@@ -30,9 +30,9 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | public function withVerbs(string ...$verbs): RouteInterface |
| 32 | 32 | { |
| 33 | - foreach ($verbs as &$verb) { |
|
| 33 | + foreach ($verbs as &$verb){ |
|
| 34 | 34 | $verb = strtoupper($verb); |
| 35 | - if (!in_array($verb, RouteInterface::VERBS, true)) { |
|
| 35 | + if (!in_array($verb, RouteInterface::VERBS, true)){ |
|
| 36 | 36 | throw new RouteException("Invalid HTTP verb `{$verb}`"); |
| 37 | 37 | } |
| 38 | 38 | |
@@ -30,9 +30,11 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | public function withVerbs(string ...$verbs): RouteInterface |
| 32 | 32 | { |
| 33 | - foreach ($verbs as &$verb) { |
|
| 33 | + foreach ($verbs as &$verb) |
|
| 34 | + { |
|
| 34 | 35 | $verb = strtoupper($verb); |
| 35 | - if (!in_array($verb, RouteInterface::VERBS, true)) { |
|
| 36 | + if (!in_array($verb, RouteInterface::VERBS, true)) |
|
| 37 | + { |
|
| 36 | 38 | throw new RouteException("Invalid HTTP verb `{$verb}`"); |
| 37 | 39 | } |
| 38 | 40 | |
@@ -65,12 +65,12 @@ |
||
| 65 | 65 | */ |
| 66 | 66 | public function match(Request $request): ?RouteInterface |
| 67 | 67 | { |
| 68 | - if (!in_array(strtoupper($request->getMethod()), $this->getVerbs(), true)) { |
|
| 68 | + if (!in_array(strtoupper($request->getMethod()), $this->getVerbs(), true)){ |
|
| 69 | 69 | return null; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | $matches = $this->uriHandler->match($request->getUri(), $this->defaults); |
| 73 | - if ($matches === null) { |
|
| 73 | + if ($matches === null){ |
|
| 74 | 74 | return null; |
| 75 | 75 | } |
| 76 | 76 | |
@@ -65,12 +65,14 @@ |
||
| 65 | 65 | */ |
| 66 | 66 | public function match(Request $request): ?RouteInterface |
| 67 | 67 | { |
| 68 | - if (!in_array(strtoupper($request->getMethod()), $this->getVerbs(), true)) { |
|
| 68 | + if (!in_array(strtoupper($request->getMethod()), $this->getVerbs(), true)) |
|
| 69 | + { |
|
| 69 | 70 | return null; |
| 70 | 71 | } |
| 71 | 72 | |
| 72 | 73 | $matches = $this->uriHandler->match($request->getUri(), $this->defaults); |
| 73 | - if ($matches === null) { |
|
| 74 | + if ($matches === null) |
|
| 75 | + { |
|
| 74 | 76 | return null; |
| 75 | 77 | } |
| 76 | 78 | |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | CoreInterface $core, |
| 61 | 61 | ScopeInterface $scope, |
| 62 | 62 | ResponseFactoryInterface $responseFactory |
| 63 | - ) { |
|
| 63 | + ){ |
|
| 64 | 64 | $this->core = $core; |
| 65 | 65 | $this->scope = $scope; |
| 66 | 66 | $this->responseFactory = $responseFactory; |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | public function handle(Request $request): Response |
| 105 | 105 | { |
| 106 | - if ($this->controller === null) { |
|
| 106 | + if ($this->controller === null){ |
|
| 107 | 107 | throw new HandlerException('Controller and action pair is not set'); |
| 108 | 108 | } |
| 109 | 109 | |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | $output = $result = null; |
| 114 | 114 | |
| 115 | 115 | $response = $this->responseFactory->createResponse(200); |
| 116 | - try { |
|
| 116 | + try{ |
|
| 117 | 117 | // run the core withing PSR-7 Request/Response scope |
| 118 | 118 | $result = $this->scope->runScope( |
| 119 | 119 | [ |
@@ -128,22 +128,22 @@ discard block |
||
| 128 | 128 | ); |
| 129 | 129 | } |
| 130 | 130 | ); |
| 131 | - } catch (ControllerException $e) { |
|
| 131 | + }catch (ControllerException $e){ |
|
| 132 | 132 | ob_get_clean(); |
| 133 | 133 | throw $this->mapException($e); |
| 134 | - } catch (\Throwable $e) { |
|
| 134 | + }catch (\Throwable $e){ |
|
| 135 | 135 | ob_get_clean(); |
| 136 | 136 | throw $e; |
| 137 | - } finally { |
|
| 138 | - while (ob_get_level() > $outputLevel + 1) { |
|
| 139 | - $output = ob_get_clean() . $output; |
|
| 137 | + }finally{ |
|
| 138 | + while (ob_get_level() > $outputLevel + 1){ |
|
| 139 | + $output = ob_get_clean().$output; |
|
| 140 | 140 | } |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | return $this->wrapResponse( |
| 144 | 144 | $response, |
| 145 | 145 | $result, |
| 146 | - ob_get_clean() . $output |
|
| 146 | + ob_get_clean().$output |
|
| 147 | 147 | ); |
| 148 | 148 | } |
| 149 | 149 | |
@@ -153,8 +153,8 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | private function getAction(Request $request): string |
| 155 | 155 | { |
| 156 | - if ($this->verbActions) { |
|
| 157 | - return strtolower($request->getMethod()) . ucfirst($this->action); |
|
| 156 | + if ($this->verbActions){ |
|
| 157 | + return strtolower($request->getMethod()).ucfirst($this->action); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | return $this->action; |
@@ -170,17 +170,17 @@ discard block |
||
| 170 | 170 | */ |
| 171 | 171 | private function wrapResponse(Response $response, $result = null, string $output = ''): Response |
| 172 | 172 | { |
| 173 | - if ($result instanceof Response) { |
|
| 174 | - if ($output !== '' && $result->getBody()->isWritable()) { |
|
| 173 | + if ($result instanceof Response){ |
|
| 174 | + if ($output !== '' && $result->getBody()->isWritable()){ |
|
| 175 | 175 | $result->getBody()->write($output); |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | return $result; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - if (is_array($result) || $result instanceof \JsonSerializable) { |
|
| 181 | + if (is_array($result) || $result instanceof \JsonSerializable){ |
|
| 182 | 182 | $response = $this->writeJson($response, $result); |
| 183 | - } else { |
|
| 183 | + }else{ |
|
| 184 | 184 | $response->getBody()->write((string)$result); |
| 185 | 185 | } |
| 186 | 186 | |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | */ |
| 199 | 199 | private function mapException(ControllerException $exception): ClientException |
| 200 | 200 | { |
| 201 | - switch ($exception->getCode()) { |
|
| 201 | + switch ($exception->getCode()){ |
|
| 202 | 202 | case ControllerException::BAD_ACTION: |
| 203 | 203 | case ControllerException::NOT_FOUND: |
| 204 | 204 | return new NotFoundException($exception->getMessage()); |
@@ -103,7 +103,8 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | public function handle(Request $request): Response |
| 105 | 105 | { |
| 106 | - if ($this->controller === null) { |
|
| 106 | + if ($this->controller === null) |
|
| 107 | + { |
|
| 107 | 108 | throw new HandlerException('Controller and action pair is not set'); |
| 108 | 109 | } |
| 109 | 110 | |
@@ -113,14 +114,16 @@ discard block |
||
| 113 | 114 | $output = $result = null; |
| 114 | 115 | |
| 115 | 116 | $response = $this->responseFactory->createResponse(200); |
| 116 | - try { |
|
| 117 | + try |
|
| 118 | + { |
|
| 117 | 119 | // run the core withing PSR-7 Request/Response scope |
| 118 | 120 | $result = $this->scope->runScope( |
| 119 | 121 | [ |
| 120 | 122 | Request::class => $request, |
| 121 | 123 | Response::class => $response |
| 122 | 124 | ], |
| 123 | - function () use ($request) { |
|
| 125 | + function () use ($request) |
|
| 126 | + { |
|
| 124 | 127 | return $this->core->callAction( |
| 125 | 128 | $this->controller, |
| 126 | 129 | $this->getAction($request), |
@@ -128,14 +131,21 @@ discard block |
||
| 128 | 131 | ); |
| 129 | 132 | } |
| 130 | 133 | ); |
| 131 | - } catch (ControllerException $e) { |
|
| 134 | + } |
|
| 135 | + catch (ControllerException $e) |
|
| 136 | + { |
|
| 132 | 137 | ob_get_clean(); |
| 133 | 138 | throw $this->mapException($e); |
| 134 | - } catch (\Throwable $e) { |
|
| 139 | + } |
|
| 140 | + catch (\Throwable $e) |
|
| 141 | + { |
|
| 135 | 142 | ob_get_clean(); |
| 136 | 143 | throw $e; |
| 137 | - } finally { |
|
| 138 | - while (ob_get_level() > $outputLevel + 1) { |
|
| 144 | + } |
|
| 145 | + finally |
|
| 146 | + { |
|
| 147 | + while (ob_get_level() > $outputLevel + 1) |
|
| 148 | + { |
|
| 139 | 149 | $output = ob_get_clean() . $output; |
| 140 | 150 | } |
| 141 | 151 | } |
@@ -153,7 +163,8 @@ discard block |
||
| 153 | 163 | */ |
| 154 | 164 | private function getAction(Request $request): string |
| 155 | 165 | { |
| 156 | - if ($this->verbActions) { |
|
| 166 | + if ($this->verbActions) |
|
| 167 | + { |
|
| 157 | 168 | return strtolower($request->getMethod()) . ucfirst($this->action); |
| 158 | 169 | } |
| 159 | 170 | |
@@ -170,17 +181,22 @@ discard block |
||
| 170 | 181 | */ |
| 171 | 182 | private function wrapResponse(Response $response, $result = null, string $output = ''): Response |
| 172 | 183 | { |
| 173 | - if ($result instanceof Response) { |
|
| 174 | - if ($output !== '' && $result->getBody()->isWritable()) { |
|
| 184 | + if ($result instanceof Response) |
|
| 185 | + { |
|
| 186 | + if ($output !== '' && $result->getBody()->isWritable()) |
|
| 187 | + { |
|
| 175 | 188 | $result->getBody()->write($output); |
| 176 | 189 | } |
| 177 | 190 | |
| 178 | 191 | return $result; |
| 179 | 192 | } |
| 180 | 193 | |
| 181 | - if (is_array($result) || $result instanceof \JsonSerializable) { |
|
| 194 | + if (is_array($result) || $result instanceof \JsonSerializable) |
|
| 195 | + { |
|
| 182 | 196 | $response = $this->writeJson($response, $result); |
| 183 | - } else { |
|
| 197 | + } |
|
| 198 | + else |
|
| 199 | + { |
|
| 184 | 200 | $response->getBody()->write((string)$result); |
| 185 | 201 | } |
| 186 | 202 | |
@@ -198,7 +214,8 @@ discard block |
||
| 198 | 214 | */ |
| 199 | 215 | private function mapException(ControllerException $exception): ClientException |
| 200 | 216 | { |
| 201 | - switch ($exception->getCode()) { |
|
| 217 | + switch ($exception->getCode()) |
|
| 218 | + { |
|
| 202 | 219 | case ControllerException::BAD_ACTION: |
| 203 | 220 | case ControllerException::NOT_FOUND: |
| 204 | 221 | return new NotFoundException($exception->getMessage()); |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | 'integer' => '\d+', |
| 33 | 33 | '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}' |
| 34 | 34 | ]; |
| 35 | - private const URI_FIXERS = [ |
|
| 35 | + private const URI_FIXERS = [ |
|
| 36 | 36 | '[]' => '', |
| 37 | 37 | '[/]' => '', |
| 38 | 38 | '[' => '', |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | public function __construct( |
| 79 | 79 | UriFactoryInterface $uriFactory, |
| 80 | 80 | SlugifyInterface $slugify = null |
| 81 | - ) { |
|
| 81 | + ){ |
|
| 82 | 82 | $this->uriFactory = $uriFactory; |
| 83 | 83 | $this->slugify = $slugify ?? new Slugify(); |
| 84 | 84 | } |
@@ -167,12 +167,12 @@ discard block |
||
| 167 | 167 | */ |
| 168 | 168 | public function match(UriInterface $uri, array $defaults): ?array |
| 169 | 169 | { |
| 170 | - if (!$this->isCompiled()) { |
|
| 170 | + if (!$this->isCompiled()){ |
|
| 171 | 171 | $this->compile(); |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | $matches = []; |
| 175 | - if (!preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
| 175 | + if (!preg_match($this->compiled, $this->fetchTarget($uri), $matches)){ |
|
| 176 | 176 | return null; |
| 177 | 177 | } |
| 178 | 178 | |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | */ |
| 191 | 191 | public function uri($parameters = [], array $defaults = []): UriInterface |
| 192 | 192 | { |
| 193 | - if (!$this->isCompiled()) { |
|
| 193 | + if (!$this->isCompiled()){ |
|
| 194 | 194 | $this->compile(); |
| 195 | 195 | } |
| 196 | 196 | |
@@ -200,8 +200,8 @@ discard block |
||
| 200 | 200 | $this->fetchOptions($parameters, $query) |
| 201 | 201 | ); |
| 202 | 202 | |
| 203 | - foreach ($this->constrains as $key => $values) { |
|
| 204 | - if (empty($parameters[$key])) { |
|
| 203 | + foreach ($this->constrains as $key => $values){ |
|
| 204 | + if (empty($parameters[$key])){ |
|
| 205 | 205 | throw new UriHandlerException("Unable to generate Uri, parameter `{$key}` is missing"); |
| 206 | 206 | } |
| 207 | 207 | } |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | $path = $this->interpolate($this->template, $parameters); |
| 211 | 211 | |
| 212 | 212 | //Uri with added prefix |
| 213 | - $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->prefix) . trim($path, '/')); |
|
| 213 | + $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->prefix).trim($path, '/')); |
|
| 214 | 214 | |
| 215 | 215 | return empty($query) ? $uri : $uri->withQuery(http_build_query($query)); |
| 216 | 216 | } |
@@ -227,18 +227,18 @@ discard block |
||
| 227 | 227 | $allowed = array_keys($this->options); |
| 228 | 228 | |
| 229 | 229 | $result = []; |
| 230 | - foreach ($parameters as $key => $parameter) { |
|
| 231 | - if (is_numeric($key) && isset($allowed[$key])) { |
|
| 230 | + foreach ($parameters as $key => $parameter){ |
|
| 231 | + if (is_numeric($key) && isset($allowed[$key])){ |
|
| 232 | 232 | // this segment fetched keys from given parameters either by name or by position |
| 233 | 233 | $key = $allowed[$key]; |
| 234 | - } elseif (!array_key_exists($key, $this->options) && is_array($parameters)) { |
|
| 234 | + } elseif (!array_key_exists($key, $this->options) && is_array($parameters)){ |
|
| 235 | 235 | // all additional parameters given in array form can be glued to query string |
| 236 | 236 | $query[$key] = $parameter; |
| 237 | 237 | continue; |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | //String must be normalized here |
| 241 | - if (is_string($parameter) && !preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
| 241 | + if (is_string($parameter) && !preg_match('/^[a-z\-_0-9]+$/i', $parameter)){ |
|
| 242 | 242 | $result[$key] = $this->slugify->slugify($parameter); |
| 243 | 243 | continue; |
| 244 | 244 | } |
@@ -259,15 +259,15 @@ discard block |
||
| 259 | 259 | { |
| 260 | 260 | $path = $uri->getPath(); |
| 261 | 261 | |
| 262 | - if (empty($path) || $path[0] !== '/') { |
|
| 263 | - $path = '/' . $path; |
|
| 262 | + if (empty($path) || $path[0] !== '/'){ |
|
| 263 | + $path = '/'.$path; |
|
| 264 | 264 | } |
| 265 | 265 | |
| 266 | - if ($this->matchHost) { |
|
| 267 | - $uriString = $uri->getHost() . $path; |
|
| 268 | - } else { |
|
| 266 | + if ($this->matchHost){ |
|
| 267 | + $uriString = $uri->getHost().$path; |
|
| 268 | + }else{ |
|
| 269 | 269 | $uriString = substr($path, strlen($this->prefix)); |
| 270 | - if ($uriString === false) { |
|
| 270 | + if ($uriString === false){ |
|
| 271 | 271 | $uriString = ''; |
| 272 | 272 | } |
| 273 | 273 | } |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | */ |
| 281 | 281 | private function compile(): void |
| 282 | 282 | { |
| 283 | - if ($this->pattern === null) { |
|
| 283 | + if ($this->pattern === null){ |
|
| 284 | 284 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
| 285 | 285 | } |
| 286 | 286 | |
@@ -288,14 +288,14 @@ discard block |
||
| 288 | 288 | $pattern = rtrim(ltrim($this->pattern, ':/'), '/'); |
| 289 | 289 | |
| 290 | 290 | // correct [/ first occurrence] |
| 291 | - if (strpos($pattern, '[/') === 0) { |
|
| 292 | - $pattern = '[' . substr($pattern, 2); |
|
| 291 | + if (strpos($pattern, '[/') === 0){ |
|
| 292 | + $pattern = '['.substr($pattern, 2); |
|
| 293 | 293 | } |
| 294 | 294 | |
| 295 | - if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
| 295 | + if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)){ |
|
| 296 | 296 | $variables = array_combine($matches[1], $matches[2]); |
| 297 | 297 | |
| 298 | - foreach ($variables as $key => $segment) { |
|
| 298 | + foreach ($variables as $key => $segment){ |
|
| 299 | 299 | $segment = $this->prepareSegment($key, $segment); |
| 300 | 300 | $replaces["<$key>"] = "(?P<$key>$segment)"; |
| 301 | 301 | $options[] = $key; |
@@ -305,13 +305,13 @@ discard block |
||
| 305 | 305 | $template = preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
| 306 | 306 | $options = array_fill_keys($options, null); |
| 307 | 307 | |
| 308 | - foreach ($this->constrains as $key => $value) { |
|
| 309 | - if ($value instanceof Autofill) { |
|
| 308 | + foreach ($this->constrains as $key => $value){ |
|
| 309 | + if ($value instanceof Autofill){ |
|
| 310 | 310 | // only forces value replacement, not required to be presented as parameter |
| 311 | 311 | continue; |
| 312 | 312 | } |
| 313 | 313 | |
| 314 | - if (!array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
| 314 | + if (!array_key_exists($key, $options) && !isset($this->defaults[$key])){ |
|
| 315 | 315 | throw new ConstrainException( |
| 316 | 316 | sprintf( |
| 317 | 317 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -322,7 +322,7 @@ discard block |
||
| 322 | 322 | } |
| 323 | 323 | } |
| 324 | 324 | |
| 325 | - $this->compiled = '/^' . strtr($template, $replaces + self::PATTERN_REPLACES) . '$/iu'; |
|
| 325 | + $this->compiled = '/^'.strtr($template, $replaces + self::PATTERN_REPLACES).'$/iu'; |
|
| 326 | 326 | $this->template = stripslashes(str_replace('?', '', $template)); |
| 327 | 327 | $this->options = $options; |
| 328 | 328 | } |
@@ -337,7 +337,7 @@ discard block |
||
| 337 | 337 | private function interpolate(string $string, array $values): string |
| 338 | 338 | { |
| 339 | 339 | $replaces = []; |
| 340 | - foreach ($values as $key => $value) { |
|
| 340 | + foreach ($values as $key => $value){ |
|
| 341 | 341 | $value = (is_array($value) || $value instanceof \Closure) ? '' : $value; |
| 342 | 342 | $replaces["<{$key}>"] = is_object($value) ? (string)$value : $value; |
| 343 | 343 | } |
@@ -354,15 +354,15 @@ discard block |
||
| 354 | 354 | */ |
| 355 | 355 | private function prepareSegment(string $name, string $segment): string |
| 356 | 356 | { |
| 357 | - if ($segment !== '') { |
|
| 357 | + if ($segment !== ''){ |
|
| 358 | 358 | return self::SEGMENT_TYPES[$segment] ?? $segment; |
| 359 | 359 | } |
| 360 | 360 | |
| 361 | - if (!isset($this->constrains[$name])) { |
|
| 361 | + if (!isset($this->constrains[$name])){ |
|
| 362 | 362 | return self::DEFAULT_SEGMENT; |
| 363 | 363 | } |
| 364 | 364 | |
| 365 | - if (is_array($this->constrains[$name])) { |
|
| 365 | + if (is_array($this->constrains[$name])){ |
|
| 366 | 366 | $values = array_map([$this, 'filterSegment'], $this->constrains[$name]); |
| 367 | 367 | |
| 368 | 368 | return implode('|', $values); |
@@ -167,12 +167,14 @@ discard block |
||
| 167 | 167 | */ |
| 168 | 168 | public function match(UriInterface $uri, array $defaults): ?array |
| 169 | 169 | { |
| 170 | - if (!$this->isCompiled()) { |
|
| 170 | + if (!$this->isCompiled()) |
|
| 171 | + { |
|
| 171 | 172 | $this->compile(); |
| 172 | 173 | } |
| 173 | 174 | |
| 174 | 175 | $matches = []; |
| 175 | - if (!preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
| 176 | + if (!preg_match($this->compiled, $this->fetchTarget($uri), $matches)) |
|
| 177 | + { |
|
| 176 | 178 | return null; |
| 177 | 179 | } |
| 178 | 180 | |
@@ -190,7 +192,8 @@ discard block |
||
| 190 | 192 | */ |
| 191 | 193 | public function uri($parameters = [], array $defaults = []): UriInterface |
| 192 | 194 | { |
| 193 | - if (!$this->isCompiled()) { |
|
| 195 | + if (!$this->isCompiled()) |
|
| 196 | + { |
|
| 194 | 197 | $this->compile(); |
| 195 | 198 | } |
| 196 | 199 | |
@@ -200,8 +203,10 @@ discard block |
||
| 200 | 203 | $this->fetchOptions($parameters, $query) |
| 201 | 204 | ); |
| 202 | 205 | |
| 203 | - foreach ($this->constrains as $key => $values) { |
|
| 204 | - if (empty($parameters[$key])) { |
|
| 206 | + foreach ($this->constrains as $key => $values) |
|
| 207 | + { |
|
| 208 | + if (empty($parameters[$key])) |
|
| 209 | + { |
|
| 205 | 210 | throw new UriHandlerException("Unable to generate Uri, parameter `{$key}` is missing"); |
| 206 | 211 | } |
| 207 | 212 | } |
@@ -227,18 +232,23 @@ discard block |
||
| 227 | 232 | $allowed = array_keys($this->options); |
| 228 | 233 | |
| 229 | 234 | $result = []; |
| 230 | - foreach ($parameters as $key => $parameter) { |
|
| 231 | - if (is_numeric($key) && isset($allowed[$key])) { |
|
| 235 | + foreach ($parameters as $key => $parameter) |
|
| 236 | + { |
|
| 237 | + if (is_numeric($key) && isset($allowed[$key])) |
|
| 238 | + { |
|
| 232 | 239 | // this segment fetched keys from given parameters either by name or by position |
| 233 | 240 | $key = $allowed[$key]; |
| 234 | - } elseif (!array_key_exists($key, $this->options) && is_array($parameters)) { |
|
| 241 | + } |
|
| 242 | + elseif (!array_key_exists($key, $this->options) && is_array($parameters)) |
|
| 243 | + { |
|
| 235 | 244 | // all additional parameters given in array form can be glued to query string |
| 236 | 245 | $query[$key] = $parameter; |
| 237 | 246 | continue; |
| 238 | 247 | } |
| 239 | 248 | |
| 240 | 249 | //String must be normalized here |
| 241 | - if (is_string($parameter) && !preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
| 250 | + if (is_string($parameter) && !preg_match('/^[a-z\-_0-9]+$/i', $parameter)) |
|
| 251 | + { |
|
| 242 | 252 | $result[$key] = $this->slugify->slugify($parameter); |
| 243 | 253 | continue; |
| 244 | 254 | } |
@@ -259,15 +269,20 @@ discard block |
||
| 259 | 269 | { |
| 260 | 270 | $path = $uri->getPath(); |
| 261 | 271 | |
| 262 | - if (empty($path) || $path[0] !== '/') { |
|
| 272 | + if (empty($path) || $path[0] !== '/') |
|
| 273 | + { |
|
| 263 | 274 | $path = '/' . $path; |
| 264 | 275 | } |
| 265 | 276 | |
| 266 | - if ($this->matchHost) { |
|
| 277 | + if ($this->matchHost) |
|
| 278 | + { |
|
| 267 | 279 | $uriString = $uri->getHost() . $path; |
| 268 | - } else { |
|
| 280 | + } |
|
| 281 | + else |
|
| 282 | + { |
|
| 269 | 283 | $uriString = substr($path, strlen($this->prefix)); |
| 270 | - if ($uriString === false) { |
|
| 284 | + if ($uriString === false) |
|
| 285 | + { |
|
| 271 | 286 | $uriString = ''; |
| 272 | 287 | } |
| 273 | 288 | } |
@@ -280,7 +295,8 @@ discard block |
||
| 280 | 295 | */ |
| 281 | 296 | private function compile(): void |
| 282 | 297 | { |
| 283 | - if ($this->pattern === null) { |
|
| 298 | + if ($this->pattern === null) |
|
| 299 | + { |
|
| 284 | 300 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
| 285 | 301 | } |
| 286 | 302 | |
@@ -288,14 +304,17 @@ discard block |
||
| 288 | 304 | $pattern = rtrim(ltrim($this->pattern, ':/'), '/'); |
| 289 | 305 | |
| 290 | 306 | // correct [/ first occurrence] |
| 291 | - if (strpos($pattern, '[/') === 0) { |
|
| 307 | + if (strpos($pattern, '[/') === 0) |
|
| 308 | + { |
|
| 292 | 309 | $pattern = '[' . substr($pattern, 2); |
| 293 | 310 | } |
| 294 | 311 | |
| 295 | - if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
| 312 | + if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) |
|
| 313 | + { |
|
| 296 | 314 | $variables = array_combine($matches[1], $matches[2]); |
| 297 | 315 | |
| 298 | - foreach ($variables as $key => $segment) { |
|
| 316 | + foreach ($variables as $key => $segment) |
|
| 317 | + { |
|
| 299 | 318 | $segment = $this->prepareSegment($key, $segment); |
| 300 | 319 | $replaces["<$key>"] = "(?P<$key>$segment)"; |
| 301 | 320 | $options[] = $key; |
@@ -305,13 +324,16 @@ discard block |
||
| 305 | 324 | $template = preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
| 306 | 325 | $options = array_fill_keys($options, null); |
| 307 | 326 | |
| 308 | - foreach ($this->constrains as $key => $value) { |
|
| 309 | - if ($value instanceof Autofill) { |
|
| 327 | + foreach ($this->constrains as $key => $value) |
|
| 328 | + { |
|
| 329 | + if ($value instanceof Autofill) |
|
| 330 | + { |
|
| 310 | 331 | // only forces value replacement, not required to be presented as parameter |
| 311 | 332 | continue; |
| 312 | 333 | } |
| 313 | 334 | |
| 314 | - if (!array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
| 335 | + if (!array_key_exists($key, $options) && !isset($this->defaults[$key])) |
|
| 336 | + { |
|
| 315 | 337 | throw new ConstrainException( |
| 316 | 338 | sprintf( |
| 317 | 339 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -337,7 +359,8 @@ discard block |
||
| 337 | 359 | private function interpolate(string $string, array $values): string |
| 338 | 360 | { |
| 339 | 361 | $replaces = []; |
| 340 | - foreach ($values as $key => $value) { |
|
| 362 | + foreach ($values as $key => $value) |
|
| 363 | + { |
|
| 341 | 364 | $value = (is_array($value) || $value instanceof \Closure) ? '' : $value; |
| 342 | 365 | $replaces["<{$key}>"] = is_object($value) ? (string)$value : $value; |
| 343 | 366 | } |
@@ -354,15 +377,18 @@ discard block |
||
| 354 | 377 | */ |
| 355 | 378 | private function prepareSegment(string $name, string $segment): string |
| 356 | 379 | { |
| 357 | - if ($segment !== '') { |
|
| 380 | + if ($segment !== '') |
|
| 381 | + { |
|
| 358 | 382 | return self::SEGMENT_TYPES[$segment] ?? $segment; |
| 359 | 383 | } |
| 360 | 384 | |
| 361 | - if (!isset($this->constrains[$name])) { |
|
| 385 | + if (!isset($this->constrains[$name])) |
|
| 386 | + { |
|
| 362 | 387 | return self::DEFAULT_SEGMENT; |
| 363 | 388 | } |
| 364 | 389 | |
| 365 | - if (is_array($this->constrains[$name])) { |
|
| 390 | + if (is_array($this->constrains[$name])) |
|
| 391 | + { |
|
| 366 | 392 | $values = array_map([$this, 'filterSegment'], $this->constrains[$name]); |
| 367 | 393 | |
| 368 | 394 | return implode('|', $values); |