Skip to content

Commit cef5e53

Browse files
authored
Merge pull request #6271 from kenjis/feat-spark-routes-route-name
feat: `spark routes` shows route name
2 parents f0405ad + 8300dce commit cef5e53

File tree

8 files changed

+256
-115
lines changed

8 files changed

+256
-115
lines changed

system/Commands/Utilities/Routes.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,17 @@ public function run(array $params)
101101
$sampleUri = $uriGenerator->get($route);
102102
$filters = $filterCollector->get($method, $sampleUri);
103103

104+
if ($handler instanceof Closure) {
105+
$handler = '(Closure)';
106+
}
107+
108+
$routeName = $collection->getRoutesOptions($route)['as'] ?? '»';
109+
104110
$tbody[] = [
105111
strtoupper($method),
106112
$route,
107-
is_string($handler) ? $handler : '(Closure)',
113+
$routeName,
114+
$handler,
108115
implode(' ', array_map('class_basename', $filters['before'])),
109116
implode(' ', array_map('class_basename', $filters['after'])),
110117
];
@@ -149,6 +156,7 @@ public function run(array $params)
149156
$thead = [
150157
'Method',
151158
'Route',
159+
'Name',
152160
'Handler',
153161
'Before Filters',
154162
'After Filters',

system/Commands/Utilities/Routes/AutoRouteCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function get(): array
5656
$tbody[] = [
5757
'auto',
5858
$item['route'],
59+
'',
5960
$item['handler'],
6061
];
6162
}

system/Commands/Utilities/Routes/AutoRouterImproved/AutoRouteCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function get(): array
8585
$tbody[] = [
8686
strtoupper($item['method']) . '(auto)',
8787
$item['route'] . $item['route_params'],
88+
'',
8889
$item['handler'],
8990
$item['before'],
9091
$item['after'],

tests/system/Commands/RoutesTest.php

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Commands;
1313

14+
use CodeIgniter\Router\RouteCollection;
1415
use CodeIgniter\Test\CIUnitTestCase;
1516
use CodeIgniter\Test\StreamFilterTrait;
1617
use Config\Services;
@@ -39,29 +40,120 @@ protected function getBuffer()
3940
return $this->getStreamFilterBuffer();
4041
}
4142

43+
private function getCleanRoutes(): RouteCollection
44+
{
45+
$routes = Services::routes();
46+
$routes->resetRoutes();
47+
$routes->loadRoutes();
48+
49+
return $routes;
50+
}
51+
4252
public function testRoutesCommand()
4353
{
54+
$this->getCleanRoutes();
55+
4456
command('routes');
4557

46-
$this->assertStringContainsString('| (Closure)', $this->getBuffer());
47-
$this->assertStringContainsString('| Route', $this->getBuffer());
48-
$this->assertStringContainsString('| testing', $this->getBuffer());
49-
$this->assertStringContainsString('\\TestController::index', $this->getBuffer());
58+
$expected = <<<'EOL'
59+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
60+
| Method | Route | Name | Handler | Before Filters | After Filters |
61+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
62+
| GET | / | » | \App\Controllers\Home::index | | toolbar |
63+
| GET | closure | » | (Closure) | | toolbar |
64+
| GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
65+
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
66+
| POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
67+
| PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
68+
| DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
69+
| OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
70+
| TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
71+
| CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
72+
| CLI | testing | testing-index | \App\Controllers\TestController::index | | |
73+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
74+
EOL;
75+
$this->assertStringContainsString($expected, $this->getBuffer());
5076
}
5177

52-
public function testRoutesCommandRouteFilterAndAutoRoute()
78+
public function testRoutesCommandAutoRouteImproved()
5379
{
54-
$routes = Services::routes();
55-
$routes->setDefaultNamespace('App\Controllers');
56-
$routes->resetRoutes();
57-
$routes->get('/', 'Home::index', ['filter' => 'csrf']);
80+
$routes = $this->getCleanRoutes();
81+
82+
$routes->setAutoRoute(true);
83+
config('Feature')->autoRoutesImproved = true;
84+
$namespace = 'Tests\Support\Controllers';
85+
$routes->setDefaultNamespace($namespace);
86+
87+
command('routes');
88+
89+
$expected = <<<'EOL'
90+
+------------+--------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
91+
| Method | Route | Name | Handler | Before Filters | After Filters |
92+
+------------+--------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
93+
| GET | / | » | \App\Controllers\Home::index | | toolbar |
94+
| GET | closure | » | (Closure) | | toolbar |
95+
| GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
96+
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
97+
| POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
98+
| PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
99+
| DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
100+
| OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
101+
| TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
102+
| CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
103+
| CLI | testing | testing-index | \App\Controllers\TestController::index | | |
104+
| GET(auto) | newautorouting | | \Tests\Support\Controllers\Newautorouting::getIndex | | toolbar |
105+
| POST(auto) | newautorouting/save/../..[/..] | | \Tests\Support\Controllers\Newautorouting::postSave | | toolbar |
106+
+------------+--------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
107+
EOL;
108+
$this->assertStringContainsString($expected, $this->getBuffer());
109+
}
110+
111+
public function testRoutesCommandRouteLegacy()
112+
{
113+
$routes = $this->getCleanRoutes();
114+
58115
$routes->setAutoRoute(true);
116+
$namespace = 'Tests\Support\Controllers';
117+
$routes->setDefaultNamespace($namespace);
59118

60119
command('routes');
61120

62-
$this->assertStringContainsString(
63-
'|auto|/|\App\Controllers\Home::index||toolbar|',
64-
str_replace(' ', '', $this->getBuffer())
65-
);
121+
$expected = <<<'EOL'
122+
+---------+-------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
123+
| Method | Route | Name | Handler | Before Filters | After Filters |
124+
+---------+-------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
125+
| GET | / | » | \App\Controllers\Home::index | | toolbar |
126+
| GET | closure | » | (Closure) | | toolbar |
127+
| GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
128+
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
129+
| POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
130+
| PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
131+
| DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
132+
| OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
133+
| TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
134+
| CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
135+
| CLI | testing | testing-index | \App\Controllers\TestController::index | | |
136+
| auto | hello | | \Tests\Support\Controllers\Hello::index | | toolbar |
137+
| auto | hello/index[/...] | | \Tests\Support\Controllers\Hello::index | | toolbar |
138+
| auto | newautorouting/getIndex[/...] | | \Tests\Support\Controllers\Newautorouting::getIndex | | toolbar |
139+
| auto | newautorouting/postSave[/...] | | \Tests\Support\Controllers\Newautorouting::postSave | | toolbar |
140+
| auto | popcorn | | \Tests\Support\Controllers\Popcorn::index | | toolbar |
141+
| auto | popcorn/index[/...] | | \Tests\Support\Controllers\Popcorn::index | | toolbar |
142+
| auto | popcorn/pop[/...] | | \Tests\Support\Controllers\Popcorn::pop | | toolbar |
143+
| auto | popcorn/popper[/...] | | \Tests\Support\Controllers\Popcorn::popper | | toolbar |
144+
| auto | popcorn/weasel[/...] | | \Tests\Support\Controllers\Popcorn::weasel | | toolbar |
145+
| auto | popcorn/oops[/...] | | \Tests\Support\Controllers\Popcorn::oops | | toolbar |
146+
| auto | popcorn/goaway[/...] | | \Tests\Support\Controllers\Popcorn::goaway | | toolbar |
147+
| auto | popcorn/index3[/...] | | \Tests\Support\Controllers\Popcorn::index3 | | toolbar |
148+
| auto | popcorn/canyon[/...] | | \Tests\Support\Controllers\Popcorn::canyon | | toolbar |
149+
| auto | popcorn/cat[/...] | | \Tests\Support\Controllers\Popcorn::cat | | toolbar |
150+
| auto | popcorn/json[/...] | | \Tests\Support\Controllers\Popcorn::json | | toolbar |
151+
| auto | popcorn/xml[/...] | | \Tests\Support\Controllers\Popcorn::xml | | toolbar |
152+
| auto | popcorn/toindex[/...] | | \Tests\Support\Controllers\Popcorn::toindex | | toolbar |
153+
| auto | popcorn/echoJson[/...] | | \Tests\Support\Controllers\Popcorn::echoJson | | toolbar |
154+
| auto | remap[/...] | | \Tests\Support\Controllers\Remap::_remap | | toolbar |
155+
+---------+-------------------------------+---------------+-----------------------------------------------------+----------------+---------------+
156+
EOL;
157+
$this->assertStringContainsString($expected, $this->getBuffer());
66158
}
67159
}

0 commit comments

Comments
 (0)