Skip to content

Commit 6429224

Browse files
committed
Add upstream routes accessor.
1 parent 4850b62 commit 6429224

3 files changed

Lines changed: 138 additions & 0 deletions

File tree

src/Config.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,31 @@ public function getPrimaryRoute() : array
322322
throw new \InvalidArgumentException(sprintf('No primary route found. This isn\'t supposed to happen'));
323323
}
324324

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 iterable
338+
* An iterable of route definitions.
339+
*/
340+
public function getUpstreamRoutes(string $appName = null) : iterable
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+
325350
/**
326351
* Returns a single route definition.
327352
*

tests/ConfigTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,45 @@ public function test_primary_route_returns_correct_route() : void
144144
$this->assertTrue($route['primary']);
145145
}
146146

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+
147186
public function test_onenterprise_returns_true_on_enterprise() : void
148187
{
149188
$env = $this->mockEnvironmentDeploy;

tests/valid/PLATFORM_ROUTES.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,80 @@
3636
"enabled" : false
3737
}
3838
},
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" : "main",
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" : "main",
109+
"ssi" : {
110+
"enabled" : false
111+
}
112+
},
39113
"http://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" : {
40114
"id" : null,
41115
"to" : "https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/",

0 commit comments

Comments
 (0)