diff --git a/composer.json b/composer.json index 833b4cc..2ce655a 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "require": { "php": ">=8.2", "ext-swoole": "*", - "utopia-php/di": "0.3.*", + "utopia-php/di": "^0.3.2", "utopia-php/servers": "0.3.*", "utopia-php/compression": "0.1.*", "utopia-php/telemetry": "0.2.*", diff --git a/composer.lock b/composer.lock index 6932a58..7860826 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "836a13c7945c3bf4de3cd30991d29bf0", + "content-hash": "0a629fd72a7f6958b59d11324b2436d4", "packages": [ { "name": "brick/math", @@ -1915,16 +1915,16 @@ }, { "name": "utopia-php/di", - "version": "0.3.1", + "version": "0.3.2", "source": { "type": "git", "url": "https://github.com/utopia-php/di.git", - "reference": "68873b7267842315d01d82a83b988bae525eab31" + "reference": "07025d721ed5d9be27932e8e640acf1467fc4b9d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/di/zipball/68873b7267842315d01d82a83b988bae525eab31", - "reference": "68873b7267842315d01d82a83b988bae525eab31", + "url": "https://api.github.com/repos/utopia-php/di/zipball/07025d721ed5d9be27932e8e640acf1467fc4b9d", + "reference": "07025d721ed5d9be27932e8e640acf1467fc4b9d", "shasum": "" }, "require": { @@ -1960,9 +1960,9 @@ ], "support": { "issues": "https://github.com/utopia-php/di/issues", - "source": "https://github.com/utopia-php/di/tree/0.3.1" + "source": "https://github.com/utopia-php/di/tree/0.3.2" }, - "time": "2026-03-13T05:47:23+00:00" + "time": "2026-03-21T07:42:10+00:00" }, { "name": "utopia-php/servers", diff --git a/src/Http/Http.php b/src/Http/Http.php index 7bdd100..3753c8d 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -110,15 +110,6 @@ class Http */ protected static array $requestHooks = []; - /** - * Route - * - * Memory cached result for chosen route - * - * @var Route|null - */ - protected ?Route $route = null; - /** * Wildcard route * If set, this get's executed if no other route is matched @@ -530,7 +521,19 @@ public static function getRoutes(): array */ public function getRoute(): ?Route { - return $this->route ?? null; + $container = $this->server->getContainer(); + + if (!$container->has('route')) { + return null; + } + + try { + $route = $container->get('route'); + } catch (\Throwable) { + return null; + } + + return $route instanceof Route ? $route : null; } /** @@ -540,7 +543,7 @@ public function getRoute(): ?Route */ public function setRoute(Route $route): self { - $this->route = $route; + $this->setRequestResource('route', fn () => $route, []); return $this; } @@ -675,8 +678,12 @@ public function start() */ public function match(Request $request, bool $fresh = true): ?Route { - if (null !== $this->route && !$fresh) { - return $this->route; + if (!$fresh) { + $cached = $this->getRoute(); + + if (null !== $cached) { + return $cached; + } } $url = \parse_url($request->getURI(), PHP_URL_PATH); @@ -684,9 +691,13 @@ public function match(Request $request, bool $fresh = true): ?Route $method = $request->getMethod(); $method = (self::REQUEST_METHOD_HEAD == $method) ? self::REQUEST_METHOD_GET : $method; - $this->route = Router::match($method, $url); + $route = Router::match($method, $url); - return $this->route; + if (null !== $route) { + $this->setRequestResource('route', fn () => $route, []); + } + + return $route; } /** @@ -837,7 +848,7 @@ public function run(Request $request, Response $response): static $attributes = [ 'url.scheme' => $request->getProtocol(), 'http.request.method' => $request->getMethod(), - 'http.route' => $this->route?->getPath(), + 'http.route' => $this->getRoute()?->getPath(), 'http.response.status_code' => $response->getStatusCode(), ]; $this->requestDuration->record($requestDuration, $attributes); @@ -907,8 +918,6 @@ private function runInternal(Request $request, Response $response): static $route = $this->match($request); $groups = ($route instanceof Route) ? $route->getGroups() : []; - $this->setRequestResource('route', fn () => $route, []); - if (self::REQUEST_METHOD_HEAD == $method) { $method = self::REQUEST_METHOD_GET; $response->disablePayload(); @@ -947,8 +956,7 @@ private function runInternal(Request $request, Response $response): static } if (null === $route && null !== self::$wildcardRoute) { - $route = self::$wildcardRoute; - $this->route = $route; + $route = clone self::$wildcardRoute; $path = \parse_url($request->getURI(), PHP_URL_PATH); $path = \is_string($path) ? ($path === '' ? '/' : $path) : '/'; $route->path($path);