1 | <?php |
||
2 | |||
3 | namespace Codervio\Envmanager\Resolver; |
||
4 | |||
5 | use Codervio\Envmanager\Exceptions\ValueException; |
||
6 | use Exception; |
||
7 | |||
8 | class VariableResolver |
||
9 | { |
||
10 | public function isComment($value = null) |
||
11 | { |
||
12 | return substr($value, 0, 1) === '#'; |
||
13 | } |
||
14 | |||
15 | public function parseSystemEnvironmentVariables($value) |
||
16 | { |
||
17 | if (preg_match_all('#\$\{([a-zA-Z0-9]+)\}#', $value, $matches, PREG_SET_ORDER)) { |
||
18 | |||
19 | if (is_array($matches)) { |
||
20 | |||
21 | foreach ($matches as $match) { |
||
22 | |||
23 | $origin = $match[0]; |
||
24 | $nameVar = $match[1]; |
||
25 | |||
26 | if (!getenv($nameVar)) { |
||
27 | // skip |
||
28 | } |
||
29 | |||
30 | if (is_array(getenv($nameVar))) { |
||
31 | foreach (getenv($nameVar) as $envValue) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
32 | $value = str_replace($origin, $envValue, $value); |
||
33 | } |
||
34 | |||
35 | } elseif (getenv($nameVar)) { |
||
36 | $value = str_replace($origin, getenv($nameVar), $value); |
||
37 | |||
38 | } else { |
||
39 | return $value; |
||
40 | |||
41 | } |
||
42 | } |
||
43 | |||
44 | } |
||
45 | } |
||
46 | |||
47 | return $value; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * |
||
52 | * Check length on Windows OS Value |
||
53 | * |
||
54 | * Maximum size of environment value on Windows is 32,767 character length |
||
55 | * |
||
56 | * @param $value |
||
57 | */ |
||
58 | public function checkLength($value) |
||
59 | { |
||
60 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
61 | if (strlen($value) > 32767) { |
||
62 | throw new ValueException('A value length on Windows OS family is limited to 32767 characters.'); |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | |||
67 | public function parseVariable($value, $collector) |
||
68 | { |
||
69 | $this->checkLength($value); |
||
70 | |||
71 | $collector = $collector->getArrayCopy(); |
||
72 | |||
73 | if (preg_match_all('#\$\{([a-zA-Z0-9]+)\}#', $value, $matches, PREG_SET_ORDER)) { |
||
74 | |||
75 | if (is_array($matches)) { |
||
76 | |||
77 | foreach ($matches as $match) { |
||
78 | |||
79 | $origin = $match[0]; |
||
80 | $nameVar = $match[1]; |
||
81 | |||
82 | if (isset($collector[$nameVar])) { |
||
83 | |||
84 | $value = str_replace($origin, $collector[$nameVar], $value); |
||
85 | |||
86 | } else { |
||
87 | throw new Exception(sprintf('Unknown %s variable. Please check environment "%s" variable', $origin, $nameVar)); |
||
88 | } |
||
89 | |||
90 | } |
||
91 | |||
92 | } |
||
93 | } |
||
94 | |||
95 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
96 | |||
97 | if (preg_match_all('/\%([a-zA-Z0-9]+)\%/i', $value, $matches, PREG_SET_ORDER)) { |
||
98 | |||
99 | if (is_array($matches)) { |
||
100 | |||
101 | foreach ($matches as $match) { |
||
102 | |||
103 | $origin = $match[0]; |
||
104 | $nameVar = $match[1]; |
||
105 | |||
106 | if (isset($collector[$nameVar])) { |
||
107 | |||
108 | $value = str_replace($origin, $collector[$nameVar], $value); |
||
109 | |||
110 | } else { |
||
111 | throw new Exception(sprintf('Unknown %s variable. Please check environment "%s" variable', $origin, $nameVar)); |
||
112 | } |
||
113 | |||
114 | } |
||
115 | |||
116 | } |
||
117 | } |
||
118 | |||
119 | } |
||
120 | |||
121 | return $value; |
||
122 | } |
||
123 | } |
||
124 |