@@ -24,230 +24,230 @@ |
||
24 | 24 | class ConfigFactory |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * Cached contents of the config files. |
|
29 | - * |
|
30 | - * @since 0.4.3 |
|
31 | - * |
|
32 | - * @var array |
|
33 | - */ |
|
34 | - protected static $configFilesCache = []; |
|
35 | - |
|
36 | - /** |
|
37 | - * Create a new ConfigInterface object from a file. |
|
38 | - * |
|
39 | - * If a comma-separated list of files is provided, they are checked in sequence until the first one could be loaded |
|
40 | - * successfully. |
|
41 | - * |
|
42 | - * @since 0.3.0 |
|
43 | - * |
|
44 | - * @param string|array $_ List of files. |
|
45 | - * |
|
46 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
47 | - */ |
|
48 | - public static function createFromFile($_) |
|
49 | - { |
|
50 | - $files = array_reverse(func_get_args()); |
|
51 | - |
|
52 | - if (is_array($files[0])) { |
|
53 | - $files = $files[0]; |
|
54 | - } |
|
55 | - |
|
56 | - while (count($files) > 0) { |
|
57 | - try { |
|
58 | - $file = array_pop($files); |
|
59 | - |
|
60 | - if (! is_string($file)) { |
|
61 | - continue; |
|
62 | - } |
|
27 | + /** |
|
28 | + * Cached contents of the config files. |
|
29 | + * |
|
30 | + * @since 0.4.3 |
|
31 | + * |
|
32 | + * @var array |
|
33 | + */ |
|
34 | + protected static $configFilesCache = []; |
|
35 | + |
|
36 | + /** |
|
37 | + * Create a new ConfigInterface object from a file. |
|
38 | + * |
|
39 | + * If a comma-separated list of files is provided, they are checked in sequence until the first one could be loaded |
|
40 | + * successfully. |
|
41 | + * |
|
42 | + * @since 0.3.0 |
|
43 | + * |
|
44 | + * @param string|array $_ List of files. |
|
45 | + * |
|
46 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
47 | + */ |
|
48 | + public static function createFromFile($_) |
|
49 | + { |
|
50 | + $files = array_reverse(func_get_args()); |
|
51 | + |
|
52 | + if (is_array($files[0])) { |
|
53 | + $files = $files[0]; |
|
54 | + } |
|
55 | + |
|
56 | + while (count($files) > 0) { |
|
57 | + try { |
|
58 | + $file = array_pop($files); |
|
59 | + |
|
60 | + if (! is_string($file)) { |
|
61 | + continue; |
|
62 | + } |
|
63 | 63 | |
64 | - if (! is_readable($file)) { |
|
65 | - continue; |
|
66 | - } |
|
67 | - |
|
68 | - $config = static::createFromArray( |
|
69 | - static::getFromCache($file, function ($file) { |
|
70 | - return Loader::load($file); |
|
71 | - }) |
|
72 | - ); |
|
73 | - |
|
74 | - if (null === $config) { |
|
75 | - continue; |
|
76 | - } |
|
77 | - |
|
78 | - return $config; |
|
79 | - } catch (Exception $exception) { |
|
80 | - // Fail silently and try next file. |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - return static::createFromArray([]); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Create a new ConfigInterface object from an array. |
|
89 | - * |
|
90 | - * @since 0.3.0 |
|
91 | - * |
|
92 | - * @param array $array Array with configuration values. |
|
93 | - * |
|
94 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
95 | - */ |
|
96 | - public static function createFromArray(array $array) |
|
97 | - { |
|
98 | - try { |
|
99 | - return new Config($array); |
|
100 | - } catch (Exception $exception) { |
|
101 | - // Fail silently and try next file. |
|
102 | - } |
|
103 | - |
|
104 | - return null; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Create a new ConfigInterface object. |
|
109 | - * |
|
110 | - * Tries to deduce the correct creation method by inspecting the provided arguments. |
|
111 | - * |
|
112 | - * @since 0.3.0 |
|
113 | - * |
|
114 | - * @param mixed $_ Array with configuration values. |
|
115 | - * |
|
116 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
117 | - */ |
|
118 | - public static function create($_) |
|
119 | - { |
|
120 | - if (func_num_args() < 1) { |
|
121 | - return static::createFromArray([]); |
|
122 | - } |
|
123 | - |
|
124 | - $arguments = func_get_args(); |
|
125 | - |
|
126 | - if (is_array($arguments[0]) && func_num_args() === 1) { |
|
127 | - return static::createFromArray($arguments[0]); |
|
128 | - } |
|
129 | - |
|
130 | - return static::createFromFile($arguments); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Create a new ConfigInterface object, by merging several files together. |
|
135 | - * |
|
136 | - * Duplicate keys in later files will override those in earlier files. |
|
137 | - * |
|
138 | - * @since 0.4.6 |
|
139 | - * |
|
140 | - * @param mixed $_ Array with configuration values. |
|
141 | - * |
|
142 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
143 | - */ |
|
144 | - public static function merge($_) |
|
145 | - { |
|
146 | - if (func_num_args() < 1) { |
|
147 | - return static::createFromArray([]); |
|
148 | - } |
|
149 | - |
|
150 | - $arguments = func_get_args(); |
|
151 | - |
|
152 | - if (is_array($arguments[0]) && func_num_args() === 1) { |
|
153 | - return static::createFromArray($arguments[0]); |
|
154 | - } |
|
155 | - |
|
156 | - return static::mergeFromFiles($arguments); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Create a new ConfigInterface object by merging data from several files. |
|
161 | - * |
|
162 | - * If a comma-separated list of files is provided, they are loaded in sequence and later files override settings in |
|
163 | - * earlier files. |
|
164 | - * |
|
165 | - * @since 0.4.6 |
|
166 | - * |
|
167 | - * @param string|array $_ List of files. |
|
168 | - * |
|
169 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
170 | - */ |
|
171 | - public static function mergeFromFiles($_) |
|
172 | - { |
|
173 | - $files = array_reverse(func_get_args()); |
|
174 | - $data = []; |
|
175 | - |
|
176 | - if (is_array($files[0])) { |
|
177 | - $files = array_reverse($files[0]); |
|
178 | - } |
|
179 | - |
|
180 | - while (count($files) > 0) { |
|
181 | - try { |
|
182 | - $file = array_pop($files); |
|
183 | - |
|
184 | - if (! is_readable($file)) { |
|
185 | - continue; |
|
186 | - } |
|
187 | - |
|
188 | - $new_data = static::getFromCache($file, function ($file) { |
|
189 | - return Loader::load($file); |
|
190 | - }); |
|
191 | - |
|
192 | - if (null === $data) { |
|
193 | - continue; |
|
194 | - } |
|
195 | - |
|
196 | - $data = array_replace_recursive($data, $new_data); |
|
197 | - } catch (Exception $exception) { |
|
198 | - // Fail silently and try next file. |
|
199 | - } |
|
200 | - } |
|
201 | - |
|
202 | - return static::createFromArray($data); |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Create a new ConfigInterface object from a file and return a sub-portion of it. |
|
207 | - * |
|
208 | - * The first argument needs to be the file name to load, and the subsequent arguments will be passed on to |
|
209 | - * `Config::getSubConfig()`. |
|
210 | - * |
|
211 | - * @since 0.4.5 |
|
212 | - * |
|
213 | - * @param mixed $_ File name of the config to load as a string, followed by an array of keys to pass to |
|
214 | - * `Config::getSubConfig()`. |
|
215 | - * |
|
216 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
217 | - */ |
|
218 | - public static function createSubConfig($_) |
|
219 | - { |
|
220 | - if (func_num_args() < 2) { |
|
221 | - return static::createFromArray([]); |
|
222 | - } |
|
223 | - |
|
224 | - $arguments = func_get_args(); |
|
225 | - $file = array_shift($arguments); |
|
226 | - |
|
227 | - $config = static::createFromFile($file); |
|
228 | - |
|
229 | - return $config->getSubConfig($arguments); |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Get a config file from the config file cache. |
|
234 | - * |
|
235 | - * @since 0.4.4 |
|
236 | - * |
|
237 | - * @param string $identifier Identifier to look for in the cache. |
|
238 | - * @param mixed $fallback Fallback to use to fill the cache. If $fallback is a callable, it will be executed |
|
239 | - * with $identifier as an argument. |
|
240 | - * |
|
241 | - * @return mixed The latest content of the cache for the given identifier. |
|
242 | - */ |
|
243 | - protected static function getFromCache($identifier, $fallback) |
|
244 | - { |
|
245 | - if (! array_key_exists($identifier, static::$configFilesCache)) { |
|
246 | - static::$configFilesCache[$identifier] = is_callable($fallback) |
|
247 | - ? $fallback($identifier) |
|
248 | - : $fallback; |
|
249 | - } |
|
250 | - |
|
251 | - return static::$configFilesCache[$identifier]; |
|
252 | - } |
|
64 | + if (! is_readable($file)) { |
|
65 | + continue; |
|
66 | + } |
|
67 | + |
|
68 | + $config = static::createFromArray( |
|
69 | + static::getFromCache($file, function ($file) { |
|
70 | + return Loader::load($file); |
|
71 | + }) |
|
72 | + ); |
|
73 | + |
|
74 | + if (null === $config) { |
|
75 | + continue; |
|
76 | + } |
|
77 | + |
|
78 | + return $config; |
|
79 | + } catch (Exception $exception) { |
|
80 | + // Fail silently and try next file. |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + return static::createFromArray([]); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Create a new ConfigInterface object from an array. |
|
89 | + * |
|
90 | + * @since 0.3.0 |
|
91 | + * |
|
92 | + * @param array $array Array with configuration values. |
|
93 | + * |
|
94 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
95 | + */ |
|
96 | + public static function createFromArray(array $array) |
|
97 | + { |
|
98 | + try { |
|
99 | + return new Config($array); |
|
100 | + } catch (Exception $exception) { |
|
101 | + // Fail silently and try next file. |
|
102 | + } |
|
103 | + |
|
104 | + return null; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Create a new ConfigInterface object. |
|
109 | + * |
|
110 | + * Tries to deduce the correct creation method by inspecting the provided arguments. |
|
111 | + * |
|
112 | + * @since 0.3.0 |
|
113 | + * |
|
114 | + * @param mixed $_ Array with configuration values. |
|
115 | + * |
|
116 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
117 | + */ |
|
118 | + public static function create($_) |
|
119 | + { |
|
120 | + if (func_num_args() < 1) { |
|
121 | + return static::createFromArray([]); |
|
122 | + } |
|
123 | + |
|
124 | + $arguments = func_get_args(); |
|
125 | + |
|
126 | + if (is_array($arguments[0]) && func_num_args() === 1) { |
|
127 | + return static::createFromArray($arguments[0]); |
|
128 | + } |
|
129 | + |
|
130 | + return static::createFromFile($arguments); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Create a new ConfigInterface object, by merging several files together. |
|
135 | + * |
|
136 | + * Duplicate keys in later files will override those in earlier files. |
|
137 | + * |
|
138 | + * @since 0.4.6 |
|
139 | + * |
|
140 | + * @param mixed $_ Array with configuration values. |
|
141 | + * |
|
142 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
143 | + */ |
|
144 | + public static function merge($_) |
|
145 | + { |
|
146 | + if (func_num_args() < 1) { |
|
147 | + return static::createFromArray([]); |
|
148 | + } |
|
149 | + |
|
150 | + $arguments = func_get_args(); |
|
151 | + |
|
152 | + if (is_array($arguments[0]) && func_num_args() === 1) { |
|
153 | + return static::createFromArray($arguments[0]); |
|
154 | + } |
|
155 | + |
|
156 | + return static::mergeFromFiles($arguments); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Create a new ConfigInterface object by merging data from several files. |
|
161 | + * |
|
162 | + * If a comma-separated list of files is provided, they are loaded in sequence and later files override settings in |
|
163 | + * earlier files. |
|
164 | + * |
|
165 | + * @since 0.4.6 |
|
166 | + * |
|
167 | + * @param string|array $_ List of files. |
|
168 | + * |
|
169 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
170 | + */ |
|
171 | + public static function mergeFromFiles($_) |
|
172 | + { |
|
173 | + $files = array_reverse(func_get_args()); |
|
174 | + $data = []; |
|
175 | + |
|
176 | + if (is_array($files[0])) { |
|
177 | + $files = array_reverse($files[0]); |
|
178 | + } |
|
179 | + |
|
180 | + while (count($files) > 0) { |
|
181 | + try { |
|
182 | + $file = array_pop($files); |
|
183 | + |
|
184 | + if (! is_readable($file)) { |
|
185 | + continue; |
|
186 | + } |
|
187 | + |
|
188 | + $new_data = static::getFromCache($file, function ($file) { |
|
189 | + return Loader::load($file); |
|
190 | + }); |
|
191 | + |
|
192 | + if (null === $data) { |
|
193 | + continue; |
|
194 | + } |
|
195 | + |
|
196 | + $data = array_replace_recursive($data, $new_data); |
|
197 | + } catch (Exception $exception) { |
|
198 | + // Fail silently and try next file. |
|
199 | + } |
|
200 | + } |
|
201 | + |
|
202 | + return static::createFromArray($data); |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Create a new ConfigInterface object from a file and return a sub-portion of it. |
|
207 | + * |
|
208 | + * The first argument needs to be the file name to load, and the subsequent arguments will be passed on to |
|
209 | + * `Config::getSubConfig()`. |
|
210 | + * |
|
211 | + * @since 0.4.5 |
|
212 | + * |
|
213 | + * @param mixed $_ File name of the config to load as a string, followed by an array of keys to pass to |
|
214 | + * `Config::getSubConfig()`. |
|
215 | + * |
|
216 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
217 | + */ |
|
218 | + public static function createSubConfig($_) |
|
219 | + { |
|
220 | + if (func_num_args() < 2) { |
|
221 | + return static::createFromArray([]); |
|
222 | + } |
|
223 | + |
|
224 | + $arguments = func_get_args(); |
|
225 | + $file = array_shift($arguments); |
|
226 | + |
|
227 | + $config = static::createFromFile($file); |
|
228 | + |
|
229 | + return $config->getSubConfig($arguments); |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Get a config file from the config file cache. |
|
234 | + * |
|
235 | + * @since 0.4.4 |
|
236 | + * |
|
237 | + * @param string $identifier Identifier to look for in the cache. |
|
238 | + * @param mixed $fallback Fallback to use to fill the cache. If $fallback is a callable, it will be executed |
|
239 | + * with $identifier as an argument. |
|
240 | + * |
|
241 | + * @return mixed The latest content of the cache for the given identifier. |
|
242 | + */ |
|
243 | + protected static function getFromCache($identifier, $fallback) |
|
244 | + { |
|
245 | + if (! array_key_exists($identifier, static::$configFilesCache)) { |
|
246 | + static::$configFilesCache[$identifier] = is_callable($fallback) |
|
247 | + ? $fallback($identifier) |
|
248 | + : $fallback; |
|
249 | + } |
|
250 | + |
|
251 | + return static::$configFilesCache[$identifier]; |
|
252 | + } |
|
253 | 253 | } |
@@ -57,16 +57,16 @@ discard block |
||
57 | 57 | try { |
58 | 58 | $file = array_pop($files); |
59 | 59 | |
60 | - if (! is_string($file)) { |
|
60 | + if ( ! is_string($file)) { |
|
61 | 61 | continue; |
62 | 62 | } |
63 | 63 | |
64 | - if (! is_readable($file)) { |
|
64 | + if ( ! is_readable($file)) { |
|
65 | 65 | continue; |
66 | 66 | } |
67 | 67 | |
68 | 68 | $config = static::createFromArray( |
69 | - static::getFromCache($file, function ($file) { |
|
69 | + static::getFromCache($file, function($file) { |
|
70 | 70 | return Loader::load($file); |
71 | 71 | }) |
72 | 72 | ); |
@@ -181,11 +181,11 @@ discard block |
||
181 | 181 | try { |
182 | 182 | $file = array_pop($files); |
183 | 183 | |
184 | - if (! is_readable($file)) { |
|
184 | + if ( ! is_readable($file)) { |
|
185 | 185 | continue; |
186 | 186 | } |
187 | 187 | |
188 | - $new_data = static::getFromCache($file, function ($file) { |
|
188 | + $new_data = static::getFromCache($file, function($file) { |
|
189 | 189 | return Loader::load($file); |
190 | 190 | }); |
191 | 191 | |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | */ |
243 | 243 | protected static function getFromCache($identifier, $fallback) |
244 | 244 | { |
245 | - if (! array_key_exists($identifier, static::$configFilesCache)) { |
|
245 | + if ( ! array_key_exists($identifier, static::$configFilesCache)) { |
|
246 | 246 | static::$configFilesCache[$identifier] = is_callable($fallback) |
247 | 247 | ? $fallback($identifier) |
248 | 248 | : $fallback; |