-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplatformsh-env.php
More file actions
82 lines (69 loc) · 2.21 KB
/
Copy pathplatformsh-env.php
File metadata and controls
82 lines (69 loc) · 2.21 KB
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
<?php
declare(strict_types=1);
namespace Platformsh\ShopwareBridge;
use Platformsh\ConfigReader\Config;
mapPlatformShEnvironment();
/**
* Map Platform.Sh environment variables to the values Shopware expects expects.
*
* This is wrapped up into a function to avoid executing code in the global
* namespace.
*
* Note: Most values are already handled by the Symfony Flex bridge. This file is just
* for the additional variables required by Shopware.
*/
function mapPlatformShEnvironment() : void
{
$config = new Config();
if (!$config->inRuntime()) {
return;
}
$config->registerFormatter('redis', __NAMESPACE__ . '\redisFormatter');
// Set the URL based on the route. This is a required route ID.
setEnvVar('APP_URL', $config->getRoute('shopware')['url']);
// Map services as feasible.
mapPlatformShRedis('rediscache', $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;
}
}
/**
* Maps the specified relationship to the REDIS_URL environment variable, if available.
*
* @param string $relationshipName
* The database relationship name.
* @param Config $config
* The config object.
*/
function mapPlatformShRedis(string $relationshipName, Config $config) : void
{
if (!$config->hasRelationship($relationshipName)) {
return;
}
setEnvVar('REDIS_URL', $config->formattedCredentials($relationshipName, 'redis'));
}
function redisFormatter(array $credentials): string
{
return "redis://{$credentials['host']}:{$credentials['port']}";
}