@@ -40,7 +40,8 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | public function __destruct() |
| 42 | 42 | { |
| 43 | - if ($this->target === null) { |
|
| 43 | + if ($this->target === null) |
|
| 44 | + { |
|
| 44 | 45 | throw new TargetException( |
| 45 | 46 | \sprintf('The [%s] route has no defined target. Call one of: `controller`, `action`, |
| 46 | 47 | `namespaced`, `groupControllers`, `callable`, `handler` methods.', $this->name) |
@@ -142,7 +143,8 @@ discard block |
||
| 142 | 143 | |
| 143 | 144 | public function middleware(MiddlewareInterface|string|array $middleware): self |
| 144 | 145 | { |
| 145 | - if (!\is_array($middleware)) { |
|
| 146 | + if (!\is_array($middleware)) |
|
| 147 | + { |
|
| 146 | 148 | $middleware = [$middleware]; |
| 147 | 149 | } |
| 148 | 150 | |
@@ -15,7 +15,8 @@ |
||
| 15 | 15 | |
| 16 | 16 | public function load(mixed $resource, string $type = null): mixed |
| 17 | 17 | { |
| 18 | - if (false === $loader = $this->registry->resolve($resource, $type)) { |
|
| 18 | + if (false === $loader = $this->registry->resolve($resource, $type)) |
|
| 19 | + { |
|
| 19 | 20 | throw new LoaderLoadException(\sprintf('Loader for type [%s] not found.', $type ?? '')); |
| 20 | 21 | } |
| 21 | 22 | |
@@ -56,12 +56,14 @@ |
||
| 56 | 56 | */ |
| 57 | 57 | public function match(Request $request): ?static |
| 58 | 58 | { |
| 59 | - if (!\in_array(\strtoupper($request->getMethod()), $this->getVerbs(), true)) { |
|
| 59 | + if (!\in_array(\strtoupper($request->getMethod()), $this->getVerbs(), true)) |
|
| 60 | + { |
|
| 60 | 61 | return null; |
| 61 | 62 | } |
| 62 | 63 | |
| 63 | 64 | $matches = $this->uriHandler->match($request->getUri(), $this->defaults); |
| 64 | - if ($matches === null) { |
|
| 65 | + if ($matches === null) |
|
| 66 | + { |
|
| 65 | 67 | return null; |
| 66 | 68 | } |
| 67 | 69 | |
@@ -67,14 +67,19 @@ discard block |
||
| 67 | 67 | |
| 68 | 68 | return $this->tracer->trace( |
| 69 | 69 | name: 'Routing', |
| 70 | - callback: function (SpanInterface $span) use ($request) { |
|
| 71 | - try { |
|
| 70 | + callback: function (SpanInterface $span) use ($request) |
|
| 71 | + { |
|
| 72 | + try |
|
| 73 | + { |
|
| 72 | 74 | $route = $this->matchRoute($request, $routeName); |
| 73 | - } catch (RouteException $e) { |
|
| 75 | + } |
|
| 76 | + catch (RouteException $e) |
|
| 77 | + { |
|
| 74 | 78 | throw new RouterException('Invalid route definition', $e->getCode(), $e); |
| 75 | 79 | } |
| 76 | 80 | |
| 77 | - if ($route === null) { |
|
| 81 | + if ($route === null) |
|
| 82 | + { |
|
| 78 | 83 | $this->eventDispatcher?->dispatch(new RouteNotFound($request)); |
| 79 | 84 | throw new RouteNotFoundException($request->getUri()); |
| 80 | 85 | } |
@@ -109,7 +114,8 @@ discard block |
||
| 109 | 114 | |
| 110 | 115 | public function getRoute(string $name): RouteInterface |
| 111 | 116 | { |
| 112 | - if (isset($this->routes[$name])) { |
|
| 117 | + if (isset($this->routes[$name])) |
|
| 118 | + { |
|
| 113 | 119 | return $this->routes[$name]; |
| 114 | 120 | } |
| 115 | 121 | |
@@ -118,7 +124,8 @@ discard block |
||
| 118 | 124 | |
| 119 | 125 | public function getRoutes(): array |
| 120 | 126 | { |
| 121 | - if (!empty($this->default)) { |
|
| 127 | + if (!empty($this->default)) |
|
| 128 | + { |
|
| 122 | 129 | return $this->routes + [null => $this->default]; |
| 123 | 130 | } |
| 124 | 131 | |
@@ -127,9 +134,12 @@ discard block |
||
| 127 | 134 | |
| 128 | 135 | public function uri(string $route, iterable $parameters = []): UriInterface |
| 129 | 136 | { |
| 130 | - try { |
|
| 137 | + try |
|
| 138 | + { |
|
| 131 | 139 | return $this->getRoute($route)->uri($parameters); |
| 132 | - } catch (UndefinedRouteException) { |
|
| 140 | + } |
|
| 141 | + catch (UndefinedRouteException) |
|
| 142 | + { |
|
| 133 | 143 | //In some cases route name can be provided as controller:action pair, we can try to |
| 134 | 144 | //generate such route automatically based on our default/fallback route |
| 135 | 145 | return $this->castRoute($route)->uri($parameters); |
@@ -141,29 +151,35 @@ discard block |
||
| 141 | 151 | /** @var GroupRegistry $groups */ |
| 142 | 152 | $groups = $this->container->get(GroupRegistry::class); |
| 143 | 153 | |
| 144 | - foreach ($routes->getCollection() as $name => $configurator) { |
|
| 154 | + foreach ($routes->getCollection() as $name => $configurator) |
|
| 155 | + { |
|
| 145 | 156 | $target = $configurator->target; |
| 146 | - if ($configurator->core !== null && $target instanceof AbstractTarget) { |
|
| 157 | + if ($configurator->core !== null && $target instanceof AbstractTarget) |
|
| 158 | + { |
|
| 147 | 159 | $target = $target->withCore($configurator->core); |
| 148 | 160 | } |
| 149 | 161 | |
| 150 | 162 | $route = new Route(\ltrim($configurator->pattern, '/'), $target, $configurator->defaults); |
| 151 | 163 | |
| 152 | - if ($configurator->middleware !== null) { |
|
| 164 | + if ($configurator->middleware !== null) |
|
| 165 | + { |
|
| 153 | 166 | $route = $route->withMiddleware(...$configurator->middleware); |
| 154 | 167 | } |
| 155 | 168 | |
| 156 | - if ($configurator->methods !== null) { |
|
| 169 | + if ($configurator->methods !== null) |
|
| 170 | + { |
|
| 157 | 171 | $route = $route->withVerbs(...$configurator->methods); |
| 158 | 172 | } |
| 159 | 173 | |
| 160 | - if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 174 | + if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) |
|
| 175 | + { |
|
| 161 | 176 | $group = $groups->getGroup($configurator->group ?? $groups->getDefaultGroup()); |
| 162 | 177 | $group->setPrefix($configurator->prefix); |
| 163 | 178 | $group->addRoute($name, $route); |
| 164 | 179 | } |
| 165 | 180 | |
| 166 | - if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 181 | + if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) |
|
| 182 | + { |
|
| 167 | 183 | $this->setDefault($route); |
| 168 | 184 | } |
| 169 | 185 | } |
@@ -174,17 +190,20 @@ discard block |
||
| 174 | 190 | */ |
| 175 | 191 | protected function matchRoute(ServerRequestInterface $request, string &$routeName = null): ?RouteInterface |
| 176 | 192 | { |
| 177 | - foreach ($this->routes as $name => $route) { |
|
| 193 | + foreach ($this->routes as $name => $route) |
|
| 194 | + { |
|
| 178 | 195 | // Matched route will return new route instance with matched parameters |
| 179 | 196 | $matched = $route->match($request); |
| 180 | 197 | |
| 181 | - if ($matched !== null) { |
|
| 198 | + if ($matched !== null) |
|
| 199 | + { |
|
| 182 | 200 | $routeName = $name; |
| 183 | 201 | return $matched; |
| 184 | 202 | } |
| 185 | 203 | } |
| 186 | 204 | |
| 187 | - if ($this->default !== null) { |
|
| 205 | + if ($this->default !== null) |
|
| 206 | + { |
|
| 188 | 207 | return $this->default->match($request); |
| 189 | 208 | } |
| 190 | 209 | |
@@ -197,14 +216,18 @@ discard block |
||
| 197 | 216 | */ |
| 198 | 217 | protected function configure(RouteInterface $route): RouteInterface |
| 199 | 218 | { |
| 200 | - if ($route instanceof ContainerizedInterface && !$route->hasContainer()) { |
|
| 219 | + if ($route instanceof ContainerizedInterface && !$route->hasContainer()) |
|
| 220 | + { |
|
| 201 | 221 | // isolating route in a given container |
| 202 | 222 | $route = $route->withContainer($this->container); |
| 203 | 223 | } |
| 204 | 224 | |
| 205 | - try { |
|
| 225 | + try |
|
| 226 | + { |
|
| 206 | 227 | $uriHandler = $route->getUriHandler(); |
| 207 | - } catch (\Throwable) { |
|
| 228 | + } |
|
| 229 | + catch (\Throwable) |
|
| 230 | + { |
|
| 208 | 231 | $uriHandler = $this->uriHandler; |
| 209 | 232 | } |
| 210 | 233 | |
@@ -236,11 +259,16 @@ discard block |
||
| 236 | 259 | /** |
| 237 | 260 | * @var Matches $matches |
| 238 | 261 | */ |
| 239 | - if (!empty($matches['name'])) { |
|
| 262 | + if (!empty($matches['name'])) |
|
| 263 | + { |
|
| 240 | 264 | $routeObject = $this->getRoute($matches['name']); |
| 241 | - } elseif ($this->default !== null) { |
|
| 265 | + } |
|
| 266 | + elseif ($this->default !== null) |
|
| 267 | + { |
|
| 242 | 268 | $routeObject = $this->default; |
| 243 | - } else { |
|
| 269 | + } |
|
| 270 | + else |
|
| 271 | + { |
|
| 244 | 272 | throw new UndefinedRouteException(\sprintf('Unable to locate route candidate for `%s`', $route)); |
| 245 | 273 | } |
| 246 | 274 | |
@@ -68,7 +68,8 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function withUriHandler(UriHandler $uriHandler): static |
| 70 | 70 | { |
| 71 | - if ($this->target instanceof TargetInterface) { |
|
| 71 | + if ($this->target instanceof TargetInterface) |
|
| 72 | + { |
|
| 72 | 73 | $uriHandler = $uriHandler->withConstrains( |
| 73 | 74 | $this->target->getConstrains(), |
| 74 | 75 | $this->defaults, |
@@ -86,7 +87,8 @@ discard block |
||
| 86 | 87 | $route = clone $this; |
| 87 | 88 | $route->container = $container; |
| 88 | 89 | |
| 89 | - if ($route->target instanceof TargetInterface) { |
|
| 90 | + if ($route->target instanceof TargetInterface) |
|
| 91 | + { |
|
| 90 | 92 | $route->target = clone $route->target; |
| 91 | 93 | } |
| 92 | 94 | |
@@ -113,7 +115,8 @@ discard block |
||
| 113 | 115 | */ |
| 114 | 116 | public function handle(ServerRequestInterface $request): ResponseInterface |
| 115 | 117 | { |
| 116 | - if (empty($this->requestHandler)) { |
|
| 118 | + if (empty($this->requestHandler)) |
|
| 119 | + { |
|
| 117 | 120 | $this->requestHandler = $this->requestHandler(); |
| 118 | 121 | } |
| 119 | 122 | |
@@ -129,29 +132,37 @@ discard block |
||
| 129 | 132 | */ |
| 130 | 133 | protected function requestHandler(): RequestHandlerInterface |
| 131 | 134 | { |
| 132 | - if (!$this->hasContainer()) { |
|
| 135 | + if (!$this->hasContainer()) |
|
| 136 | + { |
|
| 133 | 137 | throw new RouteException('Unable to configure route pipeline without associated container'); |
| 134 | 138 | } |
| 135 | 139 | |
| 136 | - if ($this->target instanceof TargetInterface) { |
|
| 137 | - try { |
|
| 140 | + if ($this->target instanceof TargetInterface) |
|
| 141 | + { |
|
| 142 | + try |
|
| 143 | + { |
|
| 138 | 144 | \assert($this->matches !== null); |
| 139 | 145 | return $this->target->getHandler($this->container, $this->matches); |
| 140 | - } catch (TargetException $e) { |
|
| 146 | + } |
|
| 147 | + catch (TargetException $e) |
|
| 148 | + { |
|
| 141 | 149 | throw new RouteException('Invalid target resolution', $e->getCode(), $e); |
| 142 | 150 | } |
| 143 | 151 | } |
| 144 | 152 | |
| 145 | - if ($this->target instanceof RequestHandlerInterface) { |
|
| 153 | + if ($this->target instanceof RequestHandlerInterface) |
|
| 154 | + { |
|
| 146 | 155 | return $this->target; |
| 147 | 156 | } |
| 148 | 157 | |
| 149 | - try { |
|
| 158 | + try |
|
| 159 | + { |
|
| 150 | 160 | $target = \is_string($this->target) |
| 151 | 161 | ? $this->container->get($this->target) |
| 152 | 162 | : $this->target; |
| 153 | 163 | |
| 154 | - if ($target instanceof RequestHandlerInterface) { |
|
| 164 | + if ($target instanceof RequestHandlerInterface) |
|
| 165 | + { |
|
| 155 | 166 | return $target; |
| 156 | 167 | } |
| 157 | 168 | |
@@ -159,7 +170,9 @@ discard block |
||
| 159 | 170 | $target, |
| 160 | 171 | $this->container->get(ResponseFactoryInterface::class) |
| 161 | 172 | ); |
| 162 | - } catch (ContainerExceptionInterface $e) { |
|
| 173 | + } |
|
| 174 | + catch (ContainerExceptionInterface $e) |
|
| 175 | + { |
|
| 163 | 176 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
| 164 | 177 | } |
| 165 | 178 | } |
@@ -43,16 +43,19 @@ discard block |
||
| 43 | 43 | $route = clone $this; |
| 44 | 44 | |
| 45 | 45 | // array fallback |
| 46 | - if (\count($middleware) === 1 && \is_array($middleware[0])) { |
|
| 46 | + if (\count($middleware) === 1 && \is_array($middleware[0])) |
|
| 47 | + { |
|
| 47 | 48 | $middleware = $middleware[0]; |
| 48 | 49 | } |
| 49 | 50 | |
| 50 | 51 | /** @var MiddlewareType[] $middleware */ |
| 51 | - foreach ($middleware as $item) { |
|
| 52 | + foreach ($middleware as $item) |
|
| 53 | + { |
|
| 52 | 54 | $route->middleware[] = $item; |
| 53 | 55 | } |
| 54 | 56 | |
| 55 | - if ($route->pipeline !== null) { |
|
| 57 | + if ($route->pipeline !== null) |
|
| 58 | + { |
|
| 56 | 59 | $route->pipeline = $route->makePipeline(); |
| 57 | 60 | } |
| 58 | 61 | |
@@ -77,11 +80,14 @@ discard block |
||
| 77 | 80 | protected function makePipeline(): Pipeline |
| 78 | 81 | { |
| 79 | 82 | \assert($this->container !== null); |
| 80 | - try { |
|
| 83 | + try |
|
| 84 | + { |
|
| 81 | 85 | return $this->container |
| 82 | 86 | ->get(PipelineFactory::class) |
| 83 | 87 | ->createWithMiddleware($this->middleware); |
| 84 | - } catch (ContainerExceptionInterface $e) { |
|
| 88 | + } |
|
| 89 | + catch (ContainerExceptionInterface $e) |
|
| 90 | + { |
|
| 85 | 91 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
| 86 | 92 | } |
| 87 | 93 | } |
@@ -103,7 +103,8 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | public function withBasePath(string $basePath): self |
| 105 | 105 | { |
| 106 | - if (!\str_ends_with($basePath, '/')) { |
|
| 106 | + if (!\str_ends_with($basePath, '/')) |
|
| 107 | + { |
|
| 107 | 108 | $basePath .= '/'; |
| 108 | 109 | } |
| 109 | 110 | |
@@ -154,12 +155,14 @@ discard block |
||
| 154 | 155 | */ |
| 155 | 156 | public function match(UriInterface $uri, array $defaults): ?array |
| 156 | 157 | { |
| 157 | - if (!$this->isCompiled()) { |
|
| 158 | + if (!$this->isCompiled()) |
|
| 159 | + { |
|
| 158 | 160 | $this->compile(); |
| 159 | 161 | } |
| 160 | 162 | |
| 161 | 163 | $matches = []; |
| 162 | - if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
| 164 | + if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) |
|
| 165 | + { |
|
| 163 | 166 | return null; |
| 164 | 167 | } |
| 165 | 168 | |
@@ -176,7 +179,8 @@ discard block |
||
| 176 | 179 | */ |
| 177 | 180 | public function uri(iterable $parameters = [], array $defaults = []): UriInterface |
| 178 | 181 | { |
| 179 | - if (!$this->isCompiled()) { |
|
| 182 | + if (!$this->isCompiled()) |
|
| 183 | + { |
|
| 180 | 184 | $this->compile(); |
| 181 | 185 | } |
| 182 | 186 | |
@@ -186,8 +190,10 @@ discard block |
||
| 186 | 190 | $this->fetchOptions($parameters, $query) |
| 187 | 191 | ); |
| 188 | 192 | |
| 189 | - foreach ($this->constrains as $key => $_) { |
|
| 190 | - if (empty($parameters[$key])) { |
|
| 193 | + foreach ($this->constrains as $key => $_) |
|
| 194 | + { |
|
| 195 | + if (empty($parameters[$key])) |
|
| 196 | + { |
|
| 191 | 197 | throw new UriHandlerException(\sprintf('Unable to generate Uri, parameter `%s` is missing', $key)); |
| 192 | 198 | } |
| 193 | 199 | } |
@@ -211,18 +217,23 @@ discard block |
||
| 211 | 217 | $allowed = \array_keys($this->options); |
| 212 | 218 | |
| 213 | 219 | $result = []; |
| 214 | - foreach ($parameters as $key => $parameter) { |
|
| 215 | - if (\is_int($key) && isset($allowed[$key])) { |
|
| 220 | + foreach ($parameters as $key => $parameter) |
|
| 221 | + { |
|
| 222 | + if (\is_int($key) && isset($allowed[$key])) |
|
| 223 | + { |
|
| 216 | 224 | // this segment fetched keys from given parameters either by name or by position |
| 217 | 225 | $key = $allowed[$key]; |
| 218 | - } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) { |
|
| 226 | + } |
|
| 227 | + elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) |
|
| 228 | + { |
|
| 219 | 229 | // all additional parameters given in array form can be glued to query string |
| 220 | 230 | $query[$key] = $parameter; |
| 221 | 231 | continue; |
| 222 | 232 | } |
| 223 | 233 | |
| 224 | 234 | //String must be normalized here |
| 225 | - if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
| 235 | + if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) |
|
| 236 | + { |
|
| 226 | 237 | $result[$key] = $this->slugify->slugify($parameter); |
| 227 | 238 | continue; |
| 228 | 239 | } |
@@ -240,15 +251,20 @@ discard block |
||
| 240 | 251 | { |
| 241 | 252 | $path = $uri->getPath(); |
| 242 | 253 | |
| 243 | - if (empty($path) || $path[0] !== '/') { |
|
| 254 | + if (empty($path) || $path[0] !== '/') |
|
| 255 | + { |
|
| 244 | 256 | $path = '/' . $path; |
| 245 | 257 | } |
| 246 | 258 | |
| 247 | - if ($this->matchHost) { |
|
| 259 | + if ($this->matchHost) |
|
| 260 | + { |
|
| 248 | 261 | $uriString = $uri->getHost() . $path; |
| 249 | - } else { |
|
| 262 | + } |
|
| 263 | + else |
|
| 264 | + { |
|
| 250 | 265 | $uriString = \substr($path, \strlen($this->basePath)); |
| 251 | - if ($uriString === false) { |
|
| 266 | + if ($uriString === false) |
|
| 267 | + { |
|
| 252 | 268 | $uriString = ''; |
| 253 | 269 | } |
| 254 | 270 | } |
@@ -264,7 +280,8 @@ discard block |
||
| 264 | 280 | */ |
| 265 | 281 | private function compile(): void |
| 266 | 282 | { |
| 267 | - if ($this->pattern === null) { |
|
| 283 | + if ($this->pattern === null) |
|
| 284 | + { |
|
| 268 | 285 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
| 269 | 286 | } |
| 270 | 287 | |
@@ -273,14 +290,17 @@ discard block |
||
| 273 | 290 | $pattern = \rtrim(\ltrim($this->getPrefix() . '/' . $this->pattern, ':/'), '/'); |
| 274 | 291 | |
| 275 | 292 | // correct [/ first occurrence] |
| 276 | - if (\str_starts_with($pattern, '[/')) { |
|
| 293 | + if (\str_starts_with($pattern, '[/')) |
|
| 294 | + { |
|
| 277 | 295 | $pattern = '[' . \substr($pattern, 2); |
| 278 | 296 | } |
| 279 | 297 | |
| 280 | - if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
| 298 | + if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) |
|
| 299 | + { |
|
| 281 | 300 | $variables = \array_combine($matches[1], $matches[2]); |
| 282 | 301 | |
| 283 | - foreach ($variables as $key => $segment) { |
|
| 302 | + foreach ($variables as $key => $segment) |
|
| 303 | + { |
|
| 284 | 304 | $segment = $this->prepareSegment($key, $segment); |
| 285 | 305 | $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment); |
| 286 | 306 | $options[] = $key; |
@@ -290,13 +310,16 @@ discard block |
||
| 290 | 310 | $template = \preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
| 291 | 311 | $options = \array_fill_keys($options, null); |
| 292 | 312 | |
| 293 | - foreach ($this->constrains as $key => $value) { |
|
| 294 | - if ($value instanceof Autofill) { |
|
| 313 | + foreach ($this->constrains as $key => $value) |
|
| 314 | + { |
|
| 315 | + if ($value instanceof Autofill) |
|
| 316 | + { |
|
| 295 | 317 | // only forces value replacement, not required to be presented as parameter |
| 296 | 318 | continue; |
| 297 | 319 | } |
| 298 | 320 | |
| 299 | - if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
| 321 | + if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) |
|
| 322 | + { |
|
| 300 | 323 | throw new ConstrainException( |
| 301 | 324 | \sprintf( |
| 302 | 325 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -318,7 +341,8 @@ discard block |
||
| 318 | 341 | private function interpolate(string $string, array $values): string |
| 319 | 342 | { |
| 320 | 343 | $replaces = []; |
| 321 | - foreach ($values as $key => $value) { |
|
| 344 | + foreach ($values as $key => $value) |
|
| 345 | + { |
|
| 322 | 346 | $replaces[\sprintf('<%s>', $key)] = match (true) { |
| 323 | 347 | $value instanceof \Stringable || \is_scalar($value) => (string)$value, |
| 324 | 348 | default => '', |
@@ -44,7 +44,8 @@ |
||
| 44 | 44 | */ |
| 45 | 45 | protected function resolveController(array $matches): string |
| 46 | 46 | { |
| 47 | - if (\preg_match('/[^a-z_0-9\-]/i', $matches['controller'])) { |
|
| 47 | + if (\preg_match('/[^a-z_0-9\-]/i', $matches['controller'])) |
|
| 48 | + { |
|
| 48 | 49 | throw new TargetException('Invalid namespace target, controller name not allowed.'); |
| 49 | 50 | } |
| 50 | 51 | |
@@ -89,7 +89,8 @@ discard block |
||
| 89 | 89 | $output = ''; |
| 90 | 90 | |
| 91 | 91 | $response = $this->responseFactory->createResponse(200); |
| 92 | - try { |
|
| 92 | + try |
|
| 93 | + { |
|
| 93 | 94 | $action = $this->verbActions |
| 94 | 95 | ? \strtolower($request->getMethod()) . \ucfirst($this->action) |
| 95 | 96 | : $this->action; |
@@ -114,14 +115,21 @@ discard block |
||
| 114 | 115 | ] |
| 115 | 116 | ) |
| 116 | 117 | ); |
| 117 | - } catch (ControllerException $e) { |
|
| 118 | + } |
|
| 119 | + catch (ControllerException $e) |
|
| 120 | + { |
|
| 118 | 121 | \ob_get_clean(); |
| 119 | 122 | throw $this->mapException($e); |
| 120 | - } catch (\Throwable $e) { |
|
| 123 | + } |
|
| 124 | + catch (\Throwable $e) |
|
| 125 | + { |
|
| 121 | 126 | \ob_get_clean(); |
| 122 | 127 | throw $e; |
| 123 | - } finally { |
|
| 124 | - while (\ob_get_level() > $outputLevel + 1) { |
|
| 128 | + } |
|
| 129 | + finally |
|
| 130 | + { |
|
| 131 | + while (\ob_get_level() > $outputLevel + 1) |
|
| 132 | + { |
|
| 125 | 133 | $output = \ob_get_clean() . $output; |
| 126 | 134 | } |
| 127 | 135 | } |
@@ -142,21 +150,27 @@ discard block |
||
| 142 | 150 | */ |
| 143 | 151 | private function wrapResponse(Response $response, mixed $result = null, string $output = ''): Response |
| 144 | 152 | { |
| 145 | - if ($result instanceof Response) { |
|
| 146 | - if ($output !== '' && $result->getBody()->isWritable()) { |
|
| 153 | + if ($result instanceof Response) |
|
| 154 | + { |
|
| 155 | + if ($output !== '' && $result->getBody()->isWritable()) |
|
| 156 | + { |
|
| 147 | 157 | $result->getBody()->write($output); |
| 148 | 158 | } |
| 149 | 159 | |
| 150 | 160 | return $result; |
| 151 | 161 | } |
| 152 | 162 | |
| 153 | - if ($result instanceof \Generator) { |
|
| 163 | + if ($result instanceof \Generator) |
|
| 164 | + { |
|
| 154 | 165 | return $response->withBody(new GeneratorStream($result)); |
| 155 | 166 | } |
| 156 | 167 | |
| 157 | - if (\is_array($result) || $result instanceof \JsonSerializable) { |
|
| 168 | + if (\is_array($result) || $result instanceof \JsonSerializable) |
|
| 169 | + { |
|
| 158 | 170 | $response = $this->writeJson($response, $result); |
| 159 | - } else { |
|
| 171 | + } |
|
| 172 | + else |
|
| 173 | + { |
|
| 160 | 174 | $response->getBody()->write((string)$result); |
| 161 | 175 | } |
| 162 | 176 | |
@@ -188,7 +202,8 @@ discard block |
||
| 188 | 202 | */ |
| 189 | 203 | private function checkValues(): void |
| 190 | 204 | { |
| 191 | - if ($this->controller === null) { |
|
| 205 | + if ($this->controller === null) |
|
| 206 | + { |
|
| 192 | 207 | throw new HandlerException('Controller and action pair are not set.'); |
| 193 | 208 | } |
| 194 | 209 | } |