Skip to content

Commit 9cb8f00

Browse files
authored
Merge pull request #15 from platformsh/routes-utils
Add useful route filtering utilities
2 parents c5ace26 + 1a6bd15 commit 9cb8f00

3 files changed

Lines changed: 252 additions & 83 deletions

File tree

src/Config.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,51 @@ 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+
325+
/**
326+
* Returns just those routes that point to a valid upstream.
327+
*
328+
* This method is similar to routes(), but filters out redirect routes that are rarely
329+
* useful for app configuration. If desired it can also filter to just those routes
330+
* whose upstream is a given application name. To retrieve routes that point to the
331+
* current application where the code is being run, use:
332+
*
333+
* $routes = $config->getUpstreamRoutes($config->applicationName);
334+
*
335+
* @param string|null $appName
336+
* The name of the upstream app on which to filter, if any.
337+
* @return array
338+
* An array of route definitions.
339+
*/
340+
public function getUpstreamRoutes(string $appName = null) : array
341+
{
342+
return array_filter($this->routes(), function (array $route) use ($appName) {
343+
return $route['type'] == 'upstream'
344+
// On Dedicated, the upstream name sometimes is `app:http` instead of just `app`.
345+
// If no name is specified then don't bother checking.
346+
&& (is_null($appName) || $appName == explode(':', $route['upstream'])[0]);
347+
});
348+
}
349+
305350
/**
306351
* Returns a single route definition.
307352
*

tests/ConfigTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,56 @@ 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+
147+
public function test_upstream_routes() : void
148+
{
149+
$config = new Config($this->mockEnvironmentDeploy);
150+
151+
$routes = $config->getUpstreamRoutes();
152+
153+
$this->assertCount(3, $routes);
154+
$this->assertArrayHasKey('https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/', $routes);
155+
$this->assertEquals('https://www.{default}/', $routes['https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/']['original_url']);
156+
}
157+
158+
public function test_upstream_routes_for_app() : void
159+
{
160+
$config = new Config($this->mockEnvironmentDeploy);
161+
162+
$routes = $config->getUpstreamRoutes('app');
163+
164+
$this->assertCount(2, $routes);
165+
$this->assertArrayHasKey('https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/', $routes);
166+
$this->assertEquals('https://www.{default}/', $routes['https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/']['original_url']);
167+
}
168+
169+
public function test_upstream_routes_for_app_on_dedicated() : void
170+
{
171+
$env = $this->mockEnvironmentDeploy;
172+
// Simulate a Dedicated-style upstream name.
173+
$routeData = $this->loadJsonFile('PLATFORM_ROUTES');
174+
$routeData['https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/']['upstream'] = 'app:http';
175+
$env['PLATFORM_ROUTES'] = $this->encode($routeData);
176+
177+
$config = new Config($env);
178+
179+
$routes = $config->getUpstreamRoutes('app');
180+
181+
$this->assertCount(2, $routes);
182+
$this->assertArrayHasKey('https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/', $routes);
183+
$this->assertEquals('https://www.{default}/', $routes['https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/']['original_url']);
184+
}
185+
136186
public function test_onenterprise_returns_true_on_enterprise() : void
137187
{
138188
$env = $this->mockEnvironmentDeploy;

tests/valid/PLATFORM_ROUTES.json

Lines changed: 157 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,160 @@
11
{
2-
"https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
3-
"original_url" : "https://www.{default}/",
4-
"attributes" : {},
5-
"type" : "upstream",
6-
"restrict_robots" : false,
7-
"tls" : {
8-
"client_authentication" : null,
9-
"min_version" : 771,
10-
"client_certificate_authorities" : [],
11-
"strict_transport_security" : {
12-
"include_subdomains" : null,
2+
"https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
3+
"original_url" : "https://www.{default}/",
4+
"attributes" : {},
5+
"type" : "upstream",
6+
"restrict_robots" : false,
7+
"tls" : {
8+
"client_authentication" : null,
9+
"min_version" : 771,
10+
"client_certificate_authorities" : [],
11+
"strict_transport_security" : {
12+
"include_subdomains" : null,
13+
"enabled" : true,
14+
"preload" : null
15+
}
16+
},
17+
"upstream" : "app",
18+
"cache" : {
1319
"enabled" : true,
14-
"preload" : null
15-
}
16-
},
17-
"upstream" : "app",
18-
"cache" : {
19-
"enabled" : true,
20-
"headers" : [
21-
"Accept",
22-
"Accept-Language"
23-
],
24-
"cookies" : [
25-
"/^SS?ESS.*/"
26-
],
27-
"default_ttl" : 0
28-
},
29-
"http_access" : {
30-
"addresses" : [],
31-
"basic_auth" : {}
32-
},
33-
"primary" : true,
34-
"id" : "main",
35-
"ssi" : {
36-
"enabled" : false
37-
}
38-
},
39-
"http://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
40-
"id" : null,
41-
"to" : "https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/",
42-
"primary" : false,
43-
"original_url" : "http://www.{default}/",
44-
"http_access" : {
45-
"basic_auth" : {},
46-
"addresses" : []
47-
},
48-
"restrict_robots" : false,
49-
"type" : "redirect"
50-
},
51-
"https://master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
52-
"id" : null,
53-
"to" : "https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/",
54-
"primary" : false,
55-
"http_access" : {
56-
"addresses" : [],
57-
"basic_auth" : {}
58-
},
59-
"tls" : {
60-
"client_authentication" : null,
61-
"min_version" : null,
62-
"strict_transport_security" : {
63-
"include_subdomains" : null,
64-
"enabled" : null,
65-
"preload" : null
66-
},
67-
"client_certificate_authorities" : []
68-
},
69-
"restrict_robots" : false,
70-
"type" : "redirect",
71-
"attributes" : {},
72-
"original_url" : "https://{default}/"
73-
},
74-
"http://master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
75-
"to" : "https://master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/",
76-
"id" : null,
77-
"primary" : false,
78-
"http_access" : {
79-
"addresses" : [],
80-
"basic_auth" : {}
81-
},
82-
"original_url" : "http://{default}/",
83-
"type" : "redirect",
84-
"restrict_robots" : false
85-
}
20+
"headers" : [
21+
"Accept",
22+
"Accept-Language"
23+
],
24+
"cookies" : [
25+
"/^SS?ESS.*/"
26+
],
27+
"default_ttl" : 0
28+
},
29+
"http_access" : {
30+
"addresses" : [],
31+
"basic_auth" : {}
32+
},
33+
"primary" : true,
34+
"id" : "main",
35+
"ssi" : {
36+
"enabled" : false
37+
}
38+
},
39+
"https://www2.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
40+
"original_url" : "https://www.{default}/",
41+
"attributes" : {},
42+
"type" : "upstream",
43+
"restrict_robots" : false,
44+
"tls" : {
45+
"client_authentication" : null,
46+
"min_version" : 771,
47+
"client_certificate_authorities" : [],
48+
"strict_transport_security" : {
49+
"include_subdomains" : null,
50+
"enabled" : true,
51+
"preload" : null
52+
}
53+
},
54+
"upstream" : "app",
55+
"cache" : {
56+
"enabled" : true,
57+
"headers" : [
58+
"Accept",
59+
"Accept-Language"
60+
],
61+
"cookies" : [
62+
"/^SS?ESS.*/"
63+
],
64+
"default_ttl" : 0
65+
},
66+
"http_access" : {
67+
"addresses" : [],
68+
"basic_auth" : {}
69+
},
70+
"primary" : false,
71+
"id" : "main2",
72+
"ssi" : {
73+
"enabled" : false
74+
}
75+
},
76+
"https://www3.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
77+
"original_url" : "https://www.{default}/",
78+
"attributes" : {},
79+
"type" : "upstream",
80+
"restrict_robots" : false,
81+
"tls" : {
82+
"client_authentication" : null,
83+
"min_version" : 771,
84+
"client_certificate_authorities" : [],
85+
"strict_transport_security" : {
86+
"include_subdomains" : null,
87+
"enabled" : true,
88+
"preload" : null
89+
}
90+
},
91+
"upstream" : "app2",
92+
"cache" : {
93+
"enabled" : true,
94+
"headers" : [
95+
"Accept",
96+
"Accept-Language"
97+
],
98+
"cookies" : [
99+
"/^SS?ESS.*/"
100+
],
101+
"default_ttl" : 0
102+
},
103+
"http_access" : {
104+
"addresses" : [],
105+
"basic_auth" : {}
106+
},
107+
"primary" : false,
108+
"id" : "main3",
109+
"ssi" : {
110+
"enabled" : false
111+
}
112+
},
113+
"http://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
114+
"id" : null,
115+
"to" : "https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/",
116+
"primary" : false,
117+
"original_url" : "http://www.{default}/",
118+
"http_access" : {
119+
"basic_auth" : {},
120+
"addresses" : []
121+
},
122+
"restrict_robots" : false,
123+
"type" : "redirect"
124+
},
125+
"https://master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
126+
"id" : null,
127+
"to" : "https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/",
128+
"primary" : false,
129+
"http_access" : {
130+
"addresses" : [],
131+
"basic_auth" : {}
132+
},
133+
"tls" : {
134+
"client_authentication" : null,
135+
"min_version" : null,
136+
"strict_transport_security" : {
137+
"include_subdomains" : null,
138+
"enabled" : null,
139+
"preload" : null
140+
},
141+
"client_certificate_authorities" : []
142+
},
143+
"restrict_robots" : false,
144+
"type" : "redirect",
145+
"attributes" : {},
146+
"original_url" : "https://{default}/"
147+
},
148+
"http://master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
149+
"to" : "https://master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/",
150+
"id" : null,
151+
"primary" : false,
152+
"http_access" : {
153+
"addresses" : [],
154+
"basic_auth" : {}
155+
},
156+
"original_url" : "http://{default}/",
157+
"type" : "redirect",
158+
"restrict_robots" : false
159+
}
86160
}

0 commit comments

Comments
 (0)