Skip to content

Commit acab083

Browse files
committed
feat: add AutoRouterImproved/ControllerMethodReader
1 parent ab63c8d commit acab083

3 files changed

Lines changed: 282 additions & 0 deletions

File tree

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 Tests\Support\Controllers;
13+
14+
use CodeIgniter\Controller;
15+
16+
class Newautorouting extends Controller
17+
{
18+
public function getIndex()
19+
{
20+
return 'Hello';
21+
}
22+
23+
public function postSave(int $a, string $b, $c = null)
24+
{
25+
return 'Saved';
26+
}
27+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 CodeIgniter\Test\CIUnitTestCase;
15+
use Tests\Support\Controllers\Newautorouting;
16+
use Tests\Support\Controllers\Remap;
17+
18+
/**
19+
* @internal
20+
*/
21+
final class ControllerMethodReaderTest extends CIUnitTestCase
22+
{
23+
private function createControllerMethodReader(): ControllerMethodReader
24+
{
25+
$methods = [
26+
'get',
27+
'post',
28+
];
29+
$namespace = 'Tests\Support\Controllers';
30+
31+
return new ControllerMethodReader($namespace, $methods);
32+
}
33+
34+
public function testRead()
35+
{
36+
$reader = $this->createControllerMethodReader();
37+
38+
$routes = $reader->read(Newautorouting::class);
39+
40+
$expected = [
41+
0 => [
42+
'method' => 'get',
43+
'route' => 'newautorouting',
44+
'route_params' => '',
45+
'handler' => '\Tests\Support\Controllers\Newautorouting::getIndex',
46+
'params' => [],
47+
],
48+
[
49+
'method' => 'post',
50+
'route' => 'newautorouting/save',
51+
'route_params' => '/../..[/..]',
52+
'handler' => '\Tests\Support\Controllers\Newautorouting::postSave',
53+
'params' => [
54+
'a' => true,
55+
'b' => true,
56+
'c' => false,
57+
],
58+
],
59+
];
60+
61+
$this->assertSame($expected, $routes);
62+
}
63+
64+
public function testReadControllerWithRemap()
65+
{
66+
$reader = $this->createControllerMethodReader();
67+
68+
$routes = $reader->read(Remap::class);
69+
70+
$expected = [];
71+
$this->assertSame($expected, $routes);
72+
}
73+
}

0 commit comments

Comments
 (0)