@@ -35,12 +35,12 @@ discard block |
||
| 35 | 35 | private readonly string $name, |
| 36 | 36 | private readonly string $pattern, |
| 37 | 37 | private readonly RouteCollection $collection |
| 38 | - ) { |
|
| 38 | + ){ |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | public function __destruct() |
| 42 | 42 | { |
| 43 | - if ($this->target === null) { |
|
| 43 | + if ($this->target === null){ |
|
| 44 | 44 | throw new TargetException( |
| 45 | 45 | \sprintf('The [%s] route has no defined target. Call one of: `controller`, `action`, |
| 46 | 46 | `namespaced`, `groupControllers`, `callable`, `handler` methods.', $this->name) |
@@ -91,21 +91,21 @@ discard block |
||
| 91 | 91 | return $this; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - public function action(string $controller, string|array $action, int $options = 0): self |
|
| 94 | + public function action(string $controller, string | array $action, int $options = 0): self |
|
| 95 | 95 | { |
| 96 | 96 | $this->target = new Action($controller, $action, $options); |
| 97 | 97 | |
| 98 | 98 | return $this; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - public function callable(array|\Closure $callable): self |
|
| 101 | + public function callable(array | \Closure $callable): self |
|
| 102 | 102 | { |
| 103 | 103 | $this->target = $callable; |
| 104 | 104 | |
| 105 | 105 | return $this; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - public function handler(TargetInterface|string $target): self |
|
| 108 | + public function handler(TargetInterface | string $target): self |
|
| 109 | 109 | { |
| 110 | 110 | $this->target = $target; |
| 111 | 111 | |
@@ -140,9 +140,9 @@ discard block |
||
| 140 | 140 | return $this; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - public function middleware(MiddlewareInterface|string|array $middleware): self |
|
| 143 | + public function middleware(MiddlewareInterface | string | array $middleware): self |
|
| 144 | 144 | { |
| 145 | - if (!\is_array($middleware)) { |
|
| 145 | + if (!\is_array($middleware)){ |
|
| 146 | 146 | $middleware = [$middleware]; |
| 147 | 147 | } |
| 148 | 148 | |
@@ -151,9 +151,9 @@ discard block |
||
| 151 | 151 | return $this; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | - public function methods(string|array $methods): self |
|
| 154 | + public function methods(string | array $methods): self |
|
| 155 | 155 | { |
| 156 | - $this->methods = (array) $methods; |
|
| 156 | + $this->methods = (array)$methods; |
|
| 157 | 157 | |
| 158 | 158 | return $this; |
| 159 | 159 | } |
@@ -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 | |
@@ -10,12 +10,12 @@ |
||
| 10 | 10 | { |
| 11 | 11 | public function __construct( |
| 12 | 12 | private readonly LoaderRegistryInterface $registry |
| 13 | - ) { |
|
| 13 | + ){ |
|
| 14 | 14 | } |
| 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 | throw new LoaderLoadException(\sprintf('Loader for type [%s] not found.', $type ?? '')); |
| 20 | 20 | } |
| 21 | 21 | |
@@ -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 | |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | public function __construct( |
| 32 | 32 | protected string $pattern, |
| 33 | 33 | array $defaults = [] |
| 34 | - ) { |
|
| 34 | + ){ |
|
| 35 | 35 | $this->defaults = $defaults; |
| 36 | 36 | } |
| 37 | 37 | |
@@ -56,12 +56,12 @@ discard block |
||
| 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 | return null; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | $matches = $this->uriHandler->match($request->getUri(), $this->defaults); |
| 64 | - if ($matches === null) { |
|
| 64 | + if ($matches === null){ |
|
| 65 | 65 | return null; |
| 66 | 66 | } |
| 67 | 67 | |
@@ -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 | |
@@ -52,9 +52,9 @@ discard block |
||
| 52 | 52 | private readonly ContainerInterface $container, |
| 53 | 53 | private readonly ?EventDispatcherInterface $eventDispatcher = null, |
| 54 | 54 | ?TracerInterface $tracer = null, |
| 55 | - ) { |
|
| 55 | + ){ |
|
| 56 | 56 | $this->tracer = $tracer ?? new NullTracer(); |
| 57 | - $this->basePath = '/' . \ltrim($basePath, '/'); |
|
| 57 | + $this->basePath = '/'.\ltrim($basePath, '/'); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | /** |
@@ -68,13 +68,13 @@ discard block |
||
| 68 | 68 | return $this->tracer->trace( |
| 69 | 69 | name: 'Routing', |
| 70 | 70 | callback: function (SpanInterface $span) use ($request) { |
| 71 | - try { |
|
| 71 | + try{ |
|
| 72 | 72 | $route = $this->matchRoute($request, $routeName); |
| 73 | - } catch (RouteException $e) { |
|
| 73 | + }catch (RouteException $e){ |
|
| 74 | 74 | throw new RouterException('Invalid route definition', $e->getCode(), $e); |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - if ($route === null) { |
|
| 77 | + if ($route === null){ |
|
| 78 | 78 | $this->eventDispatcher?->dispatch(new RouteNotFound($request)); |
| 79 | 79 | throw new RouteNotFoundException($request->getUri()); |
| 80 | 80 | } |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | public function getRoute(string $name): RouteInterface |
| 111 | 111 | { |
| 112 | - if (isset($this->routes[$name])) { |
|
| 112 | + if (isset($this->routes[$name])){ |
|
| 113 | 113 | return $this->routes[$name]; |
| 114 | 114 | } |
| 115 | 115 | |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | |
| 119 | 119 | public function getRoutes(): array |
| 120 | 120 | { |
| 121 | - if (!empty($this->default)) { |
|
| 121 | + if (!empty($this->default)){ |
|
| 122 | 122 | return $this->routes + [null => $this->default]; |
| 123 | 123 | } |
| 124 | 124 | |
@@ -127,9 +127,9 @@ discard block |
||
| 127 | 127 | |
| 128 | 128 | public function uri(string $route, iterable $parameters = []): UriInterface |
| 129 | 129 | { |
| 130 | - try { |
|
| 130 | + try{ |
|
| 131 | 131 | return $this->getRoute($route)->uri($parameters); |
| 132 | - } catch (UndefinedRouteException) { |
|
| 132 | + }catch (UndefinedRouteException){ |
|
| 133 | 133 | //In some cases route name can be provided as controller:action pair, we can try to |
| 134 | 134 | //generate such route automatically based on our default/fallback route |
| 135 | 135 | return $this->castRoute($route)->uri($parameters); |
@@ -141,29 +141,29 @@ discard block |
||
| 141 | 141 | /** @var GroupRegistry $groups */ |
| 142 | 142 | $groups = $this->container->get(GroupRegistry::class); |
| 143 | 143 | |
| 144 | - foreach ($routes->getCollection() as $name => $configurator) { |
|
| 144 | + foreach ($routes->getCollection() as $name => $configurator){ |
|
| 145 | 145 | $target = $configurator->target; |
| 146 | - if ($configurator->core !== null && $target instanceof AbstractTarget) { |
|
| 146 | + if ($configurator->core !== null && $target instanceof AbstractTarget){ |
|
| 147 | 147 | $target = $target->withCore($configurator->core); |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | $route = new Route(\ltrim($configurator->pattern, '/'), $target, $configurator->defaults); |
| 151 | 151 | |
| 152 | - if ($configurator->middleware !== null) { |
|
| 152 | + if ($configurator->middleware !== null){ |
|
| 153 | 153 | $route = $route->withMiddleware(...$configurator->middleware); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - if ($configurator->methods !== null) { |
|
| 156 | + if ($configurator->methods !== null){ |
|
| 157 | 157 | $route = $route->withVerbs(...$configurator->methods); |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 160 | + if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME){ |
|
| 161 | 161 | $group = $groups->getGroup($configurator->group ?? $groups->getDefaultGroup()); |
| 162 | 162 | $group->setPrefix($configurator->prefix); |
| 163 | 163 | $group->addRoute($name, $route); |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 166 | + if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME){ |
|
| 167 | 167 | $this->setDefault($route); |
| 168 | 168 | } |
| 169 | 169 | } |
@@ -174,17 +174,17 @@ discard block |
||
| 174 | 174 | */ |
| 175 | 175 | protected function matchRoute(ServerRequestInterface $request, string &$routeName = null): ?RouteInterface |
| 176 | 176 | { |
| 177 | - foreach ($this->routes as $name => $route) { |
|
| 177 | + foreach ($this->routes as $name => $route){ |
|
| 178 | 178 | // Matched route will return new route instance with matched parameters |
| 179 | 179 | $matched = $route->match($request); |
| 180 | 180 | |
| 181 | - if ($matched !== null) { |
|
| 181 | + if ($matched !== null){ |
|
| 182 | 182 | $routeName = $name; |
| 183 | 183 | return $matched; |
| 184 | 184 | } |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - if ($this->default !== null) { |
|
| 187 | + if ($this->default !== null){ |
|
| 188 | 188 | return $this->default->match($request); |
| 189 | 189 | } |
| 190 | 190 | |
@@ -197,14 +197,14 @@ discard block |
||
| 197 | 197 | */ |
| 198 | 198 | protected function configure(RouteInterface $route): RouteInterface |
| 199 | 199 | { |
| 200 | - if ($route instanceof ContainerizedInterface && !$route->hasContainer()) { |
|
| 200 | + if ($route instanceof ContainerizedInterface && !$route->hasContainer()){ |
|
| 201 | 201 | // isolating route in a given container |
| 202 | 202 | $route = $route->withContainer($this->container); |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | - try { |
|
| 205 | + try{ |
|
| 206 | 206 | $uriHandler = $route->getUriHandler(); |
| 207 | - } catch (\Throwable) { |
|
| 207 | + }catch (\Throwable){ |
|
| 208 | 208 | $uriHandler = $this->uriHandler; |
| 209 | 209 | } |
| 210 | 210 | |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | $route, |
| 228 | 228 | $matches |
| 229 | 229 | ) |
| 230 | - ) { |
|
| 230 | + ){ |
|
| 231 | 231 | throw new UndefinedRouteException( |
| 232 | 232 | "Unable to locate route or use default route with 'name/controller:action' pattern" |
| 233 | 233 | ); |
@@ -236,11 +236,11 @@ discard block |
||
| 236 | 236 | /** |
| 237 | 237 | * @var Matches $matches |
| 238 | 238 | */ |
| 239 | - if (!empty($matches['name'])) { |
|
| 239 | + if (!empty($matches['name'])){ |
|
| 240 | 240 | $routeObject = $this->getRoute($matches['name']); |
| 241 | - } elseif ($this->default !== null) { |
|
| 241 | + } elseif ($this->default !== null){ |
|
| 242 | 242 | $routeObject = $this->default; |
| 243 | - } else { |
|
| 243 | + }else{ |
|
| 244 | 244 | throw new UndefinedRouteException(\sprintf('Unable to locate route candidate for `%s`', $route)); |
| 245 | 245 | } |
| 246 | 246 | |
@@ -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 | |
@@ -50,9 +50,9 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | public function __construct( |
| 52 | 52 | string $pattern, |
| 53 | - string|callable|RequestHandlerInterface|TargetInterface $target, |
|
| 53 | + string | callable | RequestHandlerInterface | TargetInterface $target, |
|
| 54 | 54 | array $defaults = [] |
| 55 | - ) { |
|
| 55 | + ){ |
|
| 56 | 56 | parent::__construct( |
| 57 | 57 | $pattern, |
| 58 | 58 | $target instanceof TargetInterface |
@@ -68,7 +68,7 @@ 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 | $uriHandler = $uriHandler->withConstrains( |
| 73 | 73 | $this->target->getConstrains(), |
| 74 | 74 | $this->defaults, |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | $route = clone $this; |
| 87 | 87 | $route->container = $container; |
| 88 | 88 | |
| 89 | - if ($route->target instanceof TargetInterface) { |
|
| 89 | + if ($route->target instanceof TargetInterface){ |
|
| 90 | 90 | $route->target = clone $route->target; |
| 91 | 91 | } |
| 92 | 92 | |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | public function handle(ServerRequestInterface $request): ResponseInterface |
| 115 | 115 | { |
| 116 | - if (empty($this->requestHandler)) { |
|
| 116 | + if (empty($this->requestHandler)){ |
|
| 117 | 117 | $this->requestHandler = $this->requestHandler(); |
| 118 | 118 | } |
| 119 | 119 | |
@@ -129,29 +129,29 @@ discard block |
||
| 129 | 129 | */ |
| 130 | 130 | protected function requestHandler(): RequestHandlerInterface |
| 131 | 131 | { |
| 132 | - if (!$this->hasContainer()) { |
|
| 132 | + if (!$this->hasContainer()){ |
|
| 133 | 133 | throw new RouteException('Unable to configure route pipeline without associated container'); |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - if ($this->target instanceof TargetInterface) { |
|
| 137 | - try { |
|
| 136 | + if ($this->target instanceof TargetInterface){ |
|
| 137 | + try{ |
|
| 138 | 138 | \assert($this->matches !== null); |
| 139 | 139 | return $this->target->getHandler($this->container, $this->matches); |
| 140 | - } catch (TargetException $e) { |
|
| 140 | + }catch (TargetException $e){ |
|
| 141 | 141 | throw new RouteException('Invalid target resolution', $e->getCode(), $e); |
| 142 | 142 | } |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - if ($this->target instanceof RequestHandlerInterface) { |
|
| 145 | + if ($this->target instanceof RequestHandlerInterface){ |
|
| 146 | 146 | return $this->target; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - try { |
|
| 149 | + try{ |
|
| 150 | 150 | $target = \is_string($this->target) |
| 151 | 151 | ? $this->container->get($this->target) |
| 152 | 152 | : $this->target; |
| 153 | 153 | |
| 154 | - if ($target instanceof RequestHandlerInterface) { |
|
| 154 | + if ($target instanceof RequestHandlerInterface){ |
|
| 155 | 155 | return $target; |
| 156 | 156 | } |
| 157 | 157 | |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | $target, |
| 160 | 160 | $this->container->get(ResponseFactoryInterface::class) |
| 161 | 161 | ); |
| 162 | - } catch (ContainerExceptionInterface $e) { |
|
| 162 | + }catch (ContainerExceptionInterface $e){ |
|
| 163 | 163 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
| 164 | 164 | } |
| 165 | 165 | } |
@@ -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,16 @@ 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 | $middleware = $middleware[0]; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** @var MiddlewareType[] $middleware */ |
| 51 | - foreach ($middleware as $item) { |
|
| 51 | + foreach ($middleware as $item){ |
|
| 52 | 52 | $route->middleware[] = $item; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - if ($route->pipeline !== null) { |
|
| 55 | + if ($route->pipeline !== null){ |
|
| 56 | 56 | $route->pipeline = $route->makePipeline(); |
| 57 | 57 | } |
| 58 | 58 | |
@@ -77,11 +77,11 @@ discard block |
||
| 77 | 77 | protected function makePipeline(): Pipeline |
| 78 | 78 | { |
| 79 | 79 | \assert($this->container !== null); |
| 80 | - try { |
|
| 80 | + try{ |
|
| 81 | 81 | return $this->container |
| 82 | 82 | ->get(PipelineFactory::class) |
| 83 | 83 | ->createWithMiddleware($this->middleware); |
| 84 | - } catch (ContainerExceptionInterface $e) { |
|
| 84 | + }catch (ContainerExceptionInterface $e){ |
|
| 85 | 85 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
| 86 | 86 | } |
| 87 | 87 | } |
@@ -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 | } |
@@ -17,9 +17,9 @@ |
||
| 17 | 17 | * @param non-empty-string $name |
| 18 | 18 | * @param non-empty-string|\Stringable $pattern |
| 19 | 19 | */ |
| 20 | - public function register(string $name, string|\Stringable $pattern): void |
|
| 20 | + public function register(string $name, string | \Stringable $pattern): void |
|
| 21 | 21 | { |
| 22 | - $pattern = (string) $pattern; |
|
| 22 | + $pattern = (string)$pattern; |
|
| 23 | 23 | \assert($pattern !== ''); |
| 24 | 24 | $this->patterns[$name] = $pattern; |
| 25 | 25 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | /** @var array<MiddlewareType> */ |
| 28 | 28 | private array $middleware = []; |
| 29 | 29 | |
| 30 | - private Autowire|CoreInterface|string|null $core = null; |
|
| 30 | + private Autowire | CoreInterface | string | null $core = null; |
|
| 31 | 31 | |
| 32 | 32 | public function __construct( |
| 33 | 33 | /** @deprecated since v3.3.0 */ |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | private readonly ?RouterInterface $router = null, |
| 37 | 37 | /** @deprecated since v3.3.0 */ |
| 38 | 38 | private readonly ?UriHandler $handler = null |
| 39 | - ) { |
|
| 39 | + ){ |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | return $this; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - public function setCore(Autowire|CoreInterface|string $core): self |
|
| 70 | + public function setCore(Autowire | CoreInterface | string $core): self |
|
| 71 | 71 | { |
| 72 | 72 | $this->core = $core; |
| 73 | 73 | |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | /** |
| 78 | 78 | * @param MiddlewareType $middleware |
| 79 | 79 | */ |
| 80 | - public function addMiddleware(MiddlewareInterface|Autowire|string $middleware): self |
|
| 80 | + public function addMiddleware(MiddlewareInterface | Autowire | string $middleware): self |
|
| 81 | 81 | { |
| 82 | 82 | $this->middleware[] = $middleware; |
| 83 | 83 | |
@@ -91,21 +91,21 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function register(RouterInterface $router, FactoryInterface $factory): void |
| 93 | 93 | { |
| 94 | - foreach ($this->routes as $name => $route) { |
|
| 95 | - if ($this->core !== null) { |
|
| 96 | - if (!$this->core instanceof CoreInterface) { |
|
| 94 | + foreach ($this->routes as $name => $route){ |
|
| 95 | + if ($this->core !== null){ |
|
| 96 | + if (!$this->core instanceof CoreInterface){ |
|
| 97 | 97 | $this->core = $factory->make($this->core); |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | $target = $route->getTarget(); |
| 101 | - if ($target instanceof AbstractTarget) { |
|
| 101 | + if ($target instanceof AbstractTarget){ |
|
| 102 | 102 | $route = $route->withTarget($target->withCore($this->core)); |
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | - try { |
|
| 106 | + try{ |
|
| 107 | 107 | $uriHandler = $route->getUriHandler(); |
| 108 | - } catch (\Throwable) { |
|
| 108 | + }catch (\Throwable){ |
|
| 109 | 109 | $uriHandler = $factory->make(UriHandler::class); |
| 110 | 110 | } |
| 111 | 111 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | { |
| 130 | 130 | \assert($route instanceof Route); |
| 131 | 131 | |
| 132 | - $this->routes[$this->namePrefix . $name] = $route; |
|
| 132 | + $this->routes[$this->namePrefix.$name] = $route; |
|
| 133 | 133 | |
| 134 | 134 | return $this; |
| 135 | 135 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | private readonly UriFactoryInterface $uriFactory, |
| 54 | 54 | SlugifyInterface $slugify = null, |
| 55 | 55 | ?RoutePatternRegistryInterface $patternRegistry = null, |
| 56 | - ) { |
|
| 56 | + ){ |
|
| 57 | 57 | $this->patternRegistry = $patternRegistry ?? new DefaultPatternRegistry(); |
| 58 | 58 | $this->slugify = $slugify ?? new Slugify(); |
| 59 | 59 | } |
@@ -103,7 +103,7 @@ 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 | $basePath .= '/'; |
| 108 | 108 | } |
| 109 | 109 | |
@@ -154,12 +154,12 @@ discard block |
||
| 154 | 154 | */ |
| 155 | 155 | public function match(UriInterface $uri, array $defaults): ?array |
| 156 | 156 | { |
| 157 | - if (!$this->isCompiled()) { |
|
| 157 | + if (!$this->isCompiled()){ |
|
| 158 | 158 | $this->compile(); |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | $matches = []; |
| 162 | - if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
| 162 | + if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)){ |
|
| 163 | 163 | return null; |
| 164 | 164 | } |
| 165 | 165 | |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | */ |
| 177 | 177 | public function uri(iterable $parameters = [], array $defaults = []): UriInterface |
| 178 | 178 | { |
| 179 | - if (!$this->isCompiled()) { |
|
| 179 | + if (!$this->isCompiled()){ |
|
| 180 | 180 | $this->compile(); |
| 181 | 181 | } |
| 182 | 182 | |
@@ -186,8 +186,8 @@ discard block |
||
| 186 | 186 | $this->fetchOptions($parameters, $query) |
| 187 | 187 | ); |
| 188 | 188 | |
| 189 | - foreach ($this->constrains as $key => $_) { |
|
| 190 | - if (empty($parameters[$key])) { |
|
| 189 | + foreach ($this->constrains as $key => $_){ |
|
| 190 | + if (empty($parameters[$key])){ |
|
| 191 | 191 | throw new UriHandlerException(\sprintf('Unable to generate Uri, parameter `%s` is missing', $key)); |
| 192 | 192 | } |
| 193 | 193 | } |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | $path = $this->interpolate($this->template, $parameters); |
| 197 | 197 | |
| 198 | 198 | //Uri with added base path and prefix |
| 199 | - $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath) . \trim($path, '/')); |
|
| 199 | + $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath).\trim($path, '/')); |
|
| 200 | 200 | |
| 201 | 201 | return empty($query) ? $uri : $uri->withQuery(\http_build_query($query)); |
| 202 | 202 | } |
@@ -211,18 +211,18 @@ discard block |
||
| 211 | 211 | $allowed = \array_keys($this->options); |
| 212 | 212 | |
| 213 | 213 | $result = []; |
| 214 | - foreach ($parameters as $key => $parameter) { |
|
| 215 | - if (\is_int($key) && isset($allowed[$key])) { |
|
| 214 | + foreach ($parameters as $key => $parameter){ |
|
| 215 | + if (\is_int($key) && isset($allowed[$key])){ |
|
| 216 | 216 | // this segment fetched keys from given parameters either by name or by position |
| 217 | 217 | $key = $allowed[$key]; |
| 218 | - } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) { |
|
| 218 | + } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)){ |
|
| 219 | 219 | // all additional parameters given in array form can be glued to query string |
| 220 | 220 | $query[$key] = $parameter; |
| 221 | 221 | continue; |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | //String must be normalized here |
| 225 | - if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
| 225 | + if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)){ |
|
| 226 | 226 | $result[$key] = $this->slugify->slugify($parameter); |
| 227 | 227 | continue; |
| 228 | 228 | } |
@@ -240,15 +240,15 @@ discard block |
||
| 240 | 240 | { |
| 241 | 241 | $path = $uri->getPath(); |
| 242 | 242 | |
| 243 | - if (empty($path) || $path[0] !== '/') { |
|
| 244 | - $path = '/' . $path; |
|
| 243 | + if (empty($path) || $path[0] !== '/'){ |
|
| 244 | + $path = '/'.$path; |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - if ($this->matchHost) { |
|
| 248 | - $uriString = $uri->getHost() . $path; |
|
| 249 | - } else { |
|
| 247 | + if ($this->matchHost){ |
|
| 248 | + $uriString = $uri->getHost().$path; |
|
| 249 | + }else{ |
|
| 250 | 250 | $uriString = \substr($path, \strlen($this->basePath)); |
| 251 | - if ($uriString === false) { |
|
| 251 | + if ($uriString === false){ |
|
| 252 | 252 | $uriString = ''; |
| 253 | 253 | } |
| 254 | 254 | } |
@@ -264,23 +264,23 @@ discard block |
||
| 264 | 264 | */ |
| 265 | 265 | private function compile(): void |
| 266 | 266 | { |
| 267 | - if ($this->pattern === null) { |
|
| 267 | + if ($this->pattern === null){ |
|
| 268 | 268 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | $options = []; |
| 272 | 272 | $replaces = []; |
| 273 | - $pattern = \rtrim(\ltrim($this->getPrefix() . '/' . $this->pattern, ':/'), '/'); |
|
| 273 | + $pattern = \rtrim(\ltrim($this->getPrefix().'/'.$this->pattern, ':/'), '/'); |
|
| 274 | 274 | |
| 275 | 275 | // correct [/ first occurrence] |
| 276 | - if (\str_starts_with($pattern, '[/')) { |
|
| 277 | - $pattern = '[' . \substr($pattern, 2); |
|
| 276 | + if (\str_starts_with($pattern, '[/')){ |
|
| 277 | + $pattern = '['.\substr($pattern, 2); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
| 280 | + if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)){ |
|
| 281 | 281 | $variables = \array_combine($matches[1], $matches[2]); |
| 282 | 282 | |
| 283 | - foreach ($variables as $key => $segment) { |
|
| 283 | + foreach ($variables as $key => $segment){ |
|
| 284 | 284 | $segment = $this->prepareSegment($key, $segment); |
| 285 | 285 | $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment); |
| 286 | 286 | $options[] = $key; |
@@ -290,13 +290,13 @@ discard block |
||
| 290 | 290 | $template = \preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
| 291 | 291 | $options = \array_fill_keys($options, null); |
| 292 | 292 | |
| 293 | - foreach ($this->constrains as $key => $value) { |
|
| 294 | - if ($value instanceof Autofill) { |
|
| 293 | + foreach ($this->constrains as $key => $value){ |
|
| 294 | + if ($value instanceof Autofill){ |
|
| 295 | 295 | // only forces value replacement, not required to be presented as parameter |
| 296 | 296 | continue; |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | - if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
| 299 | + if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])){ |
|
| 300 | 300 | throw new ConstrainException( |
| 301 | 301 | \sprintf( |
| 302 | 302 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -307,7 +307,7 @@ discard block |
||
| 307 | 307 | } |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | - $this->compiled = '/^' . \strtr($template, $replaces + self::PATTERN_REPLACES) . '$/iu'; |
|
| 310 | + $this->compiled = '/^'.\strtr($template, $replaces + self::PATTERN_REPLACES).'$/iu'; |
|
| 311 | 311 | $this->template = \stripslashes(\str_replace('?', '', $template)); |
| 312 | 312 | $this->options = $options; |
| 313 | 313 | } |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | private function interpolate(string $string, array $values): string |
| 319 | 319 | { |
| 320 | 320 | $replaces = []; |
| 321 | - foreach ($values as $key => $value) { |
|
| 321 | + foreach ($values as $key => $value){ |
|
| 322 | 322 | $replaces[\sprintf('<%s>', $key)] = match (true) { |
| 323 | 323 | $value instanceof \Stringable || \is_scalar($value) => (string)$value, |
| 324 | 324 | default => '', |
@@ -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 => '', |