Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public function read(string $class, string $defaultController = 'Home', string $
$classInUri,
$classname,
$methodName,
$httpVerb
$httpVerb,
$method
);

if ($routeForDefaultController !== []) {
Expand All @@ -85,23 +86,7 @@ public function read(string $class, string $defaultController = 'Home', string $
continue;
}

$params = [];
$routeParams = '';
$refParams = $method->getParameters();

foreach ($refParams as $param) {
$required = true;
if ($param->isOptional()) {
$required = false;

$routeParams .= '[/..]';
} else {
$routeParams .= '/..';
}

// [variable_name => required?]
$params[$param->getName()] = $required;
}
[$params, $routeParams] = $this->getParameters($method);

// Route for the default method.
$output[] = [
Expand All @@ -117,23 +102,7 @@ public function read(string $class, string $defaultController = 'Home', string $

$route = $classInUri . '/' . $methodInUri;

$params = [];
$routeParams = '';
$refParams = $method->getParameters();

foreach ($refParams as $param) {
$required = true;
if ($param->isOptional()) {
$required = false;

$routeParams .= '[/..]';
} else {
$routeParams .= '/..';
}

// [variable_name => required?]
$params[$param->getName()] = $required;
}
[$params, $routeParams] = $this->getParameters($method);

// If it is the default controller, the method will not be
// routed.
Expand All @@ -155,6 +124,29 @@ public function read(string $class, string $defaultController = 'Home', string $
return $output;
}

private function getParameters(ReflectionMethod $method): array
{
$params = [];
$routeParams = '';
$refParams = $method->getParameters();

foreach ($refParams as $param) {
$required = true;
if ($param->isOptional()) {
$required = false;

$routeParams .= '[/..]';
} else {
$routeParams .= '/..';
}

// [variable_name => required?]
$params[$param->getName()] = $required;
}

return [$params, $routeParams];
}

/**
* @phpstan-param class-string $classname
*
Expand Down Expand Up @@ -189,7 +181,8 @@ private function getRouteForDefaultController(
string $uriByClass,
string $classname,
string $methodName,
string $httpVerb
string $httpVerb,
ReflectionMethod $method
): array {
$output = [];

Expand All @@ -198,12 +191,18 @@ private function getRouteForDefaultController(
$routeWithoutController = rtrim(preg_replace($pattern, '', $uriByClass), '/');
$routeWithoutController = $routeWithoutController ?: '/';

[$params, $routeParams] = $this->getParameters($method);

if ($routeWithoutController === '/' && $routeParams !== '') {
$routeWithoutController = '';
}

$output[] = [
'method' => $httpVerb,
'route' => $routeWithoutController,
'route_params' => '',
'route_params' => $routeParams,
'handler' => '\\' . $classname . '::' . $methodName,
'params' => [],
'params' => $params,
];
}

Expand Down
Loading