@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | $dispatcher |
| 55 | 55 | ->expects(self::exactly(2)) |
| 56 | 56 | ->method('dispatch') |
| 57 | - ->with($this->callback(static fn (Routing|RouteMatched $event): bool => $event->request instanceof ServerRequest)); |
|
| 57 | + ->with($this->callback(static fn (Routing | RouteMatched $event): bool => $event->request instanceof ServerRequest)); |
|
| 58 | 58 | |
| 59 | 59 | $router = $this->makeRouter('', $dispatcher); |
| 60 | 60 | $router->setDefault($route); |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $router->import($configurator); |
| 91 | 91 | $this->container->get(GroupRegistry::class)->registerRoutes($router); |
| 92 | 92 | |
| 93 | - $uri = (string) $router->uri('foo', ['host' => 'some']); |
|
| 93 | + $uri = (string)$router->uri('foo', ['host' => 'some']); |
|
| 94 | 94 | $this->assertSame('some/register', $uri); |
| 95 | 95 | $this->assertFalse(\str_contains('https://host.com', $uri)); |
| 96 | 96 | } |
@@ -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,9 +141,9 @@ 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 | |
@@ -152,21 +152,21 @@ discard block |
||
| 152 | 152 | : \ltrim($configurator->pattern, '/'); |
| 153 | 153 | $route = new Route($pattern, $target, $configurator->defaults); |
| 154 | 154 | |
| 155 | - if ($configurator->middleware !== null) { |
|
| 155 | + if ($configurator->middleware !== null){ |
|
| 156 | 156 | $route = $route->withMiddleware(...$configurator->middleware); |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - if ($configurator->methods !== null) { |
|
| 159 | + if ($configurator->methods !== null){ |
|
| 160 | 160 | $route = $route->withVerbs(...$configurator->methods); |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 163 | + if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME){ |
|
| 164 | 164 | $group = $groups->getGroup($configurator->group ?? $groups->getDefaultGroup()); |
| 165 | 165 | $group->setPrefix($configurator->prefix); |
| 166 | 166 | $group->addRoute($name, $route); |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 169 | + if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME){ |
|
| 170 | 170 | $this->setDefault($route); |
| 171 | 171 | } |
| 172 | 172 | } |
@@ -177,17 +177,17 @@ discard block |
||
| 177 | 177 | */ |
| 178 | 178 | protected function matchRoute(ServerRequestInterface $request, string &$routeName = null): ?RouteInterface |
| 179 | 179 | { |
| 180 | - foreach ($this->routes as $name => $route) { |
|
| 180 | + foreach ($this->routes as $name => $route){ |
|
| 181 | 181 | // Matched route will return new route instance with matched parameters |
| 182 | 182 | $matched = $route->match($request); |
| 183 | 183 | |
| 184 | - if ($matched !== null) { |
|
| 184 | + if ($matched !== null){ |
|
| 185 | 185 | $routeName = $name; |
| 186 | 186 | return $matched; |
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - if ($this->default !== null) { |
|
| 190 | + if ($this->default !== null){ |
|
| 191 | 191 | return $this->default->match($request); |
| 192 | 192 | } |
| 193 | 193 | |
@@ -200,14 +200,14 @@ discard block |
||
| 200 | 200 | */ |
| 201 | 201 | protected function configure(RouteInterface $route): RouteInterface |
| 202 | 202 | { |
| 203 | - if ($route instanceof ContainerizedInterface && !$route->hasContainer()) { |
|
| 203 | + if ($route instanceof ContainerizedInterface && !$route->hasContainer()){ |
|
| 204 | 204 | // isolating route in a given container |
| 205 | 205 | $route = $route->withContainer($this->container); |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | - try { |
|
| 208 | + try{ |
|
| 209 | 209 | $uriHandler = $route->getUriHandler(); |
| 210 | - } catch (\Throwable) { |
|
| 210 | + }catch (\Throwable){ |
|
| 211 | 211 | $uriHandler = $this->uriHandler; |
| 212 | 212 | } |
| 213 | 213 | |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | $route, |
| 231 | 231 | $matches |
| 232 | 232 | ) |
| 233 | - ) { |
|
| 233 | + ){ |
|
| 234 | 234 | throw new UndefinedRouteException( |
| 235 | 235 | "Unable to locate route or use default route with 'name/controller:action' pattern" |
| 236 | 236 | ); |
@@ -239,11 +239,11 @@ discard block |
||
| 239 | 239 | /** |
| 240 | 240 | * @var Matches $matches |
| 241 | 241 | */ |
| 242 | - if (!empty($matches['name'])) { |
|
| 242 | + if (!empty($matches['name'])){ |
|
| 243 | 243 | $routeObject = $this->getRoute($matches['name']); |
| 244 | - } elseif ($this->default !== null) { |
|
| 244 | + } elseif ($this->default !== null){ |
|
| 245 | 245 | $routeObject = $this->default; |
| 246 | - } else { |
|
| 246 | + }else{ |
|
| 247 | 247 | throw new UndefinedRouteException(\sprintf('Unable to locate route candidate for `%s`', $route)); |
| 248 | 248 | } |
| 249 | 249 | |
@@ -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,9 +151,11 @@ 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 | |
@@ -152,21 +164,25 @@ discard block |
||
| 152 | 164 | : \ltrim($configurator->pattern, '/'); |
| 153 | 165 | $route = new Route($pattern, $target, $configurator->defaults); |
| 154 | 166 | |
| 155 | - if ($configurator->middleware !== null) { |
|
| 167 | + if ($configurator->middleware !== null) |
|
| 168 | + { |
|
| 156 | 169 | $route = $route->withMiddleware(...$configurator->middleware); |
| 157 | 170 | } |
| 158 | 171 | |
| 159 | - if ($configurator->methods !== null) { |
|
| 172 | + if ($configurator->methods !== null) |
|
| 173 | + { |
|
| 160 | 174 | $route = $route->withVerbs(...$configurator->methods); |
| 161 | 175 | } |
| 162 | 176 | |
| 163 | - if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 177 | + if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) |
|
| 178 | + { |
|
| 164 | 179 | $group = $groups->getGroup($configurator->group ?? $groups->getDefaultGroup()); |
| 165 | 180 | $group->setPrefix($configurator->prefix); |
| 166 | 181 | $group->addRoute($name, $route); |
| 167 | 182 | } |
| 168 | 183 | |
| 169 | - if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 184 | + if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) |
|
| 185 | + { |
|
| 170 | 186 | $this->setDefault($route); |
| 171 | 187 | } |
| 172 | 188 | } |
@@ -177,17 +193,20 @@ discard block |
||
| 177 | 193 | */ |
| 178 | 194 | protected function matchRoute(ServerRequestInterface $request, string &$routeName = null): ?RouteInterface |
| 179 | 195 | { |
| 180 | - foreach ($this->routes as $name => $route) { |
|
| 196 | + foreach ($this->routes as $name => $route) |
|
| 197 | + { |
|
| 181 | 198 | // Matched route will return new route instance with matched parameters |
| 182 | 199 | $matched = $route->match($request); |
| 183 | 200 | |
| 184 | - if ($matched !== null) { |
|
| 201 | + if ($matched !== null) |
|
| 202 | + { |
|
| 185 | 203 | $routeName = $name; |
| 186 | 204 | return $matched; |
| 187 | 205 | } |
| 188 | 206 | } |
| 189 | 207 | |
| 190 | - if ($this->default !== null) { |
|
| 208 | + if ($this->default !== null) |
|
| 209 | + { |
|
| 191 | 210 | return $this->default->match($request); |
| 192 | 211 | } |
| 193 | 212 | |
@@ -200,14 +219,18 @@ discard block |
||
| 200 | 219 | */ |
| 201 | 220 | protected function configure(RouteInterface $route): RouteInterface |
| 202 | 221 | { |
| 203 | - if ($route instanceof ContainerizedInterface && !$route->hasContainer()) { |
|
| 222 | + if ($route instanceof ContainerizedInterface && !$route->hasContainer()) |
|
| 223 | + { |
|
| 204 | 224 | // isolating route in a given container |
| 205 | 225 | $route = $route->withContainer($this->container); |
| 206 | 226 | } |
| 207 | 227 | |
| 208 | - try { |
|
| 228 | + try |
|
| 229 | + { |
|
| 209 | 230 | $uriHandler = $route->getUriHandler(); |
| 210 | - } catch (\Throwable) { |
|
| 231 | + } |
|
| 232 | + catch (\Throwable) |
|
| 233 | + { |
|
| 211 | 234 | $uriHandler = $this->uriHandler; |
| 212 | 235 | } |
| 213 | 236 | |
@@ -239,11 +262,16 @@ discard block |
||
| 239 | 262 | /** |
| 240 | 263 | * @var Matches $matches |
| 241 | 264 | */ |
| 242 | - if (!empty($matches['name'])) { |
|
| 265 | + if (!empty($matches['name'])) |
|
| 266 | + { |
|
| 243 | 267 | $routeObject = $this->getRoute($matches['name']); |
| 244 | - } elseif ($this->default !== null) { |
|
| 268 | + } |
|
| 269 | + elseif ($this->default !== null) |
|
| 270 | + { |
|
| 245 | 271 | $routeObject = $this->default; |
| 246 | - } else { |
|
| 272 | + } |
|
| 273 | + else |
|
| 274 | + { |
|
| 247 | 275 | throw new UndefinedRouteException(\sprintf('Unable to locate route candidate for `%s`', $route)); |
| 248 | 276 | } |
| 249 | 277 | |