@@ -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.'), |
@@ -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 | } |
@@ -88,7 +88,7 @@ |
||
88 | 88 | public static function getLoader($loaderClass) |
89 | 89 | { |
90 | 90 | try { |
91 | - if (! array_key_exists($loaderClass, static::$loaderInstances)) { |
|
91 | + if ( ! array_key_exists($loaderClass, static::$loaderInstances)) { |
|
92 | 92 | static::$loaderInstances[$loaderClass] = new $loaderClass; |
93 | 93 | } |
94 | 94 |
@@ -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 | } |
@@ -25,20 +25,20 @@ |
||
25 | 25 | class Loader |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * Static convenience function to load a configuration from an URI. |
|
30 | - * |
|
31 | - * @since 0.4.0 |
|
32 | - * |
|
33 | - * @param string $uri URI of the resource to load. |
|
34 | - * |
|
35 | - * @return array|null Parsed data loaded from the resource. |
|
36 | - * @throws FailedToLoadConfigException If the configuration could not be loaded. |
|
37 | - */ |
|
38 | - public static function load($uri) |
|
39 | - { |
|
40 | - $loader = LoaderFactory::createFromUri($uri); |
|
28 | + /** |
|
29 | + * Static convenience function to load a configuration from an URI. |
|
30 | + * |
|
31 | + * @since 0.4.0 |
|
32 | + * |
|
33 | + * @param string $uri URI of the resource to load. |
|
34 | + * |
|
35 | + * @return array|null Parsed data loaded from the resource. |
|
36 | + * @throws FailedToLoadConfigException If the configuration could not be loaded. |
|
37 | + */ |
|
38 | + public static function load($uri) |
|
39 | + { |
|
40 | + $loader = LoaderFactory::createFromUri($uri); |
|
41 | 41 | |
42 | - return $loader->load($uri); |
|
43 | - } |
|
42 | + return $loader->load($uri); |
|
43 | + } |
|
44 | 44 | } |
@@ -33,189 +33,189 @@ |
||
33 | 33 | class Config extends AbstractConfig |
34 | 34 | { |
35 | 35 | |
36 | - /** |
|
37 | - * The schema of the Config file. |
|
38 | - * |
|
39 | - * @var Schema |
|
40 | - */ |
|
41 | - protected $schema; |
|
42 | - |
|
43 | - /** |
|
44 | - * The Validator class that gets asked to do the validation of the config. |
|
45 | - * |
|
46 | - * @since 0.1.0 |
|
47 | - * |
|
48 | - * @var Validator |
|
49 | - */ |
|
50 | - protected $validator; |
|
51 | - |
|
52 | - /** |
|
53 | - * Instantiate the Config object. |
|
54 | - * |
|
55 | - * It accepts either an array with the configuration settings, or a |
|
56 | - * filename pointing to a PHP file it can include. |
|
57 | - * |
|
58 | - * @since 0.1.0 |
|
59 | - * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
60 | - * |
|
61 | - * @param array|string $config Array with settings or filename for the |
|
62 | - * settings file. |
|
63 | - * @param Schema|null $schema Optional. Config that contains default |
|
64 | - * values that can get overwritten. |
|
65 | - * @param Validator|null $validator Optional. Validator class that does the |
|
66 | - * actual validation. |
|
67 | - * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
68 | - * configuration keys. Defaults to "\", "/" & ".". |
|
69 | - * |
|
70 | - * @throws InvalidConfigurationSourceException If the config source is not a string or array. |
|
71 | - * @throws FailedToInstantiateParentException If the parent class could not be instantiated. |
|
72 | - * @throws FailedToLoadConfigException If loading of the config source failed. |
|
73 | - * @throws FailedToResolveConfigException If the config file could not be resolved. |
|
74 | - * @throws InvalidConfigException If the config file is not valid. |
|
75 | - */ |
|
76 | - public function __construct( |
|
77 | - $config, |
|
78 | - Schema $schema = null, |
|
79 | - Validator $validator = null, |
|
80 | - $delimiter = null |
|
81 | - ) { |
|
82 | - $this->schema = $schema; |
|
83 | - $this->validator = $validator; |
|
84 | - |
|
85 | - // Make sure $config is either a string or array. |
|
86 | - if (! (is_string($config) || is_array($config))) { |
|
87 | - throw new InvalidConfigurationSourceException( |
|
88 | - sprintf( |
|
89 | - _('Invalid configuration source: %1$s'), |
|
90 | - print_r($config, true) |
|
91 | - ) |
|
92 | - ); |
|
93 | - } |
|
94 | - |
|
95 | - if (is_string($config)) { |
|
96 | - $config = Loader::load($config); |
|
97 | - } |
|
98 | - |
|
99 | - // Run the $config through the OptionsResolver. |
|
100 | - Assert\that($config)->isArray(); |
|
101 | - $config = $this->resolveOptions($config); |
|
102 | - |
|
103 | - // Instantiate the parent class. |
|
104 | - try { |
|
105 | - parent::__construct($config, $delimiter); |
|
106 | - } catch (Exception $exception) { |
|
107 | - throw new FailedToInstantiateParentException( |
|
108 | - sprintf( |
|
109 | - _('Could not instantiate the configuration through its parent. Reason: %1$s'), |
|
110 | - $exception->getMessage() |
|
111 | - ) |
|
112 | - ); |
|
113 | - } |
|
114 | - |
|
115 | - // Finally, validate the resulting config. |
|
116 | - if (! $this->isValid()) { |
|
117 | - throw new InvalidConfigException( |
|
118 | - sprintf( |
|
119 | - _('ConfigInterface file is not valid: %1$s'), |
|
120 | - print_r($config, true) |
|
121 | - ) |
|
122 | - ); |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Validate the Config file. |
|
128 | - * |
|
129 | - * @since 0.1.0 |
|
130 | - * |
|
131 | - * @return boolean |
|
132 | - */ |
|
133 | - public function isValid() |
|
134 | - { |
|
135 | - if ($this->validator) { |
|
136 | - return $this->validator->isValid($this); |
|
137 | - } |
|
138 | - |
|
139 | - return true; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Process the passed-in defaults and merge them with the new values, while |
|
144 | - * checking that all required options are set. |
|
145 | - * |
|
146 | - * @since 0.1.0 |
|
147 | - * |
|
148 | - * @param array $config Configuration settings to resolve. |
|
149 | - * |
|
150 | - * @return array Resolved configuration settings. |
|
151 | - * @throws FailedToResolveConfigException If there are errors while resolving the options. |
|
152 | - */ |
|
153 | - protected function resolveOptions($config) |
|
154 | - { |
|
155 | - if (! $this->schema) { |
|
156 | - return $config; |
|
157 | - } |
|
158 | - |
|
159 | - try { |
|
160 | - $resolver = new OptionsResolver(); |
|
161 | - if ($this->configureOptions($resolver)) { |
|
162 | - $config = $resolver->resolve($config); |
|
163 | - } |
|
164 | - } catch (Exception $exception) { |
|
165 | - throw new FailedToResolveConfigException( |
|
166 | - sprintf( |
|
167 | - _('Error while resolving config options: %1$s'), |
|
168 | - $exception->getMessage() |
|
169 | - ) |
|
170 | - ); |
|
171 | - } |
|
172 | - |
|
173 | - return $config; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Configure the possible and required options for the Config. |
|
178 | - * |
|
179 | - * This should return a bool to let the resolve_options() know whether the |
|
180 | - * actual resolving needs to be done or not. |
|
181 | - * |
|
182 | - * @since 0.1.0 |
|
183 | - * |
|
184 | - * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
185 | - * instance. |
|
186 | - * |
|
187 | - * @return bool Whether to do the resolving. |
|
188 | - * @throws FailedToResolveConfigException If there are errors while processing. |
|
189 | - */ |
|
190 | - protected function configureOptions(OptionsResolver $resolver) |
|
191 | - { |
|
192 | - $defined = $this->schema->getDefinedOptions(); |
|
193 | - $defaults = $this->schema->getDefaultOptions(); |
|
194 | - $required = $this->schema->getRequiredOptions(); |
|
195 | - |
|
196 | - if (! $defined && ! $defaults && ! $required) { |
|
197 | - return false; |
|
198 | - } |
|
199 | - |
|
200 | - try { |
|
201 | - if ($defined) { |
|
202 | - $resolver->setDefined($defined); |
|
203 | - } |
|
204 | - if ($defaults) { |
|
205 | - $resolver->setDefaults($defaults); |
|
206 | - } |
|
207 | - if ($required) { |
|
208 | - $resolver->setRequired($required); |
|
209 | - } |
|
210 | - } catch (Exception $exception) { |
|
211 | - throw new FailedToResolveConfigException( |
|
212 | - sprintf( |
|
213 | - _('Error while processing config options: %1$s'), |
|
214 | - $exception->getMessage() |
|
215 | - ) |
|
216 | - ); |
|
217 | - } |
|
218 | - |
|
219 | - return true; |
|
220 | - } |
|
36 | + /** |
|
37 | + * The schema of the Config file. |
|
38 | + * |
|
39 | + * @var Schema |
|
40 | + */ |
|
41 | + protected $schema; |
|
42 | + |
|
43 | + /** |
|
44 | + * The Validator class that gets asked to do the validation of the config. |
|
45 | + * |
|
46 | + * @since 0.1.0 |
|
47 | + * |
|
48 | + * @var Validator |
|
49 | + */ |
|
50 | + protected $validator; |
|
51 | + |
|
52 | + /** |
|
53 | + * Instantiate the Config object. |
|
54 | + * |
|
55 | + * It accepts either an array with the configuration settings, or a |
|
56 | + * filename pointing to a PHP file it can include. |
|
57 | + * |
|
58 | + * @since 0.1.0 |
|
59 | + * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
60 | + * |
|
61 | + * @param array|string $config Array with settings or filename for the |
|
62 | + * settings file. |
|
63 | + * @param Schema|null $schema Optional. Config that contains default |
|
64 | + * values that can get overwritten. |
|
65 | + * @param Validator|null $validator Optional. Validator class that does the |
|
66 | + * actual validation. |
|
67 | + * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
68 | + * configuration keys. Defaults to "\", "/" & ".". |
|
69 | + * |
|
70 | + * @throws InvalidConfigurationSourceException If the config source is not a string or array. |
|
71 | + * @throws FailedToInstantiateParentException If the parent class could not be instantiated. |
|
72 | + * @throws FailedToLoadConfigException If loading of the config source failed. |
|
73 | + * @throws FailedToResolveConfigException If the config file could not be resolved. |
|
74 | + * @throws InvalidConfigException If the config file is not valid. |
|
75 | + */ |
|
76 | + public function __construct( |
|
77 | + $config, |
|
78 | + Schema $schema = null, |
|
79 | + Validator $validator = null, |
|
80 | + $delimiter = null |
|
81 | + ) { |
|
82 | + $this->schema = $schema; |
|
83 | + $this->validator = $validator; |
|
84 | + |
|
85 | + // Make sure $config is either a string or array. |
|
86 | + if (! (is_string($config) || is_array($config))) { |
|
87 | + throw new InvalidConfigurationSourceException( |
|
88 | + sprintf( |
|
89 | + _('Invalid configuration source: %1$s'), |
|
90 | + print_r($config, true) |
|
91 | + ) |
|
92 | + ); |
|
93 | + } |
|
94 | + |
|
95 | + if (is_string($config)) { |
|
96 | + $config = Loader::load($config); |
|
97 | + } |
|
98 | + |
|
99 | + // Run the $config through the OptionsResolver. |
|
100 | + Assert\that($config)->isArray(); |
|
101 | + $config = $this->resolveOptions($config); |
|
102 | + |
|
103 | + // Instantiate the parent class. |
|
104 | + try { |
|
105 | + parent::__construct($config, $delimiter); |
|
106 | + } catch (Exception $exception) { |
|
107 | + throw new FailedToInstantiateParentException( |
|
108 | + sprintf( |
|
109 | + _('Could not instantiate the configuration through its parent. Reason: %1$s'), |
|
110 | + $exception->getMessage() |
|
111 | + ) |
|
112 | + ); |
|
113 | + } |
|
114 | + |
|
115 | + // Finally, validate the resulting config. |
|
116 | + if (! $this->isValid()) { |
|
117 | + throw new InvalidConfigException( |
|
118 | + sprintf( |
|
119 | + _('ConfigInterface file is not valid: %1$s'), |
|
120 | + print_r($config, true) |
|
121 | + ) |
|
122 | + ); |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Validate the Config file. |
|
128 | + * |
|
129 | + * @since 0.1.0 |
|
130 | + * |
|
131 | + * @return boolean |
|
132 | + */ |
|
133 | + public function isValid() |
|
134 | + { |
|
135 | + if ($this->validator) { |
|
136 | + return $this->validator->isValid($this); |
|
137 | + } |
|
138 | + |
|
139 | + return true; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Process the passed-in defaults and merge them with the new values, while |
|
144 | + * checking that all required options are set. |
|
145 | + * |
|
146 | + * @since 0.1.0 |
|
147 | + * |
|
148 | + * @param array $config Configuration settings to resolve. |
|
149 | + * |
|
150 | + * @return array Resolved configuration settings. |
|
151 | + * @throws FailedToResolveConfigException If there are errors while resolving the options. |
|
152 | + */ |
|
153 | + protected function resolveOptions($config) |
|
154 | + { |
|
155 | + if (! $this->schema) { |
|
156 | + return $config; |
|
157 | + } |
|
158 | + |
|
159 | + try { |
|
160 | + $resolver = new OptionsResolver(); |
|
161 | + if ($this->configureOptions($resolver)) { |
|
162 | + $config = $resolver->resolve($config); |
|
163 | + } |
|
164 | + } catch (Exception $exception) { |
|
165 | + throw new FailedToResolveConfigException( |
|
166 | + sprintf( |
|
167 | + _('Error while resolving config options: %1$s'), |
|
168 | + $exception->getMessage() |
|
169 | + ) |
|
170 | + ); |
|
171 | + } |
|
172 | + |
|
173 | + return $config; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Configure the possible and required options for the Config. |
|
178 | + * |
|
179 | + * This should return a bool to let the resolve_options() know whether the |
|
180 | + * actual resolving needs to be done or not. |
|
181 | + * |
|
182 | + * @since 0.1.0 |
|
183 | + * |
|
184 | + * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
185 | + * instance. |
|
186 | + * |
|
187 | + * @return bool Whether to do the resolving. |
|
188 | + * @throws FailedToResolveConfigException If there are errors while processing. |
|
189 | + */ |
|
190 | + protected function configureOptions(OptionsResolver $resolver) |
|
191 | + { |
|
192 | + $defined = $this->schema->getDefinedOptions(); |
|
193 | + $defaults = $this->schema->getDefaultOptions(); |
|
194 | + $required = $this->schema->getRequiredOptions(); |
|
195 | + |
|
196 | + if (! $defined && ! $defaults && ! $required) { |
|
197 | + return false; |
|
198 | + } |
|
199 | + |
|
200 | + try { |
|
201 | + if ($defined) { |
|
202 | + $resolver->setDefined($defined); |
|
203 | + } |
|
204 | + if ($defaults) { |
|
205 | + $resolver->setDefaults($defaults); |
|
206 | + } |
|
207 | + if ($required) { |
|
208 | + $resolver->setRequired($required); |
|
209 | + } |
|
210 | + } catch (Exception $exception) { |
|
211 | + throw new FailedToResolveConfigException( |
|
212 | + sprintf( |
|
213 | + _('Error while processing config options: %1$s'), |
|
214 | + $exception->getMessage() |
|
215 | + ) |
|
216 | + ); |
|
217 | + } |
|
218 | + |
|
219 | + return true; |
|
220 | + } |
|
221 | 221 | } |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | $this->validator = $validator; |
84 | 84 | |
85 | 85 | // Make sure $config is either a string or array. |
86 | - if (! (is_string($config) || is_array($config))) { |
|
86 | + if ( ! (is_string($config) || is_array($config))) { |
|
87 | 87 | throw new InvalidConfigurationSourceException( |
88 | 88 | sprintf( |
89 | 89 | _('Invalid configuration source: %1$s'), |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | } |
114 | 114 | |
115 | 115 | // Finally, validate the resulting config. |
116 | - if (! $this->isValid()) { |
|
116 | + if ( ! $this->isValid()) { |
|
117 | 117 | throw new InvalidConfigException( |
118 | 118 | sprintf( |
119 | 119 | _('ConfigInterface file is not valid: %1$s'), |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | */ |
153 | 153 | protected function resolveOptions($config) |
154 | 154 | { |
155 | - if (! $this->schema) { |
|
155 | + if ( ! $this->schema) { |
|
156 | 156 | return $config; |
157 | 157 | } |
158 | 158 | |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | $defaults = $this->schema->getDefaultOptions(); |
194 | 194 | $required = $this->schema->getRequiredOptions(); |
195 | 195 | |
196 | - if (! $defined && ! $defaults && ! $required) { |
|
196 | + if ( ! $defined && ! $defaults && ! $required) { |
|
197 | 197 | return false; |
198 | 198 | } |
199 | 199 |
@@ -27,227 +27,227 @@ |
||
27 | 27 | abstract class AbstractConfig extends ArrayObject implements ConfigInterface |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * Array of strings that are used as delimiters to parse configuration keys. |
|
32 | - * |
|
33 | - * @since 0.1.6 |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - protected $delimiter = ['\\', '/', '.']; |
|
38 | - |
|
39 | - /** |
|
40 | - * Instantiate the AbstractConfig object. |
|
41 | - * |
|
42 | - * @since 0.1.0 |
|
43 | - * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
44 | - * |
|
45 | - * @param array $config Array with settings. |
|
46 | - * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
47 | - * configuration keys. Defaults to "\", "/" & ".". |
|
48 | - */ |
|
49 | - public function __construct(array $config, $delimiter = null) |
|
50 | - { |
|
51 | - // Make sure the config entries can be accessed as properties. |
|
52 | - parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
|
53 | - |
|
54 | - if (null !== $delimiter) { |
|
55 | - $this->delimiter = (array)$delimiter; |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Get the value of a specific key. |
|
61 | - * |
|
62 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
63 | - * |
|
64 | - * @since 0.1.0 |
|
65 | - * @since 0.1.4 Accepts list of keys. |
|
66 | - * |
|
67 | - * @param string|array $_ List of keys. |
|
68 | - * |
|
69 | - * @return mixed |
|
70 | - * @throws KeyNotFoundException If an unknown key is requested. |
|
71 | - */ |
|
72 | - public function getKey($_) |
|
73 | - { |
|
74 | - $keys = $this->validateKeys(func_get_args()); |
|
75 | - |
|
76 | - $keys = array_reverse($keys); |
|
77 | - $array = $this->getArrayCopy(); |
|
78 | - while (count($keys) > 0) { |
|
79 | - $key = array_pop($keys); |
|
80 | - $array = $array[$key]; |
|
81 | - } |
|
82 | - |
|
83 | - return $array; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Check whether the Config has a specific key. |
|
88 | - * |
|
89 | - * To check a value several levels deep, add the keys for each level as a comma-separated list. |
|
90 | - * |
|
91 | - * @since 0.1.0 |
|
92 | - * @since 0.1.4 Accepts list of keys. |
|
93 | - * |
|
94 | - * @param string|array $_ List of keys. |
|
95 | - * |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - public function hasKey($_) |
|
99 | - { |
|
100 | - try { |
|
101 | - $keys = array_reverse($this->getKeyArguments(func_get_args())); |
|
102 | - |
|
103 | - $array = $this->getArrayCopy(); |
|
104 | - while (count($keys) > 0) { |
|
105 | - $key = array_pop($keys); |
|
106 | - if (! array_key_exists($key, $array)) { |
|
107 | - return false; |
|
108 | - } |
|
109 | - $array = $array[$key]; |
|
110 | - } |
|
111 | - } catch (Exception $exception) { |
|
112 | - return false; |
|
113 | - } |
|
114 | - |
|
115 | - return true; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
120 | - * |
|
121 | - * @since 0.1.4 |
|
122 | - * |
|
123 | - * @return array |
|
124 | - */ |
|
125 | - public function getAll() |
|
126 | - { |
|
127 | - return $this->getArrayCopy(); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Get the an array with all the keys |
|
132 | - * |
|
133 | - * @since 0.1.0 |
|
134 | - * @return array |
|
135 | - */ |
|
136 | - public function getKeys() |
|
137 | - { |
|
138 | - return array_keys((array)$this); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Get a new config at a specific sub-level. |
|
143 | - * |
|
144 | - * @since 0.1.13 |
|
145 | - * |
|
146 | - * @param string|array $_ List of keys. |
|
147 | - * |
|
148 | - * @return ConfigInterface |
|
149 | - * @throws KeyNotFoundException If an unknown key is requested. |
|
150 | - */ |
|
151 | - public function getSubConfig($_) |
|
152 | - { |
|
153 | - $keys = $this->validateKeys(func_get_args()); |
|
154 | - |
|
155 | - $subConfig = clone $this; |
|
156 | - $subConfig->reduceToSubKey($keys); |
|
157 | - |
|
158 | - return $subConfig; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Validate a set of keys to make sure they exist. |
|
163 | - * |
|
164 | - * @since 0.1.13 |
|
165 | - * |
|
166 | - * @param string|array $_ List of keys. |
|
167 | - * |
|
168 | - * @return array List of keys. |
|
169 | - * @throws KeyNotFoundException If an unknown key is requested. |
|
170 | - */ |
|
171 | - public function validateKeys($_) |
|
172 | - { |
|
173 | - $keys = $this->getKeyArguments(func_get_args()); |
|
174 | - |
|
175 | - Assert\that($keys)->all()->string()->notEmpty(); |
|
176 | - |
|
177 | - if (! $this->hasKey($keys)) { |
|
178 | - throw new KeyNotFoundException( |
|
179 | - sprintf( |
|
180 | - _('The configuration key %1$s does not exist.'), |
|
181 | - implode('->', $keys) |
|
182 | - ) |
|
183 | - ); |
|
184 | - } |
|
185 | - |
|
186 | - return $keys; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Reduce the currently stored config array to a subarray at a specific level. |
|
191 | - * |
|
192 | - * @since 0.1.13 |
|
193 | - * |
|
194 | - * @param array $keys Array of keys that point to a key down in the hierarchy. |
|
195 | - */ |
|
196 | - protected function reduceToSubKey(array $keys) |
|
197 | - { |
|
198 | - $this->exchangeArray($this->getKey($keys)); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Recursively extract the configuration key arguments from an arbitrary array. |
|
203 | - * |
|
204 | - * @since 0.1.6 |
|
205 | - * |
|
206 | - * @param array $arguments Array as fetched through get_func_args(). |
|
207 | - * |
|
208 | - * @return array Array of strings. |
|
209 | - */ |
|
210 | - protected function getKeyArguments($arguments) |
|
211 | - { |
|
212 | - Assert\that($arguments)->isArray()->notEmpty(); |
|
213 | - |
|
214 | - $keys = []; |
|
215 | - foreach ($arguments as $argument) { |
|
216 | - if (is_array($argument)) { |
|
217 | - $keys = array_merge($keys, $this->getKeyArguments($argument)); |
|
218 | - } |
|
219 | - if (is_string($argument)) { |
|
220 | - $keys = array_merge($keys, $this->parseKeysString($argument)); |
|
221 | - } |
|
222 | - } |
|
223 | - |
|
224 | - return $keys; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Extract individual keys from a delimited string. |
|
229 | - * |
|
230 | - * @since 0.1.6 |
|
231 | - * |
|
232 | - * @param string $keyString Delimited string of keys. |
|
233 | - * |
|
234 | - * @return array Array of key strings. |
|
235 | - */ |
|
236 | - protected function parseKeysString($keyString) |
|
237 | - { |
|
238 | - Assert\that($keyString)->string()->notEmpty(); |
|
239 | - |
|
240 | - // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
|
241 | - $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
|
242 | - |
|
243 | - return (array)explode($this->delimiter[0], $normalizedString); |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Validate the Config file. |
|
248 | - * |
|
249 | - * @since 0.1.0 |
|
250 | - * @return boolean |
|
251 | - */ |
|
252 | - abstract public function isValid(); |
|
30 | + /** |
|
31 | + * Array of strings that are used as delimiters to parse configuration keys. |
|
32 | + * |
|
33 | + * @since 0.1.6 |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + protected $delimiter = ['\\', '/', '.']; |
|
38 | + |
|
39 | + /** |
|
40 | + * Instantiate the AbstractConfig object. |
|
41 | + * |
|
42 | + * @since 0.1.0 |
|
43 | + * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
44 | + * |
|
45 | + * @param array $config Array with settings. |
|
46 | + * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
47 | + * configuration keys. Defaults to "\", "/" & ".". |
|
48 | + */ |
|
49 | + public function __construct(array $config, $delimiter = null) |
|
50 | + { |
|
51 | + // Make sure the config entries can be accessed as properties. |
|
52 | + parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
|
53 | + |
|
54 | + if (null !== $delimiter) { |
|
55 | + $this->delimiter = (array)$delimiter; |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Get the value of a specific key. |
|
61 | + * |
|
62 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
63 | + * |
|
64 | + * @since 0.1.0 |
|
65 | + * @since 0.1.4 Accepts list of keys. |
|
66 | + * |
|
67 | + * @param string|array $_ List of keys. |
|
68 | + * |
|
69 | + * @return mixed |
|
70 | + * @throws KeyNotFoundException If an unknown key is requested. |
|
71 | + */ |
|
72 | + public function getKey($_) |
|
73 | + { |
|
74 | + $keys = $this->validateKeys(func_get_args()); |
|
75 | + |
|
76 | + $keys = array_reverse($keys); |
|
77 | + $array = $this->getArrayCopy(); |
|
78 | + while (count($keys) > 0) { |
|
79 | + $key = array_pop($keys); |
|
80 | + $array = $array[$key]; |
|
81 | + } |
|
82 | + |
|
83 | + return $array; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Check whether the Config has a specific key. |
|
88 | + * |
|
89 | + * To check a value several levels deep, add the keys for each level as a comma-separated list. |
|
90 | + * |
|
91 | + * @since 0.1.0 |
|
92 | + * @since 0.1.4 Accepts list of keys. |
|
93 | + * |
|
94 | + * @param string|array $_ List of keys. |
|
95 | + * |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + public function hasKey($_) |
|
99 | + { |
|
100 | + try { |
|
101 | + $keys = array_reverse($this->getKeyArguments(func_get_args())); |
|
102 | + |
|
103 | + $array = $this->getArrayCopy(); |
|
104 | + while (count($keys) > 0) { |
|
105 | + $key = array_pop($keys); |
|
106 | + if (! array_key_exists($key, $array)) { |
|
107 | + return false; |
|
108 | + } |
|
109 | + $array = $array[$key]; |
|
110 | + } |
|
111 | + } catch (Exception $exception) { |
|
112 | + return false; |
|
113 | + } |
|
114 | + |
|
115 | + return true; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
120 | + * |
|
121 | + * @since 0.1.4 |
|
122 | + * |
|
123 | + * @return array |
|
124 | + */ |
|
125 | + public function getAll() |
|
126 | + { |
|
127 | + return $this->getArrayCopy(); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Get the an array with all the keys |
|
132 | + * |
|
133 | + * @since 0.1.0 |
|
134 | + * @return array |
|
135 | + */ |
|
136 | + public function getKeys() |
|
137 | + { |
|
138 | + return array_keys((array)$this); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Get a new config at a specific sub-level. |
|
143 | + * |
|
144 | + * @since 0.1.13 |
|
145 | + * |
|
146 | + * @param string|array $_ List of keys. |
|
147 | + * |
|
148 | + * @return ConfigInterface |
|
149 | + * @throws KeyNotFoundException If an unknown key is requested. |
|
150 | + */ |
|
151 | + public function getSubConfig($_) |
|
152 | + { |
|
153 | + $keys = $this->validateKeys(func_get_args()); |
|
154 | + |
|
155 | + $subConfig = clone $this; |
|
156 | + $subConfig->reduceToSubKey($keys); |
|
157 | + |
|
158 | + return $subConfig; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Validate a set of keys to make sure they exist. |
|
163 | + * |
|
164 | + * @since 0.1.13 |
|
165 | + * |
|
166 | + * @param string|array $_ List of keys. |
|
167 | + * |
|
168 | + * @return array List of keys. |
|
169 | + * @throws KeyNotFoundException If an unknown key is requested. |
|
170 | + */ |
|
171 | + public function validateKeys($_) |
|
172 | + { |
|
173 | + $keys = $this->getKeyArguments(func_get_args()); |
|
174 | + |
|
175 | + Assert\that($keys)->all()->string()->notEmpty(); |
|
176 | + |
|
177 | + if (! $this->hasKey($keys)) { |
|
178 | + throw new KeyNotFoundException( |
|
179 | + sprintf( |
|
180 | + _('The configuration key %1$s does not exist.'), |
|
181 | + implode('->', $keys) |
|
182 | + ) |
|
183 | + ); |
|
184 | + } |
|
185 | + |
|
186 | + return $keys; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Reduce the currently stored config array to a subarray at a specific level. |
|
191 | + * |
|
192 | + * @since 0.1.13 |
|
193 | + * |
|
194 | + * @param array $keys Array of keys that point to a key down in the hierarchy. |
|
195 | + */ |
|
196 | + protected function reduceToSubKey(array $keys) |
|
197 | + { |
|
198 | + $this->exchangeArray($this->getKey($keys)); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Recursively extract the configuration key arguments from an arbitrary array. |
|
203 | + * |
|
204 | + * @since 0.1.6 |
|
205 | + * |
|
206 | + * @param array $arguments Array as fetched through get_func_args(). |
|
207 | + * |
|
208 | + * @return array Array of strings. |
|
209 | + */ |
|
210 | + protected function getKeyArguments($arguments) |
|
211 | + { |
|
212 | + Assert\that($arguments)->isArray()->notEmpty(); |
|
213 | + |
|
214 | + $keys = []; |
|
215 | + foreach ($arguments as $argument) { |
|
216 | + if (is_array($argument)) { |
|
217 | + $keys = array_merge($keys, $this->getKeyArguments($argument)); |
|
218 | + } |
|
219 | + if (is_string($argument)) { |
|
220 | + $keys = array_merge($keys, $this->parseKeysString($argument)); |
|
221 | + } |
|
222 | + } |
|
223 | + |
|
224 | + return $keys; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Extract individual keys from a delimited string. |
|
229 | + * |
|
230 | + * @since 0.1.6 |
|
231 | + * |
|
232 | + * @param string $keyString Delimited string of keys. |
|
233 | + * |
|
234 | + * @return array Array of key strings. |
|
235 | + */ |
|
236 | + protected function parseKeysString($keyString) |
|
237 | + { |
|
238 | + Assert\that($keyString)->string()->notEmpty(); |
|
239 | + |
|
240 | + // Replace all of the configured delimiters by the first one, so that we can then use explode(). |
|
241 | + $normalizedString = str_replace($this->delimiter, $this->delimiter[0], $keyString); |
|
242 | + |
|
243 | + return (array)explode($this->delimiter[0], $normalizedString); |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Validate the Config file. |
|
248 | + * |
|
249 | + * @since 0.1.0 |
|
250 | + * @return boolean |
|
251 | + */ |
|
252 | + abstract public function isValid(); |
|
253 | 253 | } |