This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathplatformsh-flex-env.php
367 lines (313 loc) · 11.4 KB
/
platformsh-flex-env.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
declare(strict_types=1);
namespace Platformsh\FlexBridge;
use Platformsh\ConfigReader\Config;
// These are only for the Foundation 1.x regions. If you are using some other version
// please move to a newer region, where these values will not be needed.
const DEFAULT_MARIADB_VERSION = '10.2';
const DEFAULT_MYSQL_VERSION = '8.0';
const DEFAULT_POSTGRESQL_VERSION = '9.6';
mapPlatformShEnvironment();
/**
* Map Platform.Sh environment variables to the values Symfony Flex expects.
*
* This is wrapped up into a function to avoid executing code in the global
* namespace.
*/
function mapPlatformShEnvironment() : void
{
$config = new Config();
if (!$config->inRuntime()) {
if ($config->inBuild()) {
// In the build hook we still need to set a fake Doctrine URL in order to
// work around bugs in Doctrine.
setDefaultDoctrineUrl();
}
return;
}
$config->registerFormatter('doctrine', __NAMESPACE__ . '\doctrineFormatter');
$config->registerFormatter('rabbitmq', __NAMESPACE__ . '\rabbitMqFormatter');
// Set the application secret if it's not already set.
// We force re-setting the APP_SECRET to ensure it's set in all of PHP's various
// environment places.
$secret = getenv('APP_SECRET') ?: $config->projectEntropy;
setEnvVar('APP_SECRET', $secret);
// Default to production. You can override this value by setting
// `env:APP_ENV` as a project variable, or by adding it to the
// .platform.app.yaml variables block.
$appEnv = getenv('APP_ENV') ?: 'prod';
setEnvVar('APP_ENV', $appEnv);
// Map services as feasible.
mapPlatformShDatabase('database', $config);
mapPlatformShMongoDatabase('mongodatabase', $config);
mapPlatformShElasticSearch('elasticsearch', $config);
mapPlatformShRabbitMq('rabbitmqqueue', $config);
mapPlatformShSolr('solr', $config);
mapPlatformShRedisSession('redissession', $config);
mapPlatformShRedisCache('rediscache', $config);
// Set the Swiftmailer configuration if it's not set already.
if (!getenv('MAILER_URL')) {
mapPlatformShSwiftmailer($config);
}
// Set the Symfony Mailer configuration if it's not set already.
if (!getenv('MAILER_DSN')) {
mapPlatformShMailer($config);
}
}
/**
* Sets an environment variable in all the myriad places PHP can store it.
*
* @param string $name
* The name of the variable to set.
* @param null|string $value
* The value to set. Null to unset it.
*/
function setEnvVar(string $name, ?string $value) : void
{
if (!putenv("$name=$value")) {
throw new \RuntimeException('Failed to create environment variable: ' . $name);
}
$order = ini_get('variables_order');
if (stripos($order, 'e') !== false) {
$_ENV[$name] = $value;
}
if (stripos($order, 's') !== false) {
if (strpos($name, 'HTTP_') !== false) {
throw new \RuntimeException('Refusing to add ambiguous environment variable ' . $name . ' to $_SERVER');
}
$_SERVER[$name] = $value;
}
}
function mapPlatformShSwiftmailer(Config $config)
{
$mailUrl = sprintf(
'%s://%s:%d/',
!empty($config->smtpHost) ? 'smtp' : 'null',
!empty($config->smtpHost) ? $config->smtpHost : 'localhost',
25
);
setEnvVar('MAILER_URL', $mailUrl);
}
function mapPlatformShMailer(Config $config)
{
$mailUrl = sprintf(
'%s://%s:%d/',
!empty($config->smtpHost) ? 'smtp' : 'null',
!empty($config->smtpHost) ? $config->smtpHost : 'localhost',
25
);
setEnvVar('MAILER_DSN', $mailUrl);
}
/**
* Formatter for the Symfony RabbitMQ Messenger format.
*
* The %2f default vhost is not a formatting code, but a URL-encoded
* forward slash (/), which is the default vhost name in RabbitMQ.
* It's a weird default name, but it's what RabbitMQ uses.
*/
function rabbitMqFormatter(array $credentials): string
{
return sprintf('%s://%s:%s@%s:%d/%s/messages',
$credentials['scheme'],
$credentials['username'],
$credentials['password'],
$credentials['host'],
$credentials['port'],
$credentials['vhost'] ?? '%2f'
);
}
/**
* Formatter for Doctrine's DB URL format.
*
* Note that non-default DB versions are not supported on Foundation 1 regions.
* On those regions we cannot derive the version from the credential information
* so the hard-coded defaults defined at the top of the file are used. To use a
* different version of MariaDB or PostgreSQL, or to use Oracle MySQL at all,
* move to a Foundation 3 region.
*/
function doctrineFormatter(array $credentials) : string
{
$dbUrl = sprintf(
'%s://%s:%s@%s:%d/%s',
$credentials['scheme'],
$credentials['username'],
$credentials['password'],
$credentials['host'],
$credentials['port'],
$credentials['path']
);
if (isset($credentials['type'])) {
list($type, $version) = explode(':', $credentials['type']);
}
else {
$type = $credentials['scheme'];
$version = null;
}
// "mysql" is an alias for MariaDB, so use the same mapper for both.
$mappers['mysql'] = __NAMESPACE__ . '\doctrineFormatterMariaDB';
$mappers['mariadb'] = __NAMESPACE__ . '\doctrineFormatterMariaDB';
$mappers['oracle-mysql'] = __NAMESPACE__ . '\doctrineFormatterOracleMySQL';
// The "Scheme" is pgsql, while the type is postgresql. Both end up in the same place.
$mappers['pgsql'] = __NAMESPACE__ . '\doctrineFormatterPostgreSQL';
$mappers['postgresql'] = __NAMESPACE__ . '\doctrineFormatterPostgreSQL';
// Add a query suffix that is appropriate to the specific DB type to handle version information.
return $dbUrl . $mappers[$type]($version);
}
function doctrineFormatterMariaDB(?string $version) : string
{
$version = $version ?? DEFAULT_MARIADB_VERSION;
$version = sprintf('mariadb-%s', $version);
// Doctrine requires a full version number, even though it doesn't really matter what the patch version is,
// except for MariaDB 10.2, where it needs a verison greater than 10.2.6 to avoid certain bugs.
$version .= ($version === 'mariadb-10.2') ? '.12' : '.0';
return sprintf('?charset=utf8mb4&serverVersion=%s', $version);
}
function doctrineFormatterOracleMySQL(?string $version) : string
{
$version = $version ?? DEFAULT_MYSQL_VERSION;
return sprintf('?charset=utf8mb4&serverVersion=%s', $version);
}
function doctrineFormatterPostgreSQL(?string $version) : string
{
$version = $version ?? DEFAULT_POSTGRESQL_VERSION;
$suffix = sprintf('?serverVersion=%s', $version);
return $suffix;
}
/**
* Maps the specified relationship to the DATABASE_URL environment variable, if available.
*
* @param string $relationshipName
* The database relationship name.
* @param Config $config
* The config object.
*/
function mapPlatformShDatabase(string $relationshipName, Config $config) : void
{
if (!$config->hasRelationship($relationshipName)) {
return;
}
setEnvVar('DATABASE_URL', $config->formattedCredentials($relationshipName, 'doctrine'));
}
/**
* Maps the specified relationship to the MESSENGER_TRANSPORT_DSN environment variable, if available.
*
* @param string $relationshipName
* The database relationship name.
* @param Config $config
* The config object.
*/
function mapPlatformShRabbitMq(string $relationshipName, Config $config) : void
{
if (!$config->hasRelationship($relationshipName)) {
return;
}
setEnvVar('MESSENGER_TRANSPORT_DSN', $config->formattedCredentials($relationshipName, 'rabbitmq'));
}
/**
* Sets a default Doctrine URL.
*
* Doctrine needs a well-formed URL string with a database version even in the build hook.
* It doesn't use it, but it fails if it's not there. This default meets the minimum
* requirements of the format without actually allowing a connection.
*/
function setDefaultDoctrineUrl() : void
{
// Hack the Doctrine URL to be syntactically valid in a build hook, even
// though it shouldn't be used.
$dbUrl = sprintf(
'%s://%s:%s@%s:%s/%s?charset=utf8mb4&serverVersion=%s',
'mysql',
'',
'',
'localhost',
3306,
'',
'mariadb-10.2.12'
);
setEnvVar('DATABASE_URL', $dbUrl);
}
/**
* Maps the specified relationship to a Doctrine MongoDB connection, if available.
*
* @param string $relationshipName
* The MongoDB database relationship name.
* @param Config $config
* The config object.
*/
function mapPlatformShMongoDatabase(string $relationshipName, Config $config): void
{
if (!$config->hasRelationship($relationshipName)) {
return;
}
$credentials = $config->credentials($relationshipName);
setEnvVar('MONGODB_SERVER', sprintf('mongodb://%s:%d', $credentials['host'], $credentials['port']));
setEnvVar('MONGODB_DB', $credentials['path']);
setEnvVar('MONGODB_USERNAME', $credentials['username']);
setEnvVar('MONGODB_PASSWORD', $credentials['password']);
}
/**
* Maps the specified relationship to environment variables for Elasticsearch.
*
* @param string $relationshipName
* @param Config $config
*/
function mapPlatformShElasticSearch(string $relationshipName, Config $config): void
{
if (!$config->hasRelationship($relationshipName)) {
return;
}
$credentials = $config->credentials($relationshipName);
setEnvVar('ELASTICSEARCH_HOST', $credentials['host']);
setEnvVar('ELASTICSEARCH_PORT', (string)$credentials['port']);
setEnvVar('ELASTICSEARCH_URL', sprintf('http://%s:%s', $credentials['host'], (string) $credentials['port']));
}
/**
* Maps the specified relationship to environment variables for Solr.
*
* @param string $relationshipName
* @param Config $config
*/
function mapPlatformShSolr(string $relationshipName, Config $config): void
{
if (!$config->hasRelationship($relationshipName)) {
return;
}
$credentials = $config->credentials($relationshipName);
setEnvVar('SOLR_DSN', sprintf('http://%s:%d/solr', $credentials['host'], $credentials['port']));
if(isset($credentials['rel'])) {
setEnvVar('SOLR_CORE', $credentials['rel']);
} else {
setEnvVar('SOLR_CORE', substr($credentials['path'], 5)); // 'path' will have `solr/core`, we want to extract `core`
}
}
/**
* Maps the specified relationship to environment variables for Redis sessions.
*
* @param string $relationshipName
* @param Config $config
*/
function mapPlatformShRedisSession(string $relationshipName, Config $config): void
{
if (!$config->hasRelationship($relationshipName)) {
return;
}
$credentials = $config->credentials($relationshipName);
setEnvVar('SESSION_REDIS_HOST', $credentials['host']);
setEnvVar('SESSION_REDIS_PORT', (string)$credentials['port']);
setEnvVar('SESSION_REDIS_URL', sprintf('redis://%s:%s', $credentials['host'], (string) $credentials['port']));
}
/**
* Maps the specified relationship to environment variables for Redis cache.
*
* @param string $relationshipName
* @param Config $config
*/
function mapPlatformShRedisCache(string $relationshipName, Config $config): void
{
if (!$config->hasRelationship($relationshipName, $config)) {
return;
}
$credentials = $config->credentials($relationshipName);
setEnvVar('CACHE_DSN', sprintf('%s:%d', $credentials['host'], $credentials['port']));
setEnvVar('CACHE_URL', sprintf('redis://%s:%s', $credentials['host'], (string) $credentials['port']));
}