-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
DeploymentConfig.php
164 lines (146 loc) · 3.63 KB
/
DeploymentConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App;
use Magento\Framework\Config\ConfigOptionsListConstants;
/**
* Application deployment configuration
*/
class DeploymentConfig
{
/**
* Configuration reader
*
* @var DeploymentConfig\Reader
*/
private $reader;
/**
* Configuration data
*
* @var array
*/
private $data;
/**
* Flattened data
*
* @var array
*/
private $flatData;
/**
* Injected configuration data
*
* @var array
*/
private $overrideData;
/**
* Constructor
*
* Data can be optionally injected in the constructor. This object's public interface is intentionally immutable
*
* @param DeploymentConfig\Reader $reader
* @param array $overrideData
*/
public function __construct(DeploymentConfig\Reader $reader, $overrideData = [])
{
$this->reader = $reader;
$this->overrideData = $overrideData;
}
/**
* Gets data from flattened data
*
* @param string $key
* @param mixed $defaultValue
* @return mixed|null
*/
public function get($key = null, $defaultValue = null)
{
$this->load();
if ($key === null) {
return $this->flatData;
}
return isset($this->flatData[$key]) ? $this->flatData[$key] : $defaultValue;
}
/**
* Checks if data available
*
* @return bool
*/
public function isAvailable()
{
$this->data = null;
$this->load();
return isset($this->flatData[ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE]);
}
/**
* Gets a value specified key from config data
*
* @param string $key
* @return null|mixed
*/
public function getConfigData($key = null)
{
$this->load();
if ($key !== null && !isset($this->data[$key])) {
return null;
}
if (isset($this->data[$key])) {
return $this->data[$key];
}
return $this->data;
}
/**
* Resets config data
*
* @return void
*/
public function resetData()
{
$this->data = null;
}
/**
* Loads the configuration data
*
* @return void
*/
private function load()
{
if (null === $this->data) {
$this->data = $this->reader->load();
if ($this->overrideData) {
$this->data = array_replace($this->data, $this->overrideData);
}
// flatten data for config retrieval using get()
$this->flatData = $this->flattenParams($this->data);
}
}
/**
* Convert associative array of arbitrary depth to a flat associative array with concatenated key path as keys
* each level of array is accessible by path key
*
* @param array $params
* @param string $path
* @return array
* @throws \Exception
*/
private function flattenParams(array $params, $path = null)
{
$cache = [];
foreach ($params as $key => $param) {
if ($path) {
$newPath = $path . '/' . $key;
} else {
$newPath = $key;
}
if (isset($cache[$newPath])) {
throw new \Exception("Key collision {$newPath} is already defined.");
}
$cache[$newPath] = $param;
if (is_array($param)) {
$cache = array_merge($cache, $this->flattenParams($param, $newPath));
}
}
return $cache;
}
}