@@ -25,20 +25,20 @@ |
||
25 | 25 | class Loader |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * Static convenience function to load a configuration from an URI. |
|
30 | - * |
|
31 | - * @since 0.4.0 |
|
32 | - * |
|
33 | - * @param string $uri URI of the resource to load. |
|
34 | - * |
|
35 | - * @return array|null Parsed data loaded from the resource. |
|
36 | - * @throws FailedToLoadConfigException If the configuration could not be loaded. |
|
37 | - */ |
|
38 | - public static function load($uri) |
|
39 | - { |
|
40 | - $loader = LoaderFactory::createFromUri($uri); |
|
28 | + /** |
|
29 | + * Static convenience function to load a configuration from an URI. |
|
30 | + * |
|
31 | + * @since 0.4.0 |
|
32 | + * |
|
33 | + * @param string $uri URI of the resource to load. |
|
34 | + * |
|
35 | + * @return array|null Parsed data loaded from the resource. |
|
36 | + * @throws FailedToLoadConfigException If the configuration could not be loaded. |
|
37 | + */ |
|
38 | + public static function load($uri) |
|
39 | + { |
|
40 | + $loader = LoaderFactory::createFromUri($uri); |
|
41 | 41 | |
42 | - return $loader->load($uri); |
|
43 | - } |
|
42 | + return $loader->load($uri); |
|
43 | + } |
|
44 | 44 | } |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | $this->validator = $validator; |
84 | 84 | |
85 | 85 | // Make sure $config is either a string or array. |
86 | - if (! (is_string($config) || is_array($config))) { |
|
86 | + if ( ! (is_string($config) || is_array($config))) { |
|
87 | 87 | throw new InvalidConfigurationSourceException( |
88 | 88 | sprintf( |
89 | 89 | _('Invalid configuration source: %1$s'), |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | } |
114 | 114 | |
115 | 115 | // Finally, validate the resulting config. |
116 | - if (! $this->isValid()) { |
|
116 | + if ( ! $this->isValid()) { |
|
117 | 117 | throw new InvalidConfigException( |
118 | 118 | sprintf( |
119 | 119 | _('ConfigInterface file is not valid: %1$s'), |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | */ |
153 | 153 | protected function resolveOptions($config) |
154 | 154 | { |
155 | - if (! $this->schema) { |
|
155 | + if ( ! $this->schema) { |
|
156 | 156 | return $config; |
157 | 157 | } |
158 | 158 | |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | $defaults = $this->schema->getDefaultOptions(); |
194 | 194 | $required = $this->schema->getRequiredOptions(); |
195 | 195 | |
196 | - if (! $defined && ! $defaults && ! $required) { |
|
196 | + if ( ! $defined && ! $defaults && ! $required) { |
|
197 | 197 | return false; |
198 | 198 | } |
199 | 199 |
@@ -32,188 +32,188 @@ |
||
32 | 32 | class Config extends AbstractConfig |
33 | 33 | { |
34 | 34 | |
35 | - /** |
|
36 | - * The schema of the Config file. |
|
37 | - * |
|
38 | - * @var Schema |
|
39 | - */ |
|
40 | - protected $schema; |
|
41 | - |
|
42 | - /** |
|
43 | - * The Validator class that gets asked to do the validation of the config. |
|
44 | - * |
|
45 | - * @since 0.1.0 |
|
46 | - * |
|
47 | - * @var Validator |
|
48 | - */ |
|
49 | - protected $validator; |
|
50 | - |
|
51 | - /** |
|
52 | - * Instantiate the Config object. |
|
53 | - * |
|
54 | - * It accepts either an array with the configuration settings, or a |
|
55 | - * filename pointing to a PHP file it can include. |
|
56 | - * |
|
57 | - * @since 0.1.0 |
|
58 | - * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
59 | - * |
|
60 | - * @param array|string $config Array with settings or filename for the |
|
61 | - * settings file. |
|
62 | - * @param Schema|null $schema Optional. Config that contains default |
|
63 | - * values that can get overwritten. |
|
64 | - * @param Validator|null $validator Optional. Validator class that does the |
|
65 | - * actual validation. |
|
66 | - * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
67 | - * configuration keys. Defaults to "\", "/" & ".". |
|
68 | - * |
|
69 | - * @throws InvalidConfigurationSourceException If the config source is not a string or array. |
|
70 | - * @throws FailedToInstantiateParentException If the parent class could not be instantiated. |
|
71 | - * @throws FailedToLoadConfigException If loading of the config source failed. |
|
72 | - * @throws FailedToResolveConfigException If the config file could not be resolved. |
|
73 | - * @throws InvalidConfigException If the config file is not valid. |
|
74 | - */ |
|
75 | - public function __construct( |
|
76 | - $config, |
|
77 | - Schema $schema = null, |
|
78 | - Validator $validator = null, |
|
79 | - $delimiter = null |
|
80 | - ) { |
|
81 | - $this->schema = $schema; |
|
82 | - $this->validator = $validator; |
|
83 | - |
|
84 | - // Make sure $config is either a string or array. |
|
85 | - if (! (is_string($config) || is_array($config))) { |
|
86 | - throw new InvalidConfigurationSourceException( |
|
87 | - sprintf( |
|
88 | - _('Invalid configuration source: %1$s'), |
|
89 | - print_r($config, true) |
|
90 | - ) |
|
91 | - ); |
|
92 | - } |
|
93 | - |
|
94 | - if (is_string($config)) { |
|
95 | - $config = Loader::load($config); |
|
96 | - } |
|
97 | - |
|
98 | - // Run the $config through the OptionsResolver. |
|
99 | - $config = $this->resolveOptions($config); |
|
100 | - |
|
101 | - // Instantiate the parent class. |
|
102 | - try { |
|
103 | - parent::__construct($config, $delimiter); |
|
104 | - } catch (Exception $exception) { |
|
105 | - throw new FailedToInstantiateParentException( |
|
106 | - sprintf( |
|
107 | - _('Could not instantiate the configuration through its parent. Reason: %1$s'), |
|
108 | - $exception->getMessage() |
|
109 | - ) |
|
110 | - ); |
|
111 | - } |
|
112 | - |
|
113 | - // Finally, validate the resulting config. |
|
114 | - if (! $this->isValid()) { |
|
115 | - throw new InvalidConfigException( |
|
116 | - sprintf( |
|
117 | - _('ConfigInterface file is not valid: %1$s'), |
|
118 | - print_r($config, true) |
|
119 | - ) |
|
120 | - ); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Validate the Config file. |
|
126 | - * |
|
127 | - * @since 0.1.0 |
|
128 | - * |
|
129 | - * @return boolean |
|
130 | - */ |
|
131 | - public function isValid() |
|
132 | - { |
|
133 | - if ($this->validator) { |
|
134 | - return $this->validator->isValid($this); |
|
135 | - } |
|
136 | - |
|
137 | - return true; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Process the passed-in defaults and merge them with the new values, while |
|
142 | - * checking that all required options are set. |
|
143 | - * |
|
144 | - * @since 0.1.0 |
|
145 | - * |
|
146 | - * @param array $config Configuration settings to resolve. |
|
147 | - * |
|
148 | - * @return array Resolved configuration settings. |
|
149 | - * @throws FailedToResolveConfigException If there are errors while resolving the options. |
|
150 | - */ |
|
151 | - protected function resolveOptions($config) |
|
152 | - { |
|
153 | - if (! $this->schema) { |
|
154 | - return $config; |
|
155 | - } |
|
156 | - |
|
157 | - try { |
|
158 | - $resolver = new OptionsResolver(); |
|
159 | - if ($this->configureOptions($resolver)) { |
|
160 | - $config = $resolver->resolve($config); |
|
161 | - } |
|
162 | - } catch (Exception $exception) { |
|
163 | - throw new FailedToResolveConfigException( |
|
164 | - sprintf( |
|
165 | - _('Error while resolving config options: %1$s'), |
|
166 | - $exception->getMessage() |
|
167 | - ) |
|
168 | - ); |
|
169 | - } |
|
170 | - |
|
171 | - return $config; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Configure the possible and required options for the Config. |
|
176 | - * |
|
177 | - * This should return a bool to let the resolve_options() know whether the |
|
178 | - * actual resolving needs to be done or not. |
|
179 | - * |
|
180 | - * @since 0.1.0 |
|
181 | - * |
|
182 | - * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
183 | - * instance. |
|
184 | - * |
|
185 | - * @return bool Whether to do the resolving. |
|
186 | - * @throws FailedToResolveConfigException If there are errors while processing. |
|
187 | - */ |
|
188 | - protected function configureOptions(OptionsResolver $resolver) |
|
189 | - { |
|
190 | - $defined = $this->schema->getDefinedOptions(); |
|
191 | - $defaults = $this->schema->getDefaultOptions(); |
|
192 | - $required = $this->schema->getRequiredOptions(); |
|
193 | - |
|
194 | - if (! $defined && ! $defaults && ! $required) { |
|
195 | - return false; |
|
196 | - } |
|
197 | - |
|
198 | - try { |
|
199 | - if ($defined) { |
|
200 | - $resolver->setDefined($defined); |
|
201 | - } |
|
202 | - if ($defaults) { |
|
203 | - $resolver->setDefaults($defaults); |
|
204 | - } |
|
205 | - if ($required) { |
|
206 | - $resolver->setRequired($required); |
|
207 | - } |
|
208 | - } catch (Exception $exception) { |
|
209 | - throw new FailedToResolveConfigException( |
|
210 | - sprintf( |
|
211 | - _('Error while processing config options: %1$s'), |
|
212 | - $exception->getMessage() |
|
213 | - ) |
|
214 | - ); |
|
215 | - } |
|
216 | - |
|
217 | - return true; |
|
218 | - } |
|
35 | + /** |
|
36 | + * The schema of the Config file. |
|
37 | + * |
|
38 | + * @var Schema |
|
39 | + */ |
|
40 | + protected $schema; |
|
41 | + |
|
42 | + /** |
|
43 | + * The Validator class that gets asked to do the validation of the config. |
|
44 | + * |
|
45 | + * @since 0.1.0 |
|
46 | + * |
|
47 | + * @var Validator |
|
48 | + */ |
|
49 | + protected $validator; |
|
50 | + |
|
51 | + /** |
|
52 | + * Instantiate the Config object. |
|
53 | + * |
|
54 | + * It accepts either an array with the configuration settings, or a |
|
55 | + * filename pointing to a PHP file it can include. |
|
56 | + * |
|
57 | + * @since 0.1.0 |
|
58 | + * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
59 | + * |
|
60 | + * @param array|string $config Array with settings or filename for the |
|
61 | + * settings file. |
|
62 | + * @param Schema|null $schema Optional. Config that contains default |
|
63 | + * values that can get overwritten. |
|
64 | + * @param Validator|null $validator Optional. Validator class that does the |
|
65 | + * actual validation. |
|
66 | + * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
67 | + * configuration keys. Defaults to "\", "/" & ".". |
|
68 | + * |
|
69 | + * @throws InvalidConfigurationSourceException If the config source is not a string or array. |
|
70 | + * @throws FailedToInstantiateParentException If the parent class could not be instantiated. |
|
71 | + * @throws FailedToLoadConfigException If loading of the config source failed. |
|
72 | + * @throws FailedToResolveConfigException If the config file could not be resolved. |
|
73 | + * @throws InvalidConfigException If the config file is not valid. |
|
74 | + */ |
|
75 | + public function __construct( |
|
76 | + $config, |
|
77 | + Schema $schema = null, |
|
78 | + Validator $validator = null, |
|
79 | + $delimiter = null |
|
80 | + ) { |
|
81 | + $this->schema = $schema; |
|
82 | + $this->validator = $validator; |
|
83 | + |
|
84 | + // Make sure $config is either a string or array. |
|
85 | + if (! (is_string($config) || is_array($config))) { |
|
86 | + throw new InvalidConfigurationSourceException( |
|
87 | + sprintf( |
|
88 | + _('Invalid configuration source: %1$s'), |
|
89 | + print_r($config, true) |
|
90 | + ) |
|
91 | + ); |
|
92 | + } |
|
93 | + |
|
94 | + if (is_string($config)) { |
|
95 | + $config = Loader::load($config); |
|
96 | + } |
|
97 | + |
|
98 | + // Run the $config through the OptionsResolver. |
|
99 | + $config = $this->resolveOptions($config); |
|
100 | + |
|
101 | + // Instantiate the parent class. |
|
102 | + try { |
|
103 | + parent::__construct($config, $delimiter); |
|
104 | + } catch (Exception $exception) { |
|
105 | + throw new FailedToInstantiateParentException( |
|
106 | + sprintf( |
|
107 | + _('Could not instantiate the configuration through its parent. Reason: %1$s'), |
|
108 | + $exception->getMessage() |
|
109 | + ) |
|
110 | + ); |
|
111 | + } |
|
112 | + |
|
113 | + // Finally, validate the resulting config. |
|
114 | + if (! $this->isValid()) { |
|
115 | + throw new InvalidConfigException( |
|
116 | + sprintf( |
|
117 | + _('ConfigInterface file is not valid: %1$s'), |
|
118 | + print_r($config, true) |
|
119 | + ) |
|
120 | + ); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Validate the Config file. |
|
126 | + * |
|
127 | + * @since 0.1.0 |
|
128 | + * |
|
129 | + * @return boolean |
|
130 | + */ |
|
131 | + public function isValid() |
|
132 | + { |
|
133 | + if ($this->validator) { |
|
134 | + return $this->validator->isValid($this); |
|
135 | + } |
|
136 | + |
|
137 | + return true; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Process the passed-in defaults and merge them with the new values, while |
|
142 | + * checking that all required options are set. |
|
143 | + * |
|
144 | + * @since 0.1.0 |
|
145 | + * |
|
146 | + * @param array $config Configuration settings to resolve. |
|
147 | + * |
|
148 | + * @return array Resolved configuration settings. |
|
149 | + * @throws FailedToResolveConfigException If there are errors while resolving the options. |
|
150 | + */ |
|
151 | + protected function resolveOptions($config) |
|
152 | + { |
|
153 | + if (! $this->schema) { |
|
154 | + return $config; |
|
155 | + } |
|
156 | + |
|
157 | + try { |
|
158 | + $resolver = new OptionsResolver(); |
|
159 | + if ($this->configureOptions($resolver)) { |
|
160 | + $config = $resolver->resolve($config); |
|
161 | + } |
|
162 | + } catch (Exception $exception) { |
|
163 | + throw new FailedToResolveConfigException( |
|
164 | + sprintf( |
|
165 | + _('Error while resolving config options: %1$s'), |
|
166 | + $exception->getMessage() |
|
167 | + ) |
|
168 | + ); |
|
169 | + } |
|
170 | + |
|
171 | + return $config; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Configure the possible and required options for the Config. |
|
176 | + * |
|
177 | + * This should return a bool to let the resolve_options() know whether the |
|
178 | + * actual resolving needs to be done or not. |
|
179 | + * |
|
180 | + * @since 0.1.0 |
|
181 | + * |
|
182 | + * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
183 | + * instance. |
|
184 | + * |
|
185 | + * @return bool Whether to do the resolving. |
|
186 | + * @throws FailedToResolveConfigException If there are errors while processing. |
|
187 | + */ |
|
188 | + protected function configureOptions(OptionsResolver $resolver) |
|
189 | + { |
|
190 | + $defined = $this->schema->getDefinedOptions(); |
|
191 | + $defaults = $this->schema->getDefaultOptions(); |
|
192 | + $required = $this->schema->getRequiredOptions(); |
|
193 | + |
|
194 | + if (! $defined && ! $defaults && ! $required) { |
|
195 | + return false; |
|
196 | + } |
|
197 | + |
|
198 | + try { |
|
199 | + if ($defined) { |
|
200 | + $resolver->setDefined($defined); |
|
201 | + } |
|
202 | + if ($defaults) { |
|
203 | + $resolver->setDefaults($defaults); |
|
204 | + } |
|
205 | + if ($required) { |
|
206 | + $resolver->setRequired($required); |
|
207 | + } |
|
208 | + } catch (Exception $exception) { |
|
209 | + throw new FailedToResolveConfigException( |
|
210 | + sprintf( |
|
211 | + _('Error while processing config options: %1$s'), |
|
212 | + $exception->getMessage() |
|
213 | + ) |
|
214 | + ); |
|
215 | + } |
|
216 | + |
|
217 | + return true; |
|
218 | + } |
|
219 | 219 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | * |
134 | 134 | * @since 0.4.2 |
135 | 135 | * |
136 | - * @return ConfigInterface Configuration settings to use. |
|
136 | + * @return Config|null Configuration settings to use. |
|
137 | 137 | */ |
138 | 138 | protected function fetchDefaultConfig() |
139 | 139 | { |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | * |
152 | 152 | * @since 0.4.2 |
153 | 153 | * |
154 | - * @return ConfigInterface Configuration settings to use. |
|
154 | + * @return Config|null Configuration settings to use. |
|
155 | 155 | */ |
156 | 156 | protected function fetchConfig($configFile) |
157 | 157 | { |
@@ -139,7 +139,7 @@ |
||
139 | 139 | { |
140 | 140 | $configFile = method_exists($this, 'getDefaultConfigFile') |
141 | 141 | ? $this->getDefaultConfigFile() |
142 | - : __DIR__ . '/../config/defaults.php'; |
|
142 | + : __DIR__.'/../config/defaults.php'; |
|
143 | 143 | |
144 | 144 | return $this->fetchConfig($configFile); |
145 | 145 | } |
@@ -25,164 +25,164 @@ |
||
25 | 25 | trait ConfigTrait |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * Reference to the Config object. |
|
30 | - * |
|
31 | - * @since 0.1.2 |
|
32 | - * |
|
33 | - * @var ConfigInterface |
|
34 | - */ |
|
35 | - protected $config; |
|
36 | - |
|
37 | - /** |
|
38 | - * Process the passed-in configuration file. |
|
39 | - * |
|
40 | - * @since 0.1.2 |
|
41 | - * |
|
42 | - * @param ConfigInterface $config The Config to process. |
|
43 | - * @param string ... List of keys. |
|
44 | - * |
|
45 | - * @throws FailedToProcessConfigException If the arguments could not be parsed into a Config. |
|
46 | - */ |
|
47 | - protected function processConfig(ConfigInterface $config) |
|
48 | - { |
|
49 | - if (func_num_args() > 1) { |
|
50 | - try { |
|
51 | - $keys = func_get_args(); |
|
52 | - array_shift($keys); |
|
53 | - $config = $config->getSubConfig($keys); |
|
54 | - } catch (Exception $exception) { |
|
55 | - throw new FailedToProcessConfigException( |
|
56 | - sprintf( |
|
57 | - _('Could not process the config with the arguments "%1$s".'), |
|
58 | - print_r(func_get_args(), true) |
|
59 | - ) |
|
60 | - ); |
|
61 | - } |
|
62 | - } |
|
63 | - $this->config = $config; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Check whether the Config has a specific key. |
|
68 | - * |
|
69 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
70 | - * |
|
71 | - * @since 0.1.2 |
|
72 | - * @since 0.1.5 Accepts list of keys. |
|
73 | - * |
|
74 | - * @param string|array $_ List of keys. |
|
75 | - * |
|
76 | - * @return bool Whether the key is known. |
|
77 | - */ |
|
78 | - protected function hasConfigKey($_) |
|
79 | - { |
|
80 | - $keys = func_get_args(); |
|
81 | - |
|
82 | - return $this->config->hasKey($keys); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Get the Config value for a specific key. |
|
87 | - * |
|
88 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
89 | - * |
|
90 | - * @since 0.1.2 |
|
91 | - * @since 0.1.5 Accepts list of keys. |
|
92 | - * |
|
93 | - * @param string|array $_ List of keys. |
|
94 | - * |
|
95 | - * @return mixed Value of the key. |
|
96 | - */ |
|
97 | - protected function getConfigKey($_) |
|
98 | - { |
|
99 | - $keys = func_get_args(); |
|
100 | - |
|
101 | - return $this->config->getKey($keys); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Get the callable Config value for a specific key. |
|
106 | - * |
|
107 | - * If the fetched value is indeed a callable, it will be executed with the provided arguments, and the resultant |
|
108 | - * value will be returned instead. |
|
109 | - * |
|
110 | - * @since 0.4.8 |
|
111 | - * |
|
112 | - * @param string|array $key Key or array of nested keys. |
|
113 | - * @param array $args Optional. Array of arguments to pass to the callable. |
|
114 | - * |
|
115 | - * @return mixed Resultant value of the key's callable. |
|
116 | - */ |
|
117 | - protected function getConfigCallable($key, array $args = []) |
|
118 | - { |
|
119 | - $value = $this->config->getKey($key); |
|
120 | - |
|
121 | - if (is_callable($value)) { |
|
122 | - $value = $value(...$args); |
|
123 | - } |
|
124 | - |
|
125 | - return $value; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
130 | - * |
|
131 | - * @since 0.1.4 |
|
132 | - * |
|
133 | - * @return array All the configuration settings. |
|
134 | - */ |
|
135 | - protected function getConfigArray() |
|
136 | - { |
|
137 | - return $this->config->getAll(); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Get an array of all the keys that are known by the Config. |
|
142 | - * |
|
143 | - * @since 0.1.2 |
|
144 | - * |
|
145 | - * @return array Array of strings containing all the keys. |
|
146 | - */ |
|
147 | - protected function getConfigKeys() |
|
148 | - { |
|
149 | - return $this->config->getKeys(); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Get a default configuration in case none was injected into the constructor. |
|
154 | - * |
|
155 | - * The name and path of the configuration needs to be set as a const called DEFAULT_CONFIG within the class |
|
156 | - * containing the trait. The path needs to be relative to the location of the containing class file. |
|
157 | - * |
|
158 | - * @since 0.4.2 |
|
159 | - * |
|
160 | - * @return ConfigInterface Configuration settings to use. |
|
161 | - */ |
|
162 | - protected function fetchDefaultConfig() |
|
163 | - { |
|
164 | - $configFile = method_exists($this, 'getDefaultConfigFile') |
|
165 | - ? $this->getDefaultConfigFile() |
|
166 | - : __DIR__ . '/../config/defaults.php'; |
|
167 | - |
|
168 | - return $this->fetchConfig($configFile); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Get a configuration from a specified $file. |
|
173 | - * |
|
174 | - * If file is not accessible or readable, returns an empty Config. |
|
175 | - * |
|
176 | - * @since 0.4.2 |
|
177 | - * |
|
178 | - * @return ConfigInterface Configuration settings to use. |
|
179 | - */ |
|
180 | - protected function fetchConfig($configFile) |
|
181 | - { |
|
182 | - if (is_string($configFile) && ! is_readable($configFile)) { |
|
183 | - $configFile = []; |
|
184 | - } |
|
185 | - |
|
186 | - return ConfigFactory::create($configFile); |
|
187 | - } |
|
28 | + /** |
|
29 | + * Reference to the Config object. |
|
30 | + * |
|
31 | + * @since 0.1.2 |
|
32 | + * |
|
33 | + * @var ConfigInterface |
|
34 | + */ |
|
35 | + protected $config; |
|
36 | + |
|
37 | + /** |
|
38 | + * Process the passed-in configuration file. |
|
39 | + * |
|
40 | + * @since 0.1.2 |
|
41 | + * |
|
42 | + * @param ConfigInterface $config The Config to process. |
|
43 | + * @param string ... List of keys. |
|
44 | + * |
|
45 | + * @throws FailedToProcessConfigException If the arguments could not be parsed into a Config. |
|
46 | + */ |
|
47 | + protected function processConfig(ConfigInterface $config) |
|
48 | + { |
|
49 | + if (func_num_args() > 1) { |
|
50 | + try { |
|
51 | + $keys = func_get_args(); |
|
52 | + array_shift($keys); |
|
53 | + $config = $config->getSubConfig($keys); |
|
54 | + } catch (Exception $exception) { |
|
55 | + throw new FailedToProcessConfigException( |
|
56 | + sprintf( |
|
57 | + _('Could not process the config with the arguments "%1$s".'), |
|
58 | + print_r(func_get_args(), true) |
|
59 | + ) |
|
60 | + ); |
|
61 | + } |
|
62 | + } |
|
63 | + $this->config = $config; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Check whether the Config has a specific key. |
|
68 | + * |
|
69 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
70 | + * |
|
71 | + * @since 0.1.2 |
|
72 | + * @since 0.1.5 Accepts list of keys. |
|
73 | + * |
|
74 | + * @param string|array $_ List of keys. |
|
75 | + * |
|
76 | + * @return bool Whether the key is known. |
|
77 | + */ |
|
78 | + protected function hasConfigKey($_) |
|
79 | + { |
|
80 | + $keys = func_get_args(); |
|
81 | + |
|
82 | + return $this->config->hasKey($keys); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Get the Config value for a specific key. |
|
87 | + * |
|
88 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
89 | + * |
|
90 | + * @since 0.1.2 |
|
91 | + * @since 0.1.5 Accepts list of keys. |
|
92 | + * |
|
93 | + * @param string|array $_ List of keys. |
|
94 | + * |
|
95 | + * @return mixed Value of the key. |
|
96 | + */ |
|
97 | + protected function getConfigKey($_) |
|
98 | + { |
|
99 | + $keys = func_get_args(); |
|
100 | + |
|
101 | + return $this->config->getKey($keys); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Get the callable Config value for a specific key. |
|
106 | + * |
|
107 | + * If the fetched value is indeed a callable, it will be executed with the provided arguments, and the resultant |
|
108 | + * value will be returned instead. |
|
109 | + * |
|
110 | + * @since 0.4.8 |
|
111 | + * |
|
112 | + * @param string|array $key Key or array of nested keys. |
|
113 | + * @param array $args Optional. Array of arguments to pass to the callable. |
|
114 | + * |
|
115 | + * @return mixed Resultant value of the key's callable. |
|
116 | + */ |
|
117 | + protected function getConfigCallable($key, array $args = []) |
|
118 | + { |
|
119 | + $value = $this->config->getKey($key); |
|
120 | + |
|
121 | + if (is_callable($value)) { |
|
122 | + $value = $value(...$args); |
|
123 | + } |
|
124 | + |
|
125 | + return $value; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
130 | + * |
|
131 | + * @since 0.1.4 |
|
132 | + * |
|
133 | + * @return array All the configuration settings. |
|
134 | + */ |
|
135 | + protected function getConfigArray() |
|
136 | + { |
|
137 | + return $this->config->getAll(); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Get an array of all the keys that are known by the Config. |
|
142 | + * |
|
143 | + * @since 0.1.2 |
|
144 | + * |
|
145 | + * @return array Array of strings containing all the keys. |
|
146 | + */ |
|
147 | + protected function getConfigKeys() |
|
148 | + { |
|
149 | + return $this->config->getKeys(); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Get a default configuration in case none was injected into the constructor. |
|
154 | + * |
|
155 | + * The name and path of the configuration needs to be set as a const called DEFAULT_CONFIG within the class |
|
156 | + * containing the trait. The path needs to be relative to the location of the containing class file. |
|
157 | + * |
|
158 | + * @since 0.4.2 |
|
159 | + * |
|
160 | + * @return ConfigInterface Configuration settings to use. |
|
161 | + */ |
|
162 | + protected function fetchDefaultConfig() |
|
163 | + { |
|
164 | + $configFile = method_exists($this, 'getDefaultConfigFile') |
|
165 | + ? $this->getDefaultConfigFile() |
|
166 | + : __DIR__ . '/../config/defaults.php'; |
|
167 | + |
|
168 | + return $this->fetchConfig($configFile); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Get a configuration from a specified $file. |
|
173 | + * |
|
174 | + * If file is not accessible or readable, returns an empty Config. |
|
175 | + * |
|
176 | + * @since 0.4.2 |
|
177 | + * |
|
178 | + * @return ConfigInterface Configuration settings to use. |
|
179 | + */ |
|
180 | + protected function fetchConfig($configFile) |
|
181 | + { |
|
182 | + if (is_string($configFile) && ! is_readable($configFile)) { |
|
183 | + $configFile = []; |
|
184 | + } |
|
185 | + |
|
186 | + return ConfigFactory::create($configFile); |
|
187 | + } |
|
188 | 188 | } |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | * |
44 | 44 | * @param string|array $_ List of files. |
45 | 45 | * |
46 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
46 | + * @return Config|null Instance of a ConfigInterface implementation. |
|
47 | 47 | */ |
48 | 48 | public static function createFromFile($_) |
49 | 49 | { |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | * |
88 | 88 | * @param array $array Array with configuration values. |
89 | 89 | * |
90 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
90 | + * @return Config|null Instance of a ConfigInterface implementation. |
|
91 | 91 | */ |
92 | 92 | public static function createFromArray(array $array) |
93 | 93 | { |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | * |
110 | 110 | * @param mixed $_ Array with configuration values. |
111 | 111 | * |
112 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
112 | + * @return Config|null Instance of a ConfigInterface implementation. |
|
113 | 113 | */ |
114 | 114 | public static function create($_) |
115 | 115 | { |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | * |
136 | 136 | * @param mixed $_ Array with configuration values. |
137 | 137 | * |
138 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
138 | + * @return Config|null Instance of a ConfigInterface implementation. |
|
139 | 139 | */ |
140 | 140 | public static function merge($_) |
141 | 141 | { |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | * |
163 | 163 | * @param string|array $_ List of files. |
164 | 164 | * |
165 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
165 | + * @return Config|null Instance of a ConfigInterface implementation. |
|
166 | 166 | */ |
167 | 167 | public static function mergeFromFiles($_) |
168 | 168 | { |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | * @since 0.4.4 |
232 | 232 | * |
233 | 233 | * @param string $identifier Identifier to look for in the cache. |
234 | - * @param mixed $fallback Fallback to use to fill the cache. If $fallback is a callable, it will be executed |
|
234 | + * @param \Closure $fallback Fallback to use to fill the cache. If $fallback is a callable, it will be executed |
|
235 | 235 | * with $identifier as an argument. |
236 | 236 | * |
237 | 237 | * @return mixed The latest content of the cache for the given identifier. |
@@ -57,12 +57,12 @@ discard block |
||
57 | 57 | try { |
58 | 58 | $file = array_pop($files); |
59 | 59 | |
60 | - if (! is_readable($file)) { |
|
60 | + if ( ! is_readable($file)) { |
|
61 | 61 | continue; |
62 | 62 | } |
63 | 63 | |
64 | 64 | $config = static::createFromArray( |
65 | - static::getFromCache($file, function ($file) { |
|
65 | + static::getFromCache($file, function($file) { |
|
66 | 66 | return Loader::load($file); |
67 | 67 | }) |
68 | 68 | ); |
@@ -177,11 +177,11 @@ discard block |
||
177 | 177 | try { |
178 | 178 | $file = array_pop($files); |
179 | 179 | |
180 | - if (! is_readable($file)) { |
|
180 | + if ( ! is_readable($file)) { |
|
181 | 181 | continue; |
182 | 182 | } |
183 | 183 | |
184 | - $new_data = static::getFromCache($file, function ($file) { |
|
184 | + $new_data = static::getFromCache($file, function($file) { |
|
185 | 185 | return Loader::load($file); |
186 | 186 | }); |
187 | 187 | |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | */ |
239 | 239 | protected static function getFromCache($identifier, $fallback) |
240 | 240 | { |
241 | - if (! array_key_exists($identifier, static::$configFilesCache)) { |
|
241 | + if ( ! array_key_exists($identifier, static::$configFilesCache)) { |
|
242 | 242 | static::$configFilesCache[$identifier] = is_callable($fallback) |
243 | 243 | ? $fallback($identifier) |
244 | 244 | : $fallback; |
@@ -24,226 +24,226 @@ |
||
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_readable($file)) { |
|
61 | - continue; |
|
62 | - } |
|
63 | - |
|
64 | - $config = static::createFromArray( |
|
65 | - static::getFromCache($file, function ($file) { |
|
66 | - return Loader::load($file); |
|
67 | - }) |
|
68 | - ); |
|
69 | - |
|
70 | - if (null === $config) { |
|
71 | - continue; |
|
72 | - } |
|
73 | - |
|
74 | - return $config; |
|
75 | - } catch (Exception $exception) { |
|
76 | - // Fail silently and try next file. |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - return static::createFromArray([]); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Create a new ConfigInterface object from an array. |
|
85 | - * |
|
86 | - * @since 0.3.0 |
|
87 | - * |
|
88 | - * @param array $array Array with configuration values. |
|
89 | - * |
|
90 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
91 | - */ |
|
92 | - public static function createFromArray(array $array) |
|
93 | - { |
|
94 | - try { |
|
95 | - return new Config($array); |
|
96 | - } catch (Exception $exception) { |
|
97 | - // Fail silently and try next file. |
|
98 | - } |
|
99 | - |
|
100 | - return null; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Create a new ConfigInterface object. |
|
105 | - * |
|
106 | - * Tries to deduce the correct creation method by inspecting the provided arguments. |
|
107 | - * |
|
108 | - * @since 0.3.0 |
|
109 | - * |
|
110 | - * @param mixed $_ Array with configuration values. |
|
111 | - * |
|
112 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
113 | - */ |
|
114 | - public static function create($_) |
|
115 | - { |
|
116 | - if (func_num_args() < 1) { |
|
117 | - return static::createFromArray([]); |
|
118 | - } |
|
119 | - |
|
120 | - $arguments = func_get_args(); |
|
121 | - |
|
122 | - if (is_array($arguments[0]) && func_num_args() === 1) { |
|
123 | - return static::createFromArray($arguments[0]); |
|
124 | - } |
|
125 | - |
|
126 | - return static::createFromFile($arguments); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Create a new ConfigInterface object, by merging several files together. |
|
131 | - * |
|
132 | - * Duplicate keys in later files will override those in earlier files. |
|
133 | - * |
|
134 | - * @since 0.4.6 |
|
135 | - * |
|
136 | - * @param mixed $_ Array with configuration values. |
|
137 | - * |
|
138 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
139 | - */ |
|
140 | - public static function merge($_) |
|
141 | - { |
|
142 | - if (func_num_args() < 1) { |
|
143 | - return static::createFromArray([]); |
|
144 | - } |
|
145 | - |
|
146 | - $arguments = func_get_args(); |
|
147 | - |
|
148 | - if (is_array($arguments[0]) && func_num_args() === 1) { |
|
149 | - return static::createFromArray($arguments[0]); |
|
150 | - } |
|
151 | - |
|
152 | - return static::mergeFromFiles($arguments); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Create a new ConfigInterface object by merging data from several files. |
|
157 | - * |
|
158 | - * If a comma-separated list of files is provided, they are loaded in sequence and later files override settings in |
|
159 | - * earlier files. |
|
160 | - * |
|
161 | - * @since 0.4.6 |
|
162 | - * |
|
163 | - * @param string|array $_ List of files. |
|
164 | - * |
|
165 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
166 | - */ |
|
167 | - public static function mergeFromFiles($_) |
|
168 | - { |
|
169 | - $files = array_reverse(func_get_args()); |
|
170 | - $data = []; |
|
171 | - |
|
172 | - if (is_array($files[0])) { |
|
173 | - $files = array_reverse($files[0]); |
|
174 | - } |
|
175 | - |
|
176 | - while (count($files) > 0) { |
|
177 | - try { |
|
178 | - $file = array_pop($files); |
|
179 | - |
|
180 | - if (! is_readable($file)) { |
|
181 | - continue; |
|
182 | - } |
|
183 | - |
|
184 | - $new_data = static::getFromCache($file, function ($file) { |
|
185 | - return Loader::load($file); |
|
186 | - }); |
|
187 | - |
|
188 | - if (null === $data) { |
|
189 | - continue; |
|
190 | - } |
|
191 | - |
|
192 | - $data = array_replace_recursive($data, $new_data); |
|
193 | - } catch (Exception $exception) { |
|
194 | - // Fail silently and try next file. |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - return static::createFromArray($data); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Create a new ConfigInterface object from a file and return a sub-portion of it. |
|
203 | - * |
|
204 | - * The first argument needs to be the file name to load, and the subsequent arguments will be passed on to |
|
205 | - * `Config::getSubConfig()`. |
|
206 | - * |
|
207 | - * @since 0.4.5 |
|
208 | - * |
|
209 | - * @param mixed $_ File name of the config to load as a string, followed by an array of keys to pass to |
|
210 | - * `Config::getSubConfig()`. |
|
211 | - * |
|
212 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
213 | - */ |
|
214 | - public static function createSubConfig($_) |
|
215 | - { |
|
216 | - if (func_num_args() < 2) { |
|
217 | - return static::createFromArray([]); |
|
218 | - } |
|
219 | - |
|
220 | - $arguments = func_get_args(); |
|
221 | - $file = array_shift($arguments); |
|
222 | - |
|
223 | - $config = static::createFromFile($file); |
|
224 | - |
|
225 | - return $config->getSubConfig($arguments); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Get a config file from the config file cache. |
|
230 | - * |
|
231 | - * @since 0.4.4 |
|
232 | - * |
|
233 | - * @param string $identifier Identifier to look for in the cache. |
|
234 | - * @param mixed $fallback Fallback to use to fill the cache. If $fallback is a callable, it will be executed |
|
235 | - * with $identifier as an argument. |
|
236 | - * |
|
237 | - * @return mixed The latest content of the cache for the given identifier. |
|
238 | - */ |
|
239 | - protected static function getFromCache($identifier, $fallback) |
|
240 | - { |
|
241 | - if (! array_key_exists($identifier, static::$configFilesCache)) { |
|
242 | - static::$configFilesCache[$identifier] = is_callable($fallback) |
|
243 | - ? $fallback($identifier) |
|
244 | - : $fallback; |
|
245 | - } |
|
246 | - |
|
247 | - return static::$configFilesCache[$identifier]; |
|
248 | - } |
|
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_readable($file)) { |
|
61 | + continue; |
|
62 | + } |
|
63 | + |
|
64 | + $config = static::createFromArray( |
|
65 | + static::getFromCache($file, function ($file) { |
|
66 | + return Loader::load($file); |
|
67 | + }) |
|
68 | + ); |
|
69 | + |
|
70 | + if (null === $config) { |
|
71 | + continue; |
|
72 | + } |
|
73 | + |
|
74 | + return $config; |
|
75 | + } catch (Exception $exception) { |
|
76 | + // Fail silently and try next file. |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + return static::createFromArray([]); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Create a new ConfigInterface object from an array. |
|
85 | + * |
|
86 | + * @since 0.3.0 |
|
87 | + * |
|
88 | + * @param array $array Array with configuration values. |
|
89 | + * |
|
90 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
91 | + */ |
|
92 | + public static function createFromArray(array $array) |
|
93 | + { |
|
94 | + try { |
|
95 | + return new Config($array); |
|
96 | + } catch (Exception $exception) { |
|
97 | + // Fail silently and try next file. |
|
98 | + } |
|
99 | + |
|
100 | + return null; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Create a new ConfigInterface object. |
|
105 | + * |
|
106 | + * Tries to deduce the correct creation method by inspecting the provided arguments. |
|
107 | + * |
|
108 | + * @since 0.3.0 |
|
109 | + * |
|
110 | + * @param mixed $_ Array with configuration values. |
|
111 | + * |
|
112 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
113 | + */ |
|
114 | + public static function create($_) |
|
115 | + { |
|
116 | + if (func_num_args() < 1) { |
|
117 | + return static::createFromArray([]); |
|
118 | + } |
|
119 | + |
|
120 | + $arguments = func_get_args(); |
|
121 | + |
|
122 | + if (is_array($arguments[0]) && func_num_args() === 1) { |
|
123 | + return static::createFromArray($arguments[0]); |
|
124 | + } |
|
125 | + |
|
126 | + return static::createFromFile($arguments); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Create a new ConfigInterface object, by merging several files together. |
|
131 | + * |
|
132 | + * Duplicate keys in later files will override those in earlier files. |
|
133 | + * |
|
134 | + * @since 0.4.6 |
|
135 | + * |
|
136 | + * @param mixed $_ Array with configuration values. |
|
137 | + * |
|
138 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
139 | + */ |
|
140 | + public static function merge($_) |
|
141 | + { |
|
142 | + if (func_num_args() < 1) { |
|
143 | + return static::createFromArray([]); |
|
144 | + } |
|
145 | + |
|
146 | + $arguments = func_get_args(); |
|
147 | + |
|
148 | + if (is_array($arguments[0]) && func_num_args() === 1) { |
|
149 | + return static::createFromArray($arguments[0]); |
|
150 | + } |
|
151 | + |
|
152 | + return static::mergeFromFiles($arguments); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Create a new ConfigInterface object by merging data from several files. |
|
157 | + * |
|
158 | + * If a comma-separated list of files is provided, they are loaded in sequence and later files override settings in |
|
159 | + * earlier files. |
|
160 | + * |
|
161 | + * @since 0.4.6 |
|
162 | + * |
|
163 | + * @param string|array $_ List of files. |
|
164 | + * |
|
165 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
166 | + */ |
|
167 | + public static function mergeFromFiles($_) |
|
168 | + { |
|
169 | + $files = array_reverse(func_get_args()); |
|
170 | + $data = []; |
|
171 | + |
|
172 | + if (is_array($files[0])) { |
|
173 | + $files = array_reverse($files[0]); |
|
174 | + } |
|
175 | + |
|
176 | + while (count($files) > 0) { |
|
177 | + try { |
|
178 | + $file = array_pop($files); |
|
179 | + |
|
180 | + if (! is_readable($file)) { |
|
181 | + continue; |
|
182 | + } |
|
183 | + |
|
184 | + $new_data = static::getFromCache($file, function ($file) { |
|
185 | + return Loader::load($file); |
|
186 | + }); |
|
187 | + |
|
188 | + if (null === $data) { |
|
189 | + continue; |
|
190 | + } |
|
191 | + |
|
192 | + $data = array_replace_recursive($data, $new_data); |
|
193 | + } catch (Exception $exception) { |
|
194 | + // Fail silently and try next file. |
|
195 | + } |
|
196 | + } |
|
197 | + |
|
198 | + return static::createFromArray($data); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Create a new ConfigInterface object from a file and return a sub-portion of it. |
|
203 | + * |
|
204 | + * The first argument needs to be the file name to load, and the subsequent arguments will be passed on to |
|
205 | + * `Config::getSubConfig()`. |
|
206 | + * |
|
207 | + * @since 0.4.5 |
|
208 | + * |
|
209 | + * @param mixed $_ File name of the config to load as a string, followed by an array of keys to pass to |
|
210 | + * `Config::getSubConfig()`. |
|
211 | + * |
|
212 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
213 | + */ |
|
214 | + public static function createSubConfig($_) |
|
215 | + { |
|
216 | + if (func_num_args() < 2) { |
|
217 | + return static::createFromArray([]); |
|
218 | + } |
|
219 | + |
|
220 | + $arguments = func_get_args(); |
|
221 | + $file = array_shift($arguments); |
|
222 | + |
|
223 | + $config = static::createFromFile($file); |
|
224 | + |
|
225 | + return $config->getSubConfig($arguments); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Get a config file from the config file cache. |
|
230 | + * |
|
231 | + * @since 0.4.4 |
|
232 | + * |
|
233 | + * @param string $identifier Identifier to look for in the cache. |
|
234 | + * @param mixed $fallback Fallback to use to fill the cache. If $fallback is a callable, it will be executed |
|
235 | + * with $identifier as an argument. |
|
236 | + * |
|
237 | + * @return mixed The latest content of the cache for the given identifier. |
|
238 | + */ |
|
239 | + protected static function getFromCache($identifier, $fallback) |
|
240 | + { |
|
241 | + if (! array_key_exists($identifier, static::$configFilesCache)) { |
|
242 | + static::$configFilesCache[$identifier] = is_callable($fallback) |
|
243 | + ? $fallback($identifier) |
|
244 | + : $fallback; |
|
245 | + } |
|
246 | + |
|
247 | + return static::$configFilesCache[$identifier]; |
|
248 | + } |
|
249 | 249 | } |