Skip to content

Commit 4850b62

Browse files
committed
Add primary route accessor method.
1 parent c5ace26 commit 4850b62

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/Config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,26 @@ public function routes() : array
302302
return $this->routesDef;
303303
}
304304

305+
/**
306+
* Returns the primary route.
307+
*
308+
* The primary route is the one marked primary in `routes.yaml`, or else
309+
* the first non-redirect route in that file if none are marked.
310+
*
311+
* @return array
312+
* The route definition. The generated URL of the route is added as a "url" key.
313+
*/
314+
public function getPrimaryRoute() : array
315+
{
316+
foreach ($this->routes() as $url => $route) {
317+
if ($route['primary'] == true) {
318+
return $route;
319+
}
320+
}
321+
322+
throw new \InvalidArgumentException(sprintf('No primary route found. This isn\'t supposed to happen'));
323+
}
324+
305325
/**
306326
* Returns a single route definition.
307327
*

tests/ConfigTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ public function test_get_non_existent_route_throws_exception() : void
133133
$route = $config->getRoute('missing');
134134
}
135135

136+
public function test_primary_route_returns_correct_route() : void
137+
{
138+
$config = new Config($this->mockEnvironmentDeploy);
139+
140+
$route = $config->getPrimaryRoute();
141+
142+
$this->assertEquals('https://www.{default}/', $route['original_url']);
143+
$this->assertEquals('main', $route['id']);
144+
$this->assertTrue($route['primary']);
145+
}
146+
136147
public function test_onenterprise_returns_true_on_enterprise() : void
137148
{
138149
$env = $this->mockEnvironmentDeploy;

0 commit comments

Comments
 (0)