Skip to content

Commit 0b989f1

Browse files
authored
Merge pull request #7213 from kenjis/feat-spark-routes-host
feat: add `--host` option to `spark routes`
2 parents c682fc6 + 15c3528 commit 0b989f1

File tree

6 files changed

+99
-6
lines changed

6 files changed

+99
-6
lines changed

psalm-baseline.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@
125125
</UndefinedGlobalVariable>
126126
</file>
127127
<file src="tests/_support/Config/Routes.php">
128-
<UndefinedGlobalVariable occurrences="2">
129-
<code>$routes</code>
128+
<UndefinedGlobalVariable occurrences="5">
130129
<code>$routes</code>
131130
</UndefinedGlobalVariable>
132131
</file>

system/Commands/Utilities/Routes.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class Routes extends BaseCommand
6969
* @var array<string, string>
7070
*/
7171
protected $options = [
72-
'-h' => 'Sort by Handler.',
72+
'-h' => 'Sort by Handler.',
73+
'--host' => 'Specify hostname in request URI.',
7374
];
7475

7576
/**
@@ -78,9 +79,24 @@ class Routes extends BaseCommand
7879
public function run(array $params)
7980
{
8081
$sortByHandler = array_key_exists('h', $params);
82+
$host = $params['host'] ?? null;
83+
84+
// Set HTTP_HOST
85+
if ($host) {
86+
$request = Services::request();
87+
$_SERVER = $request->getServer();
88+
$_SERVER['HTTP_HOST'] = $host;
89+
$request->setGlobal('server', $_SERVER);
90+
}
8191

8292
$collection = Services::routes()->loadRoutes();
83-
$methods = [
93+
94+
// Reset HTTP_HOST
95+
if ($host) {
96+
unset($_SERVER['HTTP_HOST']);
97+
}
98+
99+
$methods = [
84100
'get',
85101
'head',
86102
'post',
@@ -171,6 +187,10 @@ public function run(array $params)
171187
usort($tbody, static fn ($handler1, $handler2) => strcmp($handler1[3], $handler2[3]));
172188
}
173189

190+
if ($host) {
191+
CLI::write('Host: ' . $host);
192+
}
193+
174194
CLI::table($tbody, $thead);
175195
}
176196
}

tests/_support/Config/Routes.php

+3
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@
1414
// This is a simple file to include for testing the RouteCollection class.
1515
$routes->add('testing', 'TestController::index', ['as' => 'testing-index']);
1616
$routes->get('closure', static fn () => 'closure test');
17+
$routes->get('/', 'Blog::index', ['hostname' => 'blog.example.com']);
18+
$routes->get('/', 'Sub::index', ['subdomain' => 'sub']);
19+
$routes->get('/all', 'AllDomain::index', ['subdomain' => '*']);

tests/system/Commands/RoutesTest.php

+59-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private function getCleanRoutes(): RouteCollection
5353

5454
public function testRoutesCommand()
5555
{
56-
$this->getCleanRoutes();
56+
Services::injectMock('routes', null);
5757

5858
command('routes');
5959

@@ -79,7 +79,7 @@ public function testRoutesCommand()
7979

8080
public function testRoutesCommandSortByHandler()
8181
{
82-
$this->getCleanRoutes();
82+
Services::injectMock('routes', null);
8383

8484
command('routes -h');
8585

@@ -103,6 +103,62 @@ public function testRoutesCommandSortByHandler()
103103
$this->assertStringContainsString($expected, $this->getBuffer());
104104
}
105105

106+
public function testRoutesCommandHostHostname()
107+
{
108+
Services::injectMock('routes', null);
109+
110+
command('routes --host blog.example.com');
111+
112+
$expected = <<<'EOL'
113+
Host: blog.example.com
114+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
115+
| Method | Route | Name | Handler | Before Filters | After Filters |
116+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
117+
| GET | / | » | \App\Controllers\Blog::index | | toolbar |
118+
| GET | closure | » | (Closure) | | toolbar |
119+
| GET | all | » | \App\Controllers\AllDomain::index | | toolbar |
120+
| GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
121+
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
122+
| POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
123+
| PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
124+
| DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
125+
| OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
126+
| TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
127+
| CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
128+
| CLI | testing | testing-index | \App\Controllers\TestController::index | | |
129+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
130+
EOL;
131+
$this->assertStringContainsString($expected, $this->getBuffer());
132+
}
133+
134+
public function testRoutesCommandHostSubdomain()
135+
{
136+
Services::injectMock('routes', null);
137+
138+
command('routes --host sub.example.com');
139+
140+
$expected = <<<'EOL'
141+
Host: sub.example.com
142+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
143+
| Method | Route | Name | Handler | Before Filters | After Filters |
144+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
145+
| GET | / | » | \App\Controllers\Sub::index | | toolbar |
146+
| GET | closure | » | (Closure) | | toolbar |
147+
| GET | all | » | \App\Controllers\AllDomain::index | | toolbar |
148+
| GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
149+
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
150+
| POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
151+
| PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
152+
| DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
153+
| OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
154+
| TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
155+
| CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
156+
| CLI | testing | testing-index | \App\Controllers\TestController::index | | |
157+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
158+
EOL;
159+
$this->assertStringContainsString($expected, $this->getBuffer());
160+
}
161+
106162
public function testRoutesCommandAutoRouteImproved()
107163
{
108164
$routes = $this->getCleanRoutes();
@@ -139,6 +195,7 @@ public function testRoutesCommandAutoRouteImproved()
139195
public function testRoutesCommandRouteLegacy()
140196
{
141197
$routes = $this->getCleanRoutes();
198+
$routes->loadRoutes();
142199

143200
$routes->setAutoRoute(true);
144201
$namespace = 'Tests\Support\Controllers';

user_guide_src/source/changelogs/v4.4.0.rst

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Enhancements
3232
Commands
3333
========
3434

35+
- Now ``spark routes`` command can specify the host in the request URL.
36+
See :ref:`routing-spark-routes-specify-host`.
37+
3538
Testing
3639
=======
3740

user_guide_src/source/incoming/routing.rst

+11
Original file line numberDiff line numberDiff line change
@@ -868,3 +868,14 @@ Sort by Handler
868868
You can sort the routes by *Handler*::
869869

870870
> php spark routes -h
871+
872+
.. _routing-spark-routes-specify-host:
873+
874+
Specify Host
875+
------------
876+
877+
.. versionadded:: 4.4.0
878+
879+
You can specify the host in the request URL with the ``--host`` option::
880+
881+
> php spark routes --host accounts.example.com

0 commit comments

Comments
 (0)