@@ -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 | |