chore: resolve all PSR-12 line length warnings in src directory
Some checks failed
CI / build (push) Has been cancelled
Some checks failed
CI / build (push) Has been cancelled
This commit is contained in:
parent
83c18bd82d
commit
dd081da81c
|
|
@ -288,7 +288,9 @@ class TestRouteCommand
|
||||||
|
|
||||||
if ($result->isFound()) {
|
if ($result->isFound()) {
|
||||||
echo "Match Found!" . PHP_EOL;
|
echo "Match Found!" . PHP_EOL;
|
||||||
echo "Route: " . $result->getRoute()->getName() . " [" . $result->getRoute()->getMethod() . " " . $result->getRoute()->getPath() . "]" . PHP_EOL;
|
echo "Route: " . $result->getRoute()->getName() . " [" .
|
||||||
|
$result->getRoute()->getMethod() . " " .
|
||||||
|
$result->getRoute()->getPath() . "]" . PHP_EOL;
|
||||||
echo "Parameters: " . json_encode($result->getParameters()) . PHP_EOL;
|
echo "Parameters: " . json_encode($result->getParameters()) . PHP_EOL;
|
||||||
exit(0);
|
exit(0);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -61,8 +61,11 @@ class ModuleLoader
|
||||||
* @param string|null $moduleName Optional module name
|
* @param string|null $moduleName Optional module name
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function loadModuleRoutes(string $routesFile, string|null $prefix = null, string|null $moduleName = null): void
|
private function loadModuleRoutes(
|
||||||
{
|
string $routesFile,
|
||||||
|
string|null $prefix = null,
|
||||||
|
string|null $moduleName = null
|
||||||
|
): void {
|
||||||
$moduleRoutes = require $routesFile;
|
$moduleRoutes = require $routesFile;
|
||||||
|
|
||||||
$options = [];
|
$options = [];
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,15 @@ class RouteGroup
|
||||||
$defaults = $this->options['defaults'] ?? [];
|
$defaults = $this->options['defaults'] ?? [];
|
||||||
|
|
||||||
if ($this->router) {
|
if ($this->router) {
|
||||||
return $this->router->registerCustomRoute('GET', $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
return $this->router->registerCustomRoute(
|
||||||
|
'GET',
|
||||||
|
$fullPath,
|
||||||
|
$handler,
|
||||||
|
$name,
|
||||||
|
$middleware,
|
||||||
|
$validation,
|
||||||
|
$defaults
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new RouteDefinition('GET', $fullPath, $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
return new RouteDefinition('GET', $fullPath, $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
||||||
|
|
@ -55,7 +63,15 @@ class RouteGroup
|
||||||
$defaults = $this->options['defaults'] ?? [];
|
$defaults = $this->options['defaults'] ?? [];
|
||||||
|
|
||||||
if ($this->router) {
|
if ($this->router) {
|
||||||
return $this->router->registerCustomRoute('POST', $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
return $this->router->registerCustomRoute(
|
||||||
|
'POST',
|
||||||
|
$fullPath,
|
||||||
|
$handler,
|
||||||
|
$name,
|
||||||
|
$middleware,
|
||||||
|
$validation,
|
||||||
|
$defaults
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new RouteDefinition('POST', $fullPath, $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
return new RouteDefinition('POST', $fullPath, $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
||||||
|
|
@ -69,7 +85,15 @@ class RouteGroup
|
||||||
$defaults = $this->options['defaults'] ?? [];
|
$defaults = $this->options['defaults'] ?? [];
|
||||||
|
|
||||||
if ($this->router) {
|
if ($this->router) {
|
||||||
return $this->router->registerCustomRoute('PUT', $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
return $this->router->registerCustomRoute(
|
||||||
|
'PUT',
|
||||||
|
$fullPath,
|
||||||
|
$handler,
|
||||||
|
$name,
|
||||||
|
$middleware,
|
||||||
|
$validation,
|
||||||
|
$defaults
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new RouteDefinition('PUT', $fullPath, $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
return new RouteDefinition('PUT', $fullPath, $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
||||||
|
|
@ -83,7 +107,15 @@ class RouteGroup
|
||||||
$defaults = $this->options['defaults'] ?? [];
|
$defaults = $this->options['defaults'] ?? [];
|
||||||
|
|
||||||
if ($this->router) {
|
if ($this->router) {
|
||||||
return $this->router->registerCustomRoute('PATCH', $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
return $this->router->registerCustomRoute(
|
||||||
|
'PATCH',
|
||||||
|
$fullPath,
|
||||||
|
$handler,
|
||||||
|
$name,
|
||||||
|
$middleware,
|
||||||
|
$validation,
|
||||||
|
$defaults
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new RouteDefinition('PATCH', $fullPath, $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
return new RouteDefinition('PATCH', $fullPath, $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
||||||
|
|
@ -97,10 +129,27 @@ class RouteGroup
|
||||||
$defaults = $this->options['defaults'] ?? [];
|
$defaults = $this->options['defaults'] ?? [];
|
||||||
|
|
||||||
if ($this->router) {
|
if ($this->router) {
|
||||||
return $this->router->registerCustomRoute('DELETE', $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
return $this->router->registerCustomRoute(
|
||||||
|
'DELETE',
|
||||||
|
$fullPath,
|
||||||
|
$handler,
|
||||||
|
$name,
|
||||||
|
$middleware,
|
||||||
|
$validation,
|
||||||
|
$defaults
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new RouteDefinition('DELETE', $fullPath, $fullPath, $handler, $name, $middleware, $validation, $defaults);
|
return new RouteDefinition(
|
||||||
|
'DELETE',
|
||||||
|
$fullPath,
|
||||||
|
$fullPath,
|
||||||
|
$handler,
|
||||||
|
$name,
|
||||||
|
$middleware,
|
||||||
|
$validation,
|
||||||
|
$defaults
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function redirect(string $path, string $destination, int $status = 302): RouteDefinition
|
public function redirect(string $path, string $destination, int $status = 302): RouteDefinition
|
||||||
|
|
@ -111,10 +160,27 @@ class RouteGroup
|
||||||
$defaults = $this->options['defaults'] ?? [];
|
$defaults = $this->options['defaults'] ?? [];
|
||||||
|
|
||||||
if ($this->router) {
|
if ($this->router) {
|
||||||
return $this->router->registerCustomRoute('REDIRECT', $fullPath, $destination, null, $middleware, $validation, $defaults)->attr('status', $status);
|
return $this->router->registerCustomRoute(
|
||||||
|
'REDIRECT',
|
||||||
|
$fullPath,
|
||||||
|
$destination,
|
||||||
|
null,
|
||||||
|
$middleware,
|
||||||
|
$validation,
|
||||||
|
$defaults
|
||||||
|
)->attr('status', $status);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (new RouteDefinition('REDIRECT', $fullPath, $fullPath, $destination, null, $middleware, $validation, $defaults))->attr('status', $status);
|
return (new RouteDefinition(
|
||||||
|
'REDIRECT',
|
||||||
|
$fullPath,
|
||||||
|
$fullPath,
|
||||||
|
$destination,
|
||||||
|
null,
|
||||||
|
$middleware,
|
||||||
|
$validation,
|
||||||
|
$defaults
|
||||||
|
))->attr('status', $status);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fallback(mixed $handler): self
|
public function fallback(mixed $handler): self
|
||||||
|
|
@ -125,7 +191,13 @@ class RouteGroup
|
||||||
$middleware = $this->options['middleware'] ?? [];
|
$middleware = $this->options['middleware'] ?? [];
|
||||||
|
|
||||||
if ($this->router) {
|
if ($this->router) {
|
||||||
$this->router->registerCustomRoute('FALLBACK', $this->joinPaths($prefix, '/_fallback'), $handler, null, $middleware)
|
$this->router->registerCustomRoute(
|
||||||
|
'FALLBACK',
|
||||||
|
$this->joinPaths($prefix, '/_fallback'),
|
||||||
|
$handler,
|
||||||
|
null,
|
||||||
|
$middleware
|
||||||
|
)
|
||||||
->attr('_fallback', $handler)
|
->attr('_fallback', $handler)
|
||||||
->attr('_fallback_prefix', $this->normalizePath($prefix));
|
->attr('_fallback_prefix', $this->normalizePath($prefix));
|
||||||
}
|
}
|
||||||
|
|
@ -133,8 +205,15 @@ class RouteGroup
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerCustomRoute(string $method, string $path, mixed $handler, string|null $name = null, array $middleware = [], array $validation = [], array $defaults = []): RouteDefinition
|
public function registerCustomRoute(
|
||||||
{
|
string $method,
|
||||||
|
string $path,
|
||||||
|
mixed $handler,
|
||||||
|
string|null $name = null,
|
||||||
|
array $middleware = [],
|
||||||
|
array $validation = [],
|
||||||
|
array $defaults = []
|
||||||
|
): RouteDefinition {
|
||||||
$fullPath = $this->buildFullPath($path);
|
$fullPath = $this->buildFullPath($path);
|
||||||
$mergedMiddleware = array_merge($this->options['middleware'] ?? [], $middleware);
|
$mergedMiddleware = array_merge($this->options['middleware'] ?? [], $middleware);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -112,8 +112,14 @@ class RouteMatcher
|
||||||
* @param array $attributes Extracted attributes
|
* @param array $attributes Extracted attributes
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function isMatch(string $method, string $path, string $host, RouteDefinition $route, array &$attributes, ?string $overridePath = null): bool
|
private function isMatch(
|
||||||
{
|
string $method,
|
||||||
|
string $path,
|
||||||
|
string $host,
|
||||||
|
RouteDefinition $route,
|
||||||
|
array &$attributes,
|
||||||
|
?string $overridePath = null
|
||||||
|
): bool {
|
||||||
$routeMethod = strtoupper($route->getMethod());
|
$routeMethod = strtoupper($route->getMethod());
|
||||||
if ($routeMethod !== $method && $routeMethod !== 'REDIRECT') {
|
if ($routeMethod !== $method && $routeMethod !== 'REDIRECT') {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -128,7 +134,9 @@ class RouteMatcher
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$pattern = $overridePath ? $this->compilePatternFromPath($overridePath, $route) : $this->getPatternForRoute($route);
|
$pattern = $overridePath
|
||||||
|
? $this->compilePatternFromPath($overridePath, $route)
|
||||||
|
: $this->getPatternForRoute($route);
|
||||||
|
|
||||||
if (preg_match($pattern, $path, $matches)) {
|
if (preg_match($pattern, $path, $matches)) {
|
||||||
$attributes = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
|
$attributes = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
|
||||||
|
|
@ -175,33 +183,36 @@ class RouteMatcher
|
||||||
$validation = $route->getValidation();
|
$validation = $route->getValidation();
|
||||||
$defaults = $route->getDefaults();
|
$defaults = $route->getDefaults();
|
||||||
|
|
||||||
// Replace {{param?}} and {{param}} with regex
|
$pattern = preg_replace_callback(
|
||||||
$pattern = preg_replace_callback('#/\{\{([a-zA-Z0-9_]+)(\?)?\}\}#', function ($matches) use ($validation, $defaults) {
|
'#/\{\{([a-zA-Z0-9_]+)(\?)?\}\}#',
|
||||||
$name = $matches[1];
|
function ($matches) use ($validation, $defaults) {
|
||||||
$optional = (isset($matches[2]) && $matches[2] === '?') || array_key_exists($name, $defaults);
|
$name = $matches[1];
|
||||||
|
$optional = (isset($matches[2]) && $matches[2] === '?') || array_key_exists($name, $defaults);
|
||||||
|
|
||||||
$rules = $validation[$name] ?? [];
|
$rules = $validation[$name] ?? [];
|
||||||
$regex = '[^/]+';
|
$regex = '[^/]+';
|
||||||
|
|
||||||
// Validation rules support
|
// Validation rules support
|
||||||
foreach ((array)$rules as $rule) {
|
foreach ((array)$rules as $rule) {
|
||||||
if ($rule === 'numeric' || $rule === 'int') {
|
if ($rule === 'numeric' || $rule === 'int') {
|
||||||
$regex = '[0-9]+';
|
$regex = '[0-9]+';
|
||||||
} elseif ($rule === 'alpha') {
|
} elseif ($rule === 'alpha') {
|
||||||
$regex = '[a-zA-Z]+';
|
$regex = '[a-zA-Z]+';
|
||||||
} elseif ($rule === 'alphanumeric') {
|
} elseif ($rule === 'alphanumeric') {
|
||||||
$regex = '[a-zA-Z0-9]+';
|
$regex = '[a-zA-Z0-9]+';
|
||||||
} elseif (str_starts_with($rule, 'regex:')) {
|
} elseif (str_starts_with($rule, 'regex:')) {
|
||||||
$regex = substr($rule, 6);
|
$regex = substr($rule, 6);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ($optional) {
|
if ($optional) {
|
||||||
return '(?:/(?P<' . $name . '>' . $regex . '))?';
|
return '(?:/(?P<' . $name . '>' . $regex . '))?';
|
||||||
}
|
}
|
||||||
|
|
||||||
return '/(?P<' . $name . '>' . $regex . ')';
|
return '/(?P<' . $name . '>' . $regex . ')';
|
||||||
}, $path);
|
},
|
||||||
|
$path
|
||||||
|
);
|
||||||
|
|
||||||
$pattern = str_replace('//', '/', $pattern);
|
$pattern = str_replace('//', '/', $pattern);
|
||||||
|
|
||||||
|
|
@ -215,8 +226,10 @@ class RouteMatcher
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @return RouteDefinition
|
* @return RouteDefinition
|
||||||
*/
|
*/
|
||||||
private function applyAttributes(RouteDefinition $route, array $attributes): RouteDefinition
|
private function applyAttributes(
|
||||||
{
|
RouteDefinition $route,
|
||||||
|
array $attributes
|
||||||
|
): RouteDefinition {
|
||||||
$data = $route->toArray();
|
$data = $route->toArray();
|
||||||
$data['attributes'] = array_merge($data['attributes'], $attributes);
|
$data['attributes'] = array_merge($data['attributes'], $attributes);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,13 +49,27 @@ class Router
|
||||||
return $this->registerRoute('DELETE', $path, $handler, $name);
|
return $this->registerRoute('DELETE', $path, $handler, $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerCustomRoute(string $method, string $path, mixed $handler, string|null $name = null, array $middleware = [], array $validation = [], array $defaults = []): RouteDefinition
|
public function registerCustomRoute(
|
||||||
{
|
string $method,
|
||||||
|
string $path,
|
||||||
|
mixed $handler,
|
||||||
|
string|null $name = null,
|
||||||
|
array $middleware = [],
|
||||||
|
array $validation = [],
|
||||||
|
array $defaults = []
|
||||||
|
): RouteDefinition {
|
||||||
return $this->registerRoute($method, $path, $handler, $name, $middleware, $validation, $defaults);
|
return $this->registerRoute($method, $path, $handler, $name, $middleware, $validation, $defaults);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function registerRoute(string $method, string $path, mixed $handler, string|null $name = null, array $middleware = [], array $validation = [], array $defaults = []): RouteDefinition
|
private function registerRoute(
|
||||||
{
|
string $method,
|
||||||
|
string $path,
|
||||||
|
mixed $handler,
|
||||||
|
string|null $name = null,
|
||||||
|
array $middleware = [],
|
||||||
|
array $validation = [],
|
||||||
|
array $defaults = []
|
||||||
|
): RouteDefinition {
|
||||||
$normalizedPath = $this->normalizePath($path);
|
$normalizedPath = $this->normalizePath($path);
|
||||||
$routeDefinition = new RouteDefinition(
|
$routeDefinition = new RouteDefinition(
|
||||||
$method,
|
$method,
|
||||||
|
|
@ -211,7 +225,9 @@ class Router
|
||||||
} elseif ($isOptional[$index] === '?') {
|
} elseif ($isOptional[$index] === '?') {
|
||||||
$path = str_replace($pattern, '', $path);
|
$path = str_replace($pattern, '', $path);
|
||||||
} else {
|
} else {
|
||||||
throw new \InvalidArgumentException(sprintf('Missing required parameter "%s" for route URL generation', $name));
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf('Missing required parameter "%s" for route URL generation', $name)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue