1 | <?php |
||
7 | class Config |
||
8 | { |
||
9 | use PathHelpers; |
||
10 | |||
11 | /** |
||
12 | * Instance of self. |
||
13 | * |
||
14 | * @var Config |
||
15 | */ |
||
16 | private static $instance; |
||
17 | |||
18 | /** |
||
19 | * Yarak config array. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $configArray; |
||
24 | |||
25 | /** |
||
26 | * Default setting values. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | const DEFAULTS = [ |
||
31 | 'migratorType' => 'fileDate', |
||
32 | 'migrationRepository' => 'database', |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Private constructor. |
||
37 | * |
||
38 | * @param array $configArray |
||
39 | */ |
||
40 | private function __construct(array $configArray) |
||
41 | { |
||
42 | $this->configArray = $configArray; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Get instance of self with config array set. |
||
47 | * |
||
48 | * @param array $configArray |
||
49 | * |
||
50 | * @return Config |
||
51 | */ |
||
52 | public static function getInstance(array $configArray = []) |
||
53 | { |
||
54 | if (empty(self::$instance)) { |
||
55 | if (empty($configArray)) { |
||
56 | $configArray = DI::getDefault() |
||
57 | ->getShared('yarak') |
||
58 | ->getConfigArray(); |
||
59 | } |
||
60 | |||
61 | self::$instance = new self($configArray); |
||
62 | } |
||
63 | |||
64 | return self::$instance; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Get a value from the config array. |
||
69 | * |
||
70 | * @param string|array $value |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | public function get($value) |
||
75 | { |
||
76 | $current = $this->configArray; |
||
77 | |||
78 | foreach ($this->makeArray($value) as $configItem) { |
||
79 | if (!isset($current[$configItem])) { |
||
80 | return $this->getDefault($configItem); |
||
81 | } |
||
82 | |||
83 | $current = $current[$configItem]; |
||
84 | } |
||
85 | |||
86 | return $current; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Return true if config array has given value. |
||
91 | * |
||
92 | * @param mixed $value |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function has($value) |
||
97 | { |
||
98 | return !($this->get($value) === null); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get a setting's default value. |
||
103 | * |
||
104 | * @param string $value |
||
105 | * |
||
106 | * @return mixed|null |
||
107 | */ |
||
108 | public function getDefault($value) |
||
114 | |||
115 | /** |
||
116 | * Set an item in the config. |
||
117 | * |
||
118 | * @param mixed $keys |
||
119 | * @param mixed $value |
||
120 | */ |
||
121 | public function set($keys, $value) |
||
131 | |||
132 | /** |
||
133 | * Remove an item from the config. |
||
134 | * |
||
135 | * @param mixed $keys |
||
136 | */ |
||
137 | public function remove($keys) |
||
151 | |||
152 | /** |
||
153 | * Return the config array. |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | public function toArray() |
||
161 | |||
162 | /** |
||
163 | * Make a variable an array if not one already. |
||
164 | * |
||
165 | * @param mixed $value |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | protected function makeArray($value) |
||
177 | } |
||
178 |