|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/configuration |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Configuration\Driver; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Configuration\ConfigurationInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Environment |
|
16
|
|
|
* |
|
17
|
|
|
* @package Slick\Configuration\Driver |
|
18
|
|
|
*/ |
|
19
|
|
|
class Environment implements ConfigurationInterface |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
use CommonDriverMethods; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @inheritdoc |
|
26
|
|
|
*/ |
|
27
|
|
View Code Duplication |
public function get($key, $default = null) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
$stored = self::getValue($key, false, $this->data); |
|
30
|
|
|
if ($stored !== false) { |
|
31
|
|
|
return $stored; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$value = $default; |
|
35
|
|
|
$fromEnvironment = getenv($this->transformKey($key)); |
|
36
|
|
|
|
|
37
|
|
|
if ($fromEnvironment !== false) { |
|
38
|
|
|
$value = $fromEnvironment; |
|
39
|
|
|
self::setValue($key, $value, $this->data); |
|
40
|
|
|
} |
|
41
|
|
|
return $value; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Transforms the provided key to a environment variable name |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $key |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
private function transformKey($key) |
|
51
|
|
|
{ |
|
52
|
|
|
$regEx = '/(?#! splitCamelCase Rev:20140412) |
|
53
|
|
|
# Split camelCase "words". Two global alternatives. Either g1of2: |
|
54
|
|
|
(?<=[a-z]) # Position is after a lowercase, |
|
55
|
|
|
(?=[A-Z]) # and before an uppercase letter. |
|
56
|
|
|
| (?<=[A-Z]) # Or g2of2; Position is after uppercase, |
|
57
|
|
|
(?=[A-Z][a-z]) # and before upper-then-lower case. |
|
58
|
|
|
/x'; |
|
59
|
|
|
|
|
60
|
|
|
$words = preg_split($regEx, $key); |
|
61
|
|
|
$envName = implode('_', $words); |
|
62
|
|
|
$envName = str_replace(['.', '_', '-'], '_', $envName); |
|
63
|
|
|
$envName = strtoupper($envName); |
|
64
|
|
|
|
|
65
|
|
|
return $envName; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.