Passed
Pull Request — master (#30)
by Dmitriy
145:02 queued 129:54
created

Params::calcValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Yiisoft\Composer\Config\Config;
4
5
/**
6
 * Params class represents output configuration file with params definitions.
7
 */
8
class Params extends Config
9
{
10
    protected function calcValues(array $sources): array
11
    {
12
        return $this->pushEnvVars(parent::calcValues($sources));
13
    }
14
15
    protected function pushEnvVars(array $vars): array
16
    {
17
        if (empty($vars)) {
18
            return [];
19
        }
20
21
        $env = $this->builder->getConfig('envs')->getValues();
22
23
        foreach ($vars as $key => &$value) {
24
            if (is_array($value)) {
25
                foreach (array_keys($value) as $subkey) {
26
                    $envKey = $this->getEnvKey($key . '_' . $subkey);
27
                    if (isset($env[$envKey])) {
28
                        $value[$subkey] = $env[$envKey];
29
                    }
30
                }
31
            } else {
32
                $envKey = $this->getEnvKey($key);
33
                if (isset($env[$envKey])) {
34
                    $vars[$key] = $env[$envKey];
35
                }
36
            }
37
        }
38
39
        return $vars;
40
    }
41
42
    private function getEnvKey(string $key): string
43
    {
44
        return strtoupper(strtr($key, '.-', '__'));
45
    }
46
47
    public function paramsRequired(): bool
48
    {
49
        return false;
50
    }
51
}
52