1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the php-formatter package |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2016 Marc Morera |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* Feel free to edit as you please, and have fun. |
12
|
|
|
* |
13
|
|
|
* @author Marc Morera <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Mmoreram\PHPFormatter\Loader; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ConfigLoader. |
20
|
|
|
*/ |
21
|
|
|
class ConfigLoader |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* This method parses the config file, if exists, and determines the real |
25
|
|
|
* options values. |
26
|
|
|
* |
27
|
|
|
* * If an option is defined as a command parameter, this will be used |
28
|
|
|
* * Otherwise, if an option is defined in the configuration file, this will |
29
|
|
|
* be used. |
30
|
|
|
* * Otherwise, default values will be used |
31
|
|
|
* |
32
|
|
|
* @param string $commandName Command called |
33
|
|
|
* @param array $configValues Config values |
34
|
|
|
* @param array $commandValues Values defined in command |
35
|
|
|
* @param array $defaultValues Default values to use if these are not defined |
36
|
|
|
* |
37
|
|
|
* @return array $usableValues Usable values |
38
|
|
|
*/ |
39
|
|
|
public function loadConfigValues( |
40
|
|
|
$commandName, |
41
|
|
|
array $configValues, |
42
|
|
|
array $commandValues = [], |
43
|
|
|
array $defaultValues = [] |
44
|
|
|
) { |
45
|
|
|
$configValues = isset($configValues[$commandName]) && is_array($configValues[$commandName]) |
46
|
|
|
? $configValues[$commandName] |
47
|
|
|
: []; |
48
|
|
|
|
49
|
|
|
return array_merge( |
50
|
|
|
$defaultValues, |
51
|
|
|
array_filter($configValues), |
52
|
|
|
array_filter($commandValues) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This method parses the config file, if exists, and determines the real |
58
|
|
|
* option value. |
59
|
|
|
* |
60
|
|
|
* * If an option is defined as a command parameter, this will be used |
61
|
|
|
* * Otherwise, if an option is defined in the configuration file, this will |
62
|
|
|
* be used. |
63
|
|
|
* * Otherwise, default values will be used |
64
|
|
|
* |
65
|
|
|
* @param string $commandName Command called |
66
|
|
|
* @param array $configValues Config values |
67
|
|
|
* @param array $commandValue Value defined in command |
68
|
|
|
* @param array $defaultValue Default value to use if this is not defined |
69
|
|
|
* |
70
|
|
|
* @return array $usableValues Usable values |
71
|
|
|
*/ |
72
|
|
|
public function loadConfigValue( |
73
|
|
|
$commandName, |
74
|
|
|
array $configValues, |
75
|
|
|
array $commandValue = null, |
76
|
|
|
array $defaultValue = null |
77
|
|
|
) { |
78
|
|
|
return isset($configValues[$commandName]) |
79
|
|
|
? $configValues[$commandName] |
80
|
|
|
: ($commandValue |
81
|
|
|
?: $defaultValue); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|