1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acquia\Cloud\Environment; |
4
|
|
|
|
5
|
|
|
use Acquia\Environment\Environment; |
6
|
|
|
use Acquia\Json\Json; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @see https://docs.acquia.com/cloud/configure/env-variable |
10
|
|
|
*/ |
11
|
|
|
class CloudEnvironment extends Environment implements CloudEnvironmentInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $sitegroup; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $filepath; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $creds; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Acquia Cloud variables may be set in settings.inc after PHP init, |
30
|
|
|
* so make sure that we are loading them. |
31
|
|
|
* |
32
|
|
|
* @param string $key |
33
|
|
|
* @return string The value of the environment variable or false if not found |
34
|
|
|
* @see https://github.com/acquia/acquia-sdk-php/pull/58#issuecomment-45167451 |
35
|
|
|
*/ |
36
|
78 |
|
protected function getenv($key) |
|
|
|
|
37
|
|
|
{ |
38
|
78 |
|
$value = getenv($key); |
39
|
78 |
|
if ($value === false) { |
40
|
63 |
|
if (isset($_ENV[$key])) { |
41
|
|
|
$value = $_ENV[$key]; |
42
|
|
|
} |
43
|
63 |
|
if (isset($_SERVER[$key])) { |
44
|
|
|
$value = $_SERVER[$key]; |
45
|
|
|
} |
46
|
63 |
|
} |
47
|
78 |
|
return $value; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
78 |
|
public function init() |
54
|
|
|
{ |
55
|
78 |
|
$environment = $this->getenv('AH_SITE_ENVIRONMENT'); |
56
|
78 |
|
return $environment ?: self::LOCAL; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritDoc} |
61
|
|
|
*/ |
62
|
6 |
|
public function isAcquia() |
63
|
|
|
{ |
64
|
6 |
|
return $this->getEnvironment() != self::LOCAL; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
6 |
|
public function isProduction() |
71
|
|
|
{ |
72
|
6 |
|
return (bool) $this->getenv('AH_PRODUCTION'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $sitegroup |
77
|
|
|
* |
78
|
|
|
* @return \Acquia\Cloud\Environment\CloudEnvironment |
79
|
|
|
*/ |
80
|
3 |
|
public function setSiteGroup($sitegroup) |
81
|
|
|
{ |
82
|
3 |
|
$this->sitegroup = $sitegroup; |
83
|
3 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @rturn string |
88
|
|
|
* |
89
|
|
|
* @throws \UnexpectedValueException |
90
|
|
|
*/ |
91
|
15 |
|
public function getSiteGroup() |
92
|
|
|
{ |
93
|
15 |
|
if (!isset($this->sitegroup)) { |
94
|
12 |
|
$this->sitegroup = $this->getenv('AH_SITE_GROUP'); |
95
|
12 |
|
if (!$this->sitegroup) { |
96
|
3 |
|
throw new \UnexpectedValueException('Expecting environment variable AH_SITE_GROUP to be set'); |
97
|
|
|
} |
98
|
9 |
|
} |
99
|
12 |
|
return $this->sitegroup; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $filepath |
104
|
|
|
* |
105
|
|
|
* @return \Acquia\Cloud\Environment\CloudEnvironment |
106
|
|
|
*/ |
107
|
21 |
|
public function setCredentialsFilepath($filepath) |
108
|
|
|
{ |
109
|
21 |
|
$this->filepath = $filepath; |
110
|
21 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
21 |
|
public function getCredentialsFilepath() |
117
|
|
|
{ |
118
|
21 |
|
if (!isset($this->filepath)) { |
119
|
3 |
|
$settingsDir = $this->getSiteGroup() . '.' . $this->getEnvironment(); |
120
|
3 |
|
$this->filepath = '/var/www/site-php/' . $settingsDir . '/creds.json'; |
121
|
3 |
|
} |
122
|
21 |
|
return $this->filepath; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return array |
127
|
|
|
* |
128
|
|
|
* @throws \RuntimeException |
129
|
|
|
*/ |
130
|
15 |
|
public function serviceCredentials() |
131
|
|
|
{ |
132
|
15 |
|
if (!isset($this->creds)) { |
133
|
15 |
|
$filepath = $this->getCredentialsFilepath(); |
134
|
15 |
|
$this->creds = Json::parseFile($filepath); |
135
|
15 |
|
} |
136
|
15 |
|
return $this->creds; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: