Skip to content

Commit df209c9

Browse files
committed
Prefix calls to getenv() during config resolution
1 parent 5cf1d72 commit df209c9

File tree

5 files changed

+46
-74
lines changed

5 files changed

+46
-74
lines changed

system/Config/BaseConfig.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ protected function initEnvValue(&$property, string $name, string $prefix, string
134134
protected function getEnvValue(string $property, string $prefix, string $shortPrefix)
135135
{
136136
$shortPrefix = ltrim($shortPrefix, '\\');
137+
137138
switch (true)
138139
{
139140
case array_key_exists("{$shortPrefix}.{$property}", $_ENV):
@@ -145,7 +146,9 @@ protected function getEnvValue(string $property, string $prefix, string $shortPr
145146
case array_key_exists("{$prefix}.{$property}", $_SERVER):
146147
return $_SERVER["{$prefix}.{$property}"];
147148
default:
148-
$value = getenv($property);
149+
$value = getenv("{$shortPrefix}.{$property}");
150+
$value = $value === false ? getenv("{$prefix}.{$property}") : $value;
151+
149152
return $value === false ? null : $value;
150153
}
151154
}

tests/system/Config/BaseConfigTest.php

Lines changed: 36 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010

1111
class BaseConfigTest extends CIUnitTestCase
1212
{
13-
1413
protected $fixturesFolder;
1514

16-
//--------------------------------------------------------------------
17-
1815
protected function setUp(): void
1916
{
2017
parent::setUp();
@@ -37,21 +34,16 @@ protected function setUp(): void
3734
}
3835
}
3936

40-
//--------------------------------------------------------------------
41-
4237
public function testBasicValues()
4338
{
4439
$dotenv = new DotEnv($this->fixturesFolder, '.env');
4540
$dotenv->load();
4641
$config = new SimpleConfig();
4742

48-
$this->assertEquals('bar', $config->FOO);
49-
// empty treated as boolean false
50-
$this->assertEquals(false, $config->echo);
51-
// 'true' should be treated as boolean true
43+
$this->assertNull($config->FOO);
44+
$this->assertSame('', $config->echo);
5245
$this->assertTrue($config->foxtrot);
53-
// numbers should be treated properly
54-
$this->assertEquals(18, $config->golf);
46+
$this->assertSame(18, $config->golf);
5547
}
5648

5749
/**
@@ -68,12 +60,10 @@ public function testServerValues()
6860
$dotenv->load();
6961
$config = new SimpleConfig();
7062

71-
$this->assertEquals(123, $config->shortie);
72-
$this->assertEquals(456, $config->longie);
63+
$this->assertSame('123', $config->shortie);
64+
$this->assertSame('456', $config->longie);
7365
}
7466

75-
//--------------------------------------------------------------------
76-
7767
public function testEnvironmentOverrides()
7868
{
7969
$dotenv = new DotEnv($this->fixturesFolder, '.env');
@@ -82,81 +72,69 @@ public function testEnvironmentOverrides()
8272
$config = new SimpleConfig();
8373

8474
// override config with ENV var
85-
$this->assertEquals('pow', $config->alpha);
75+
$this->assertSame('pow', $config->alpha);
8676
// config should not be over-written by wrongly named ENV var
87-
$this->assertEquals('three', $config->charlie);
77+
$this->assertSame('three', $config->charlie);
8878
// override config with shortPrefix ENV var
89-
$this->assertEquals('hubbahubba', $config->delta);
79+
$this->assertSame('hubbahubba', $config->delta);
9080
// incorrect env name should not inject property
9181
$this->assertObjectNotHasAttribute('notthere', $config);
92-
// same ENV var as property, but not namespaced, still over-rides
93-
$this->assertEquals('kazaam', $config->bravo);
9482
// empty ENV var should not affect config setting
95-
$this->assertEquals('pineapple', $config->fruit);
83+
$this->assertSame('pineapple', $config->fruit);
9684
// non-empty ENV var should overrideconfig setting
97-
$this->assertEquals('banana', $config->dessert);
85+
$this->assertSame('banana', $config->dessert);
9886
// null property should not be affected
9987
$this->assertNull($config->QEMPTYSTR);
10088
}
10189

102-
//--------------------------------------------------------------------
103-
10490
public function testPrefixedValues()
10591
{
10692
$dotenv = new DotEnv($this->fixturesFolder, '.env');
10793
$dotenv->load();
10894

10995
$config = new SimpleConfig();
11096

111-
$this->assertEquals('baz', $config->onedeep);
97+
$this->assertSame('baz', $config->onedeep);
11298
}
11399

114-
//--------------------------------------------------------------------
115-
116100
public function testPrefixedArrayValues()
117101
{
118102
$dotenv = new DotEnv($this->fixturesFolder, '.env');
119103
$dotenv->load();
120104

121105
$config = new SimpleConfig();
122106

123-
$this->assertEquals('ci4', $config->default['name']);
124-
$this->assertEquals('Malcolm', $config->crew['captain']);
125-
$this->assertEquals('Spock', $config->crew['science']);
126-
$this->assertFalse(array_key_exists('pilot', $config->crew));
107+
$this->assertSame('ci4', $config->default['name']);
108+
$this->assertSame('Malcolm', $config->crew['captain']);
109+
$this->assertSame('Spock', $config->crew['science']);
110+
$this->assertArrayNotHasKey('pilot', $config->crew);
127111
$this->assertTrue($config->crew['comms']);
128112
$this->assertFalse($config->crew['doctor']);
129113
}
130114

131-
//--------------------------------------------------------------------
132-
133115
public function testArrayValues()
134116
{
135117
$dotenv = new DotEnv($this->fixturesFolder, '.env');
136118
$dotenv->load();
137119

138120
$config = new SimpleConfig();
139121

140-
$this->assertEquals('complex', $config->simple['name']);
141-
$this->assertEquals('foo', $config->first);
142-
$this->assertEquals('bar', $config->second);
122+
$this->assertSame('complex', $config->simple['name']);
123+
$this->assertSame('foo', $config->first);
124+
$this->assertSame('bar', $config->second);
143125
}
144126

145-
//--------------------------------------------------------------------
146-
147127
public function testSetsDefaultValues()
148128
{
149129
$dotenv = new DotEnv($this->fixturesFolder, 'commented.env');
150130
$dotenv->load();
151131

152132
$config = new SimpleConfig();
153133

154-
$this->assertEquals('foo', $config->first);
155-
$this->assertEquals('bar', $config->second);
134+
$this->assertSame('foo', $config->first);
135+
$this->assertSame('bar', $config->second);
156136
}
157137

158-
//--------------------------------------------------------------------
159-
160138
/**
161139
* @runInSeparateProcess
162140
* @preserveGlobalState disabled
@@ -168,12 +146,10 @@ public function testSetsDefaultValuesEncryptionUsingHex2Bin()
168146
$config = new Encryption();
169147

170148
// override config with ENV var
171-
$this->assertEquals('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6', bin2hex($config->key));
172-
$this->assertEquals('OpenSSL', $config->driver);
149+
$this->assertSame('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6', bin2hex($config->key));
150+
$this->assertSame('OpenSSL', $config->driver);
173151
}
174152

175-
//--------------------------------------------------------------------
176-
177153
/**
178154
* @runInSeparateProcess
179155
* @preserveGlobalState disabled
@@ -184,37 +160,31 @@ public function testSetDefaultValuesEncryptionUsingBase64()
184160
$dotenv->load();
185161
$config = new Encryption('base64');
186162

187-
$this->assertEquals('L40bKo6b8Nu541LeVeZ1i5RXfGgnkar42CPTfukhGhw=', base64_encode($config->key));
188-
$this->assertEquals('OpenSSL', $config->driver);
163+
$this->assertSame('L40bKo6b8Nu541LeVeZ1i5RXfGgnkar42CPTfukhGhw=', base64_encode($config->key));
164+
$this->assertSame('OpenSSL', $config->driver);
189165
}
190166

191-
//--------------------------------------------------------------------
192-
193167
public function testSetsDefaultValuesHex2Bin()
194168
{
195169
$dotenv = new DotEnv($this->fixturesFolder, 'commented.env');
196170
$dotenv->load();
197171
$config = new Encryption();
198172

199173
// override config with ENV var
200-
$this->assertEquals('84cf2c0811d5daf9e1c897825a3debce91f9a33391e639f72f7a4740b30675a2', bin2hex($config->key));
201-
$this->assertEquals('MCrypt', $config->driver);
174+
$this->assertSame('84cf2c0811d5daf9e1c897825a3debce91f9a33391e639f72f7a4740b30675a2', bin2hex($config->key));
175+
$this->assertSame('MCrypt', $config->driver);
202176
}
203177

204-
//--------------------------------------------------------------------
205-
206178
public function testSetDefaultValuesBase64()
207179
{
208180
$dotenv = new DotEnv($this->fixturesFolder, 'commented.env');
209181
$dotenv->load();
210182
$config = new Encryption('base64');
211183

212-
$this->assertEquals('Psf8bUHRh1UJYG2M7e+5ec3MdjpKpzAr0twamcAvOcI=', base64_encode($config->key));
213-
$this->assertEquals('MCrypt', $config->driver);
184+
$this->assertSame('Psf8bUHRh1UJYG2M7e+5ec3MdjpKpzAr0twamcAvOcI=', base64_encode($config->key));
185+
$this->assertSame('MCrypt', $config->driver);
214186
}
215187

216-
//--------------------------------------------------------------------
217-
218188
public function testRecognizesLooseValues()
219189
{
220190
$dotenv = new DotEnv($this->fixturesFolder, 'loose.env');
@@ -224,12 +194,10 @@ public function testRecognizesLooseValues()
224194

225195
$this->assertEquals(0, $config->QZERO);
226196
$this->assertSame('0', $config->QZEROSTR);
227-
$this->assertEquals(' ', $config->QEMPTYSTR);
197+
$this->assertSame(' ', $config->QEMPTYSTR);
228198
$this->assertFalse($config->QFALSE);
229199
}
230200

231-
//--------------------------------------------------------------------
232-
233201
public function testRegistrars()
234202
{
235203
$config = new RegistrarConfig();
@@ -239,13 +207,13 @@ public function testRegistrars()
239207
$method();
240208

241209
// no change to unmodified property
242-
$this->assertEquals('bar', $config->foo);
210+
$this->assertSame('bar', $config->foo);
243211
// add to an existing array property
244-
$this->assertEquals(['baz', 'first', 'second'], $config->bar);
212+
$this->assertSame(['baz', 'first', 'second'], $config->bar);
245213
// add a new property
246-
$this->assertEquals('nice', $config->format);
214+
$this->assertSame('nice', $config->format);
247215
// add a new array property
248-
$this->assertEquals(['apple', 'banana'], $config->fruit);
216+
$this->assertSame(['apple', 'banana'], $config->fruit);
249217
}
250218

251219
public function testBadRegistrar()
@@ -259,7 +227,7 @@ public function testBadRegistrar()
259227
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
260228
$method();
261229

262-
$this->assertEquals('bar', $config->foo);
230+
$this->assertSame('bar', $config->foo);
263231
}
264232

265233
public function testNotEnabled()
@@ -274,7 +242,7 @@ public function testNotEnabled()
274242
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
275243
$method();
276244

277-
$this->assertEquals($expected, $config::$registrars);
245+
$this->assertSame($expected, $config::$registrars);
278246
}
279247

280248
public function testDidDiscovery()
@@ -289,7 +257,7 @@ public function testDidDiscovery()
289257
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
290258
$method();
291259

292-
$this->assertEquals(true, $this->getPrivateProperty($config, 'didDiscovery'));
260+
$this->assertSame(true, $this->getPrivateProperty($config, 'didDiscovery'));
293261
}
294262

295263
}

tests/system/Config/fixtures/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ NULL=
66

77
SimpleConfig.onedeep=baz
88
SimpleConfig.default.name=ci4
9-
simple.name=complex
9+
SimpleConfig.simple.name=complex
1010

1111
# for environment override testing
1212
SimpleConfig.alpha=pow
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
QZERO=0
2-
QZEROSTR="0"
3-
QEMPTYSTR=" "
4-
QFALSE=false
1+
SimpleConfig.QZERO=0
2+
SimpleConfig.QZEROSTR="0"
3+
SimpleConfig.QEMPTYSTR=" "
4+
SimpleConfig.QFALSE=false

user_guide_src/source/changelogs/v4.1.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Changes:
2323
- Entity. Timestamp casting now throws an exception when an invalid value is passed
2424
- ``Entity::castAsJson`` uses external cast handler ``CastAsJson::get``.
2525
- ``Entity::mutateDate`` uses external cast handler ``CastAsDatetime::get``.
26+
- In order for ``Config\**`` classes to get their respective properties' values from the ``.env``, it is now necessary to namespace the property with the name of the class. Previously, the property names are enough but now disallowed because it can get system environment variables, like ``PATH``.
2627

2728
Deprecations:
2829

0 commit comments

Comments
 (0)