Skip to content

Commit 6aaab7b

Browse files
committed
feat(routing): processed feedback from brendt
- Renamed Regexes to Regex as it is already plural - Make Regex limit a power of 2.. because it should be - Make RouteMatch isFound a computed method based on routeMark
1 parent 3e57646 commit 6aaab7b

File tree

11 files changed

+36
-32
lines changed

11 files changed

+36
-32
lines changed

src/Tempest/Http/src/RouteConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Tempest\Http;
66

7-
use Tempest\Http\Routing\Matching\MatchingRegexes;
7+
use Tempest\Http\Routing\Matching\MatchingRegex;
88

99
final class RouteConfig
1010
{
@@ -13,7 +13,7 @@ public function __construct(
1313
public array $staticRoutes = [],
1414
/** @var array<string, array<string, Route>> */
1515
public array $dynamicRoutes = [],
16-
/** @var array<string, MatchingRegexes> */
16+
/** @var array<string, MatchingRegex> */
1717
public array $matchingRegexes = [],
1818
) {
1919
}

src/Tempest/Http/src/Routing/Construction/RouteMatchingRegexesBuilder.php renamed to src/Tempest/Http/src/Routing/Construction/RouteMatchingRegexBuilder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44

55
namespace Tempest\Http\Routing\Construction;
66

7-
use Tempest\Http\Routing\Matching\MatchingRegexes;
7+
use Tempest\Http\Routing\Matching\MatchingRegex;
88

9-
final readonly class RouteMatchingRegexesBuilder
9+
final readonly class RouteMatchingRegexBuilder
1010
{
1111
// This limit is guesstimated using a small script with an ever in pattern feed into preg_match
12-
private const int PREG_REGEX_SIZE_LIMIT = 32764;
12+
private const int PREG_REGEX_SIZE_LIMIT = 32768;
1313

14-
private const int REGEX_SIZE_MARGIN = 264;
14+
private const int REGEX_SIZE_MARGIN = 256;
1515

1616
private const REGEX_SIZE_LIMIT = self::PREG_REGEX_SIZE_LIMIT - self::REGEX_SIZE_MARGIN;
1717

1818
public function __construct(private RouteTreeNode $rootNode)
1919
{
2020
}
2121

22-
public function toRegex(): MatchingRegexes
22+
public function toRegex(): MatchingRegex
2323
{
2424
// Holds all regex "chunks"
2525
$regexes = [];
@@ -101,7 +101,7 @@ public function toRegex(): MatchingRegexes
101101
}
102102

103103
// Return all regex chunks including the current one
104-
return new MatchingRegexes([
104+
return new MatchingRegex([
105105
...$regexes,
106106
'#' . substr($regex, 1) . '#',
107107
]);

src/Tempest/Http/src/Routing/Construction/RoutingTree.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Tempest\Http\Routing\Construction;
66

7-
use Tempest\Http\Routing\Matching\MatchingRegexes;
7+
use Tempest\Http\Routing\Matching\MatchingRegex;
88

99
/**
1010
* @internal
@@ -34,9 +34,9 @@ public function add(MarkedRoute $markedRoute): void
3434
$node->setTargetRoute($markedRoute);
3535
}
3636

37-
/** @return array<string, MatchingRegexes> */
37+
/** @return array<string, MatchingRegex> */
3838
public function toMatchingRegexes(): array
3939
{
40-
return array_map(static fn (RouteTreeNode $node) => (new RouteMatchingRegexesBuilder($node))->toRegex(), $this->roots);
40+
return array_map(static fn (RouteTreeNode $node) => (new RouteMatchingRegexBuilder($node))->toRegex(), $this->roots);
4141
}
4242
}

src/Tempest/Http/src/Routing/Matching/GenericRouteMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private function matchDynamicRoute(PsrRequest $request): ?MatchedRoute
5151
// Then we'll use this regex to see whether we have a match or not
5252
$matchResult = $matchingRegexForMethod->match($request->getUri()->getPath());
5353

54-
if (! $matchResult->isFound) {
54+
if (! $matchResult->isFound()) {
5555
return null;
5656
}
5757

src/Tempest/Http/src/Routing/Matching/MatchingRegexes.php renamed to src/Tempest/Http/src/Routing/Matching/MatchingRegex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use RuntimeException;
88
use Tempest\Http\Routing\Construction\MarkedRoute;
99

10-
final readonly class MatchingRegexes
10+
final readonly class MatchingRegex
1111
{
1212
/**
1313
* @param string[] $patterns

src/Tempest/Http/src/Routing/Matching/RouteMatch.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@
99
final readonly class RouteMatch
1010
{
1111
private function __construct(
12-
public bool $isFound,
1312
public ?string $mark,
1413
public array $matches,
1514
) {
1615
}
1716

1817
public static function match(array $params): self
1918
{
20-
return new self(true, $params[MarkedRoute::REGEX_MARK_TOKEN], $params);
19+
return new self($params[MarkedRoute::REGEX_MARK_TOKEN], $params);
2120
}
2221

2322
public static function notFound(): self
2423
{
25-
return new self(false, null, []);
24+
return new self(null, []);
25+
}
26+
27+
public function isFound(): bool
28+
{
29+
return $this->mark !== null;
2630
}
2731
}

src/Tempest/Http/tests/RouteConfigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Tempest\Http\Method;
99
use Tempest\Http\Route;
1010
use Tempest\Http\RouteConfig;
11-
use Tempest\Http\Routing\Matching\MatchingRegexes;
11+
use Tempest\Http\Routing\Matching\MatchingRegex;
1212

1313
/**
1414
* @internal
@@ -25,7 +25,7 @@ public function test_serialization(): void
2525
'POST' => ['b' => new Route('/', Method::POST)],
2626
],
2727
[
28-
'POST' => new MatchingRegexes(['#^(?|/([^/]++)(?|/1\/?$(*MARK:b)|/3\/?$(*MARK:d)))#']),
28+
'POST' => new MatchingRegex(['#^(?|/([^/]++)(?|/1\/?$(*MARK:b)|/3\/?$(*MARK:d)))#']),
2929
]
3030
);
3131

src/Tempest/Http/tests/Routing/Construction/RouteConfiguratorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Tempest\Http\Route;
1010
use Tempest\Http\RouteConfig;
1111
use Tempest\Http\Routing\Construction\RouteConfigurator;
12-
use Tempest\Http\Routing\Matching\MatchingRegexes;
12+
use Tempest\Http\Routing\Matching\MatchingRegex;
1313

1414
/**
1515
* @internal
@@ -89,10 +89,10 @@ public function test_adding_dynamic_routes(): void
8989
], $config->dynamicRoutes);
9090

9191
$this->assertEquals([
92-
'GET' => new MatchingRegexes([
92+
'GET' => new MatchingRegex([
9393
'#^(?|/dynamic(?|/([^/]++)(?|\/?$(*MARK:b)|/view\/?$(*MARK:d)|/([^/]++)(?|/([^/]++)(?|/([^/]++)\/?$(*MARK:e))))))#',
9494
]),
95-
'PATCH' => new MatchingRegexes([
95+
'PATCH' => new MatchingRegex([
9696
'#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:c)))#',
9797
]),
9898
], $config->matchingRegexes);

src/Tempest/Http/tests/Routing/Construction/RoutingTreeTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Tempest\Http\Routing\Construction\DuplicateRouteException;
1111
use Tempest\Http\Routing\Construction\MarkedRoute;
1212
use Tempest\Http\Routing\Construction\RoutingTree;
13-
use Tempest\Http\Routing\Matching\MatchingRegexes;
13+
use Tempest\Http\Routing\Matching\MatchingRegex;
1414

1515
/**
1616
* @internal
@@ -42,7 +42,7 @@ public function test_multiple_routes(): void
4242
$subject->add(new MarkedRoute('e', new Route('/{greeting}/brent', Method::GET)));
4343

4444
$this->assertEquals([
45-
'GET' => new MatchingRegexes([
45+
'GET' => new MatchingRegex([
4646
'#^(?|\/?$(*MARK:a)|/([^/]++)(?|/brent\/?$(*MARK:e)|/hello(?|/brent\/?$(*MARK:c)|/([^/]++)\/?$(*MARK:b))|/([^/]++)\/?$(*MARK:d)))#',
4747
]),
4848
], $subject->toMatchingRegexes());
@@ -72,8 +72,8 @@ public function test_multiple_http_methods(): void
7272
$subject->add(new MarkedRoute('b', new Route('/', Method::POST)));
7373

7474
$this->assertEquals([
75-
'GET' => new MatchingRegexes(['#^\/?$(*MARK:a)#']),
76-
'POST' => new MatchingRegexes(['#^\/?$(*MARK:b)#']),
75+
'GET' => new MatchingRegex(['#^\/?$(*MARK:a)#']),
76+
'POST' => new MatchingRegex(['#^\/?$(*MARK:b)#']),
7777
], $subject->toMatchingRegexes());
7878
}
7979
}

src/Tempest/Http/tests/Routing/Matching/GenericRouteMatcherTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Tempest\Http\Route;
1111
use Tempest\Http\RouteConfig;
1212
use Tempest\Http\Routing\Matching\GenericRouteMatcher;
13-
use Tempest\Http\Routing\Matching\MatchingRegexes;
13+
use Tempest\Http\Routing\Matching\MatchingRegex;
1414

1515
/**
1616
* @internal
@@ -42,8 +42,8 @@ protected function setUp(): void
4242
],
4343
],
4444
[
45-
'GET' => new MatchingRegexes(['#^(?|/dynamic(?|/([^/]++)(?|/view\/?$(*MARK:d)|/([^/]++)(?|/([^/]++)(?|/([^/]++)\/?$(*MARK:e)))|\/?$(*MARK:b))))#']),
46-
'PATCH' => new MatchingRegexes(['#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:c)))#']),
45+
'GET' => new MatchingRegex(['#^(?|/dynamic(?|/([^/]++)(?|/view\/?$(*MARK:d)|/([^/]++)(?|/([^/]++)(?|/([^/]++)\/?$(*MARK:e)))|\/?$(*MARK:b))))#']),
46+
'PATCH' => new MatchingRegex(['#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:c)))#']),
4747
]
4848
);
4949

0 commit comments

Comments
 (0)