Skip to content

Commit 08f240f

Browse files
authored
Add multi-host support for Group (#168)
1 parent bba6ad6 commit 08f240f

File tree

7 files changed

+63
-22
lines changed

7 files changed

+63
-22
lines changed

.phpstorm.meta.php/Group.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'prefix',
1010
'namePrefix',
1111
'host',
12+
'hosts',
1213
'corsMiddleware',
1314
'items',
1415
'middlewareDefinitions',

.phpstorm.meta.php/Route.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'routeDataKeys',
99
'name',
1010
'host',
11+
'hosts',
1112
'pattern',
1213
'methods',
1314
'override',

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Yii Router Change Log
22

3-
## 1.0.1 under development
3+
## 1.1.0 under development
44

5-
- Enh #163: Allow multiple separate hosts with new `Route::hosts` method (Gerych1984)
5+
- Enh #163: Allow multiple separate hosts with new `Route::hosts()` method (@Gerych1984)
6+
- Enh #168: Allow multiple separate hosts with new `Group::hosts()` method (@rustamwin)
67

78
## 1.0.0 December 30, 2021
89

src/Group.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ final class Group
2626
*/
2727
private array $middlewareDefinitions = [];
2828

29-
private ?string $host = null;
29+
/**
30+
* @var string[]
31+
*/
32+
private array $hosts = [];
3033
private ?string $namePrefix = null;
3134
private bool $routesAdded = false;
3235
private bool $middlewareAdded = false;
@@ -166,9 +169,27 @@ public function namePrefix(string $namePrefix): self
166169
}
167170

168171
public function host(string $host): self
172+
{
173+
return $this->hosts($host);
174+
}
175+
176+
/**
177+
* @param string ...$hosts
178+
*
179+
* @return self
180+
*/
181+
public function hosts(string ...$hosts): self
169182
{
170183
$new = clone $this;
171-
$new->host = rtrim($host, '/');
184+
185+
foreach ($hosts as $host) {
186+
$host = rtrim($host, '/');
187+
188+
if ($host !== '' && !in_array($host, $new->hosts, true)) {
189+
$new->hosts[] = $host;
190+
}
191+
}
192+
172193
return $new;
173194
}
174195

@@ -201,9 +222,11 @@ public function disableMiddleware(...$middlewareDefinition): self
201222
* @psalm-return (
202223
* T is ('prefix'|'namePrefix'|'host') ? string|null :
203224
* (T is 'items' ? Group[]|Route[] :
204-
* (T is ('hasCorsMiddleware'|'hasDispatcher') ? bool :
205-
* (T is 'middlewareDefinitions' ? list<array|callable|string> :
206-
* (T is 'corsMiddleware' ? array|callable|string|null : mixed)
225+
* (T is 'hosts' ? array<array-key, string> :
226+
* (T is ('hasCorsMiddleware'|'hasDispatcher') ? bool :
227+
* (T is 'middlewareDefinitions' ? list<array|callable|string> :
228+
* (T is 'corsMiddleware' ? array|callable|string|null : mixed)
229+
* )
207230
* )
208231
* )
209232
* )
@@ -217,7 +240,9 @@ public function getData(string $key)
217240
case 'namePrefix':
218241
return $this->namePrefix;
219242
case 'host':
220-
return $this->host;
243+
return $this->hosts[0] ?? null;
244+
case 'hosts':
245+
return $this->hosts;
221246
case 'corsMiddleware':
222247
return $this->corsMiddleware;
223248
case 'items':

src/RouteCollection.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,14 @@ private function injectGroup(Group $group, array &$tree, string $prefix = '', st
111111
$namePrefix .= (string) $group->getData('namePrefix');
112112
$items = $group->getData('items');
113113
$pattern = null;
114-
$host = null;
114+
$hosts = [];
115115
foreach ($items as $item) {
116116
if (!$this->isStaticRoute($item)) {
117117
$item = $item->prependMiddleware(...$group->getData('middlewareDefinitions'));
118118
}
119119

120-
if ($group->getData('host') !== null && $item->getData('host') === null) {
121-
/** @psalm-suppress PossiblyNullArgument Checked group host on not null above. */
122-
$item = $item->host($group->getData('host'));
120+
if (!empty($group->getData('hosts')) && empty($item->getData('hosts'))) {
121+
$item = $item->hosts(...$group->getData('hosts'));
123122
}
124123

125124
if ($item instanceof Group) {
@@ -150,7 +149,7 @@ private function injectGroup(Group $group, array &$tree, string $prefix = '', st
150149
}
151150

152151
if ($group->getData('hasCorsMiddleware')) {
153-
$this->processCors($group, $host, $pattern, $modifiedItem, $tree);
152+
$this->processCors($group, $hosts, $pattern, $modifiedItem, $tree);
154153
}
155154

156155
$routeName = $modifiedItem->getData('name');
@@ -167,21 +166,21 @@ private function injectGroup(Group $group, array &$tree, string $prefix = '', st
167166
*/
168167
private function processCors(
169168
Group $group,
170-
?string &$host,
169+
array &$hosts,
171170
?string &$pattern,
172171
Route &$modifiedItem,
173172
array &$tree
174173
): void {
175174
/** @var array|callable|string $middleware */
176175
$middleware = $group->getData('corsMiddleware');
177176
$isNotDuplicate = !in_array(Method::OPTIONS, $modifiedItem->getData('methods'), true)
178-
&& ($pattern !== $modifiedItem->getData('pattern') || $host !== $modifiedItem->getData('host'));
177+
&& ($pattern !== $modifiedItem->getData('pattern') || $hosts !== $modifiedItem->getData('hosts'));
179178

180179
$pattern = $modifiedItem->getData('pattern');
181-
$host = $modifiedItem->getData('host');
180+
$hosts = $modifiedItem->getData('hosts');
182181
$optionsRoute = Route::options($pattern);
183-
if ($host !== null) {
184-
$optionsRoute = $optionsRoute->host($host);
182+
if (!empty($hosts)) {
183+
$optionsRoute = $optionsRoute->hosts(...$hosts);
185184
}
186185
if ($isNotDuplicate) {
187186
$optionsRoute = $optionsRoute->middleware($middleware);

tests/GroupTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,21 @@ public function testHost(): void
245245
{
246246
$group = Group::create()->host('https://yiiframework.com/');
247247

248-
$this->assertSame($group->getData('host'), 'https://yiiframework.com');
248+
$this->assertSame('https://yiiframework.com', $group->getData('host'));
249+
}
250+
251+
public function testHosts(): void
252+
{
253+
$group = Group::create()->hosts('https://yiiframework.com/', 'https://yiiframework.ru/');
254+
255+
$this->assertSame(['https://yiiframework.com', 'https://yiiframework.ru'], $group->getData('hosts'));
249256
}
250257

251258
public function testName(): void
252259
{
253260
$group = Group::create()->namePrefix('api');
254261

255-
$this->assertSame($group->getData('namePrefix'), 'api');
262+
$this->assertSame('api', $group->getData('namePrefix'));
256263
}
257264

258265
public function testGetDataWithWrongKey(): void

tests/RouteCollectionTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ public function testGroupHost(): void
193193
->routes(
194194
Route::get('/project/{name}')->name('project')
195195
)
196-
->host('https://yiipowered.com/'),
196+
->hosts('https://yiipowered.com/', 'https://yiiframework.ru/'),
197+
Group::create()
198+
->routes(
199+
Route::get('/user/{username}')->name('user')
200+
),
197201
Route::get('/images/{name}')->name('image')
198202
)
199203
->host('https://yiiframework.com/');
@@ -204,8 +208,11 @@ public function testGroupHost(): void
204208
$routeCollection = new RouteCollection($collector);
205209
$route1 = $routeCollection->getRoute('image');
206210
$route2 = $routeCollection->getRoute('project');
211+
$route3 = $routeCollection->getRoute('user');
207212
$this->assertSame('https://yiiframework.com', $route1->getData('host'));
208-
$this->assertSame('https://yiipowered.com', $route2->getData('host'));
213+
$this->assertCount(2, $route2->getData('hosts'));
214+
$this->assertSame(['https://yiipowered.com', 'https://yiiframework.ru'], $route2->getData('hosts'));
215+
$this->assertSame('https://yiiframework.com', $route3->getData('host'));
209216
}
210217

211218
public function testGroupName(): void

0 commit comments

Comments
 (0)