@@ -28,176 +28,176 @@ |
||
28 | 28 | abstract class AbstractConfig extends ArrayObject implements ConfigInterface |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * Array of strings that are used as delimiters to parse configuration keys. |
|
33 | - * |
|
34 | - * @since 0.1.6 |
|
35 | - * |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $delimiter = ['\\', '/', '.']; |
|
39 | - |
|
40 | - /** |
|
41 | - * Instantiate the AbstractConfig object. |
|
42 | - * |
|
43 | - * @since 0.1.0 |
|
44 | - * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
45 | - * |
|
46 | - * @param array $config Array with settings. |
|
47 | - * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
48 | - * configuration keys. Defaults to "\", "/" & ".". |
|
49 | - */ |
|
50 | - public function __construct(array $config, $delimiter = null) |
|
51 | - { |
|
52 | - // Make sure the config entries can be accessed as properties. |
|
53 | - parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
|
54 | - |
|
55 | - if (null !== $delimiter) { |
|
56 | - $this->delimiter = (array)$delimiter; |
|
57 | - } |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Get the value of a specific key. |
|
62 | - * |
|
63 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
64 | - * |
|
65 | - * @since 0.1.0 |
|
66 | - * @since 0.1.4 Accepts list of keys. |
|
67 | - * |
|
68 | - * @param string ... List of keys. |
|
69 | - * @return mixed |
|
70 | - * @throws BadMethodCallException If no argument was provided. |
|
71 | - * @throws OutOfRangeException If an unknown key is requested. |
|
72 | - */ |
|
73 | - public function getKey() |
|
74 | - { |
|
75 | - $keys = $this->getKeyArguments(func_get_args()); |
|
76 | - Assert\that($keys)->all()->string()->notEmpty(); |
|
77 | - |
|
78 | - if (! $this->hasKey($keys)) { |
|
79 | - throw new OutOfRangeException( |
|
80 | - sprintf( |
|
81 | - _('The configuration key %1$s does not exist.'), |
|
82 | - implode('->', $keys) |
|
83 | - ) |
|
84 | - ); |
|
85 | - } |
|
86 | - |
|
87 | - $keys = array_reverse($keys); |
|
88 | - $array = $this->getArrayCopy(); |
|
89 | - while (count($keys) > 0) { |
|
90 | - $key = array_pop($keys); |
|
91 | - $array = $array[$key]; |
|
92 | - } |
|
93 | - |
|
94 | - return $array; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Check whether the Config has a specific key. |
|
99 | - * |
|
100 | - * To check a value several levels deep, add the keys for each level as a comma-separated list. |
|
101 | - * |
|
102 | - * @since 0.1.0 |
|
103 | - * @since 0.1.4 Accepts list of keys. |
|
104 | - * |
|
105 | - * @param string ... List of keys. |
|
106 | - * @return bool |
|
107 | - */ |
|
108 | - public function hasKey() |
|
109 | - { |
|
110 | - try { |
|
111 | - $keys = array_reverse($this->getKeyArguments(func_get_args())); |
|
112 | - Assert\thatAll($keys)->string()->notEmpty(); |
|
113 | - |
|
114 | - $array = $this->getArrayCopy(); |
|
115 | - while (count($keys) > 0) { |
|
116 | - $key = array_pop($keys); |
|
117 | - if (! array_key_exists($key, $array)) { |
|
118 | - return false; |
|
119 | - } |
|
120 | - $array = $array[$key]; |
|
121 | - } |
|
122 | - } catch (Exception $exception) { |
|
123 | - return false; |
|
124 | - } |
|
125 | - |
|
126 | - return true; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
131 | - * |
|
132 | - * @since 0.1.4 |
|
133 | - * |
|
134 | - * @return array |
|
135 | - */ |
|
136 | - public function getAll() |
|
137 | - { |
|
138 | - return $this->getArrayCopy(); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Get the an array with all the keys |
|
143 | - * |
|
144 | - * @since 0.1.0 |
|
145 | - * @return array |
|
146 | - */ |
|
147 | - public function getKeys() |
|
148 | - { |
|
149 | - return array_keys((array)$this); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Recursively extract the configuration key arguments from an arbitrary array. |
|
154 | - * |
|
155 | - * @since 0.1.6 |
|
156 | - * |
|
157 | - * @param array $arguments Array as fetched through get_func_args(). |
|
158 | - * @return array Array of strings. |
|
159 | - * @throws BadMethodCallException If no argument was provided. |
|
160 | - */ |
|
161 | - protected function getKeyArguments($arguments) |
|
162 | - { |
|
163 | - Assert\that($arguments)->isArray()->notEmpty(); |
|
164 | - |
|
165 | - $keys = []; |
|
166 | - foreach ($arguments as $argument) { |
|
167 | - if (is_array($argument)) { |
|
168 | - $keys = array_merge($keys, $this->getKeyArguments($argument)); |
|
169 | - } |
|
170 | - if (is_string($argument)) { |
|
171 | - $keys = array_merge($keys, $this->parseKeysString($argument)); |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - return $keys; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Extract individual keys from a delimited string. |
|
180 | - * |
|
181 | - * @since 0.1.6 |
|
182 | - * |
|
183 | - * @param string $keyString Delimited string of keys. |
|
184 | - * @return array Array of key strings. |
|
185 | - */ |
|
186 | - protected function parseKeysString($keyString) |
|
187 | - { |
|
188 | - Assert\that($keyString)->string()->notEmpty(); |
|
189 | - |
|
190 | - // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
|
191 | - $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
|
192 | - |
|
193 | - return (array)explode($this->delimiter[0], $normalizedString); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Validate the Config file. |
|
198 | - * |
|
199 | - * @since 0.1.0 |
|
200 | - * @return boolean |
|
201 | - */ |
|
202 | - abstract public function isValid(); |
|
31 | + /** |
|
32 | + * Array of strings that are used as delimiters to parse configuration keys. |
|
33 | + * |
|
34 | + * @since 0.1.6 |
|
35 | + * |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $delimiter = ['\\', '/', '.']; |
|
39 | + |
|
40 | + /** |
|
41 | + * Instantiate the AbstractConfig object. |
|
42 | + * |
|
43 | + * @since 0.1.0 |
|
44 | + * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
45 | + * |
|
46 | + * @param array $config Array with settings. |
|
47 | + * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
48 | + * configuration keys. Defaults to "\", "/" & ".". |
|
49 | + */ |
|
50 | + public function __construct(array $config, $delimiter = null) |
|
51 | + { |
|
52 | + // Make sure the config entries can be accessed as properties. |
|
53 | + parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
|
54 | + |
|
55 | + if (null !== $delimiter) { |
|
56 | + $this->delimiter = (array)$delimiter; |
|
57 | + } |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Get the value of a specific key. |
|
62 | + * |
|
63 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
64 | + * |
|
65 | + * @since 0.1.0 |
|
66 | + * @since 0.1.4 Accepts list of keys. |
|
67 | + * |
|
68 | + * @param string ... List of keys. |
|
69 | + * @return mixed |
|
70 | + * @throws BadMethodCallException If no argument was provided. |
|
71 | + * @throws OutOfRangeException If an unknown key is requested. |
|
72 | + */ |
|
73 | + public function getKey() |
|
74 | + { |
|
75 | + $keys = $this->getKeyArguments(func_get_args()); |
|
76 | + Assert\that($keys)->all()->string()->notEmpty(); |
|
77 | + |
|
78 | + if (! $this->hasKey($keys)) { |
|
79 | + throw new OutOfRangeException( |
|
80 | + sprintf( |
|
81 | + _('The configuration key %1$s does not exist.'), |
|
82 | + implode('->', $keys) |
|
83 | + ) |
|
84 | + ); |
|
85 | + } |
|
86 | + |
|
87 | + $keys = array_reverse($keys); |
|
88 | + $array = $this->getArrayCopy(); |
|
89 | + while (count($keys) > 0) { |
|
90 | + $key = array_pop($keys); |
|
91 | + $array = $array[$key]; |
|
92 | + } |
|
93 | + |
|
94 | + return $array; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Check whether the Config has a specific key. |
|
99 | + * |
|
100 | + * To check a value several levels deep, add the keys for each level as a comma-separated list. |
|
101 | + * |
|
102 | + * @since 0.1.0 |
|
103 | + * @since 0.1.4 Accepts list of keys. |
|
104 | + * |
|
105 | + * @param string ... List of keys. |
|
106 | + * @return bool |
|
107 | + */ |
|
108 | + public function hasKey() |
|
109 | + { |
|
110 | + try { |
|
111 | + $keys = array_reverse($this->getKeyArguments(func_get_args())); |
|
112 | + Assert\thatAll($keys)->string()->notEmpty(); |
|
113 | + |
|
114 | + $array = $this->getArrayCopy(); |
|
115 | + while (count($keys) > 0) { |
|
116 | + $key = array_pop($keys); |
|
117 | + if (! array_key_exists($key, $array)) { |
|
118 | + return false; |
|
119 | + } |
|
120 | + $array = $array[$key]; |
|
121 | + } |
|
122 | + } catch (Exception $exception) { |
|
123 | + return false; |
|
124 | + } |
|
125 | + |
|
126 | + return true; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
131 | + * |
|
132 | + * @since 0.1.4 |
|
133 | + * |
|
134 | + * @return array |
|
135 | + */ |
|
136 | + public function getAll() |
|
137 | + { |
|
138 | + return $this->getArrayCopy(); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Get the an array with all the keys |
|
143 | + * |
|
144 | + * @since 0.1.0 |
|
145 | + * @return array |
|
146 | + */ |
|
147 | + public function getKeys() |
|
148 | + { |
|
149 | + return array_keys((array)$this); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Recursively extract the configuration key arguments from an arbitrary array. |
|
154 | + * |
|
155 | + * @since 0.1.6 |
|
156 | + * |
|
157 | + * @param array $arguments Array as fetched through get_func_args(). |
|
158 | + * @return array Array of strings. |
|
159 | + * @throws BadMethodCallException If no argument was provided. |
|
160 | + */ |
|
161 | + protected function getKeyArguments($arguments) |
|
162 | + { |
|
163 | + Assert\that($arguments)->isArray()->notEmpty(); |
|
164 | + |
|
165 | + $keys = []; |
|
166 | + foreach ($arguments as $argument) { |
|
167 | + if (is_array($argument)) { |
|
168 | + $keys = array_merge($keys, $this->getKeyArguments($argument)); |
|
169 | + } |
|
170 | + if (is_string($argument)) { |
|
171 | + $keys = array_merge($keys, $this->parseKeysString($argument)); |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + return $keys; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Extract individual keys from a delimited string. |
|
180 | + * |
|
181 | + * @since 0.1.6 |
|
182 | + * |
|
183 | + * @param string $keyString Delimited string of keys. |
|
184 | + * @return array Array of key strings. |
|
185 | + */ |
|
186 | + protected function parseKeysString($keyString) |
|
187 | + { |
|
188 | + Assert\that($keyString)->string()->notEmpty(); |
|
189 | + |
|
190 | + // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
|
191 | + $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
|
192 | + |
|
193 | + return (array)explode($this->delimiter[0], $normalizedString); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Validate the Config file. |
|
198 | + * |
|
199 | + * @since 0.1.0 |
|
200 | + * @return boolean |
|
201 | + */ |
|
202 | + abstract public function isValid(); |
|
203 | 203 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Generic Config contract implementation |
|
4 | - * |
|
5 | - * @package BrightNucleus\Config |
|
6 | - * @author Alain Schlesser <[email protected]> |
|
7 | - * @license GPL-2.0+ |
|
8 | - * @link http://www.brightnucleus.com/ |
|
9 | - * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | - */ |
|
3 | + * Generic Config contract implementation |
|
4 | + * |
|
5 | + * @package BrightNucleus\Config |
|
6 | + * @author Alain Schlesser <[email protected]> |
|
7 | + * @license GPL-2.0+ |
|
8 | + * @link http://www.brightnucleus.com/ |
|
9 | + * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | + */ |
|
11 | 11 | |
12 | 12 | namespace BrightNucleus\Config; |
13 | 13 | |
@@ -31,227 +31,227 @@ discard block |
||
31 | 31 | class Config extends AbstractConfig |
32 | 32 | { |
33 | 33 | |
34 | - /** |
|
35 | - * The schema of the Config file. |
|
36 | - * |
|
37 | - * @var Schema |
|
38 | - */ |
|
39 | - protected $schema; |
|
34 | + /** |
|
35 | + * The schema of the Config file. |
|
36 | + * |
|
37 | + * @var Schema |
|
38 | + */ |
|
39 | + protected $schema; |
|
40 | 40 | |
41 | - /** |
|
42 | - * The Validator class that gets asked to do the validation of the config. |
|
43 | - * |
|
44 | - * @since 0.1.0 |
|
45 | - * |
|
46 | - * @var Validator |
|
47 | - */ |
|
48 | - protected $validator; |
|
41 | + /** |
|
42 | + * The Validator class that gets asked to do the validation of the config. |
|
43 | + * |
|
44 | + * @since 0.1.0 |
|
45 | + * |
|
46 | + * @var Validator |
|
47 | + */ |
|
48 | + protected $validator; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Instantiate the Config object. |
|
52 | - * |
|
53 | - * It accepts either an array with the configuration settings, or a |
|
54 | - * filename pointing to a PHP file it can include. |
|
55 | - * |
|
56 | - * @since 0.1.0 |
|
57 | - * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
58 | - * |
|
59 | - * @param array|string $config Array with settings or filename for the |
|
60 | - * settings file. |
|
61 | - * @param Schema|null $schema Optional. Config that contains default |
|
62 | - * values that can get overwritten. |
|
63 | - * @param Validator|null $validator Optional. Validator class that does the |
|
64 | - * actual validation. |
|
65 | - * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
66 | - * configuration keys. Defaults to "\", "/" & ".". |
|
67 | - * @throws InvalidArgumentException If the config source is not a string or |
|
68 | - * array. |
|
69 | - * @throws RuntimeException If loading of the config source failed. |
|
70 | - * @throws UnexpectedValueException If the config file is not valid. |
|
71 | - */ |
|
72 | - public function __construct( |
|
73 | - $config, |
|
74 | - Schema $schema = null, |
|
75 | - Validator $validator = null, |
|
76 | - $delimiter = null |
|
77 | - ) { |
|
78 | - $this->schema = $schema; |
|
79 | - $this->validator = $validator; |
|
50 | + /** |
|
51 | + * Instantiate the Config object. |
|
52 | + * |
|
53 | + * It accepts either an array with the configuration settings, or a |
|
54 | + * filename pointing to a PHP file it can include. |
|
55 | + * |
|
56 | + * @since 0.1.0 |
|
57 | + * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
58 | + * |
|
59 | + * @param array|string $config Array with settings or filename for the |
|
60 | + * settings file. |
|
61 | + * @param Schema|null $schema Optional. Config that contains default |
|
62 | + * values that can get overwritten. |
|
63 | + * @param Validator|null $validator Optional. Validator class that does the |
|
64 | + * actual validation. |
|
65 | + * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
66 | + * configuration keys. Defaults to "\", "/" & ".". |
|
67 | + * @throws InvalidArgumentException If the config source is not a string or |
|
68 | + * array. |
|
69 | + * @throws RuntimeException If loading of the config source failed. |
|
70 | + * @throws UnexpectedValueException If the config file is not valid. |
|
71 | + */ |
|
72 | + public function __construct( |
|
73 | + $config, |
|
74 | + Schema $schema = null, |
|
75 | + Validator $validator = null, |
|
76 | + $delimiter = null |
|
77 | + ) { |
|
78 | + $this->schema = $schema; |
|
79 | + $this->validator = $validator; |
|
80 | 80 | |
81 | - // Make sure $config is either a string or array. |
|
82 | - if (! (is_string($config) || is_array($config))) { |
|
83 | - throw new InvalidArgumentException( |
|
84 | - sprintf( |
|
85 | - _('Invalid configuration source: %1$s'), |
|
86 | - print_r($config, true) |
|
87 | - ) |
|
88 | - ); |
|
89 | - } |
|
81 | + // Make sure $config is either a string or array. |
|
82 | + if (! (is_string($config) || is_array($config))) { |
|
83 | + throw new InvalidArgumentException( |
|
84 | + sprintf( |
|
85 | + _('Invalid configuration source: %1$s'), |
|
86 | + print_r($config, true) |
|
87 | + ) |
|
88 | + ); |
|
89 | + } |
|
90 | 90 | |
91 | - if (is_string($config)) { |
|
92 | - $config = $this->fetchArrayData($config); |
|
93 | - } |
|
91 | + if (is_string($config)) { |
|
92 | + $config = $this->fetchArrayData($config); |
|
93 | + } |
|
94 | 94 | |
95 | - // Run the $config through the OptionsResolver. |
|
96 | - Assert\that($config)->isArray(); |
|
97 | - $config = $this->resolveOptions($config); |
|
95 | + // Run the $config through the OptionsResolver. |
|
96 | + Assert\that($config)->isArray(); |
|
97 | + $config = $this->resolveOptions($config); |
|
98 | 98 | |
99 | - // Instantiate the parent class. |
|
100 | - try { |
|
101 | - parent::__construct($config, $delimiter); |
|
102 | - } catch (Exception $exception) { |
|
103 | - throw new RuntimeException( |
|
104 | - sprintf( |
|
105 | - _('Could not instantiate the configuration through its parent. Reason: %1$s'), |
|
106 | - $exception->getMessage() |
|
107 | - ) |
|
108 | - ); |
|
109 | - } |
|
99 | + // Instantiate the parent class. |
|
100 | + try { |
|
101 | + parent::__construct($config, $delimiter); |
|
102 | + } catch (Exception $exception) { |
|
103 | + throw new RuntimeException( |
|
104 | + sprintf( |
|
105 | + _('Could not instantiate the configuration through its parent. Reason: %1$s'), |
|
106 | + $exception->getMessage() |
|
107 | + ) |
|
108 | + ); |
|
109 | + } |
|
110 | 110 | |
111 | - // Finally, validate the resulting config. |
|
112 | - if (! $this->isValid()) { |
|
113 | - throw new UnexpectedValueException( |
|
114 | - sprintf( |
|
115 | - _('ConfigInterface file is not valid: %1$s'), |
|
116 | - print_r($config, true) |
|
117 | - ) |
|
118 | - ); |
|
119 | - } |
|
120 | - } |
|
111 | + // Finally, validate the resulting config. |
|
112 | + if (! $this->isValid()) { |
|
113 | + throw new UnexpectedValueException( |
|
114 | + sprintf( |
|
115 | + _('ConfigInterface file is not valid: %1$s'), |
|
116 | + print_r($config, true) |
|
117 | + ) |
|
118 | + ); |
|
119 | + } |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Validate the Config file. |
|
124 | - * |
|
125 | - * @since 0.1.0 |
|
126 | - * |
|
127 | - * @return boolean |
|
128 | - */ |
|
129 | - public function isValid() |
|
130 | - { |
|
131 | - if ($this->validator) { |
|
132 | - return $this->validator->isValid($this); |
|
133 | - } |
|
122 | + /** |
|
123 | + * Validate the Config file. |
|
124 | + * |
|
125 | + * @since 0.1.0 |
|
126 | + * |
|
127 | + * @return boolean |
|
128 | + */ |
|
129 | + public function isValid() |
|
130 | + { |
|
131 | + if ($this->validator) { |
|
132 | + return $this->validator->isValid($this); |
|
133 | + } |
|
134 | 134 | |
135 | - return true; |
|
136 | - } |
|
135 | + return true; |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * Fetch array data from a string pointing to a file. |
|
140 | - * |
|
141 | - * @since 0.1.0 |
|
142 | - * |
|
143 | - * @param string $filename Filename for the settings file. |
|
144 | - * @return array Array with configuration settings. |
|
145 | - * @throws RuntimeException If the config source is a non-existing |
|
146 | - * file. |
|
147 | - * @throws RuntimeException If loading of the config source failed. |
|
148 | - */ |
|
149 | - protected function fetchArrayData($filename) |
|
150 | - { |
|
151 | - try { |
|
152 | - // Assert that $filename is a readable file. |
|
153 | - Assert\that($filename) |
|
154 | - ->notEmpty() |
|
155 | - ->file() |
|
156 | - ->readable(); |
|
138 | + /** |
|
139 | + * Fetch array data from a string pointing to a file. |
|
140 | + * |
|
141 | + * @since 0.1.0 |
|
142 | + * |
|
143 | + * @param string $filename Filename for the settings file. |
|
144 | + * @return array Array with configuration settings. |
|
145 | + * @throws RuntimeException If the config source is a non-existing |
|
146 | + * file. |
|
147 | + * @throws RuntimeException If loading of the config source failed. |
|
148 | + */ |
|
149 | + protected function fetchArrayData($filename) |
|
150 | + { |
|
151 | + try { |
|
152 | + // Assert that $filename is a readable file. |
|
153 | + Assert\that($filename) |
|
154 | + ->notEmpty() |
|
155 | + ->file() |
|
156 | + ->readable(); |
|
157 | 157 | |
158 | - // Try to load the file through PHP's include(). |
|
159 | - // Make sure we don't accidentally create output. |
|
160 | - ob_get_contents(); |
|
161 | - $config = include($filename); |
|
162 | - ob_clean(); |
|
158 | + // Try to load the file through PHP's include(). |
|
159 | + // Make sure we don't accidentally create output. |
|
160 | + ob_get_contents(); |
|
161 | + $config = include($filename); |
|
162 | + ob_clean(); |
|
163 | 163 | |
164 | - // The included should return an array. |
|
165 | - Assert\that($config)->isArray(); |
|
166 | - } catch (Exception $exception) { |
|
167 | - throw new RuntimeException( |
|
168 | - sprintf( |
|
169 | - _('Loading from configuration source %1$s failed. Reason: %2$s'), |
|
170 | - (string)$filename, |
|
171 | - (string)$exception->getMessage() |
|
172 | - ) |
|
173 | - ); |
|
174 | - } |
|
164 | + // The included should return an array. |
|
165 | + Assert\that($config)->isArray(); |
|
166 | + } catch (Exception $exception) { |
|
167 | + throw new RuntimeException( |
|
168 | + sprintf( |
|
169 | + _('Loading from configuration source %1$s failed. Reason: %2$s'), |
|
170 | + (string)$filename, |
|
171 | + (string)$exception->getMessage() |
|
172 | + ) |
|
173 | + ); |
|
174 | + } |
|
175 | 175 | |
176 | - return $config; |
|
177 | - } |
|
176 | + return $config; |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * Process the passed-in defaults and merge them with the new values, while |
|
181 | - * checking that all required options are set. |
|
182 | - * |
|
183 | - * @since 0.1.0 |
|
184 | - * |
|
185 | - * @param array $config Configuration settings to resolve. |
|
186 | - * @return array Resolved configuration settings. |
|
187 | - * @throws UnexpectedValueException If there are errors while resolving the |
|
188 | - * options. |
|
189 | - */ |
|
190 | - protected function resolveOptions($config) |
|
191 | - { |
|
192 | - if (! $this->schema) { |
|
193 | - return $config; |
|
194 | - } |
|
179 | + /** |
|
180 | + * Process the passed-in defaults and merge them with the new values, while |
|
181 | + * checking that all required options are set. |
|
182 | + * |
|
183 | + * @since 0.1.0 |
|
184 | + * |
|
185 | + * @param array $config Configuration settings to resolve. |
|
186 | + * @return array Resolved configuration settings. |
|
187 | + * @throws UnexpectedValueException If there are errors while resolving the |
|
188 | + * options. |
|
189 | + */ |
|
190 | + protected function resolveOptions($config) |
|
191 | + { |
|
192 | + if (! $this->schema) { |
|
193 | + return $config; |
|
194 | + } |
|
195 | 195 | |
196 | - try { |
|
197 | - $resolver = new OptionsResolver(); |
|
198 | - if ($this->configureOptions($resolver)) { |
|
199 | - $config = $resolver->resolve($config); |
|
200 | - } |
|
201 | - } catch (Exception $exception) { |
|
202 | - throw new UnexpectedValueException( |
|
203 | - sprintf( |
|
204 | - _('Error while resolving config options: %1$s'), |
|
205 | - $exception->getMessage() |
|
206 | - ) |
|
207 | - ); |
|
208 | - } |
|
196 | + try { |
|
197 | + $resolver = new OptionsResolver(); |
|
198 | + if ($this->configureOptions($resolver)) { |
|
199 | + $config = $resolver->resolve($config); |
|
200 | + } |
|
201 | + } catch (Exception $exception) { |
|
202 | + throw new UnexpectedValueException( |
|
203 | + sprintf( |
|
204 | + _('Error while resolving config options: %1$s'), |
|
205 | + $exception->getMessage() |
|
206 | + ) |
|
207 | + ); |
|
208 | + } |
|
209 | 209 | |
210 | - return $config; |
|
211 | - } |
|
210 | + return $config; |
|
211 | + } |
|
212 | 212 | |
213 | - /** |
|
214 | - * Configure the possible and required options for the Config. |
|
215 | - * |
|
216 | - * This should return a bool to let the resolve_options() know whether the |
|
217 | - * actual resolving needs to be done or not. |
|
218 | - * |
|
219 | - * @since 0.1.0 |
|
220 | - * |
|
221 | - * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
222 | - * instance. |
|
223 | - * @return bool Whether to do the resolving. |
|
224 | - * @throws UnexpectedValueException If there are errors while processing. |
|
225 | - */ |
|
226 | - protected function configureOptions(OptionsResolver $resolver) |
|
227 | - { |
|
228 | - $defined = $this->schema->getDefinedOptions(); |
|
229 | - $defaults = $this->schema->getDefaultOptions(); |
|
230 | - $required = $this->schema->getRequiredOptions(); |
|
213 | + /** |
|
214 | + * Configure the possible and required options for the Config. |
|
215 | + * |
|
216 | + * This should return a bool to let the resolve_options() know whether the |
|
217 | + * actual resolving needs to be done or not. |
|
218 | + * |
|
219 | + * @since 0.1.0 |
|
220 | + * |
|
221 | + * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
222 | + * instance. |
|
223 | + * @return bool Whether to do the resolving. |
|
224 | + * @throws UnexpectedValueException If there are errors while processing. |
|
225 | + */ |
|
226 | + protected function configureOptions(OptionsResolver $resolver) |
|
227 | + { |
|
228 | + $defined = $this->schema->getDefinedOptions(); |
|
229 | + $defaults = $this->schema->getDefaultOptions(); |
|
230 | + $required = $this->schema->getRequiredOptions(); |
|
231 | 231 | |
232 | - if (! $defined && ! $defaults && ! $required) { |
|
233 | - return false; |
|
234 | - } |
|
232 | + if (! $defined && ! $defaults && ! $required) { |
|
233 | + return false; |
|
234 | + } |
|
235 | 235 | |
236 | - try { |
|
237 | - if ($defined) { |
|
238 | - $resolver->setDefined($defined); |
|
239 | - } |
|
240 | - if ($defaults) { |
|
241 | - $resolver->setDefaults($defaults); |
|
242 | - } |
|
243 | - if ($required) { |
|
244 | - $resolver->setRequired($required); |
|
245 | - } |
|
246 | - } catch (Exception $exception) { |
|
247 | - throw new UnexpectedValueException( |
|
248 | - sprintf( |
|
249 | - _('Error while processing config options: %1$s'), |
|
250 | - $exception->getMessage() |
|
251 | - ) |
|
252 | - ); |
|
253 | - } |
|
236 | + try { |
|
237 | + if ($defined) { |
|
238 | + $resolver->setDefined($defined); |
|
239 | + } |
|
240 | + if ($defaults) { |
|
241 | + $resolver->setDefaults($defaults); |
|
242 | + } |
|
243 | + if ($required) { |
|
244 | + $resolver->setRequired($required); |
|
245 | + } |
|
246 | + } catch (Exception $exception) { |
|
247 | + throw new UnexpectedValueException( |
|
248 | + sprintf( |
|
249 | + _('Error while processing config options: %1$s'), |
|
250 | + $exception->getMessage() |
|
251 | + ) |
|
252 | + ); |
|
253 | + } |
|
254 | 254 | |
255 | - return true; |
|
256 | - } |
|
255 | + return true; |
|
256 | + } |
|
257 | 257 | } |
@@ -1,13 +1,13 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Config Trait |
|
4 | - * |
|
5 | - * @package BrightNucleus\Config |
|
6 | - * @author Alain Schlesser <[email protected]> |
|
7 | - * @license GPL-2.0+ |
|
8 | - * @link http://www.brightnucleus.com/ |
|
9 | - * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | - */ |
|
3 | + * Config Trait |
|
4 | + * |
|
5 | + * @package BrightNucleus\Config |
|
6 | + * @author Alain Schlesser <[email protected]> |
|
7 | + * @license GPL-2.0+ |
|
8 | + * @link http://www.brightnucleus.com/ |
|
9 | + * @copyright 2016 Alain Schlesser, Bright Nucleus |
|
10 | + */ |
|
11 | 11 | |
12 | 12 | namespace BrightNucleus\Config; |
13 | 13 | |
@@ -18,103 +18,103 @@ discard block |
||
18 | 18 | trait ConfigTrait |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * Reference to the Config object. |
|
23 | - * |
|
24 | - * @since 0.1.2 |
|
25 | - * |
|
26 | - * @var ConfigInterface |
|
27 | - */ |
|
28 | - protected $config; |
|
21 | + /** |
|
22 | + * Reference to the Config object. |
|
23 | + * |
|
24 | + * @since 0.1.2 |
|
25 | + * |
|
26 | + * @var ConfigInterface |
|
27 | + */ |
|
28 | + protected $config; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Process the passed-in configuration file. |
|
32 | - * |
|
33 | - * @since 0.1.2 |
|
34 | - * |
|
35 | - * @param ConfigInterface $config The Config to process. |
|
36 | - * @param string ... List of keys. |
|
37 | - * @throws RuntimeException If the arguments could not be parsed into a Config. |
|
38 | - */ |
|
39 | - protected function processConfig(ConfigInterface $config) |
|
40 | - { |
|
41 | - if (func_num_args() > 1) { |
|
42 | - try { |
|
43 | - $keys = func_get_args(); |
|
44 | - array_shift($keys); |
|
45 | - Assert\that($keys)->all()->string()->notEmpty(); |
|
46 | - $config = new Config($config->getKey($keys)); |
|
47 | - } catch (Exception $exception) { |
|
48 | - throw new RuntimeException( |
|
49 | - sprintf( |
|
50 | - _('Could not process the config with the arguments "%1$s".'), |
|
51 | - print_r(func_get_args(), true) |
|
52 | - ) |
|
53 | - ); |
|
54 | - } |
|
55 | - } |
|
56 | - $this->config = $config; |
|
57 | - } |
|
30 | + /** |
|
31 | + * Process the passed-in configuration file. |
|
32 | + * |
|
33 | + * @since 0.1.2 |
|
34 | + * |
|
35 | + * @param ConfigInterface $config The Config to process. |
|
36 | + * @param string ... List of keys. |
|
37 | + * @throws RuntimeException If the arguments could not be parsed into a Config. |
|
38 | + */ |
|
39 | + protected function processConfig(ConfigInterface $config) |
|
40 | + { |
|
41 | + if (func_num_args() > 1) { |
|
42 | + try { |
|
43 | + $keys = func_get_args(); |
|
44 | + array_shift($keys); |
|
45 | + Assert\that($keys)->all()->string()->notEmpty(); |
|
46 | + $config = new Config($config->getKey($keys)); |
|
47 | + } catch (Exception $exception) { |
|
48 | + throw new RuntimeException( |
|
49 | + sprintf( |
|
50 | + _('Could not process the config with the arguments "%1$s".'), |
|
51 | + print_r(func_get_args(), true) |
|
52 | + ) |
|
53 | + ); |
|
54 | + } |
|
55 | + } |
|
56 | + $this->config = $config; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Check whether the Config has a specific key. |
|
61 | - * |
|
62 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
63 | - * |
|
64 | - * @since 0.1.2 |
|
65 | - * @since 0.1.5 Accepts list of keys. |
|
66 | - * |
|
67 | - * @param string ... List of keys. |
|
68 | - * @return bool Whether the key is known. |
|
69 | - */ |
|
70 | - protected function hasConfigKey() |
|
71 | - { |
|
72 | - $keys = func_get_args(); |
|
73 | - Assert\that($keys)->all()->string()->notEmpty(); |
|
59 | + /** |
|
60 | + * Check whether the Config has a specific key. |
|
61 | + * |
|
62 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
63 | + * |
|
64 | + * @since 0.1.2 |
|
65 | + * @since 0.1.5 Accepts list of keys. |
|
66 | + * |
|
67 | + * @param string ... List of keys. |
|
68 | + * @return bool Whether the key is known. |
|
69 | + */ |
|
70 | + protected function hasConfigKey() |
|
71 | + { |
|
72 | + $keys = func_get_args(); |
|
73 | + Assert\that($keys)->all()->string()->notEmpty(); |
|
74 | 74 | |
75 | - return $this->config->hasKey($keys); |
|
76 | - } |
|
75 | + return $this->config->hasKey($keys); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Get the Config value for a specific key. |
|
80 | - * |
|
81 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
82 | - * |
|
83 | - * @since 0.1.2 |
|
84 | - * @since 0.1.5 Accepts list of keys. |
|
85 | - * |
|
86 | - * @param string ... List of keys. |
|
87 | - * @return mixed Value of the key. |
|
88 | - */ |
|
89 | - protected function getConfigKey() |
|
90 | - { |
|
91 | - $keys = func_get_args(); |
|
92 | - Assert\that($keys)->all()->string()->notEmpty(); |
|
78 | + /** |
|
79 | + * Get the Config value for a specific key. |
|
80 | + * |
|
81 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
82 | + * |
|
83 | + * @since 0.1.2 |
|
84 | + * @since 0.1.5 Accepts list of keys. |
|
85 | + * |
|
86 | + * @param string ... List of keys. |
|
87 | + * @return mixed Value of the key. |
|
88 | + */ |
|
89 | + protected function getConfigKey() |
|
90 | + { |
|
91 | + $keys = func_get_args(); |
|
92 | + Assert\that($keys)->all()->string()->notEmpty(); |
|
93 | 93 | |
94 | - return $this->config->getKey($keys); |
|
95 | - } |
|
94 | + return $this->config->getKey($keys); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
99 | - * |
|
100 | - * @since 0.1.4 |
|
101 | - * |
|
102 | - * @return array All the configuration settings. |
|
103 | - */ |
|
104 | - protected function getConfigArray() |
|
105 | - { |
|
106 | - return $this->config->getAll(); |
|
107 | - } |
|
97 | + /** |
|
98 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
99 | + * |
|
100 | + * @since 0.1.4 |
|
101 | + * |
|
102 | + * @return array All the configuration settings. |
|
103 | + */ |
|
104 | + protected function getConfigArray() |
|
105 | + { |
|
106 | + return $this->config->getAll(); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * Get an array of all the keys that are known by the Config. |
|
111 | - * |
|
112 | - * @since 0.1.2 |
|
113 | - * |
|
114 | - * @return array Array of strings containing all the keys. |
|
115 | - */ |
|
116 | - protected function getConfigKeys() |
|
117 | - { |
|
118 | - return $this->config->getKeys(); |
|
119 | - } |
|
109 | + /** |
|
110 | + * Get an array of all the keys that are known by the Config. |
|
111 | + * |
|
112 | + * @since 0.1.2 |
|
113 | + * |
|
114 | + * @return array Array of strings containing all the keys. |
|
115 | + */ |
|
116 | + protected function getConfigKeys() |
|
117 | + { |
|
118 | + return $this->config->getKeys(); |
|
119 | + } |
|
120 | 120 | } |