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 $sitename; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $currentregion; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $filepath; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
78 |
|
private $creds; |
37
|
|
|
|
38
|
78 |
|
/** |
39
|
78 |
|
* Acquia Cloud variables may be set in settings.inc after PHP init, |
40
|
63 |
|
* so make sure that we are loading them. |
41
|
|
|
* |
42
|
|
|
* @param string $key |
43
|
63 |
|
* @return string The value of the environment variable or false if not found |
44
|
|
|
* @see https://github.com/acquia/acquia-sdk-php/pull/58#issuecomment-45167451 |
45
|
|
|
*/ |
46
|
63 |
|
protected function getenv($key) |
|
|
|
|
47
|
78 |
|
{ |
48
|
|
|
$value = getenv($key); |
49
|
|
|
if ($value === false) { |
50
|
|
|
if (isset($_ENV[$key])) { |
51
|
|
|
$value = $_ENV[$key]; |
52
|
|
|
} |
53
|
78 |
|
if (isset($_SERVER[$key])) { |
54
|
|
|
$value = $_SERVER[$key]; |
55
|
78 |
|
} |
56
|
78 |
|
} |
57
|
|
|
return $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
6 |
|
*/ |
63
|
|
|
public function init() |
64
|
6 |
|
{ |
65
|
|
|
$environment = $this->getenv('AH_SITE_ENVIRONMENT'); |
66
|
|
|
return $environment ?: self::LOCAL; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
6 |
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
6 |
|
public function isAcquia() |
73
|
|
|
{ |
74
|
|
|
return $this->getEnvironment() != self::LOCAL; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
3 |
|
public function isProduction() |
81
|
|
|
{ |
82
|
3 |
|
return (bool) $this->getenv('AH_PRODUCTION'); |
83
|
3 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $sitegroup |
87
|
|
|
* |
88
|
|
|
* @return \Acquia\Cloud\Environment\CloudEnvironment |
89
|
|
|
*/ |
90
|
|
|
public function setSiteGroup($sitegroup) |
91
|
15 |
|
{ |
92
|
|
|
$this->sitegroup = $sitegroup; |
93
|
15 |
|
return $this; |
94
|
12 |
|
} |
95
|
12 |
|
|
96
|
3 |
|
/** |
97
|
|
|
* @rturn string |
98
|
9 |
|
* |
99
|
12 |
|
* @throws \UnexpectedValueException |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function getSiteGroup() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
if (!isset($this->sitegroup)) { |
104
|
|
|
$this->sitegroup = $this->getenv('AH_SITE_GROUP'); |
105
|
|
|
if (!$this->sitegroup) { |
106
|
|
|
throw new \UnexpectedValueException('Expecting environment variable AH_SITE_GROUP to be set'); |
107
|
21 |
|
} |
108
|
|
|
} |
109
|
21 |
|
return $this->sitegroup; |
110
|
21 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @rturn string |
114
|
|
|
* |
115
|
|
|
* @throws \UnexpectedValueException |
116
|
21 |
|
*/ |
117
|
|
View Code Duplication |
public function getSiteName() |
|
|
|
|
118
|
21 |
|
{ |
119
|
3 |
|
if (!isset($this->sitename)) { |
120
|
3 |
|
$this->sitename = $this->getenv('AH_SITE_NAME'); |
121
|
3 |
|
if (!$this->sitename) { |
122
|
21 |
|
throw new \UnexpectedValueException('Expecting environment variable AH_SITE_NAME to be set'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return $this->sitename; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @rturn string |
130
|
15 |
|
* |
131
|
|
|
* @throws \UnexpectedValueException |
132
|
15 |
|
*/ |
133
|
15 |
View Code Duplication |
public function getCurrentRegion() |
|
|
|
|
134
|
15 |
|
{ |
135
|
15 |
|
if (!isset($this->currentregion)) { |
136
|
15 |
|
$this->currentregion = $this->getenv('AH_CURRENT_REGION'); |
137
|
|
|
if (!$this->currentregion) { |
138
|
|
|
throw new \UnexpectedValueException('Expecting environment variable AH_CURRENT_REGION to be set'); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
return $this->currentregion; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $filepath |
146
|
|
|
* |
147
|
|
|
* @return \Acquia\Cloud\Environment\CloudEnvironment |
148
|
|
|
*/ |
149
|
|
|
public function setCredentialsFilepath($filepath) |
150
|
|
|
{ |
151
|
|
|
$this->filepath = $filepath; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getCredentialsFilepath() |
159
|
|
|
{ |
160
|
|
|
if (!isset($this->filepath)) { |
161
|
|
|
$settingsDir = $this->getSiteGroup() . '.' . $this->getEnvironment(); |
162
|
|
|
$this->filepath = '/var/www/site-php/' . $settingsDir . '/creds.json'; |
163
|
|
|
} |
164
|
|
|
return $this->filepath; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return array |
169
|
|
|
* |
170
|
|
|
* @throws \RuntimeException |
171
|
|
|
*/ |
172
|
|
|
public function serviceCredentials() |
173
|
|
|
{ |
174
|
|
|
if (!isset($this->creds)) { |
175
|
|
|
$filepath = $this->getCredentialsFilepath(); |
176
|
|
|
$this->creds = Json::parseFile($filepath); |
177
|
|
|
} |
178
|
|
|
return $this->creds; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
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: