Skip to content
This repository was archived by the owner on Feb 15, 2023. It is now read-only.

Commit eddf8ec

Browse files
committed
Fix Postgres support. Cleanup the code.
1 parent dd4807b commit eddf8ec

File tree

1 file changed

+59
-33
lines changed

1 file changed

+59
-33
lines changed

platformsh-flex-env.php

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,71 @@
1212
*/
1313
function mapPlatformShEnvironment() : void
1414
{
15+
// Set the application secret if it's not already set.
16+
if (!isset($_SERVER['APP_SECRET']) && isset($_SERVER['PLATFORM_PROJECT_ENTROPY'])) {
17+
$_SERVER['APP_SECRET'] = $_SERVER['PLATFORM_PROJECT_ENTROPY'];
18+
}
19+
20+
// Default to production. You can override this value by setting
21+
// `env:APP_ENV` as a project variable, or by adding it to the
22+
// .platform.app.yaml variables block.
23+
if (!isset($_SERVER['APP_ENV'])) {
24+
$_SERVER['APP_ENV'] = 'prod';
25+
}
26+
27+
if (isset($_SERVER['DATABASE_URL'])) {
28+
return;
29+
}
1530

16-
$dbRelationshipName = 'database';
1731
// Set the DATABASE_URL for Doctrine, if necessary.
18-
if (!getenv('DATABASE_URL')) {
19-
# "mysql://[email protected]:3306/symfony?charset=utf8mb4&serverVersion=5.7";
20-
if (isset($_ENV['PLATFORM_RELATIONSHIPS'])) {
21-
$relationships = json_decode(base64_decode(getenv('PLATFORM_RELATIONSHIPS')),true);
22-
foreach ($relationships[$dbRelationshipName] as $endpoint) {
23-
$dbUrl = '';
24-
if (!empty($endpoint['query']['is_master'])) {
25-
$dbUrl = sprintf("%s://%s:%s@%s:%s/%s?charset=utf8mb4&serverVersion=10.2",
26-
$endpoint['scheme'], $endpoint['username'], $endpoint['password'],
27-
$endpoint['host'], $endpoint['port'],
28-
$endpoint['path']);
29-
putenv('DATABASE_URL=' . $dbUrl);
32+
# "mysql://[email protected]:3306/symfony?charset=utf8mb4&serverVersion=5.7";
33+
if (isset($_SERVER['PLATFORM_RELATIONSHIPS'])) {
34+
$relationships = json_decode(base64_decode($_SERVER['PLATFORM_RELATIONSHIPS']), true);
35+
foreach ($relationships['database'] as $endpoint) {
36+
if (empty($endpoint['query']['is_master'])) {
37+
continue;
38+
}
39+
40+
$dbUrl = sprintf(
41+
'%s://%s:%s@%s:%d/%s',
42+
$endpoint['scheme'],
43+
$endpoint['username'],
44+
$endpoint['password'],
45+
$endpoint['host'],
46+
$endpoint['port'],
47+
$endpoint['path']
48+
);
49+
50+
switch ($endpoint['scheme']) {
51+
case 'mysql':
52+
// Defaults to the latest MariaDB version
53+
$dbUrl .= '?serverVersion=10.2&charset=utf8mb4';
3054
break;
31-
}
55+
56+
case 'pgsql':
57+
// Postgres 9.6 is the latest supported version on Platform.sh
58+
$dbUrl .= '?serverVersion=9.6';
3259
}
60+
61+
$_SERVER['DATABASE_URL'] = $dbUrl;
62+
63+
return;
3364
}
34-
else {
35-
// Hack the Doctrine URL to be syntactically valid in a build hook, even
36-
// though it shouldn't be used.
37-
$dbUrl = sprintf("%s://%s:%s@%s:%s/%s?charset=utf8mb4&serverVersion=10.2",
38-
'mysql', '', '',
39-
'localhost', 3306,
40-
'');
41-
$_ENV['DATABASE_URL'] = $dbUrl;
42-
}
43-
}
4465

45-
// Set the application secret if it's not already set.
46-
if (!getenv('APP_SECRET') && getenv('PLATFORM_PROJECT_ENTROPY')) {
47-
putenv('APP_SECRET=' . getenv('PLATFORM_PROJECT_ENTROPY'));
66+
return;
4867
}
4968

50-
// Default to production. You can override this value by setting
51-
// `env:APP_ENV` as a project variable, or by adding it to the
52-
// .platform.app.yaml variables block.
53-
if (!getenv('APP_ENV')) {
54-
putenv('APP_ENV=prod');
55-
}
69+
// Hack the Doctrine URL to be syntactically valid in a build hook, even
70+
// though it shouldn't be used.
71+
$dbUrl = sprintf(
72+
'%s://%s:%s@%s:%s/%s?charset=utf8mb4&serverVersion=10.2',
73+
'mysql',
74+
'',
75+
'',
76+
'localhost',
77+
3306,
78+
''
79+
);
80+
81+
$_SERVER['DATABASE_URL'] = $dbUrl;
5682
}

0 commit comments

Comments
 (0)