@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function getDefinedOptions() |
| 54 | 54 | { |
| 55 | - if (! $this->defined) { |
|
| 55 | + if ( ! $this->defined) { |
|
| 56 | 56 | return null; |
| 57 | 57 | } |
| 58 | 58 | |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | return $this->defined->getArrayCopy(); |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - return (array)$this->defined; |
|
| 63 | + return (array) $this->defined; |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | public function getDefaultOptions() |
| 74 | 74 | { |
| 75 | - if (! $this->defaults) { |
|
| 75 | + if ( ! $this->defaults) { |
|
| 76 | 76 | return null; |
| 77 | 77 | } |
| 78 | 78 | |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | return $this->defaults->getArrayCopy(); |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - return (array)$this->defaults; |
|
| 83 | + return (array) $this->defaults; |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | public function getRequiredOptions() |
| 94 | 94 | { |
| 95 | - if (! $this->required) { |
|
| 95 | + if ( ! $this->required) { |
|
| 96 | 96 | return null; |
| 97 | 97 | } |
| 98 | 98 | |
@@ -100,6 +100,6 @@ discard block |
||
| 100 | 100 | return $this->required->getArrayCopy(); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - return (array)$this->required; |
|
| 103 | + return (array) $this->required; |
|
| 104 | 104 | } |
| 105 | 105 | } |
@@ -22,84 +22,84 @@ |
||
| 22 | 22 | abstract class AbstractConfigSchema implements ConfigSchemaInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The defined values that are recognized. |
|
| 27 | - * |
|
| 28 | - * @var ConfigInterface |
|
| 29 | - */ |
|
| 30 | - protected $defined; |
|
| 25 | + /** |
|
| 26 | + * The defined values that are recognized. |
|
| 27 | + * |
|
| 28 | + * @var ConfigInterface |
|
| 29 | + */ |
|
| 30 | + protected $defined; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The default values that can be overwritten. |
|
| 34 | - * |
|
| 35 | - * @var ConfigInterface |
|
| 36 | - */ |
|
| 37 | - protected $defaults; |
|
| 32 | + /** |
|
| 33 | + * The default values that can be overwritten. |
|
| 34 | + * |
|
| 35 | + * @var ConfigInterface |
|
| 36 | + */ |
|
| 37 | + protected $defaults; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * The required values that need to be set. |
|
| 41 | - * |
|
| 42 | - * @var ConfigInterface |
|
| 43 | - */ |
|
| 44 | - protected $required; |
|
| 39 | + /** |
|
| 40 | + * The required values that need to be set. |
|
| 41 | + * |
|
| 42 | + * @var ConfigInterface |
|
| 43 | + */ |
|
| 44 | + protected $required; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Get the set of defined options. |
|
| 48 | - * |
|
| 49 | - * @since 0.1.0 |
|
| 50 | - * |
|
| 51 | - * @return array|null |
|
| 52 | - */ |
|
| 53 | - public function getDefinedOptions() |
|
| 54 | - { |
|
| 55 | - if (! $this->defined) { |
|
| 56 | - return null; |
|
| 57 | - } |
|
| 46 | + /** |
|
| 47 | + * Get the set of defined options. |
|
| 48 | + * |
|
| 49 | + * @since 0.1.0 |
|
| 50 | + * |
|
| 51 | + * @return array|null |
|
| 52 | + */ |
|
| 53 | + public function getDefinedOptions() |
|
| 54 | + { |
|
| 55 | + if (! $this->defined) { |
|
| 56 | + return null; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - if ($this->defined instanceof ConfigInterface) { |
|
| 60 | - return $this->defined->getArrayCopy(); |
|
| 61 | - } |
|
| 59 | + if ($this->defined instanceof ConfigInterface) { |
|
| 60 | + return $this->defined->getArrayCopy(); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - return (array)$this->defined; |
|
| 64 | - } |
|
| 63 | + return (array)$this->defined; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Get the set of default options. |
|
| 68 | - * |
|
| 69 | - * @since 0.1.0 |
|
| 70 | - * |
|
| 71 | - * @return array|null |
|
| 72 | - */ |
|
| 73 | - public function getDefaultOptions() |
|
| 74 | - { |
|
| 75 | - if (! $this->defaults) { |
|
| 76 | - return null; |
|
| 77 | - } |
|
| 66 | + /** |
|
| 67 | + * Get the set of default options. |
|
| 68 | + * |
|
| 69 | + * @since 0.1.0 |
|
| 70 | + * |
|
| 71 | + * @return array|null |
|
| 72 | + */ |
|
| 73 | + public function getDefaultOptions() |
|
| 74 | + { |
|
| 75 | + if (! $this->defaults) { |
|
| 76 | + return null; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - if ($this->defaults instanceof ConfigInterface) { |
|
| 80 | - return $this->defaults->getArrayCopy(); |
|
| 81 | - } |
|
| 79 | + if ($this->defaults instanceof ConfigInterface) { |
|
| 80 | + return $this->defaults->getArrayCopy(); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - return (array)$this->defaults; |
|
| 84 | - } |
|
| 83 | + return (array)$this->defaults; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Get the set of required options. |
|
| 88 | - * |
|
| 89 | - * @since 0.1.0 |
|
| 90 | - * |
|
| 91 | - * @return array|null |
|
| 92 | - */ |
|
| 93 | - public function getRequiredOptions() |
|
| 94 | - { |
|
| 95 | - if (! $this->required) { |
|
| 96 | - return null; |
|
| 97 | - } |
|
| 86 | + /** |
|
| 87 | + * Get the set of required options. |
|
| 88 | + * |
|
| 89 | + * @since 0.1.0 |
|
| 90 | + * |
|
| 91 | + * @return array|null |
|
| 92 | + */ |
|
| 93 | + public function getRequiredOptions() |
|
| 94 | + { |
|
| 95 | + if (! $this->required) { |
|
| 96 | + return null; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - if ($this->required instanceof ConfigInterface) { |
|
| 100 | - return $this->required->getArrayCopy(); |
|
| 101 | - } |
|
| 99 | + if ($this->required instanceof ConfigInterface) { |
|
| 100 | + return $this->required->getArrayCopy(); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - return (array)$this->required; |
|
| 104 | - } |
|
| 103 | + return (array)$this->required; |
|
| 104 | + } |
|
| 105 | 105 | } |
@@ -63,7 +63,7 @@ |
||
| 63 | 63 | $schema = $schema->getArrayCopy(); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - if (! is_array($schema)) { |
|
| 66 | + if ( ! is_array($schema)) { |
|
| 67 | 67 | throw new InvalidArgumentException( |
| 68 | 68 | sprintf( |
| 69 | 69 | _('Invalid schema source: %1$s'), |
@@ -24,135 +24,135 @@ |
||
| 24 | 24 | class ConfigSchema extends AbstractConfigSchema |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * The key that is used in the schema to define a default value. |
|
| 29 | - */ |
|
| 30 | - const DEFAULT_VALUE = 'default'; |
|
| 31 | - /** |
|
| 32 | - * The key that is used in the schema to define a required value. |
|
| 33 | - */ |
|
| 34 | - const REQUIRED_KEY = 'required'; |
|
| 27 | + /** |
|
| 28 | + * The key that is used in the schema to define a default value. |
|
| 29 | + */ |
|
| 30 | + const DEFAULT_VALUE = 'default'; |
|
| 31 | + /** |
|
| 32 | + * The key that is used in the schema to define a required value. |
|
| 33 | + */ |
|
| 34 | + const REQUIRED_KEY = 'required'; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Instantiate a ConfigSchema object. |
|
| 38 | - * |
|
| 39 | - * @since 0.1.0 |
|
| 40 | - * |
|
| 41 | - * @param ConfigInterface|array $schema The schema to parse. |
|
| 42 | - * |
|
| 43 | - * @throws InvalidArgumentException |
|
| 44 | - */ |
|
| 45 | - public function __construct($schema) |
|
| 46 | - { |
|
| 47 | - if ($schema instanceof ConfigInterface) { |
|
| 48 | - $schema = $schema->getArrayCopy(); |
|
| 49 | - } |
|
| 36 | + /** |
|
| 37 | + * Instantiate a ConfigSchema object. |
|
| 38 | + * |
|
| 39 | + * @since 0.1.0 |
|
| 40 | + * |
|
| 41 | + * @param ConfigInterface|array $schema The schema to parse. |
|
| 42 | + * |
|
| 43 | + * @throws InvalidArgumentException |
|
| 44 | + */ |
|
| 45 | + public function __construct($schema) |
|
| 46 | + { |
|
| 47 | + if ($schema instanceof ConfigInterface) { |
|
| 48 | + $schema = $schema->getArrayCopy(); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - if (! is_array($schema)) { |
|
| 52 | - throw new InvalidArgumentException( |
|
| 53 | - sprintf( |
|
| 54 | - _('Invalid schema source: %1$s'), |
|
| 55 | - print_r($schema, true) |
|
| 56 | - ) |
|
| 57 | - ); |
|
| 58 | - } |
|
| 51 | + if (! is_array($schema)) { |
|
| 52 | + throw new InvalidArgumentException( |
|
| 53 | + sprintf( |
|
| 54 | + _('Invalid schema source: %1$s'), |
|
| 55 | + print_r($schema, true) |
|
| 56 | + ) |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - array_walk($schema, [$this, 'parseSchema']); |
|
| 61 | - } |
|
| 60 | + array_walk($schema, [$this, 'parseSchema']); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Parse a single provided schema entry. |
|
| 65 | - * |
|
| 66 | - * @since 0.1.0 |
|
| 67 | - * |
|
| 68 | - * @param mixed $data The data associated with the key. |
|
| 69 | - * @param string $key The key of the schema data. |
|
| 70 | - */ |
|
| 71 | - protected function parseSchema($data, $key) |
|
| 72 | - { |
|
| 73 | - $this->parseDefined($key); |
|
| 63 | + /** |
|
| 64 | + * Parse a single provided schema entry. |
|
| 65 | + * |
|
| 66 | + * @since 0.1.0 |
|
| 67 | + * |
|
| 68 | + * @param mixed $data The data associated with the key. |
|
| 69 | + * @param string $key The key of the schema data. |
|
| 70 | + */ |
|
| 71 | + protected function parseSchema($data, $key) |
|
| 72 | + { |
|
| 73 | + $this->parseDefined($key); |
|
| 74 | 74 | |
| 75 | - if (array_key_exists(self::REQUIRED_KEY, $data)) { |
|
| 76 | - $this->parseRequired( |
|
| 77 | - $key, |
|
| 78 | - $data[self::REQUIRED_KEY] |
|
| 79 | - ); |
|
| 80 | - } |
|
| 75 | + if (array_key_exists(self::REQUIRED_KEY, $data)) { |
|
| 76 | + $this->parseRequired( |
|
| 77 | + $key, |
|
| 78 | + $data[self::REQUIRED_KEY] |
|
| 79 | + ); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - if (array_key_exists(self::DEFAULT_VALUE, $data)) { |
|
| 83 | - $this->parseDefault( |
|
| 84 | - $key, |
|
| 85 | - $data[self::DEFAULT_VALUE] |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - } |
|
| 82 | + if (array_key_exists(self::DEFAULT_VALUE, $data)) { |
|
| 83 | + $this->parseDefault( |
|
| 84 | + $key, |
|
| 85 | + $data[self::DEFAULT_VALUE] |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Parse the set of defined values. |
|
| 92 | - * |
|
| 93 | - * @since 0.1.0 |
|
| 94 | - * |
|
| 95 | - * @param string $key The key of the schema data. |
|
| 96 | - */ |
|
| 97 | - protected function parseDefined($key) |
|
| 98 | - { |
|
| 99 | - $this->defined[] = $key; |
|
| 100 | - } |
|
| 90 | + /** |
|
| 91 | + * Parse the set of defined values. |
|
| 92 | + * |
|
| 93 | + * @since 0.1.0 |
|
| 94 | + * |
|
| 95 | + * @param string $key The key of the schema data. |
|
| 96 | + */ |
|
| 97 | + protected function parseDefined($key) |
|
| 98 | + { |
|
| 99 | + $this->defined[] = $key; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Parse the set of required values. |
|
| 104 | - * |
|
| 105 | - * @since 0.1.0 |
|
| 106 | - * |
|
| 107 | - * @param string $key The key of the schema data. |
|
| 108 | - * @param mixed $data The data associated with the key. |
|
| 109 | - */ |
|
| 110 | - protected function parseRequired($key, $data) |
|
| 111 | - { |
|
| 112 | - if ($this->isTruthy($data)) { |
|
| 113 | - $this->required[] = $key; |
|
| 114 | - } |
|
| 115 | - } |
|
| 102 | + /** |
|
| 103 | + * Parse the set of required values. |
|
| 104 | + * |
|
| 105 | + * @since 0.1.0 |
|
| 106 | + * |
|
| 107 | + * @param string $key The key of the schema data. |
|
| 108 | + * @param mixed $data The data associated with the key. |
|
| 109 | + */ |
|
| 110 | + protected function parseRequired($key, $data) |
|
| 111 | + { |
|
| 112 | + if ($this->isTruthy($data)) { |
|
| 113 | + $this->required[] = $key; |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Parse the set of default values. |
|
| 119 | - * |
|
| 120 | - * @since 0.1.0 |
|
| 121 | - * |
|
| 122 | - * @param string $key The key of the schema data. |
|
| 123 | - * @param mixed $data The data associated with the key. |
|
| 124 | - */ |
|
| 125 | - protected function parseDefault($key, $data) |
|
| 126 | - { |
|
| 127 | - $this->defaults[$key] = $data; |
|
| 128 | - } |
|
| 117 | + /** |
|
| 118 | + * Parse the set of default values. |
|
| 119 | + * |
|
| 120 | + * @since 0.1.0 |
|
| 121 | + * |
|
| 122 | + * @param string $key The key of the schema data. |
|
| 123 | + * @param mixed $data The data associated with the key. |
|
| 124 | + */ |
|
| 125 | + protected function parseDefault($key, $data) |
|
| 126 | + { |
|
| 127 | + $this->defaults[$key] = $data; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Return a boolean true or false for an arbitrary set of data. Recognizes |
|
| 132 | - * several different string values that should be valued as true. |
|
| 133 | - * |
|
| 134 | - * @since 0.1.0 |
|
| 135 | - * |
|
| 136 | - * @param mixed $data The data to evaluate. |
|
| 137 | - * |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - protected function isTruthy($data) |
|
| 141 | - { |
|
| 142 | - $truthy_values = [ |
|
| 143 | - true, |
|
| 144 | - 1, |
|
| 145 | - 'true', |
|
| 146 | - 'True', |
|
| 147 | - 'TRUE', |
|
| 148 | - 'y', |
|
| 149 | - 'Y', |
|
| 150 | - 'yes', |
|
| 151 | - 'Yes', |
|
| 152 | - 'YES', |
|
| 153 | - '√', |
|
| 154 | - ]; |
|
| 130 | + /** |
|
| 131 | + * Return a boolean true or false for an arbitrary set of data. Recognizes |
|
| 132 | + * several different string values that should be valued as true. |
|
| 133 | + * |
|
| 134 | + * @since 0.1.0 |
|
| 135 | + * |
|
| 136 | + * @param mixed $data The data to evaluate. |
|
| 137 | + * |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + protected function isTruthy($data) |
|
| 141 | + { |
|
| 142 | + $truthy_values = [ |
|
| 143 | + true, |
|
| 144 | + 1, |
|
| 145 | + 'true', |
|
| 146 | + 'True', |
|
| 147 | + 'TRUE', |
|
| 148 | + 'y', |
|
| 149 | + 'Y', |
|
| 150 | + 'yes', |
|
| 151 | + 'Yes', |
|
| 152 | + 'YES', |
|
| 153 | + '√', |
|
| 154 | + ]; |
|
| 155 | 155 | |
| 156 | - return in_array($data, $truthy_values, true); |
|
| 157 | - } |
|
| 156 | + return in_array($data, $truthy_values, true); |
|
| 157 | + } |
|
| 158 | 158 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
| 54 | 54 | |
| 55 | 55 | if (null !== $delimiter) { |
| 56 | - $this->delimiter = (array)$delimiter; |
|
| 56 | + $this->delimiter = (array) $delimiter; |
|
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | $array = $this->getArrayCopy(); |
| 104 | 104 | while (count($keys) > 0) { |
| 105 | 105 | $key = array_pop($keys); |
| 106 | - if (! array_key_exists($key, $array)) { |
|
| 106 | + if ( ! array_key_exists($key, $array)) { |
|
| 107 | 107 | return false; |
| 108 | 108 | } |
| 109 | 109 | $array = $array[$key]; |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | */ |
| 136 | 136 | public function getKeys() |
| 137 | 137 | { |
| 138 | - return array_keys((array)$this); |
|
| 138 | + return array_keys((array) $this); |
|
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | /** |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | |
| 175 | 175 | Assert\that($keys)->all()->string()->notEmpty(); |
| 176 | 176 | |
| 177 | - if (! $this->hasKey($keys)) { |
|
| 177 | + if ( ! $this->hasKey($keys)) { |
|
| 178 | 178 | throw new OutOfRangeException( |
| 179 | 179 | sprintf( |
| 180 | 180 | _('The configuration key %1$s does not exist.'), |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
| 240 | 240 | $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
| 241 | 241 | |
| 242 | - return (array)explode($this->delimiter[0], $normalizedString); |
|
| 242 | + return (array) explode($this->delimiter[0], $normalizedString); |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | /** |
@@ -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 | } |
@@ -22,14 +22,14 @@ |
||
| 22 | 22 | interface ConfigValidatorInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Check whether the passed-in Config is valid. |
|
| 27 | - * |
|
| 28 | - * @since 0.1.0 |
|
| 29 | - * |
|
| 30 | - * @param ConfigInterface $config |
|
| 31 | - * |
|
| 32 | - * @return bool |
|
| 33 | - */ |
|
| 34 | - public function isValid(ConfigInterface $config); |
|
| 25 | + /** |
|
| 26 | + * Check whether the passed-in Config is valid. |
|
| 27 | + * |
|
| 28 | + * @since 0.1.0 |
|
| 29 | + * |
|
| 30 | + * @param ConfigInterface $config |
|
| 31 | + * |
|
| 32 | + * @return bool |
|
| 33 | + */ |
|
| 34 | + public function isValid(ConfigInterface $config); |
|
| 35 | 35 | } |
@@ -22,30 +22,30 @@ |
||
| 22 | 22 | interface ConfigSchemaInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Get the set of defined options. |
|
| 27 | - * |
|
| 28 | - * @since 0.1.0 |
|
| 29 | - * |
|
| 30 | - * @return array|null |
|
| 31 | - */ |
|
| 32 | - public function getDefinedOptions(); |
|
| 25 | + /** |
|
| 26 | + * Get the set of defined options. |
|
| 27 | + * |
|
| 28 | + * @since 0.1.0 |
|
| 29 | + * |
|
| 30 | + * @return array|null |
|
| 31 | + */ |
|
| 32 | + public function getDefinedOptions(); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Get the set of default options. |
|
| 36 | - * |
|
| 37 | - * @since 0.1.0 |
|
| 38 | - * |
|
| 39 | - * @return array|null |
|
| 40 | - */ |
|
| 41 | - public function getDefaultOptions(); |
|
| 34 | + /** |
|
| 35 | + * Get the set of default options. |
|
| 36 | + * |
|
| 37 | + * @since 0.1.0 |
|
| 38 | + * |
|
| 39 | + * @return array|null |
|
| 40 | + */ |
|
| 41 | + public function getDefaultOptions(); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Get the set of required options. |
|
| 45 | - * |
|
| 46 | - * @since 0.1.0 |
|
| 47 | - * |
|
| 48 | - * @return array|null |
|
| 49 | - */ |
|
| 50 | - public function getRequiredOptions(); |
|
| 43 | + /** |
|
| 44 | + * Get the set of required options. |
|
| 45 | + * |
|
| 46 | + * @since 0.1.0 |
|
| 47 | + * |
|
| 48 | + * @return array|null |
|
| 49 | + */ |
|
| 50 | + public function getRequiredOptions(); |
|
| 51 | 51 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @param string|array $_ List of files. |
| 36 | 36 | * |
| 37 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
| 37 | + * @return Config|null Instance of a ConfigInterface implementation. |
|
| 38 | 38 | */ |
| 39 | 39 | public static function createFromFile($_) |
| 40 | 40 | { |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * |
| 77 | 77 | * @param array $array Array with configuration values. |
| 78 | 78 | * |
| 79 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
| 79 | + * @return Config|null Instance of a ConfigInterface implementation. |
|
| 80 | 80 | */ |
| 81 | 81 | public static function createFromArray(array $array) |
| 82 | 82 | { |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @param mixed $_ Array with configuration values. |
| 100 | 100 | * |
| 101 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
| 101 | + * @return Config|null Instance of a ConfigInterface implementation. |
|
| 102 | 102 | */ |
| 103 | 103 | public static function create($_) |
| 104 | 104 | { |
@@ -24,94 +24,94 @@ |
||
| 24 | 24 | class ConfigFactory |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Create a new ConfigInterface object from a file. |
|
| 29 | - * |
|
| 30 | - * If a comma-separated list of files is provided, they are checked in sequence until the first one could be loaded |
|
| 31 | - * successfully. |
|
| 32 | - * |
|
| 33 | - * @since 0.3.0 |
|
| 34 | - * |
|
| 35 | - * @param string|array $_ List of files. |
|
| 36 | - * |
|
| 37 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
| 38 | - */ |
|
| 39 | - public static function createFromFile($_) |
|
| 40 | - { |
|
| 41 | - $files = array_reverse(func_get_args()); |
|
| 42 | - |
|
| 43 | - if (is_array($files[0])) { |
|
| 44 | - $files = $files[0]; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - while (count($files) > 0) { |
|
| 48 | - try { |
|
| 49 | - $file = array_pop($files); |
|
| 50 | - |
|
| 51 | - if (! is_readable($file)) { |
|
| 52 | - continue; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $data = (array)Loader::load($file); |
|
| 56 | - |
|
| 57 | - $config = static::createFromArray($data); |
|
| 58 | - |
|
| 59 | - if (null === $config) { |
|
| 60 | - continue; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - return $config; |
|
| 64 | - } catch (Exception $exception) { |
|
| 65 | - // Fail silently and try next file. |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return static::createFromArray([]); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Create a new ConfigInterface object from an array. |
|
| 74 | - * |
|
| 75 | - * @since 0.3.0 |
|
| 76 | - * |
|
| 77 | - * @param array $array Array with configuration values. |
|
| 78 | - * |
|
| 79 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
| 80 | - */ |
|
| 81 | - public static function createFromArray(array $array) |
|
| 82 | - { |
|
| 83 | - try { |
|
| 84 | - return new Config($array); |
|
| 85 | - } catch (Exception $exception) { |
|
| 86 | - // Fail silently and try next file. |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - return null; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Create a new ConfigInterface object. |
|
| 94 | - * |
|
| 95 | - * Tries to deduce the correct creation method by inspecting the provided arguments. |
|
| 96 | - * |
|
| 97 | - * @since 0.3.0 |
|
| 98 | - * |
|
| 99 | - * @param mixed $_ Array with configuration values. |
|
| 100 | - * |
|
| 101 | - * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
| 102 | - */ |
|
| 103 | - public static function create($_) |
|
| 104 | - { |
|
| 105 | - if (func_num_args() < 1) { |
|
| 106 | - return static::createFromArray([]); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - $arguments = func_get_args(); |
|
| 110 | - |
|
| 111 | - if (is_array($arguments[0]) && func_num_args() === 1) { |
|
| 112 | - return static::createFromArray($arguments[0]); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return static::createFromFile($arguments); |
|
| 116 | - } |
|
| 27 | + /** |
|
| 28 | + * Create a new ConfigInterface object from a file. |
|
| 29 | + * |
|
| 30 | + * If a comma-separated list of files is provided, they are checked in sequence until the first one could be loaded |
|
| 31 | + * successfully. |
|
| 32 | + * |
|
| 33 | + * @since 0.3.0 |
|
| 34 | + * |
|
| 35 | + * @param string|array $_ List of files. |
|
| 36 | + * |
|
| 37 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
| 38 | + */ |
|
| 39 | + public static function createFromFile($_) |
|
| 40 | + { |
|
| 41 | + $files = array_reverse(func_get_args()); |
|
| 42 | + |
|
| 43 | + if (is_array($files[0])) { |
|
| 44 | + $files = $files[0]; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + while (count($files) > 0) { |
|
| 48 | + try { |
|
| 49 | + $file = array_pop($files); |
|
| 50 | + |
|
| 51 | + if (! is_readable($file)) { |
|
| 52 | + continue; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $data = (array)Loader::load($file); |
|
| 56 | + |
|
| 57 | + $config = static::createFromArray($data); |
|
| 58 | + |
|
| 59 | + if (null === $config) { |
|
| 60 | + continue; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + return $config; |
|
| 64 | + } catch (Exception $exception) { |
|
| 65 | + // Fail silently and try next file. |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return static::createFromArray([]); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Create a new ConfigInterface object from an array. |
|
| 74 | + * |
|
| 75 | + * @since 0.3.0 |
|
| 76 | + * |
|
| 77 | + * @param array $array Array with configuration values. |
|
| 78 | + * |
|
| 79 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
| 80 | + */ |
|
| 81 | + public static function createFromArray(array $array) |
|
| 82 | + { |
|
| 83 | + try { |
|
| 84 | + return new Config($array); |
|
| 85 | + } catch (Exception $exception) { |
|
| 86 | + // Fail silently and try next file. |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + return null; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Create a new ConfigInterface object. |
|
| 94 | + * |
|
| 95 | + * Tries to deduce the correct creation method by inspecting the provided arguments. |
|
| 96 | + * |
|
| 97 | + * @since 0.3.0 |
|
| 98 | + * |
|
| 99 | + * @param mixed $_ Array with configuration values. |
|
| 100 | + * |
|
| 101 | + * @return ConfigInterface Instance of a ConfigInterface implementation. |
|
| 102 | + */ |
|
| 103 | + public static function create($_) |
|
| 104 | + { |
|
| 105 | + if (func_num_args() < 1) { |
|
| 106 | + return static::createFromArray([]); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + $arguments = func_get_args(); |
|
| 110 | + |
|
| 111 | + if (is_array($arguments[0]) && func_num_args() === 1) { |
|
| 112 | + return static::createFromArray($arguments[0]); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return static::createFromFile($arguments); |
|
| 116 | + } |
|
| 117 | 117 | } |
@@ -48,11 +48,11 @@ |
||
| 48 | 48 | try { |
| 49 | 49 | $file = array_pop($files); |
| 50 | 50 | |
| 51 | - if (! is_readable($file)) { |
|
| 51 | + if ( ! is_readable($file)) { |
|
| 52 | 52 | continue; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - $data = (array)Loader::load($file); |
|
| 55 | + $data = (array) Loader::load($file); |
|
| 56 | 56 | |
| 57 | 57 | $config = static::createFromArray($data); |
| 58 | 58 | |
@@ -32,7 +32,7 @@ |
||
| 32 | 32 | * |
| 33 | 33 | * @param string $uri URI of the resource to load. |
| 34 | 34 | * |
| 35 | - * @return array Data contained within the resource. |
|
| 35 | + * @return boolean Data contained within the resource. |
|
| 36 | 36 | */ |
| 37 | 37 | public static function canLoad($uri) |
| 38 | 38 | { |
@@ -25,76 +25,76 @@ |
||
| 25 | 25 | class PHPLoader extends AbstractLoader |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Load the 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 Data contained within the resource. |
|
| 36 | - */ |
|
| 37 | - public static function canLoad($uri) |
|
| 38 | - { |
|
| 39 | - $path = pathinfo($uri); |
|
| 28 | + /** |
|
| 29 | + * Load the 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 Data contained within the resource. |
|
| 36 | + */ |
|
| 37 | + public static function canLoad($uri) |
|
| 38 | + { |
|
| 39 | + $path = pathinfo($uri); |
|
| 40 | 40 | |
| 41 | - return 'php' === mb_strtolower($path['extension']); |
|
| 42 | - } |
|
| 41 | + return 'php' === 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 | - // Try to load the file through PHP's include(). |
|
| 58 | - // Make sure we don't accidentally create output. |
|
| 59 | - ob_start(); |
|
| 60 | - $data = include($uri); |
|
| 61 | - 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 | + // Try to load the file through PHP's include(). |
|
| 58 | + // Make sure we don't accidentally create output. |
|
| 59 | + ob_start(); |
|
| 60 | + $data = include($uri); |
|
| 61 | + ob_end_clean(); |
|
| 62 | 62 | |
| 63 | - return $data; |
|
| 64 | - } catch (Exception $exception) { |
|
| 65 | - throw new FailedToLoadConfigException( |
|
| 66 | - sprintf( |
|
| 67 | - _('Could not include PHP config file "%1$s". Reason: "%2$s".'), |
|
| 68 | - $uri, |
|
| 69 | - $exception->getMessage() |
|
| 70 | - ), |
|
| 71 | - $exception->getCode(), |
|
| 72 | - $exception |
|
| 73 | - ); |
|
| 74 | - } |
|
| 75 | - } |
|
| 63 | + return $data; |
|
| 64 | + } catch (Exception $exception) { |
|
| 65 | + throw new FailedToLoadConfigException( |
|
| 66 | + sprintf( |
|
| 67 | + _('Could not include PHP config file "%1$s". Reason: "%2$s".'), |
|
| 68 | + $uri, |
|
| 69 | + $exception->getMessage() |
|
| 70 | + ), |
|
| 71 | + $exception->getCode(), |
|
| 72 | + $exception |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Validate and return the URI. |
|
| 79 | - * |
|
| 80 | - * @since 0.4.0 |
|
| 81 | - * |
|
| 82 | - * @param string $uri URI of the resource to load. |
|
| 83 | - * |
|
| 84 | - * @return string Validated URI. |
|
| 85 | - * @throws FailedToLoadConfigException If the URI does not exist or is not readable. |
|
| 86 | - */ |
|
| 87 | - protected function validateUri($uri) |
|
| 88 | - { |
|
| 89 | - if (! is_readable($uri)) { |
|
| 90 | - throw new FailedToLoadConfigException( |
|
| 91 | - sprintf( |
|
| 92 | - _('The requested PHP config file "%1$s" does not exist or is not readable.'), |
|
| 93 | - $uri |
|
| 94 | - ) |
|
| 95 | - ); |
|
| 96 | - } |
|
| 77 | + /** |
|
| 78 | + * Validate and return the URI. |
|
| 79 | + * |
|
| 80 | + * @since 0.4.0 |
|
| 81 | + * |
|
| 82 | + * @param string $uri URI of the resource to load. |
|
| 83 | + * |
|
| 84 | + * @return string Validated URI. |
|
| 85 | + * @throws FailedToLoadConfigException If the URI does not exist or is not readable. |
|
| 86 | + */ |
|
| 87 | + protected function validateUri($uri) |
|
| 88 | + { |
|
| 89 | + if (! is_readable($uri)) { |
|
| 90 | + throw new FailedToLoadConfigException( |
|
| 91 | + sprintf( |
|
| 92 | + _('The requested PHP config file "%1$s" does not exist or is not readable.'), |
|
| 93 | + $uri |
|
| 94 | + ) |
|
| 95 | + ); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - return $uri; |
|
| 99 | - } |
|
| 98 | + return $uri; |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -86,7 +86,7 @@ |
||
| 86 | 86 | */ |
| 87 | 87 | protected function validateUri($uri) |
| 88 | 88 | { |
| 89 | - if (! is_readable($uri)) { |
|
| 89 | + if ( ! is_readable($uri)) { |
|
| 90 | 90 | throw new FailedToLoadConfigException( |
| 91 | 91 | sprintf( |
| 92 | 92 | _('The requested PHP config file "%1$s" does not exist or is not readable.'), |
@@ -27,82 +27,82 @@ |
||
| 27 | 27 | interface ConfigInterface extends IteratorAggregate, ArrayAccess, Serializable, Countable |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Creates a copy of the ArrayObject. |
|
| 32 | - * |
|
| 33 | - * Returns a copy of the array. When the ArrayObject refers to an object an |
|
| 34 | - * array of the public properties of that object will be returned. |
|
| 35 | - * This is implemented by \ArrayObject. |
|
| 36 | - * |
|
| 37 | - * @since 0.1.0 |
|
| 38 | - * |
|
| 39 | - * @return array Copy of the array. |
|
| 40 | - */ |
|
| 41 | - public function getArrayCopy(); |
|
| 30 | + /** |
|
| 31 | + * Creates a copy of the ArrayObject. |
|
| 32 | + * |
|
| 33 | + * Returns a copy of the array. When the ArrayObject refers to an object an |
|
| 34 | + * array of the public properties of that object will be returned. |
|
| 35 | + * This is implemented by \ArrayObject. |
|
| 36 | + * |
|
| 37 | + * @since 0.1.0 |
|
| 38 | + * |
|
| 39 | + * @return array Copy of the array. |
|
| 40 | + */ |
|
| 41 | + public function getArrayCopy(); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Check whether the Config has a specific key. |
|
| 45 | - * |
|
| 46 | - * To check a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 47 | - * |
|
| 48 | - * @since 0.1.0 |
|
| 49 | - * @since 0.1.4 Accepts list of keys. |
|
| 50 | - * |
|
| 51 | - * @param string $_ List of keys. |
|
| 52 | - * |
|
| 53 | - * @return bool |
|
| 54 | - */ |
|
| 55 | - public function hasKey($_); |
|
| 43 | + /** |
|
| 44 | + * Check whether the Config has a specific key. |
|
| 45 | + * |
|
| 46 | + * To check a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 47 | + * |
|
| 48 | + * @since 0.1.0 |
|
| 49 | + * @since 0.1.4 Accepts list of keys. |
|
| 50 | + * |
|
| 51 | + * @param string $_ List of keys. |
|
| 52 | + * |
|
| 53 | + * @return bool |
|
| 54 | + */ |
|
| 55 | + public function hasKey($_); |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Get the value of a specific key. |
|
| 59 | - * |
|
| 60 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 61 | - * |
|
| 62 | - * @since 0.1.0 |
|
| 63 | - * @since 0.1.4 Accepts list of keys. |
|
| 64 | - * |
|
| 65 | - * @param string $_ List of keys. |
|
| 66 | - * |
|
| 67 | - * @return mixed |
|
| 68 | - */ |
|
| 69 | - public function getKey($_); |
|
| 57 | + /** |
|
| 58 | + * Get the value of a specific key. |
|
| 59 | + * |
|
| 60 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 61 | + * |
|
| 62 | + * @since 0.1.0 |
|
| 63 | + * @since 0.1.4 Accepts list of keys. |
|
| 64 | + * |
|
| 65 | + * @param string $_ List of keys. |
|
| 66 | + * |
|
| 67 | + * @return mixed |
|
| 68 | + */ |
|
| 69 | + public function getKey($_); |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
| 73 | - * |
|
| 74 | - * @since 0.1.4 |
|
| 75 | - * |
|
| 76 | - * @return array |
|
| 77 | - */ |
|
| 78 | - public function getAll(); |
|
| 71 | + /** |
|
| 72 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
| 73 | + * |
|
| 74 | + * @since 0.1.4 |
|
| 75 | + * |
|
| 76 | + * @return array |
|
| 77 | + */ |
|
| 78 | + public function getAll(); |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Get the an array with all the keys |
|
| 82 | - * |
|
| 83 | - * @since 0.1.0 |
|
| 84 | - * |
|
| 85 | - * @return mixed |
|
| 86 | - */ |
|
| 87 | - public function getKeys(); |
|
| 80 | + /** |
|
| 81 | + * Get the an array with all the keys |
|
| 82 | + * |
|
| 83 | + * @since 0.1.0 |
|
| 84 | + * |
|
| 85 | + * @return mixed |
|
| 86 | + */ |
|
| 87 | + public function getKeys(); |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Is the Config valid? |
|
| 91 | - * |
|
| 92 | - * @since 0.1.0 |
|
| 93 | - * |
|
| 94 | - * @return boolean |
|
| 95 | - */ |
|
| 96 | - public function isValid(); |
|
| 89 | + /** |
|
| 90 | + * Is the Config valid? |
|
| 91 | + * |
|
| 92 | + * @since 0.1.0 |
|
| 93 | + * |
|
| 94 | + * @return boolean |
|
| 95 | + */ |
|
| 96 | + public function isValid(); |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Get a new config at a specific sub-level. |
|
| 100 | - * |
|
| 101 | - * @since 0.1.13 |
|
| 102 | - * |
|
| 103 | - * @param string $_ List of keys. |
|
| 104 | - * |
|
| 105 | - * @return ConfigInterface |
|
| 106 | - */ |
|
| 107 | - public function getSubConfig($_); |
|
| 98 | + /** |
|
| 99 | + * Get a new config at a specific sub-level. |
|
| 100 | + * |
|
| 101 | + * @since 0.1.13 |
|
| 102 | + * |
|
| 103 | + * @param string $_ List of keys. |
|
| 104 | + * |
|
| 105 | + * @return ConfigInterface |
|
| 106 | + */ |
|
| 107 | + public function getSubConfig($_); |
|
| 108 | 108 | } |
@@ -25,72 +25,72 @@ |
||
| 25 | 25 | abstract class AbstractLoader implements LoaderInterface |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Load the 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 Data contained within the resource. Null if no data could be loaded/parsed. |
|
| 36 | - * @throws FailedToLoadConfigException If the configuration could not be loaded. |
|
| 37 | - */ |
|
| 38 | - public function load($uri) |
|
| 39 | - { |
|
| 40 | - try { |
|
| 41 | - $uri = $this->validateUri($uri); |
|
| 42 | - $data = $this->loadUri($uri); |
|
| 28 | + /** |
|
| 29 | + * Load the 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 Data contained within the resource. Null if no data could be loaded/parsed. |
|
| 36 | + * @throws FailedToLoadConfigException If the configuration could not be loaded. |
|
| 37 | + */ |
|
| 38 | + public function load($uri) |
|
| 39 | + { |
|
| 40 | + try { |
|
| 41 | + $uri = $this->validateUri($uri); |
|
| 42 | + $data = $this->loadUri($uri); |
|
| 43 | 43 | |
| 44 | - return $this->parseData($data); |
|
| 45 | - } catch (Exception $exception) { |
|
| 46 | - throw new FailedToLoadConfigException( |
|
| 47 | - sprintf( |
|
| 48 | - _('Could not load resource located at "%1$s". Reason: "%2$s".'), |
|
| 49 | - $uri, |
|
| 50 | - $exception->getMessage() |
|
| 51 | - ), |
|
| 52 | - $exception->getCode(), |
|
| 53 | - $exception |
|
| 54 | - ); |
|
| 55 | - } |
|
| 56 | - } |
|
| 44 | + return $this->parseData($data); |
|
| 45 | + } catch (Exception $exception) { |
|
| 46 | + throw new FailedToLoadConfigException( |
|
| 47 | + sprintf( |
|
| 48 | + _('Could not load resource located at "%1$s". Reason: "%2$s".'), |
|
| 49 | + $uri, |
|
| 50 | + $exception->getMessage() |
|
| 51 | + ), |
|
| 52 | + $exception->getCode(), |
|
| 53 | + $exception |
|
| 54 | + ); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Validate and return the URI. |
|
| 60 | - * |
|
| 61 | - * @since 0.4.0 |
|
| 62 | - * |
|
| 63 | - * @param string $uri URI of the resource to load. |
|
| 64 | - * |
|
| 65 | - * @return string Validated URI. |
|
| 66 | - */ |
|
| 67 | - protected function validateUri($uri) |
|
| 68 | - { |
|
| 69 | - return $uri; |
|
| 70 | - } |
|
| 58 | + /** |
|
| 59 | + * Validate and return the URI. |
|
| 60 | + * |
|
| 61 | + * @since 0.4.0 |
|
| 62 | + * |
|
| 63 | + * @param string $uri URI of the resource to load. |
|
| 64 | + * |
|
| 65 | + * @return string Validated URI. |
|
| 66 | + */ |
|
| 67 | + protected function validateUri($uri) |
|
| 68 | + { |
|
| 69 | + return $uri; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Parse the raw data and return it in parsed form. |
|
| 74 | - * |
|
| 75 | - * @since 0.4.0 |
|
| 76 | - * |
|
| 77 | - * @param array|null $data Raw data to be parsed. |
|
| 78 | - * |
|
| 79 | - * @return array|null Data in parsed form. Null if no parsable data found. |
|
| 80 | - */ |
|
| 81 | - protected function parseData($data) |
|
| 82 | - { |
|
| 83 | - return $data; |
|
| 84 | - } |
|
| 72 | + /** |
|
| 73 | + * Parse the raw data and return it in parsed form. |
|
| 74 | + * |
|
| 75 | + * @since 0.4.0 |
|
| 76 | + * |
|
| 77 | + * @param array|null $data Raw data to be parsed. |
|
| 78 | + * |
|
| 79 | + * @return array|null Data in parsed form. Null if no parsable data found. |
|
| 80 | + */ |
|
| 81 | + protected function parseData($data) |
|
| 82 | + { |
|
| 83 | + return $data; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Load the contents of an resource identified by an URI. |
|
| 88 | - * |
|
| 89 | - * @since 0.4.0 |
|
| 90 | - * |
|
| 91 | - * @param string $uri URI of the resource. |
|
| 92 | - * |
|
| 93 | - * @return array|null Raw data loaded from the resource. Null if no data found. |
|
| 94 | - */ |
|
| 95 | - abstract protected function loadUri($uri); |
|
| 86 | + /** |
|
| 87 | + * Load the contents of an resource identified by an URI. |
|
| 88 | + * |
|
| 89 | + * @since 0.4.0 |
|
| 90 | + * |
|
| 91 | + * @param string $uri URI of the resource. |
|
| 92 | + * |
|
| 93 | + * @return array|null Raw data loaded from the resource. Null if no data found. |
|
| 94 | + */ |
|
| 95 | + abstract protected function loadUri($uri); |
|
| 96 | 96 | } |