Upstream nikic/FastRoute state of the touched files before 8bb83fe8c2

This commit is contained in:
upstream committed 2026-09-04 05:29:02 +02:00
commit c0905f9089
6 files changed
+720

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace FastRoute\Dispatcher;
use FastRoute\Dispatcher\Result\Matched;
use function assert;
use function end;
use function preg_match;
/** @final */
class CharCountBased extends RegexBasedAbstract
{
/** @inheritDoc */
protected function dispatchVariableRoute(array $routeData, string $uri): ?Matched
{
foreach ($routeData as $data) {
assert(isset($data['suffix']));
if (preg_match($data['regex'], $uri . $data['suffix'], $matches) !== 1) {
continue;
}
[$handler, $varNames, $extraParameters] = $data['routeMap'][end($matches)];
$vars = [];
$i = 0;
foreach ($varNames as $varName) {
$vars[$varName] = $matches[++$i];
}
$result = new Matched();
$result->handler = $handler;
$result->variables = $vars;
$result->extraParameters = $extraParameters;
return $result;
}
return null;
}
}
+40
View File
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace FastRoute\Dispatcher;
use FastRoute\Dispatcher\Result\Matched;
use function count;
use function preg_match;
/** @final */
class GroupCountBased extends RegexBasedAbstract
{
/** @inheritDoc */
protected function dispatchVariableRoute(array $routeData, string $uri): ?Matched
{
foreach ($routeData as $data) {
if (preg_match($data['regex'], $uri, $matches) !== 1) {
continue;
}
[$handler, $varNames, $extraParameters] = $data['routeMap'][count($matches)];
$vars = [];
$i = 0;
foreach ($varNames as $varName) {
$vars[$varName] = $matches[++$i];
}
$result = new Matched();
$result->handler = $handler;
$result->variables = $vars;
$result->extraParameters = $extraParameters;
return $result;
}
return null;
}
}
+46
View File
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace FastRoute\Dispatcher;
use FastRoute\Dispatcher\Result\Matched;
use function assert;
use function preg_match;
/** @final */
class GroupPosBased extends RegexBasedAbstract
{
/** @inheritDoc */
protected function dispatchVariableRoute(array $routeData, string $uri): ?Matched
{
foreach ($routeData as $data) {
if (preg_match($data['regex'], $uri, $matches) !== 1) {
continue;
}
// find first non-empty match
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedFor
for ($i = 1; $matches[$i] === ''; ++$i) {
}
assert(isset($i));
[$handler, $varNames, $extraParameters] = $data['routeMap'][$i];
$vars = [];
foreach ($varNames as $varName) {
$vars[$varName] = $matches[$i++];
}
$result = new Matched();
$result->handler = $handler;
$result->variables = $vars;
$result->extraParameters = $extraParameters;
return $result;
}
return null;
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace FastRoute\Dispatcher;
use FastRoute\Dispatcher\Result\Matched;
use function preg_match;
/** @final */
class MarkBased extends RegexBasedAbstract
{
/** @inheritDoc */
protected function dispatchVariableRoute(array $routeData, string $uri): ?Matched
{
foreach ($routeData as $data) {
if (preg_match($data['regex'], $uri, $matches) !== 1) {
continue;
}
[$handler, $varNames, $extraParameters] = $data['routeMap'][$matches['MARK']];
$vars = [];
$i = 0;
foreach ($varNames as $varName) {
$vars[$varName] = $matches[++$i];
}
$result = new Matched();
$result->handler = $handler;
$result->variables = $vars;
$result->extraParameters = $extraParameters;
return $result;
}
return null;
}
}
+123
View File
@@ -0,0 +1,123 @@
<?php
declare(strict_types=1);
namespace FastRoute\Dispatcher;
use FastRoute\DataGenerator;
use FastRoute\Dispatcher;
use FastRoute\Dispatcher\Result\Matched;
use FastRoute\Dispatcher\Result\MethodNotAllowed;
use FastRoute\Dispatcher\Result\NotMatched;
/**
* @internal
*
* @phpstan-import-type StaticRoutes from DataGenerator
* @phpstan-import-type DynamicRouteChunk from DataGenerator
* @phpstan-import-type DynamicRouteChunks from DataGenerator
* @phpstan-import-type DynamicRoutes from DataGenerator
* @phpstan-import-type RouteData from DataGenerator
*/
abstract class RegexBasedAbstract implements Dispatcher
{
/** @var StaticRoutes */
protected array $staticRouteMap = [];
/** @var DynamicRoutes */
protected array $variableRouteData = [];
/** @param RouteData $data */
public function __construct(array $data)
{
[$this->staticRouteMap, $this->variableRouteData] = $data;
}
/** @param DynamicRouteChunks $routeData */
abstract protected function dispatchVariableRoute(array $routeData, string $uri): ?Matched;
public function dispatch(string $httpMethod, string $uri): Matched|NotMatched|MethodNotAllowed
{
if (isset($this->staticRouteMap[$httpMethod][$uri])) {
$result = new Matched();
$result->handler = $this->staticRouteMap[$httpMethod][$uri][0];
$result->extraParameters = $this->staticRouteMap[$httpMethod][$uri][1];
return $result;
}
if (isset($this->variableRouteData[$httpMethod])) {
$result = $this->dispatchVariableRoute($this->variableRouteData[$httpMethod], $uri);
if ($result !== null) {
return $result;
}
}
// For HEAD requests, attempt fallback to GET
if ($httpMethod === 'HEAD') {
if (isset($this->staticRouteMap['GET'][$uri])) {
$result = new Matched();
$result->handler = $this->staticRouteMap['GET'][$uri][0];
$result->extraParameters = $this->staticRouteMap['GET'][$uri][1];
return $result;
}
if (isset($this->variableRouteData['GET'])) {
$result = $this->dispatchVariableRoute($this->variableRouteData['GET'], $uri);
if ($result !== null) {
return $result;
}
}
}
// If nothing else matches, try fallback routes
if (isset($this->staticRouteMap['*'][$uri])) {
$result = new Matched();
$result->handler = $this->staticRouteMap['*'][$uri][0];
$result->extraParameters = $this->staticRouteMap['*'][$uri][1];
return $result;
}
if (isset($this->variableRouteData['*'])) {
$result = $this->dispatchVariableRoute($this->variableRouteData['*'], $uri);
if ($result !== null) {
return $result;
}
}
// Find allowed methods for this URI by matching against all other HTTP methods as well
$allowedMethods = [];
foreach ($this->staticRouteMap as $method => $uriMap) {
if ($method === $httpMethod || ! isset($uriMap[$uri])) {
continue;
}
$allowedMethods[] = $method;
}
foreach ($this->variableRouteData as $method => $routeData) {
if ($method === $httpMethod) {
continue;
}
$result = $this->dispatchVariableRoute($routeData, $uri);
if ($result === null) {
continue;
}
$allowedMethods[] = $method;
}
// If there are no allowed methods the route simply does not exist
if ($allowedMethods !== []) {
$result = new MethodNotAllowed();
$result->allowedMethods = $allowedMethods;
return $result;
}
return new NotMatched();
}
}