Skip to content
Merged
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
91 changes: 39 additions & 52 deletions composer.lock

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

4 changes: 3 additions & 1 deletion src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,9 @@ public function execute(Route $route, Request $request, Response $response): sta
{
$arguments = [];
$groups = $route->getGroups();
$pathValues = $route->getPathValues($request);

$preparedPath = Router::preparePath($route->getMatchedPath());
$pathValues = $route->getPathValues($request, $preparedPath[0]);

try {
if ($route->getHook()) {
Expand Down
23 changes: 18 additions & 5 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Route extends Hook
/**
* Path params.
*
* @var array<string,string>
* @var array<string,array<string, string>>
*/
protected array $pathParams = [];

Expand All @@ -46,6 +46,8 @@ class Route extends Hook
*/
protected int $order;

protected string $matchedPath = '';

public function __construct(string $method, string $path)
{
$this->path($path);
Expand All @@ -55,6 +57,17 @@ public function __construct(string $method, string $path)
};
}

public function setMatchedPath(string $path): self
{
$this->matchedPath = $path;
return $this;
}

public function getMatchedPath(): string
{
return $this->matchedPath;
}

/**
* Get Route Order ID
*
Expand Down Expand Up @@ -141,9 +154,9 @@ public function getHook(): bool
* @param int $index
* @return void
*/
public function setPathParam(string $key, int $index): void
public function setPathParam(string $path, string $key, int $index): void
{
$this->pathParams[$key] = $index;
$this->pathParams[$path][$key] = $index;
}

/**
Expand All @@ -152,12 +165,12 @@ public function setPathParam(string $key, int $index): void
* @param \Utopia\Request $request
* @return array
*/
public function getPathValues(Request $request): array
public function getPathValues(Request $request, string $path): array
{
$pathValues = [];
$parts = explode('/', ltrim($request->getURI(), '/'));

foreach ($this->pathParams as $key => $index) {
foreach (($this->pathParams[$path] ?? []) as $key => $index) {
if (array_key_exists($index, $parts)) {
$pathValues[$key] = $parts[$index];
}
Expand Down
22 changes: 16 additions & 6 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function addRoute(Route $route): void
}

foreach ($params as $key => $index) {
$route->setPathParam($key, $index);
$route->setPathParam($path, $key, $index);
}

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

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

foreach ($params as $key => $index) {
$route->setPathParam($alias, $key, $index);
}

self::$routes[$route->getMethod()][$alias] = $route;
}

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

if (array_key_exists($match, self::$routes[$method])) {
return self::$routes[$method][$match];
$route = self::$routes[$method][$match];
$route->setMatchedPath($match);
return $route;
}
}

Expand All @@ -147,7 +153,9 @@ public static function match(string $method, string $path): Route|null
*/
$match = self::WILDCARD_TOKEN;
if (array_key_exists($match, self::$routes[$method])) {
return self::$routes[$method][$match];
$route = self::$routes[$method][$match];
$route->setMatchedPath($match);
return $route;
}

/**
Expand All @@ -157,7 +165,9 @@ public static function match(string $method, string $path): Route|null
$current = ($current ?? '') . "{$part}/";
$match = $current . self::WILDCARD_TOKEN;
if (array_key_exists($match, self::$routes[$method])) {
return self::$routes[$method][$match];
$route = self::$routes[$method][$match];
$route->setMatchedPath($match);
return $route;
}
}

Expand Down Expand Up @@ -192,7 +202,7 @@ protected static function combinations(array $set): iterable
* @param string $path
* @return array
*/
protected static function preparePath(string $path): array
public static function preparePath(string $path): array
{
$parts = array_values(array_filter(explode('/', $path)));
$prepare = '';
Expand Down
24 changes: 23 additions & 1 deletion tests/e2e/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,35 @@ public function __construct()
public function call(string $method, string $path = '', array $headers = [], array $params = [])
{
usleep(50000);
$url = $this->baseUrl.$path.(($method == self::METHOD_GET && !empty($params)) ? '?'.http_build_query($params) : '');
$ch = curl_init($this->baseUrl.$path.(($method == self::METHOD_GET && !empty($params)) ? '?'.http_build_query($params) : ''));
$responseHeaders = [];
$responseStatus = -1;
$responseType = '';
$responseBody = '';

$query = match ($headers['content-type'] ?? '') {
'application/json' => \json_encode($params),
'text/plain' => $params,
default => \http_build_query($params),
};

$formattedHeaders = [];
foreach ($headers as $key => $value) {
if (strtolower($key) === 'accept-encoding') {
curl_setopt($ch, CURLOPT_ENCODING, $value);
continue;
} else {
$formattedHeaders[] = $key . ': ' . $value;
}
}

curl_setopt($ch, CURLOPT_PATH_AS_IS, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $formattedHeaders);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) {
Expand All @@ -80,6 +98,10 @@ public function call(string $method, string $path = '', array $headers = [], arr
return $len;
});

if ($method !== self::METHOD_GET) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
}

$responseBody = curl_exec($ch);
$responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

Expand Down
Loading