@@ -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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -22,25 +22,25 @@ |
||
| 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 Data contained within the resource. |
|
| 44 | - */ |
|
| 45 | - 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 Data contained within the resource. |
|
| 44 | + */ |
|
| 45 | + public function load($uri); |
|
| 46 | 46 | } |
@@ -25,97 +25,97 @@ |
||
| 25 | 25 | class LoaderFactory |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Array of fully qualified class names of known loaders. |
|
| 30 | - * |
|
| 31 | - * @var array<string> |
|
| 32 | - * |
|
| 33 | - * @since 0.4.0 |
|
| 34 | - */ |
|
| 35 | - protected static $loaders = [ |
|
| 36 | - 'BrightNucleus\Config\Loader\PHPLoader', |
|
| 37 | - ]; |
|
| 28 | + /** |
|
| 29 | + * Array of fully qualified class names of known loaders. |
|
| 30 | + * |
|
| 31 | + * @var array<string> |
|
| 32 | + * |
|
| 33 | + * @since 0.4.0 |
|
| 34 | + */ |
|
| 35 | + protected static $loaders = [ |
|
| 36 | + 'BrightNucleus\Config\Loader\PHPLoader', |
|
| 37 | + ]; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Array of instantiated loaders. |
|
| 41 | - * |
|
| 42 | - * These are lazily instantiated and added as needed. |
|
| 43 | - * |
|
| 44 | - * @var LoaderInterface[] |
|
| 45 | - * |
|
| 46 | - * @since 0.4.0 |
|
| 47 | - */ |
|
| 48 | - protected static $loaderInstances = []; |
|
| 39 | + /** |
|
| 40 | + * Array of instantiated loaders. |
|
| 41 | + * |
|
| 42 | + * These are lazily instantiated and added as needed. |
|
| 43 | + * |
|
| 44 | + * @var LoaderInterface[] |
|
| 45 | + * |
|
| 46 | + * @since 0.4.0 |
|
| 47 | + */ |
|
| 48 | + protected static $loaderInstances = []; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Create a new Loader from an URI. |
|
| 52 | - * |
|
| 53 | - * @since 0.4.0 |
|
| 54 | - * |
|
| 55 | - * @param string $uri URI of the resource to create a loader for. |
|
| 56 | - * |
|
| 57 | - * @return LoaderInterface Loader that is able to load the given URI. |
|
| 58 | - * @throws FailedToLoadConfigException If no suitable loader was found. |
|
| 59 | - */ |
|
| 60 | - public static function createFromUri($uri) |
|
| 61 | - { |
|
| 62 | - foreach (static::$loaders as $loader) { |
|
| 63 | - if ($loader::canLoad($uri)) { |
|
| 64 | - return static::getLoader($loader); |
|
| 65 | - } |
|
| 66 | - } |
|
| 50 | + /** |
|
| 51 | + * Create a new Loader from an URI. |
|
| 52 | + * |
|
| 53 | + * @since 0.4.0 |
|
| 54 | + * |
|
| 55 | + * @param string $uri URI of the resource to create a loader for. |
|
| 56 | + * |
|
| 57 | + * @return LoaderInterface Loader that is able to load the given URI. |
|
| 58 | + * @throws FailedToLoadConfigException If no suitable loader was found. |
|
| 59 | + */ |
|
| 60 | + public static function createFromUri($uri) |
|
| 61 | + { |
|
| 62 | + foreach (static::$loaders as $loader) { |
|
| 63 | + if ($loader::canLoad($uri)) { |
|
| 64 | + return static::getLoader($loader); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - throw new FailedToLoadConfigException( |
|
| 69 | - sprintf( |
|
| 70 | - _('Could not find a suitable loader for URI "%1$s".'), |
|
| 71 | - $uri |
|
| 72 | - ) |
|
| 73 | - ); |
|
| 74 | - } |
|
| 68 | + throw new FailedToLoadConfigException( |
|
| 69 | + sprintf( |
|
| 70 | + _('Could not find a suitable loader for URI "%1$s".'), |
|
| 71 | + $uri |
|
| 72 | + ) |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Get an instance of a specific loader. |
|
| 78 | - * |
|
| 79 | - * The loader is lazily instantiated if needed. |
|
| 80 | - * |
|
| 81 | - * @since 0.4.0 |
|
| 82 | - * |
|
| 83 | - * @param string $loaderClass Fully qualified class name of the loader to get. |
|
| 84 | - * |
|
| 85 | - * @return LoaderInterface Instance of the requested loader. |
|
| 86 | - * @throws FailedToLoadConfigException If the loader class could not be instantiated. |
|
| 87 | - */ |
|
| 88 | - public static function getLoader($loaderClass) |
|
| 89 | - { |
|
| 90 | - try { |
|
| 91 | - if (! array_key_exists($loaderClass, static::$loaderInstances)) { |
|
| 92 | - static::$loaderInstances[$loaderClass] = new $loaderClass; |
|
| 93 | - } |
|
| 76 | + /** |
|
| 77 | + * Get an instance of a specific loader. |
|
| 78 | + * |
|
| 79 | + * The loader is lazily instantiated if needed. |
|
| 80 | + * |
|
| 81 | + * @since 0.4.0 |
|
| 82 | + * |
|
| 83 | + * @param string $loaderClass Fully qualified class name of the loader to get. |
|
| 84 | + * |
|
| 85 | + * @return LoaderInterface Instance of the requested loader. |
|
| 86 | + * @throws FailedToLoadConfigException If the loader class could not be instantiated. |
|
| 87 | + */ |
|
| 88 | + public static function getLoader($loaderClass) |
|
| 89 | + { |
|
| 90 | + try { |
|
| 91 | + if (! array_key_exists($loaderClass, static::$loaderInstances)) { |
|
| 92 | + static::$loaderInstances[$loaderClass] = new $loaderClass; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - return static::$loaderInstances[$loaderClass]; |
|
| 96 | - } catch (Exception $exception) { |
|
| 97 | - throw new FailedToLoadConfigException( |
|
| 98 | - sprintf( |
|
| 99 | - _('Could not instantiate the requested loader class "%1$s".'), |
|
| 100 | - $loaderClass |
|
| 101 | - ) |
|
| 102 | - ); |
|
| 103 | - } |
|
| 104 | - } |
|
| 95 | + return static::$loaderInstances[$loaderClass]; |
|
| 96 | + } catch (Exception $exception) { |
|
| 97 | + throw new FailedToLoadConfigException( |
|
| 98 | + sprintf( |
|
| 99 | + _('Could not instantiate the requested loader class "%1$s".'), |
|
| 100 | + $loaderClass |
|
| 101 | + ) |
|
| 102 | + ); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Register a new loader. |
|
| 108 | - * |
|
| 109 | - * @since 0.4.0 |
|
| 110 | - * |
|
| 111 | - * @param string $loader Fully qualified class name of a loader implementing LoaderInterface. |
|
| 112 | - */ |
|
| 113 | - public static function registerLoader($loader) |
|
| 114 | - { |
|
| 115 | - if (in_array($loader, static::$loaders, true)) { |
|
| 116 | - return; |
|
| 117 | - } |
|
| 106 | + /** |
|
| 107 | + * Register a new loader. |
|
| 108 | + * |
|
| 109 | + * @since 0.4.0 |
|
| 110 | + * |
|
| 111 | + * @param string $loader Fully qualified class name of a loader implementing LoaderInterface. |
|
| 112 | + */ |
|
| 113 | + public static function registerLoader($loader) |
|
| 114 | + { |
|
| 115 | + if (in_array($loader, static::$loaders, true)) { |
|
| 116 | + return; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - static::$loaders [] = $loader; |
|
| 120 | - } |
|
| 119 | + static::$loaders [] = $loader; |
|
| 120 | + } |
|
| 121 | 121 | } |
@@ -26,105 +26,105 @@ |
||
| 26 | 26 | trait ConfigTrait |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Reference to the Config object. |
|
| 31 | - * |
|
| 32 | - * @since 0.1.2 |
|
| 33 | - * |
|
| 34 | - * @var ConfigInterface |
|
| 35 | - */ |
|
| 36 | - protected $config; |
|
| 29 | + /** |
|
| 30 | + * Reference to the Config object. |
|
| 31 | + * |
|
| 32 | + * @since 0.1.2 |
|
| 33 | + * |
|
| 34 | + * @var ConfigInterface |
|
| 35 | + */ |
|
| 36 | + protected $config; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Process the passed-in configuration file. |
|
| 40 | - * |
|
| 41 | - * @since 0.1.2 |
|
| 42 | - * |
|
| 43 | - * @param ConfigInterface $config The Config to process. |
|
| 44 | - * @param string ... List of keys. |
|
| 45 | - * |
|
| 46 | - * @throws FailedToProcessConfigException If the arguments could not be parsed into a Config. |
|
| 47 | - */ |
|
| 48 | - protected function processConfig(ConfigInterface $config) |
|
| 49 | - { |
|
| 50 | - if (func_num_args() > 1) { |
|
| 51 | - try { |
|
| 52 | - $keys = func_get_args(); |
|
| 53 | - array_shift($keys); |
|
| 54 | - $config = $config->getSubConfig($keys); |
|
| 55 | - } catch (Exception $exception) { |
|
| 56 | - throw new FailedToProcessConfigException( |
|
| 57 | - sprintf( |
|
| 58 | - _('Could not process the config with the arguments "%1$s".'), |
|
| 59 | - print_r(func_get_args(), true) |
|
| 60 | - ) |
|
| 61 | - ); |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - $this->config = $config; |
|
| 65 | - } |
|
| 38 | + /** |
|
| 39 | + * Process the passed-in configuration file. |
|
| 40 | + * |
|
| 41 | + * @since 0.1.2 |
|
| 42 | + * |
|
| 43 | + * @param ConfigInterface $config The Config to process. |
|
| 44 | + * @param string ... List of keys. |
|
| 45 | + * |
|
| 46 | + * @throws FailedToProcessConfigException If the arguments could not be parsed into a Config. |
|
| 47 | + */ |
|
| 48 | + protected function processConfig(ConfigInterface $config) |
|
| 49 | + { |
|
| 50 | + if (func_num_args() > 1) { |
|
| 51 | + try { |
|
| 52 | + $keys = func_get_args(); |
|
| 53 | + array_shift($keys); |
|
| 54 | + $config = $config->getSubConfig($keys); |
|
| 55 | + } catch (Exception $exception) { |
|
| 56 | + throw new FailedToProcessConfigException( |
|
| 57 | + sprintf( |
|
| 58 | + _('Could not process the config with the arguments "%1$s".'), |
|
| 59 | + print_r(func_get_args(), true) |
|
| 60 | + ) |
|
| 61 | + ); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + $this->config = $config; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Check whether the Config has a specific key. |
|
| 69 | - * |
|
| 70 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 71 | - * |
|
| 72 | - * @since 0.1.2 |
|
| 73 | - * @since 0.1.5 Accepts list of keys. |
|
| 74 | - * |
|
| 75 | - * @param string|array $_ List of keys. |
|
| 76 | - * |
|
| 77 | - * @return bool Whether the key is known. |
|
| 78 | - */ |
|
| 79 | - protected function hasConfigKey($_) |
|
| 80 | - { |
|
| 81 | - $keys = func_get_args(); |
|
| 82 | - Assert\that($keys)->all()->string()->notEmpty(); |
|
| 67 | + /** |
|
| 68 | + * Check whether the Config has a specific key. |
|
| 69 | + * |
|
| 70 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 71 | + * |
|
| 72 | + * @since 0.1.2 |
|
| 73 | + * @since 0.1.5 Accepts list of keys. |
|
| 74 | + * |
|
| 75 | + * @param string|array $_ List of keys. |
|
| 76 | + * |
|
| 77 | + * @return bool Whether the key is known. |
|
| 78 | + */ |
|
| 79 | + protected function hasConfigKey($_) |
|
| 80 | + { |
|
| 81 | + $keys = func_get_args(); |
|
| 82 | + Assert\that($keys)->all()->string()->notEmpty(); |
|
| 83 | 83 | |
| 84 | - return $this->config->hasKey($keys); |
|
| 85 | - } |
|
| 84 | + return $this->config->hasKey($keys); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Get the Config value for a specific key. |
|
| 89 | - * |
|
| 90 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 91 | - * |
|
| 92 | - * @since 0.1.2 |
|
| 93 | - * @since 0.1.5 Accepts list of keys. |
|
| 94 | - * |
|
| 95 | - * @param string|array $_ List of keys. |
|
| 96 | - * |
|
| 97 | - * @return mixed Value of the key. |
|
| 98 | - */ |
|
| 99 | - protected function getConfigKey($_) |
|
| 100 | - { |
|
| 101 | - $keys = func_get_args(); |
|
| 102 | - Assert\that($keys)->all()->string()->notEmpty(); |
|
| 87 | + /** |
|
| 88 | + * Get the Config value for a specific key. |
|
| 89 | + * |
|
| 90 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 91 | + * |
|
| 92 | + * @since 0.1.2 |
|
| 93 | + * @since 0.1.5 Accepts list of keys. |
|
| 94 | + * |
|
| 95 | + * @param string|array $_ List of keys. |
|
| 96 | + * |
|
| 97 | + * @return mixed Value of the key. |
|
| 98 | + */ |
|
| 99 | + protected function getConfigKey($_) |
|
| 100 | + { |
|
| 101 | + $keys = func_get_args(); |
|
| 102 | + Assert\that($keys)->all()->string()->notEmpty(); |
|
| 103 | 103 | |
| 104 | - return $this->config->getKey($keys); |
|
| 105 | - } |
|
| 104 | + return $this->config->getKey($keys); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
| 109 | - * |
|
| 110 | - * @since 0.1.4 |
|
| 111 | - * |
|
| 112 | - * @return array All the configuration settings. |
|
| 113 | - */ |
|
| 114 | - protected function getConfigArray() |
|
| 115 | - { |
|
| 116 | - return $this->config->getAll(); |
|
| 117 | - } |
|
| 107 | + /** |
|
| 108 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
| 109 | + * |
|
| 110 | + * @since 0.1.4 |
|
| 111 | + * |
|
| 112 | + * @return array All the configuration settings. |
|
| 113 | + */ |
|
| 114 | + protected function getConfigArray() |
|
| 115 | + { |
|
| 116 | + return $this->config->getAll(); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Get an array of all the keys that are known by the Config. |
|
| 121 | - * |
|
| 122 | - * @since 0.1.2 |
|
| 123 | - * |
|
| 124 | - * @return array Array of strings containing all the keys. |
|
| 125 | - */ |
|
| 126 | - protected function getConfigKeys() |
|
| 127 | - { |
|
| 128 | - return $this->config->getKeys(); |
|
| 129 | - } |
|
| 119 | + /** |
|
| 120 | + * Get an array of all the keys that are known by the Config. |
|
| 121 | + * |
|
| 122 | + * @since 0.1.2 |
|
| 123 | + * |
|
| 124 | + * @return array Array of strings containing all the keys. |
|
| 125 | + */ |
|
| 126 | + protected function getConfigKeys() |
|
| 127 | + { |
|
| 128 | + return $this->config->getKeys(); |
|
| 129 | + } |
|
| 130 | 130 | } |