-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from membrane-php/fix-priority
Fix prioritisation when handling partially dynamic routes
- Loading branch information
Showing
14 changed files
with
472 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Membrane\OpenAPIRouter\Router\Route; | ||
|
||
use JsonSerializable; | ||
|
||
final class Path implements JsonSerializable | ||
{ | ||
/** @var array<string, string> */ | ||
private array $operations = []; | ||
|
||
public function __construct( | ||
public readonly string $url, | ||
public readonly string $regex, | ||
) { | ||
} | ||
|
||
public function addRoute(Route $route): void | ||
{ | ||
$this->operations[$route->method] = $route->operationId; | ||
} | ||
|
||
public function isDynamic(): bool | ||
{ | ||
return $this->url !== $this->regex; | ||
} | ||
|
||
public function howManyDynamicComponents(): int | ||
{ | ||
return substr_count($this->regex, '([^/]+)'); | ||
} | ||
|
||
public function isEmpty(): bool | ||
{ | ||
return count($this->operations) === 0; | ||
} | ||
|
||
/** @return array<string, string> */ | ||
public function jsonSerialize(): array | ||
{ | ||
return [...$this->operations]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Membrane\OpenAPIRouter\Router\Route; | ||
|
||
use JsonSerializable; | ||
|
||
final class Server implements JsonSerializable | ||
{ | ||
/** @var array<string, Path>*/ | ||
private array $paths = []; | ||
|
||
public function __construct( | ||
public readonly string $url, | ||
public readonly string $regex, | ||
) { | ||
} | ||
|
||
public function addRoute(Route $route): void | ||
{ | ||
if (!isset($this->paths[$route->path])) { | ||
$this->addPath(new Path($route->path, $route->pathRegex)); | ||
} | ||
|
||
$this->paths[$route->path]->addRoute($route); | ||
} | ||
|
||
public function isDynamic(): bool | ||
{ | ||
return $this->url !== $this->regex; | ||
} | ||
|
||
public function howManyDynamicComponents(): int | ||
{ | ||
return substr_count($this->regex, '([^/]+)'); | ||
} | ||
|
||
public function isEmpty(): bool | ||
{ | ||
return count(array_filter($this->paths, fn($p) => !$p->isEmpty())) === 0; | ||
} | ||
|
||
public function isHosted(): bool | ||
{ | ||
return parse_url($this->url, PHP_URL_HOST) !== null; | ||
} | ||
|
||
/** @return array{ | ||
* 'static': array<string, array<string,string>>, | ||
* 'dynamic': array{'regex': string, 'paths': array<string, array<string,string>>} | ||
* } | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
$filteredPaths = array_filter($this->paths, fn($p) => !$p->isEmpty()); | ||
usort($filteredPaths, fn($a, $b) => $a->howManyDynamicComponents() <=> $b->howManyDynamicComponents()); | ||
|
||
$staticPaths = $dynamicPaths = $regex = []; | ||
foreach ($filteredPaths as $path) { | ||
if ($path->isDynamic()) { | ||
$dynamicPaths[$path->url] = $path->jsonSerialize(); | ||
$regex[] = sprintf('%s(*MARK:%s)', $path->regex, $path->url); | ||
} else { | ||
$staticPaths[$path->url] = $path->jsonSerialize(); | ||
} | ||
} | ||
|
||
return [ | ||
'static' => $staticPaths, | ||
'dynamic' => [ | ||
'regex' => sprintf('#^(?|%s)$#', implode('|', $regex)), | ||
'paths' => $dynamicPaths | ||
] | ||
]; | ||
} | ||
|
||
private function addPath(Path $path): void | ||
{ | ||
if (!isset($this->paths[$path->url])) { | ||
$this->paths[$path->url] = $path; | ||
} | ||
} | ||
} |
Oops, something went wrong.