Skip to content

Commit 2f1b5ab

Browse files
committed
try fix alias
1 parent af22ce5 commit 2f1b5ab

4 files changed

Lines changed: 71 additions & 31 deletions

File tree

src/Http/Adapter/Swoole/Server.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ public function __construct(string $host, ?string $port = null, array $settings
2828
public function onRequest(callable $callback)
2929
{
3030
$this->server->on('request', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
31-
go(function () use ($request, $response, $callback) {
32-
$requestContainer = new Container($this->container);
33-
$requestContainer->set('swooleRequest', fn () => $request);
34-
$requestContainer->set('swooleResponse', fn () => $response);
31+
$requestContainer = new Container($this->container);
32+
$requestContainer->set('swooleRequest', fn () => $request);
33+
$requestContainer->set('swooleResponse', fn () => $response);
3534

36-
Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer;
35+
Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer;
3736

38-
\call_user_func($callback, new Request($request), new Response($response));
39-
});
37+
\call_user_func($callback, new Request($request), new Response($response));
4038
});
4139
}
4240

src/Http/Http.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public function __construct(Adapter $server, string $timezone)
146146
\date_default_timezone_set($timezone);
147147
$this->files = new Files();
148148
$this->server = $server;
149+
$this->container = $server->getContainer();
149150
}
150151

151152
/**
@@ -427,6 +428,16 @@ public function getResources(array $list): array
427428
* @param string[] $injections
428429
*/
429430
public function setResource(string $name, callable $callback, array $injections = []): void
431+
{
432+
$this->container->set($name, $callback, $injections);
433+
}
434+
435+
/**
436+
* Set a request-scoped resource on the current request's container.
437+
*
438+
* @param string[] $injections
439+
*/
440+
protected function setRequestResource(string $name, callable $callback, array $injections = []): void
430441
{
431442
$this->server->getContainer()->set($name, $callback, $injections);
432443
}
@@ -648,7 +659,9 @@ public function execute(Route $route, Request $request): static
648659
{
649660
$arguments = [];
650661
$groups = $route->getGroups();
651-
$pathValues = $route->getPathValues($request);
662+
663+
$preparedPath = Router::preparePath($route->getMatchedPath());
664+
$pathValues = $route->getPathValues($request, $preparedPath[0]);
652665

653666
try {
654667
if ($route->getHook()) {
@@ -690,7 +703,7 @@ public function execute(Route $route, Request $request): static
690703
}
691704
}
692705
} catch (\Throwable $e) {
693-
$this->setResource('error', fn () => $e, []);
706+
$this->setRequestResource('error', fn () => $e, []);
694707

695708
foreach ($groups as $group) {
696709
foreach (self::$errors as $error) { // Group error hooks
@@ -782,16 +795,16 @@ public function run(Request $request, Response $response): static
782795
$response->setCompressionSupported($this->compressionSupported);
783796
}
784797

785-
$this->setResource('request', fn () => $request);
786-
$this->setResource('response', fn () => $response);
798+
$this->setRequestResource('request', fn () => $request);
799+
$this->setRequestResource('response', fn () => $response);
787800

788801
try {
789802
foreach (self::$requestHooks as $hook) {
790803
$arguments = $this->getArguments($hook, [], []);
791804
\call_user_func_array($hook->getAction(), $arguments);
792805
}
793806
} catch (\Exception $e) {
794-
$this->setResource('error', fn () => $e, []);
807+
$this->setRequestResource('error', fn () => $e, []);
795808

796809
foreach (self::$errors as $error) { // Global error hooks
797810
if (\in_array('*', $error->getGroups())) {
@@ -821,7 +834,7 @@ public function run(Request $request, Response $response): static
821834
$route = $this->match($request);
822835
$groups = ($route instanceof Route) ? $route->getGroups() : [];
823836

824-
$this->setResource('route', fn () => $route, []);
837+
$this->setRequestResource('route', fn () => $route, []);
825838

826839
if (self::REQUEST_METHOD_HEAD == $method) {
827840
$method = self::REQUEST_METHOD_GET;
@@ -849,7 +862,7 @@ public function run(Request $request, Response $response): static
849862
foreach (self::$errors as $error) { // Global error hooks
850863
/** @var Hook $error */
851864
if (\in_array('*', $error->getGroups())) {
852-
$this->setResource('error', function () use ($e) {
865+
$this->setRequestResource('error', function () use ($e) {
853866
return $e;
854867
}, []);
855868
\call_user_func_array($error->getAction(), $this->getArguments($error, [], $request->getParams()));
@@ -866,7 +879,7 @@ public function run(Request $request, Response $response): static
866879
$path = \parse_url($request->getURI(), PHP_URL_PATH);
867880
$route->path($path);
868881

869-
$this->setResource('route', fn () => $route, []);
882+
$this->setRequestResource('route', fn () => $route, []);
870883
}
871884

872885
if (null !== $route) {
@@ -889,7 +902,7 @@ public function run(Request $request, Response $response): static
889902
} catch (\Throwable $e) {
890903
foreach (self::$errors as $error) { // Global error hooks
891904
if (\in_array('*', $error->getGroups())) {
892-
$this->setResource('error', function () use ($e) {
905+
$this->setRequestResource('error', function () use ($e) {
893906
return $e;
894907
}, []);
895908
\call_user_func_array($error->getAction(), $this->getArguments($error, [], $request->getParams()));
@@ -899,7 +912,7 @@ public function run(Request $request, Response $response): static
899912
} else {
900913
foreach (self::$errors as $error) { // Global error hooks
901914
if (\in_array('*', $error->getGroups())) {
902-
$this->setResource('error', fn () => new Exception('Not Found', 404), []);
915+
$this->setRequestResource('error', fn () => new Exception('Not Found', 404), []);
903916
\call_user_func_array($error->getAction(), $this->getArguments($error, [], $request->getParams()));
904917
}
905918
}

src/Http/Route.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Route extends Hook
3030
/**
3131
* Path params.
3232
*
33-
* @var array<string,string>
33+
* @var array<string,array<string, string>>
3434
*/
3535
protected array $pathParams = [];
3636

@@ -48,13 +48,25 @@ class Route extends Hook
4848
*/
4949
protected int $order;
5050

51+
protected string $matchedPath = '';
52+
5153
public function __construct(string $method, string $path)
5254
{
55+
parent::__construct();
5356
$this->path($path);
5457
$this->method = $method;
5558
$this->order = ++self::$counter;
56-
$this->action = function (): void {
57-
};
59+
}
60+
61+
public function setMatchedPath(string $path): self
62+
{
63+
$this->matchedPath = $path;
64+
return $this;
65+
}
66+
67+
public function getMatchedPath(): string
68+
{
69+
return $this->matchedPath;
5870
}
5971

6072
/**
@@ -143,23 +155,30 @@ public function getHook(): bool
143155
* @param int $index
144156
* @return void
145157
*/
146-
public function setPathParam(string $key, int $index): void
158+
public function setPathParam(string $key, int $index, string $path = ''): void
147159
{
148-
$this->pathParams[$key] = $index;
160+
$this->pathParams[$path][$key] = $index;
149161
}
150162

151163
/**
152164
* Get path params.
153165
*
154166
* @param \Utopia\Http\Request $request
167+
* @param string $path
155168
* @return array
156169
*/
157-
public function getPathValues(Request $request): array
170+
public function getPathValues(Request $request, string $path = ''): array
158171
{
159172
$pathValues = [];
160173
$parts = explode('/', ltrim($request->getURI(), '/'));
161174

162-
foreach ($this->pathParams as $key => $index) {
175+
if (empty($path)) {
176+
$pathParams = $this->pathParams[$path] ?? \array_values($this->pathParams)[0] ?? [];
177+
} else {
178+
$pathParams = $this->pathParams[$path] ?? [];
179+
}
180+
181+
foreach ($pathParams as $key => $index) {
163182
if (array_key_exists($index, $parts)) {
164183
$pathValues[$key] = $parts[$index];
165184
}

src/Http/Router.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static function addRoute(Route $route): void
8686
}
8787

8888
foreach ($params as $key => $index) {
89-
$route->setPathParam($key, $index);
89+
$route->setPathParam($key, $index, $path);
9090
}
9191

9292
self::$routes[$route->getMethod()][$path] = $route;
@@ -101,12 +101,16 @@ public static function addRoute(Route $route): void
101101
*/
102102
public static function addRouteAlias(string $path, Route $route): void
103103
{
104-
[$alias] = self::preparePath($path);
104+
[$alias, $params] = self::preparePath($path);
105105

106106
if (array_key_exists($alias, self::$routes[$route->getMethod()]) && !self::$allowOverride) {
107107
throw new Exception("Route for ({$route->getMethod()}:{$alias}) already registered.");
108108
}
109109

110+
foreach ($params as $key => $index) {
111+
$route->setPathParam($key, $index, $alias);
112+
}
113+
110114
self::$routes[$route->getMethod()][$alias] = $route;
111115
}
112116

@@ -123,7 +127,7 @@ public static function match(string $method, string $path): Route|null
123127
return null;
124128
}
125129

126-
$parts = array_values(array_filter(explode('/', $path)));
130+
$parts = array_values(array_filter(explode('/', $path), fn ($segment) => $segment !== ''));
127131
$length = count($parts) - 1;
128132
$filteredParams = array_filter(self::$params, fn ($i) => $i <= $length);
129133

@@ -138,7 +142,9 @@ public static function match(string $method, string $path): Route|null
138142
);
139143

140144
if (array_key_exists($match, self::$routes[$method])) {
141-
return self::$routes[$method][$match];
145+
$route = self::$routes[$method][$match];
146+
$route->setMatchedPath($match);
147+
return $route;
142148
}
143149
}
144150

@@ -147,7 +153,9 @@ public static function match(string $method, string $path): Route|null
147153
*/
148154
$match = self::WILDCARD_TOKEN;
149155
if (array_key_exists($match, self::$routes[$method])) {
150-
return self::$routes[$method][$match];
156+
$route = self::$routes[$method][$match];
157+
$route->setMatchedPath($match);
158+
return $route;
151159
}
152160

153161
/**
@@ -157,7 +165,9 @@ public static function match(string $method, string $path): Route|null
157165
$current = ($current ?? '') . "{$part}/";
158166
$match = $current . self::WILDCARD_TOKEN;
159167
if (array_key_exists($match, self::$routes[$method])) {
160-
return self::$routes[$method][$match];
168+
$route = self::$routes[$method][$match];
169+
$route->setMatchedPath($match);
170+
return $route;
161171
}
162172
}
163173

@@ -192,7 +202,7 @@ protected static function combinations(array $set): iterable
192202
* @param string $path
193203
* @return array
194204
*/
195-
protected static function preparePath(string $path): array
205+
public static function preparePath(string $path): array
196206
{
197207
$parts = array_values(array_filter(explode('/', $path)));
198208
$prepare = '';

0 commit comments

Comments
 (0)