Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 28 additions & 20 deletions src/Http/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -540,7 +543,7 @@ public function getRoute(): ?Route
*/
public function setRoute(Route $route): self
{
$this->route = $route;
$this->setRequestResource('route', fn () => $route, []);

return $this;
}
Expand Down Expand Up @@ -675,18 +678,26 @@ 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);
$url = \is_string($url) ? ($url === '' ? '/' : $url) : '/';
$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;
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Loading