|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved; |
| 13 | + |
| 14 | +use ReflectionClass; |
| 15 | +use ReflectionMethod; |
| 16 | + |
| 17 | +/** |
| 18 | + * Reads a controller and returns a list of auto route listing. |
| 19 | + */ |
| 20 | +final class ControllerMethodReader |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var string the default namespace |
| 24 | + */ |
| 25 | + private string $namespace; |
| 26 | + |
| 27 | + private array $httpMethods; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param string $namespace the default namespace |
| 31 | + */ |
| 32 | + public function __construct(string $namespace, array $httpMethods) |
| 33 | + { |
| 34 | + $this->namespace = $namespace; |
| 35 | + $this->httpMethods = $httpMethods; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Returns found route info in the controller. |
| 40 | + * |
| 41 | + * @phpstan-param class-string $class |
| 42 | + * |
| 43 | + * @return array<int, array<string, array|string>> |
| 44 | + * @phpstan-return list<array<string, string|array>> |
| 45 | + */ |
| 46 | + public function read(string $class, string $defaultController = 'Home', string $defaultMethod = 'index'): array |
| 47 | + { |
| 48 | + $reflection = new ReflectionClass($class); |
| 49 | + |
| 50 | + if ($reflection->isAbstract()) { |
| 51 | + return []; |
| 52 | + } |
| 53 | + |
| 54 | + $classname = $reflection->getName(); |
| 55 | + $classShortname = $reflection->getShortName(); |
| 56 | + |
| 57 | + $output = []; |
| 58 | + $classInUri = $this->getUriByClass($classname); |
| 59 | + |
| 60 | + foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
| 61 | + $methodName = $method->getName(); |
| 62 | + |
| 63 | + foreach ($this->httpMethods as $httpVerb) { |
| 64 | + if (strpos($methodName, $httpVerb) === 0) { |
| 65 | + // Remove HTTP verb prefix. |
| 66 | + $methodInUri = lcfirst(substr($methodName, strlen($httpVerb))); |
| 67 | + |
| 68 | + if ($methodInUri === $defaultMethod) { |
| 69 | + $routeWithoutController = $this->getRouteWithoutController( |
| 70 | + $classShortname, |
| 71 | + $defaultController, |
| 72 | + $classInUri, |
| 73 | + $classname, |
| 74 | + $methodName, |
| 75 | + $httpVerb |
| 76 | + ); |
| 77 | + |
| 78 | + if ($routeWithoutController !== []) { |
| 79 | + $output = [...$output, ...$routeWithoutController]; |
| 80 | + |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + // Route for the default method. |
| 85 | + $output[] = [ |
| 86 | + 'method' => $httpVerb, |
| 87 | + 'route' => $classInUri, |
| 88 | + 'route_params' => '', |
| 89 | + 'handler' => '\\' . $classname . '::' . $methodName, |
| 90 | + 'params' => [], |
| 91 | + ]; |
| 92 | + |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + $route = $classInUri . '/' . $methodInUri; |
| 97 | + |
| 98 | + $params = []; |
| 99 | + $routeParams = ''; |
| 100 | + $refParams = $method->getParameters(); |
| 101 | + |
| 102 | + foreach ($refParams as $param) { |
| 103 | + $required = true; |
| 104 | + if ($param->isOptional()) { |
| 105 | + $required = false; |
| 106 | + |
| 107 | + $routeParams .= '[/..]'; |
| 108 | + } else { |
| 109 | + $routeParams .= '/..'; |
| 110 | + } |
| 111 | + |
| 112 | + // [variable_name => required?] |
| 113 | + $params[$param->getName()] = $required; |
| 114 | + } |
| 115 | + |
| 116 | + $output[] = [ |
| 117 | + 'method' => $httpVerb, |
| 118 | + 'route' => $route, |
| 119 | + 'route_params' => $routeParams, |
| 120 | + 'handler' => '\\' . $classname . '::' . $methodName, |
| 121 | + 'params' => $params, |
| 122 | + ]; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return $output; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @phpstan-param class-string $classname |
| 132 | + * |
| 133 | + * @return string URI path part from the folder(s) and controller |
| 134 | + */ |
| 135 | + private function getUriByClass(string $classname): string |
| 136 | + { |
| 137 | + // remove the namespace |
| 138 | + $pattern = '/' . preg_quote($this->namespace, '/') . '/'; |
| 139 | + $class = ltrim(preg_replace($pattern, '', $classname), '\\'); |
| 140 | + |
| 141 | + $classParts = explode('\\', $class); |
| 142 | + $classPath = ''; |
| 143 | + |
| 144 | + foreach ($classParts as $part) { |
| 145 | + // make the first letter lowercase, because auto routing makes |
| 146 | + // the URI path's first letter uppercase and search the controller |
| 147 | + $classPath .= lcfirst($part) . '/'; |
| 148 | + } |
| 149 | + |
| 150 | + return rtrim($classPath, '/'); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Gets a route without default controller. |
| 155 | + */ |
| 156 | + private function getRouteWithoutController( |
| 157 | + string $classShortname, |
| 158 | + string $defaultController, |
| 159 | + string $uriByClass, |
| 160 | + string $classname, |
| 161 | + string $methodName, |
| 162 | + string $httpVerb |
| 163 | + ): array { |
| 164 | + $output = []; |
| 165 | + |
| 166 | + if ($classShortname === $defaultController) { |
| 167 | + $pattern = '#' . preg_quote(lcfirst($defaultController), '#') . '\z#'; |
| 168 | + $routeWithoutController = rtrim(preg_replace($pattern, '', $uriByClass), '/'); |
| 169 | + $routeWithoutController = $routeWithoutController ?: '/'; |
| 170 | + |
| 171 | + $output[] = [ |
| 172 | + 'method' => $httpVerb, |
| 173 | + 'route' => $routeWithoutController, |
| 174 | + 'route_params' => '', |
| 175 | + 'handler' => '\\' . $classname . '::' . $methodName, |
| 176 | + 'params' => [], |
| 177 | + ]; |
| 178 | + } |
| 179 | + |
| 180 | + return $output; |
| 181 | + } |
| 182 | +} |
0 commit comments