@@ -26,221 +26,221 @@ |
||
| 26 | 26 | abstract class AbstractConfig extends ArrayObject implements ConfigInterface |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Array of strings that are used as delimiters to parse configuration keys. |
|
| 31 | - * |
|
| 32 | - * @since 0.1.6 |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - protected $delimiter = ['\\', '/', '.']; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Instantiate the AbstractConfig object. |
|
| 40 | - * |
|
| 41 | - * @since 0.1.0 |
|
| 42 | - * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
| 43 | - * |
|
| 44 | - * @param array $config Array with settings. |
|
| 45 | - * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
| 46 | - * configuration keys. Defaults to "\", "/" & ".". |
|
| 47 | - */ |
|
| 48 | - public function __construct(array $config, $delimiter = null) |
|
| 49 | - { |
|
| 50 | - // Make sure the config entries can be accessed as properties. |
|
| 51 | - parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
|
| 52 | - |
|
| 53 | - if (null !== $delimiter) { |
|
| 54 | - $this->delimiter = (array)$delimiter; |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Get the value of a specific key. |
|
| 60 | - * |
|
| 61 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 62 | - * |
|
| 63 | - * @since 0.1.0 |
|
| 64 | - * @since 0.1.4 Accepts list of keys. |
|
| 65 | - * |
|
| 66 | - * @param string|array $_ List of keys. |
|
| 67 | - * |
|
| 68 | - * @return mixed |
|
| 69 | - * @throws KeyNotFoundException If an unknown key is requested. |
|
| 70 | - */ |
|
| 71 | - public function getKey($_) |
|
| 72 | - { |
|
| 73 | - $keys = $this->validateKeys(func_get_args()); |
|
| 74 | - |
|
| 75 | - $keys = array_reverse($keys); |
|
| 76 | - $array = $this->getArrayCopy(); |
|
| 77 | - while (count($keys) > 0) { |
|
| 78 | - $key = array_pop($keys); |
|
| 79 | - $array = $array[$key]; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return $array; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Check whether the Config has a specific key. |
|
| 87 | - * |
|
| 88 | - * To check a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 89 | - * |
|
| 90 | - * @since 0.1.0 |
|
| 91 | - * @since 0.1.4 Accepts list of keys. |
|
| 92 | - * |
|
| 93 | - * @param string|array $_ List of keys. |
|
| 94 | - * |
|
| 95 | - * @return bool |
|
| 96 | - */ |
|
| 97 | - public function hasKey($_) |
|
| 98 | - { |
|
| 99 | - try { |
|
| 100 | - $keys = array_reverse($this->getKeyArguments(func_get_args())); |
|
| 101 | - |
|
| 102 | - $array = $this->getArrayCopy(); |
|
| 103 | - while (count($keys) > 0) { |
|
| 104 | - $key = array_pop($keys); |
|
| 105 | - if (! array_key_exists($key, $array)) { |
|
| 106 | - return false; |
|
| 107 | - } |
|
| 108 | - $array = $array[$key]; |
|
| 109 | - } |
|
| 110 | - } catch (Exception $exception) { |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return true; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
| 119 | - * |
|
| 120 | - * @since 0.1.4 |
|
| 121 | - * |
|
| 122 | - * @return array |
|
| 123 | - */ |
|
| 124 | - public function getAll() |
|
| 125 | - { |
|
| 126 | - return $this->getArrayCopy(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Get the an array with all the keys |
|
| 131 | - * |
|
| 132 | - * @since 0.1.0 |
|
| 133 | - * @return array |
|
| 134 | - */ |
|
| 135 | - public function getKeys() |
|
| 136 | - { |
|
| 137 | - return array_keys((array)$this); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Get a new config at a specific sub-level. |
|
| 142 | - * |
|
| 143 | - * @since 0.1.13 |
|
| 144 | - * |
|
| 145 | - * @param string|array $_ List of keys. |
|
| 146 | - * |
|
| 147 | - * @return ConfigInterface |
|
| 148 | - * @throws KeyNotFoundException If an unknown key is requested. |
|
| 149 | - */ |
|
| 150 | - public function getSubConfig($_) |
|
| 151 | - { |
|
| 152 | - $keys = $this->validateKeys(func_get_args()); |
|
| 153 | - |
|
| 154 | - $subConfig = clone $this; |
|
| 155 | - $subConfig->reduceToSubKey($keys); |
|
| 156 | - |
|
| 157 | - return $subConfig; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Validate a set of keys to make sure they exist. |
|
| 162 | - * |
|
| 163 | - * @since 0.1.13 |
|
| 164 | - * |
|
| 165 | - * @param string|array $_ List of keys. |
|
| 166 | - * |
|
| 167 | - * @return array List of keys. |
|
| 168 | - * @throws KeyNotFoundException If an unknown key is requested. |
|
| 169 | - */ |
|
| 170 | - public function validateKeys($_) |
|
| 171 | - { |
|
| 172 | - $keys = $this->getKeyArguments(func_get_args()); |
|
| 173 | - |
|
| 174 | - if (! $this->hasKey($keys)) { |
|
| 175 | - throw new KeyNotFoundException( |
|
| 176 | - sprintf( |
|
| 177 | - _('The configuration key %1$s does not exist.'), |
|
| 178 | - implode('->', $keys) |
|
| 179 | - ) |
|
| 180 | - ); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return $keys; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Reduce the currently stored config array to a subarray at a specific level. |
|
| 188 | - * |
|
| 189 | - * @since 0.1.13 |
|
| 190 | - * |
|
| 191 | - * @param array $keys Array of keys that point to a key down in the hierarchy. |
|
| 192 | - */ |
|
| 193 | - protected function reduceToSubKey(array $keys) |
|
| 194 | - { |
|
| 195 | - $this->exchangeArray($this->getKey($keys)); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Recursively extract the configuration key arguments from an arbitrary array. |
|
| 200 | - * |
|
| 201 | - * @since 0.1.6 |
|
| 202 | - * |
|
| 203 | - * @param array $arguments Array as fetched through get_func_args(). |
|
| 204 | - * |
|
| 205 | - * @return array Array of strings. |
|
| 206 | - */ |
|
| 207 | - protected function getKeyArguments($arguments) |
|
| 208 | - { |
|
| 209 | - $keys = []; |
|
| 210 | - foreach ($arguments as $argument) { |
|
| 211 | - if (is_array($argument)) { |
|
| 212 | - $keys = array_merge($keys, $this->getKeyArguments($argument)); |
|
| 213 | - } |
|
| 214 | - if (is_string($argument)) { |
|
| 215 | - $keys = array_merge($keys, $this->parseKeysString($argument)); |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - return $keys; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Extract individual keys from a delimited string. |
|
| 224 | - * |
|
| 225 | - * @since 0.1.6 |
|
| 226 | - * |
|
| 227 | - * @param string $keyString Delimited string of keys. |
|
| 228 | - * |
|
| 229 | - * @return array Array of key strings. |
|
| 230 | - */ |
|
| 231 | - protected function parseKeysString($keyString) |
|
| 232 | - { |
|
| 233 | - // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
|
| 234 | - $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
|
| 235 | - |
|
| 236 | - return (array)explode($this->delimiter[0], $normalizedString); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Validate the Config file. |
|
| 241 | - * |
|
| 242 | - * @since 0.1.0 |
|
| 243 | - * @return boolean |
|
| 244 | - */ |
|
| 245 | - abstract public function isValid(); |
|
| 29 | + /** |
|
| 30 | + * Array of strings that are used as delimiters to parse configuration keys. |
|
| 31 | + * |
|
| 32 | + * @since 0.1.6 |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + protected $delimiter = ['\\', '/', '.']; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Instantiate the AbstractConfig object. |
|
| 40 | + * |
|
| 41 | + * @since 0.1.0 |
|
| 42 | + * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
| 43 | + * |
|
| 44 | + * @param array $config Array with settings. |
|
| 45 | + * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
| 46 | + * configuration keys. Defaults to "\", "/" & ".". |
|
| 47 | + */ |
|
| 48 | + public function __construct(array $config, $delimiter = null) |
|
| 49 | + { |
|
| 50 | + // Make sure the config entries can be accessed as properties. |
|
| 51 | + parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
|
| 52 | + |
|
| 53 | + if (null !== $delimiter) { |
|
| 54 | + $this->delimiter = (array)$delimiter; |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Get the value of a specific key. |
|
| 60 | + * |
|
| 61 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 62 | + * |
|
| 63 | + * @since 0.1.0 |
|
| 64 | + * @since 0.1.4 Accepts list of keys. |
|
| 65 | + * |
|
| 66 | + * @param string|array $_ List of keys. |
|
| 67 | + * |
|
| 68 | + * @return mixed |
|
| 69 | + * @throws KeyNotFoundException If an unknown key is requested. |
|
| 70 | + */ |
|
| 71 | + public function getKey($_) |
|
| 72 | + { |
|
| 73 | + $keys = $this->validateKeys(func_get_args()); |
|
| 74 | + |
|
| 75 | + $keys = array_reverse($keys); |
|
| 76 | + $array = $this->getArrayCopy(); |
|
| 77 | + while (count($keys) > 0) { |
|
| 78 | + $key = array_pop($keys); |
|
| 79 | + $array = $array[$key]; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return $array; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Check whether the Config has a specific key. |
|
| 87 | + * |
|
| 88 | + * To check a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 89 | + * |
|
| 90 | + * @since 0.1.0 |
|
| 91 | + * @since 0.1.4 Accepts list of keys. |
|
| 92 | + * |
|
| 93 | + * @param string|array $_ List of keys. |
|
| 94 | + * |
|
| 95 | + * @return bool |
|
| 96 | + */ |
|
| 97 | + public function hasKey($_) |
|
| 98 | + { |
|
| 99 | + try { |
|
| 100 | + $keys = array_reverse($this->getKeyArguments(func_get_args())); |
|
| 101 | + |
|
| 102 | + $array = $this->getArrayCopy(); |
|
| 103 | + while (count($keys) > 0) { |
|
| 104 | + $key = array_pop($keys); |
|
| 105 | + if (! array_key_exists($key, $array)) { |
|
| 106 | + return false; |
|
| 107 | + } |
|
| 108 | + $array = $array[$key]; |
|
| 109 | + } |
|
| 110 | + } catch (Exception $exception) { |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return true; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
| 119 | + * |
|
| 120 | + * @since 0.1.4 |
|
| 121 | + * |
|
| 122 | + * @return array |
|
| 123 | + */ |
|
| 124 | + public function getAll() |
|
| 125 | + { |
|
| 126 | + return $this->getArrayCopy(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Get the an array with all the keys |
|
| 131 | + * |
|
| 132 | + * @since 0.1.0 |
|
| 133 | + * @return array |
|
| 134 | + */ |
|
| 135 | + public function getKeys() |
|
| 136 | + { |
|
| 137 | + return array_keys((array)$this); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Get a new config at a specific sub-level. |
|
| 142 | + * |
|
| 143 | + * @since 0.1.13 |
|
| 144 | + * |
|
| 145 | + * @param string|array $_ List of keys. |
|
| 146 | + * |
|
| 147 | + * @return ConfigInterface |
|
| 148 | + * @throws KeyNotFoundException If an unknown key is requested. |
|
| 149 | + */ |
|
| 150 | + public function getSubConfig($_) |
|
| 151 | + { |
|
| 152 | + $keys = $this->validateKeys(func_get_args()); |
|
| 153 | + |
|
| 154 | + $subConfig = clone $this; |
|
| 155 | + $subConfig->reduceToSubKey($keys); |
|
| 156 | + |
|
| 157 | + return $subConfig; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Validate a set of keys to make sure they exist. |
|
| 162 | + * |
|
| 163 | + * @since 0.1.13 |
|
| 164 | + * |
|
| 165 | + * @param string|array $_ List of keys. |
|
| 166 | + * |
|
| 167 | + * @return array List of keys. |
|
| 168 | + * @throws KeyNotFoundException If an unknown key is requested. |
|
| 169 | + */ |
|
| 170 | + public function validateKeys($_) |
|
| 171 | + { |
|
| 172 | + $keys = $this->getKeyArguments(func_get_args()); |
|
| 173 | + |
|
| 174 | + if (! $this->hasKey($keys)) { |
|
| 175 | + throw new KeyNotFoundException( |
|
| 176 | + sprintf( |
|
| 177 | + _('The configuration key %1$s does not exist.'), |
|
| 178 | + implode('->', $keys) |
|
| 179 | + ) |
|
| 180 | + ); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return $keys; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Reduce the currently stored config array to a subarray at a specific level. |
|
| 188 | + * |
|
| 189 | + * @since 0.1.13 |
|
| 190 | + * |
|
| 191 | + * @param array $keys Array of keys that point to a key down in the hierarchy. |
|
| 192 | + */ |
|
| 193 | + protected function reduceToSubKey(array $keys) |
|
| 194 | + { |
|
| 195 | + $this->exchangeArray($this->getKey($keys)); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Recursively extract the configuration key arguments from an arbitrary array. |
|
| 200 | + * |
|
| 201 | + * @since 0.1.6 |
|
| 202 | + * |
|
| 203 | + * @param array $arguments Array as fetched through get_func_args(). |
|
| 204 | + * |
|
| 205 | + * @return array Array of strings. |
|
| 206 | + */ |
|
| 207 | + protected function getKeyArguments($arguments) |
|
| 208 | + { |
|
| 209 | + $keys = []; |
|
| 210 | + foreach ($arguments as $argument) { |
|
| 211 | + if (is_array($argument)) { |
|
| 212 | + $keys = array_merge($keys, $this->getKeyArguments($argument)); |
|
| 213 | + } |
|
| 214 | + if (is_string($argument)) { |
|
| 215 | + $keys = array_merge($keys, $this->parseKeysString($argument)); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + return $keys; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Extract individual keys from a delimited string. |
|
| 224 | + * |
|
| 225 | + * @since 0.1.6 |
|
| 226 | + * |
|
| 227 | + * @param string $keyString Delimited string of keys. |
|
| 228 | + * |
|
| 229 | + * @return array Array of key strings. |
|
| 230 | + */ |
|
| 231 | + protected function parseKeysString($keyString) |
|
| 232 | + { |
|
| 233 | + // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
|
| 234 | + $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
|
| 235 | + |
|
| 236 | + return (array)explode($this->delimiter[0], $normalizedString); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Validate the Config file. |
|
| 241 | + * |
|
| 242 | + * @since 0.1.0 |
|
| 243 | + * @return boolean |
|
| 244 | + */ |
|
| 245 | + abstract public function isValid(); |
|
| 246 | 246 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
| 52 | 52 | |
| 53 | 53 | if (null !== $delimiter) { |
| 54 | - $this->delimiter = (array)$delimiter; |
|
| 54 | + $this->delimiter = (array) $delimiter; |
|
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | 57 | |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | $array = $this->getArrayCopy(); |
| 103 | 103 | while (count($keys) > 0) { |
| 104 | 104 | $key = array_pop($keys); |
| 105 | - if (! array_key_exists($key, $array)) { |
|
| 105 | + if ( ! array_key_exists($key, $array)) { |
|
| 106 | 106 | return false; |
| 107 | 107 | } |
| 108 | 108 | $array = $array[$key]; |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | */ |
| 135 | 135 | public function getKeys() |
| 136 | 136 | { |
| 137 | - return array_keys((array)$this); |
|
| 137 | + return array_keys((array) $this); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | { |
| 172 | 172 | $keys = $this->getKeyArguments(func_get_args()); |
| 173 | 173 | |
| 174 | - if (! $this->hasKey($keys)) { |
|
| 174 | + if ( ! $this->hasKey($keys)) { |
|
| 175 | 175 | throw new KeyNotFoundException( |
| 176 | 176 | sprintf( |
| 177 | 177 | _('The configuration key %1$s does not exist.'), |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
| 234 | 234 | $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
| 235 | 235 | |
| 236 | - return (array)explode($this->delimiter[0], $normalizedString); |
|
| 236 | + return (array) explode($this->delimiter[0], $normalizedString); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | /** |
@@ -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 | } |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | $data = json_decode(file_get_contents($uri), true); |
| 59 | 59 | ob_end_clean(); |
| 60 | 60 | |
| 61 | - return (array)$data; |
|
| 61 | + return (array) $data; |
|
| 62 | 62 | } catch (Exception $exception) { |
| 63 | 63 | throw new FailedToLoadConfigException( |
| 64 | 64 | sprintf( |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | */ |
| 85 | 85 | protected function validateUri($uri) |
| 86 | 86 | { |
| 87 | - if (! is_readable($uri)) { |
|
| 87 | + if ( ! is_readable($uri)) { |
|
| 88 | 88 | throw new FailedToLoadConfigException( |
| 89 | 89 | sprintf( |
| 90 | 90 | _('The requested JSON config file "%1$s" does not exist or is not readable.'), |
@@ -25,74 +25,74 @@ |
||
| 25 | 25 | class JSONLoader extends AbstractLoader |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Check whether the loader is able to load a given URI. |
|
| 30 | - * |
|
| 31 | - * @since 0.4.0 |
|
| 32 | - * |
|
| 33 | - * @param string $uri URI to check. |
|
| 34 | - * |
|
| 35 | - * @return bool Whether the loader can load the given URI. |
|
| 36 | - */ |
|
| 37 | - public static function canLoad($uri) |
|
| 38 | - { |
|
| 39 | - $path = pathinfo($uri); |
|
| 28 | + /** |
|
| 29 | + * Check whether the loader is able to load a given URI. |
|
| 30 | + * |
|
| 31 | + * @since 0.4.0 |
|
| 32 | + * |
|
| 33 | + * @param string $uri URI to check. |
|
| 34 | + * |
|
| 35 | + * @return bool Whether the loader can load the given URI. |
|
| 36 | + */ |
|
| 37 | + public static function canLoad($uri) |
|
| 38 | + { |
|
| 39 | + $path = pathinfo($uri); |
|
| 40 | 40 | |
| 41 | - return 'json' === mb_strtolower($path['extension']); |
|
| 42 | - } |
|
| 41 | + return 'json' === mb_strtolower($path['extension']); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Load the contents of an resource identified by an URI. |
|
| 46 | - * |
|
| 47 | - * @since 0.4.0 |
|
| 48 | - * |
|
| 49 | - * @param string $uri URI of the resource. |
|
| 50 | - * |
|
| 51 | - * @return array|null Raw data loaded from the resource. Null if no data found. |
|
| 52 | - * @throws FailedToLoadConfigException If the resource could not be loaded. |
|
| 53 | - */ |
|
| 54 | - protected function loadUri($uri) |
|
| 55 | - { |
|
| 56 | - try { |
|
| 57 | - ob_start(); |
|
| 58 | - $data = json_decode(file_get_contents($uri), true); |
|
| 59 | - ob_end_clean(); |
|
| 44 | + /** |
|
| 45 | + * Load the contents of an resource identified by an URI. |
|
| 46 | + * |
|
| 47 | + * @since 0.4.0 |
|
| 48 | + * |
|
| 49 | + * @param string $uri URI of the resource. |
|
| 50 | + * |
|
| 51 | + * @return array|null Raw data loaded from the resource. Null if no data found. |
|
| 52 | + * @throws FailedToLoadConfigException If the resource could not be loaded. |
|
| 53 | + */ |
|
| 54 | + protected function loadUri($uri) |
|
| 55 | + { |
|
| 56 | + try { |
|
| 57 | + ob_start(); |
|
| 58 | + $data = json_decode(file_get_contents($uri), true); |
|
| 59 | + ob_end_clean(); |
|
| 60 | 60 | |
| 61 | - return (array)$data; |
|
| 62 | - } catch (Exception $exception) { |
|
| 63 | - throw new FailedToLoadConfigException( |
|
| 64 | - sprintf( |
|
| 65 | - _('Could not include JSON config file "%1$s". Reason: "%2$s".'), |
|
| 66 | - $uri, |
|
| 67 | - $exception->getMessage() |
|
| 68 | - ), |
|
| 69 | - $exception->getCode(), |
|
| 70 | - $exception |
|
| 71 | - ); |
|
| 72 | - } |
|
| 73 | - } |
|
| 61 | + return (array)$data; |
|
| 62 | + } catch (Exception $exception) { |
|
| 63 | + throw new FailedToLoadConfigException( |
|
| 64 | + sprintf( |
|
| 65 | + _('Could not include JSON config file "%1$s". Reason: "%2$s".'), |
|
| 66 | + $uri, |
|
| 67 | + $exception->getMessage() |
|
| 68 | + ), |
|
| 69 | + $exception->getCode(), |
|
| 70 | + $exception |
|
| 71 | + ); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Validate and return the URI. |
|
| 77 | - * |
|
| 78 | - * @since 0.4.0 |
|
| 79 | - * |
|
| 80 | - * @param string $uri URI of the resource to load. |
|
| 81 | - * |
|
| 82 | - * @return string Validated URI. |
|
| 83 | - * @throws FailedToLoadConfigException If the URI does not exist or is not readable. |
|
| 84 | - */ |
|
| 85 | - protected function validateUri($uri) |
|
| 86 | - { |
|
| 87 | - if (! is_readable($uri)) { |
|
| 88 | - throw new FailedToLoadConfigException( |
|
| 89 | - sprintf( |
|
| 90 | - _('The requested JSON config file "%1$s" does not exist or is not readable.'), |
|
| 91 | - $uri |
|
| 92 | - ) |
|
| 93 | - ); |
|
| 94 | - } |
|
| 75 | + /** |
|
| 76 | + * Validate and return the URI. |
|
| 77 | + * |
|
| 78 | + * @since 0.4.0 |
|
| 79 | + * |
|
| 80 | + * @param string $uri URI of the resource to load. |
|
| 81 | + * |
|
| 82 | + * @return string Validated URI. |
|
| 83 | + * @throws FailedToLoadConfigException If the URI does not exist or is not readable. |
|
| 84 | + */ |
|
| 85 | + protected function validateUri($uri) |
|
| 86 | + { |
|
| 87 | + if (! is_readable($uri)) { |
|
| 88 | + throw new FailedToLoadConfigException( |
|
| 89 | + sprintf( |
|
| 90 | + _('The requested JSON config file "%1$s" does not exist or is not readable.'), |
|
| 91 | + $uri |
|
| 92 | + ) |
|
| 93 | + ); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - return $uri; |
|
| 97 | - } |
|
| 96 | + return $uri; |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -22,26 +22,26 @@ |
||
| 22 | 22 | interface LoaderInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Check whether the loader is able to load a given URI. |
|
| 27 | - * |
|
| 28 | - * @since 0.4.0 |
|
| 29 | - * |
|
| 30 | - * @param string $uri URI to check. |
|
| 31 | - * |
|
| 32 | - * @return bool Whether the loader can load the given URI. |
|
| 33 | - */ |
|
| 34 | - public static function canLoad($uri); |
|
| 25 | + /** |
|
| 26 | + * Check whether the loader is able to load a given URI. |
|
| 27 | + * |
|
| 28 | + * @since 0.4.0 |
|
| 29 | + * |
|
| 30 | + * @param string $uri URI to check. |
|
| 31 | + * |
|
| 32 | + * @return bool Whether the loader can load the given URI. |
|
| 33 | + */ |
|
| 34 | + public static function canLoad($uri); |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Load the configuration from an URI. |
|
| 38 | - * |
|
| 39 | - * @since 0.4.0 |
|
| 40 | - * |
|
| 41 | - * @param string $uri URI of the resource to load. |
|
| 42 | - * |
|
| 43 | - * @return array|null Data contained within the resource. Null if no data could be loaded/parsed. |
|
| 44 | - * @throws FailedToLoadConfigException If the configuration could not be loaded. |
|
| 45 | - */ |
|
| 46 | - public function load($uri); |
|
| 36 | + /** |
|
| 37 | + * Load the configuration from an URI. |
|
| 38 | + * |
|
| 39 | + * @since 0.4.0 |
|
| 40 | + * |
|
| 41 | + * @param string $uri URI of the resource to load. |
|
| 42 | + * |
|
| 43 | + * @return array|null Data contained within the resource. Null if no data could be loaded/parsed. |
|
| 44 | + * @throws FailedToLoadConfigException If the configuration could not be loaded. |
|
| 45 | + */ |
|
| 46 | + public function load($uri); |
|
| 47 | 47 | } |
@@ -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; |