@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | */ |
53 | 53 | public function getDefinedOptions() |
54 | 54 | { |
55 | - if (! $this->defined) { |
|
55 | + if ( ! $this->defined) { |
|
56 | 56 | return null; |
57 | 57 | } |
58 | 58 | |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | return $this->defined->getArrayCopy(); |
61 | 61 | } |
62 | 62 | |
63 | - return (array)$this->defined; |
|
63 | + return (array) $this->defined; |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | /** |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | */ |
73 | 73 | public function getDefaultOptions() |
74 | 74 | { |
75 | - if (! $this->defaults) { |
|
75 | + if ( ! $this->defaults) { |
|
76 | 76 | return null; |
77 | 77 | } |
78 | 78 | |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | return $this->defaults->getArrayCopy(); |
81 | 81 | } |
82 | 82 | |
83 | - return (array)$this->defaults; |
|
83 | + return (array) $this->defaults; |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | */ |
93 | 93 | public function getRequiredOptions() |
94 | 94 | { |
95 | - if (! $this->required) { |
|
95 | + if ( ! $this->required) { |
|
96 | 96 | return null; |
97 | 97 | } |
98 | 98 | |
@@ -100,6 +100,6 @@ discard block |
||
100 | 100 | return $this->required->getArrayCopy(); |
101 | 101 | } |
102 | 102 | |
103 | - return (array)$this->required; |
|
103 | + return (array) $this->required; |
|
104 | 104 | } |
105 | 105 | } |
@@ -22,84 +22,84 @@ |
||
22 | 22 | abstract class AbstractConfigSchema implements ConfigSchemaInterface |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * The defined values that are recognized. |
|
27 | - * |
|
28 | - * @var ConfigInterface |
|
29 | - */ |
|
30 | - protected $defined; |
|
25 | + /** |
|
26 | + * The defined values that are recognized. |
|
27 | + * |
|
28 | + * @var ConfigInterface |
|
29 | + */ |
|
30 | + protected $defined; |
|
31 | 31 | |
32 | - /** |
|
33 | - * The default values that can be overwritten. |
|
34 | - * |
|
35 | - * @var ConfigInterface |
|
36 | - */ |
|
37 | - protected $defaults; |
|
32 | + /** |
|
33 | + * The default values that can be overwritten. |
|
34 | + * |
|
35 | + * @var ConfigInterface |
|
36 | + */ |
|
37 | + protected $defaults; |
|
38 | 38 | |
39 | - /** |
|
40 | - * The required values that need to be set. |
|
41 | - * |
|
42 | - * @var ConfigInterface |
|
43 | - */ |
|
44 | - protected $required; |
|
39 | + /** |
|
40 | + * The required values that need to be set. |
|
41 | + * |
|
42 | + * @var ConfigInterface |
|
43 | + */ |
|
44 | + protected $required; |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the set of defined options. |
|
48 | - * |
|
49 | - * @since 0.1.0 |
|
50 | - * |
|
51 | - * @return array|null |
|
52 | - */ |
|
53 | - public function getDefinedOptions() |
|
54 | - { |
|
55 | - if (! $this->defined) { |
|
56 | - return null; |
|
57 | - } |
|
46 | + /** |
|
47 | + * Get the set of defined options. |
|
48 | + * |
|
49 | + * @since 0.1.0 |
|
50 | + * |
|
51 | + * @return array|null |
|
52 | + */ |
|
53 | + public function getDefinedOptions() |
|
54 | + { |
|
55 | + if (! $this->defined) { |
|
56 | + return null; |
|
57 | + } |
|
58 | 58 | |
59 | - if ($this->defined instanceof ConfigInterface) { |
|
60 | - return $this->defined->getArrayCopy(); |
|
61 | - } |
|
59 | + if ($this->defined instanceof ConfigInterface) { |
|
60 | + return $this->defined->getArrayCopy(); |
|
61 | + } |
|
62 | 62 | |
63 | - return (array)$this->defined; |
|
64 | - } |
|
63 | + return (array)$this->defined; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Get the set of default options. |
|
68 | - * |
|
69 | - * @since 0.1.0 |
|
70 | - * |
|
71 | - * @return array|null |
|
72 | - */ |
|
73 | - public function getDefaultOptions() |
|
74 | - { |
|
75 | - if (! $this->defaults) { |
|
76 | - return null; |
|
77 | - } |
|
66 | + /** |
|
67 | + * Get the set of default options. |
|
68 | + * |
|
69 | + * @since 0.1.0 |
|
70 | + * |
|
71 | + * @return array|null |
|
72 | + */ |
|
73 | + public function getDefaultOptions() |
|
74 | + { |
|
75 | + if (! $this->defaults) { |
|
76 | + return null; |
|
77 | + } |
|
78 | 78 | |
79 | - if ($this->defaults instanceof ConfigInterface) { |
|
80 | - return $this->defaults->getArrayCopy(); |
|
81 | - } |
|
79 | + if ($this->defaults instanceof ConfigInterface) { |
|
80 | + return $this->defaults->getArrayCopy(); |
|
81 | + } |
|
82 | 82 | |
83 | - return (array)$this->defaults; |
|
84 | - } |
|
83 | + return (array)$this->defaults; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get the set of required options. |
|
88 | - * |
|
89 | - * @since 0.1.0 |
|
90 | - * |
|
91 | - * @return array|null |
|
92 | - */ |
|
93 | - public function getRequiredOptions() |
|
94 | - { |
|
95 | - if (! $this->required) { |
|
96 | - return null; |
|
97 | - } |
|
86 | + /** |
|
87 | + * Get the set of required options. |
|
88 | + * |
|
89 | + * @since 0.1.0 |
|
90 | + * |
|
91 | + * @return array|null |
|
92 | + */ |
|
93 | + public function getRequiredOptions() |
|
94 | + { |
|
95 | + if (! $this->required) { |
|
96 | + return null; |
|
97 | + } |
|
98 | 98 | |
99 | - if ($this->required instanceof ConfigInterface) { |
|
100 | - return $this->required->getArrayCopy(); |
|
101 | - } |
|
99 | + if ($this->required instanceof ConfigInterface) { |
|
100 | + return $this->required->getArrayCopy(); |
|
101 | + } |
|
102 | 102 | |
103 | - return (array)$this->required; |
|
104 | - } |
|
103 | + return (array)$this->required; |
|
104 | + } |
|
105 | 105 | } |
@@ -63,7 +63,7 @@ |
||
63 | 63 | $schema = $schema->getArrayCopy(); |
64 | 64 | } |
65 | 65 | |
66 | - if (! is_array($schema)) { |
|
66 | + if ( ! is_array($schema)) { |
|
67 | 67 | throw new InvalidArgumentException( |
68 | 68 | sprintf( |
69 | 69 | _('Invalid schema source: %1$s'), |
@@ -24,135 +24,135 @@ |
||
24 | 24 | class ConfigSchema extends AbstractConfigSchema |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * The key that is used in the schema to define a default value. |
|
29 | - */ |
|
30 | - const DEFAULT_VALUE = 'default'; |
|
31 | - /** |
|
32 | - * The key that is used in the schema to define a required value. |
|
33 | - */ |
|
34 | - const REQUIRED_KEY = 'required'; |
|
27 | + /** |
|
28 | + * The key that is used in the schema to define a default value. |
|
29 | + */ |
|
30 | + const DEFAULT_VALUE = 'default'; |
|
31 | + /** |
|
32 | + * The key that is used in the schema to define a required value. |
|
33 | + */ |
|
34 | + const REQUIRED_KEY = 'required'; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Instantiate a ConfigSchema object. |
|
38 | - * |
|
39 | - * @since 0.1.0 |
|
40 | - * |
|
41 | - * @param ConfigInterface|array $schema The schema to parse. |
|
42 | - * |
|
43 | - * @throws InvalidArgumentException |
|
44 | - */ |
|
45 | - public function __construct($schema) |
|
46 | - { |
|
47 | - if ($schema instanceof ConfigInterface) { |
|
48 | - $schema = $schema->getArrayCopy(); |
|
49 | - } |
|
36 | + /** |
|
37 | + * Instantiate a ConfigSchema object. |
|
38 | + * |
|
39 | + * @since 0.1.0 |
|
40 | + * |
|
41 | + * @param ConfigInterface|array $schema The schema to parse. |
|
42 | + * |
|
43 | + * @throws InvalidArgumentException |
|
44 | + */ |
|
45 | + public function __construct($schema) |
|
46 | + { |
|
47 | + if ($schema instanceof ConfigInterface) { |
|
48 | + $schema = $schema->getArrayCopy(); |
|
49 | + } |
|
50 | 50 | |
51 | - if (! is_array($schema)) { |
|
52 | - throw new InvalidArgumentException( |
|
53 | - sprintf( |
|
54 | - _('Invalid schema source: %1$s'), |
|
55 | - print_r($schema, true) |
|
56 | - ) |
|
57 | - ); |
|
58 | - } |
|
51 | + if (! is_array($schema)) { |
|
52 | + throw new InvalidArgumentException( |
|
53 | + sprintf( |
|
54 | + _('Invalid schema source: %1$s'), |
|
55 | + print_r($schema, true) |
|
56 | + ) |
|
57 | + ); |
|
58 | + } |
|
59 | 59 | |
60 | - array_walk($schema, [$this, 'parseSchema']); |
|
61 | - } |
|
60 | + array_walk($schema, [$this, 'parseSchema']); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Parse a single provided schema entry. |
|
65 | - * |
|
66 | - * @since 0.1.0 |
|
67 | - * |
|
68 | - * @param mixed $data The data associated with the key. |
|
69 | - * @param string $key The key of the schema data. |
|
70 | - */ |
|
71 | - protected function parseSchema($data, $key) |
|
72 | - { |
|
73 | - $this->parseDefined($key); |
|
63 | + /** |
|
64 | + * Parse a single provided schema entry. |
|
65 | + * |
|
66 | + * @since 0.1.0 |
|
67 | + * |
|
68 | + * @param mixed $data The data associated with the key. |
|
69 | + * @param string $key The key of the schema data. |
|
70 | + */ |
|
71 | + protected function parseSchema($data, $key) |
|
72 | + { |
|
73 | + $this->parseDefined($key); |
|
74 | 74 | |
75 | - if (array_key_exists(self::REQUIRED_KEY, $data)) { |
|
76 | - $this->parseRequired( |
|
77 | - $key, |
|
78 | - $data[self::REQUIRED_KEY] |
|
79 | - ); |
|
80 | - } |
|
75 | + if (array_key_exists(self::REQUIRED_KEY, $data)) { |
|
76 | + $this->parseRequired( |
|
77 | + $key, |
|
78 | + $data[self::REQUIRED_KEY] |
|
79 | + ); |
|
80 | + } |
|
81 | 81 | |
82 | - if (array_key_exists(self::DEFAULT_VALUE, $data)) { |
|
83 | - $this->parseDefault( |
|
84 | - $key, |
|
85 | - $data[self::DEFAULT_VALUE] |
|
86 | - ); |
|
87 | - } |
|
88 | - } |
|
82 | + if (array_key_exists(self::DEFAULT_VALUE, $data)) { |
|
83 | + $this->parseDefault( |
|
84 | + $key, |
|
85 | + $data[self::DEFAULT_VALUE] |
|
86 | + ); |
|
87 | + } |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Parse the set of defined values. |
|
92 | - * |
|
93 | - * @since 0.1.0 |
|
94 | - * |
|
95 | - * @param string $key The key of the schema data. |
|
96 | - */ |
|
97 | - protected function parseDefined($key) |
|
98 | - { |
|
99 | - $this->defined[] = $key; |
|
100 | - } |
|
90 | + /** |
|
91 | + * Parse the set of defined values. |
|
92 | + * |
|
93 | + * @since 0.1.0 |
|
94 | + * |
|
95 | + * @param string $key The key of the schema data. |
|
96 | + */ |
|
97 | + protected function parseDefined($key) |
|
98 | + { |
|
99 | + $this->defined[] = $key; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Parse the set of required values. |
|
104 | - * |
|
105 | - * @since 0.1.0 |
|
106 | - * |
|
107 | - * @param string $key The key of the schema data. |
|
108 | - * @param mixed $data The data associated with the key. |
|
109 | - */ |
|
110 | - protected function parseRequired($key, $data) |
|
111 | - { |
|
112 | - if ($this->isTruthy($data)) { |
|
113 | - $this->required[] = $key; |
|
114 | - } |
|
115 | - } |
|
102 | + /** |
|
103 | + * Parse the set of required values. |
|
104 | + * |
|
105 | + * @since 0.1.0 |
|
106 | + * |
|
107 | + * @param string $key The key of the schema data. |
|
108 | + * @param mixed $data The data associated with the key. |
|
109 | + */ |
|
110 | + protected function parseRequired($key, $data) |
|
111 | + { |
|
112 | + if ($this->isTruthy($data)) { |
|
113 | + $this->required[] = $key; |
|
114 | + } |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Parse the set of default values. |
|
119 | - * |
|
120 | - * @since 0.1.0 |
|
121 | - * |
|
122 | - * @param string $key The key of the schema data. |
|
123 | - * @param mixed $data The data associated with the key. |
|
124 | - */ |
|
125 | - protected function parseDefault($key, $data) |
|
126 | - { |
|
127 | - $this->defaults[$key] = $data; |
|
128 | - } |
|
117 | + /** |
|
118 | + * Parse the set of default values. |
|
119 | + * |
|
120 | + * @since 0.1.0 |
|
121 | + * |
|
122 | + * @param string $key The key of the schema data. |
|
123 | + * @param mixed $data The data associated with the key. |
|
124 | + */ |
|
125 | + protected function parseDefault($key, $data) |
|
126 | + { |
|
127 | + $this->defaults[$key] = $data; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Return a boolean true or false for an arbitrary set of data. Recognizes |
|
132 | - * several different string values that should be valued as true. |
|
133 | - * |
|
134 | - * @since 0.1.0 |
|
135 | - * |
|
136 | - * @param mixed $data The data to evaluate. |
|
137 | - * |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - protected function isTruthy($data) |
|
141 | - { |
|
142 | - $truthy_values = [ |
|
143 | - true, |
|
144 | - 1, |
|
145 | - 'true', |
|
146 | - 'True', |
|
147 | - 'TRUE', |
|
148 | - 'y', |
|
149 | - 'Y', |
|
150 | - 'yes', |
|
151 | - 'Yes', |
|
152 | - 'YES', |
|
153 | - '√', |
|
154 | - ]; |
|
130 | + /** |
|
131 | + * Return a boolean true or false for an arbitrary set of data. Recognizes |
|
132 | + * several different string values that should be valued as true. |
|
133 | + * |
|
134 | + * @since 0.1.0 |
|
135 | + * |
|
136 | + * @param mixed $data The data to evaluate. |
|
137 | + * |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + protected function isTruthy($data) |
|
141 | + { |
|
142 | + $truthy_values = [ |
|
143 | + true, |
|
144 | + 1, |
|
145 | + 'true', |
|
146 | + 'True', |
|
147 | + 'TRUE', |
|
148 | + 'y', |
|
149 | + 'Y', |
|
150 | + 'yes', |
|
151 | + 'Yes', |
|
152 | + 'YES', |
|
153 | + '√', |
|
154 | + ]; |
|
155 | 155 | |
156 | - return in_array($data, $truthy_values, true); |
|
157 | - } |
|
156 | + return in_array($data, $truthy_values, true); |
|
157 | + } |
|
158 | 158 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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 |
@@ -25,98 +25,98 @@ |
||
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 | - 'BrightNucleus\Config\Loader\JSONLoader', |
|
38 | - ]; |
|
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 | + 'BrightNucleus\Config\Loader\JSONLoader', |
|
38 | + ]; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Array of instantiated loaders. |
|
42 | - * |
|
43 | - * These are lazily instantiated and added as needed. |
|
44 | - * |
|
45 | - * @var LoaderInterface[] |
|
46 | - * |
|
47 | - * @since 0.4.0 |
|
48 | - */ |
|
49 | - protected static $loaderInstances = []; |
|
40 | + /** |
|
41 | + * Array of instantiated loaders. |
|
42 | + * |
|
43 | + * These are lazily instantiated and added as needed. |
|
44 | + * |
|
45 | + * @var LoaderInterface[] |
|
46 | + * |
|
47 | + * @since 0.4.0 |
|
48 | + */ |
|
49 | + protected static $loaderInstances = []; |
|
50 | 50 | |
51 | - /** |
|
52 | - * Create a new Loader from an URI. |
|
53 | - * |
|
54 | - * @since 0.4.0 |
|
55 | - * |
|
56 | - * @param string $uri URI of the resource to create a loader for. |
|
57 | - * |
|
58 | - * @return LoaderInterface Loader that is able to load the given URI. |
|
59 | - * @throws FailedToLoadConfigException If no suitable loader was found. |
|
60 | - */ |
|
61 | - public static function createFromUri($uri) |
|
62 | - { |
|
63 | - foreach (static::$loaders as $loader) { |
|
64 | - if ($loader::canLoad($uri)) { |
|
65 | - return static::getLoader($loader); |
|
66 | - } |
|
67 | - } |
|
51 | + /** |
|
52 | + * Create a new Loader from an URI. |
|
53 | + * |
|
54 | + * @since 0.4.0 |
|
55 | + * |
|
56 | + * @param string $uri URI of the resource to create a loader for. |
|
57 | + * |
|
58 | + * @return LoaderInterface Loader that is able to load the given URI. |
|
59 | + * @throws FailedToLoadConfigException If no suitable loader was found. |
|
60 | + */ |
|
61 | + public static function createFromUri($uri) |
|
62 | + { |
|
63 | + foreach (static::$loaders as $loader) { |
|
64 | + if ($loader::canLoad($uri)) { |
|
65 | + return static::getLoader($loader); |
|
66 | + } |
|
67 | + } |
|
68 | 68 | |
69 | - throw new FailedToLoadConfigException( |
|
70 | - sprintf( |
|
71 | - _('Could not find a suitable loader for URI "%1$s".'), |
|
72 | - $uri |
|
73 | - ) |
|
74 | - ); |
|
75 | - } |
|
69 | + throw new FailedToLoadConfigException( |
|
70 | + sprintf( |
|
71 | + _('Could not find a suitable loader for URI "%1$s".'), |
|
72 | + $uri |
|
73 | + ) |
|
74 | + ); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Get an instance of a specific loader. |
|
79 | - * |
|
80 | - * The loader is lazily instantiated if needed. |
|
81 | - * |
|
82 | - * @since 0.4.0 |
|
83 | - * |
|
84 | - * @param string $loaderClass Fully qualified class name of the loader to get. |
|
85 | - * |
|
86 | - * @return LoaderInterface Instance of the requested loader. |
|
87 | - * @throws FailedToLoadConfigException If the loader class could not be instantiated. |
|
88 | - */ |
|
89 | - public static function getLoader($loaderClass) |
|
90 | - { |
|
91 | - try { |
|
92 | - if (! array_key_exists($loaderClass, static::$loaderInstances)) { |
|
93 | - static::$loaderInstances[$loaderClass] = new $loaderClass; |
|
94 | - } |
|
77 | + /** |
|
78 | + * Get an instance of a specific loader. |
|
79 | + * |
|
80 | + * The loader is lazily instantiated if needed. |
|
81 | + * |
|
82 | + * @since 0.4.0 |
|
83 | + * |
|
84 | + * @param string $loaderClass Fully qualified class name of the loader to get. |
|
85 | + * |
|
86 | + * @return LoaderInterface Instance of the requested loader. |
|
87 | + * @throws FailedToLoadConfigException If the loader class could not be instantiated. |
|
88 | + */ |
|
89 | + public static function getLoader($loaderClass) |
|
90 | + { |
|
91 | + try { |
|
92 | + if (! array_key_exists($loaderClass, static::$loaderInstances)) { |
|
93 | + static::$loaderInstances[$loaderClass] = new $loaderClass; |
|
94 | + } |
|
95 | 95 | |
96 | - return static::$loaderInstances[$loaderClass]; |
|
97 | - } catch (Exception $exception) { |
|
98 | - throw new FailedToLoadConfigException( |
|
99 | - sprintf( |
|
100 | - _('Could not instantiate the requested loader class "%1$s".'), |
|
101 | - $loaderClass |
|
102 | - ) |
|
103 | - ); |
|
104 | - } |
|
105 | - } |
|
96 | + return static::$loaderInstances[$loaderClass]; |
|
97 | + } catch (Exception $exception) { |
|
98 | + throw new FailedToLoadConfigException( |
|
99 | + sprintf( |
|
100 | + _('Could not instantiate the requested loader class "%1$s".'), |
|
101 | + $loaderClass |
|
102 | + ) |
|
103 | + ); |
|
104 | + } |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Register a new loader. |
|
109 | - * |
|
110 | - * @since 0.4.0 |
|
111 | - * |
|
112 | - * @param string $loader Fully qualified class name of a loader implementing LoaderInterface. |
|
113 | - */ |
|
114 | - public static function registerLoader($loader) |
|
115 | - { |
|
116 | - if (in_array($loader, static::$loaders, true)) { |
|
117 | - return; |
|
118 | - } |
|
107 | + /** |
|
108 | + * Register a new loader. |
|
109 | + * |
|
110 | + * @since 0.4.0 |
|
111 | + * |
|
112 | + * @param string $loader Fully qualified class name of a loader implementing LoaderInterface. |
|
113 | + */ |
|
114 | + public static function registerLoader($loader) |
|
115 | + { |
|
116 | + if (in_array($loader, static::$loaders, true)) { |
|
117 | + return; |
|
118 | + } |
|
119 | 119 | |
120 | - static::$loaders [] = $loader; |
|
121 | - } |
|
120 | + static::$loaders [] = $loader; |
|
121 | + } |
|
122 | 122 | } |
@@ -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,76 +25,76 @@ |
||
25 | 25 | class PHPLoader extends AbstractLoader |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * Check whether the loader is able to load a given URI. |
|
30 | - * |
|
31 | - * @since 0.4.0 |
|
32 | - * |
|
33 | - * @param string $uri URI to check. |
|
34 | - * |
|
35 | - * @return bool Whether the loader can load the given URI. |
|
36 | - */ |
|
37 | - public static function canLoad($uri) |
|
38 | - { |
|
39 | - $path = pathinfo($uri); |
|
28 | + /** |
|
29 | + * Check whether the loader is able to load a given URI. |
|
30 | + * |
|
31 | + * @since 0.4.0 |
|
32 | + * |
|
33 | + * @param string $uri URI to check. |
|
34 | + * |
|
35 | + * @return bool Whether the loader can load the given URI. |
|
36 | + */ |
|
37 | + public static function canLoad($uri) |
|
38 | + { |
|
39 | + $path = pathinfo($uri); |
|
40 | 40 | |
41 | - return '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 | } |
@@ -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 | } |
@@ -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 |
@@ -32,188 +32,188 @@ |
||
32 | 32 | class Config extends AbstractConfig |
33 | 33 | { |
34 | 34 | |
35 | - /** |
|
36 | - * The schema of the Config file. |
|
37 | - * |
|
38 | - * @var Schema |
|
39 | - */ |
|
40 | - protected $schema; |
|
41 | - |
|
42 | - /** |
|
43 | - * The Validator class that gets asked to do the validation of the config. |
|
44 | - * |
|
45 | - * @since 0.1.0 |
|
46 | - * |
|
47 | - * @var Validator |
|
48 | - */ |
|
49 | - protected $validator; |
|
50 | - |
|
51 | - /** |
|
52 | - * Instantiate the Config object. |
|
53 | - * |
|
54 | - * It accepts either an array with the configuration settings, or a |
|
55 | - * filename pointing to a PHP file it can include. |
|
56 | - * |
|
57 | - * @since 0.1.0 |
|
58 | - * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
59 | - * |
|
60 | - * @param array|string $config Array with settings or filename for the |
|
61 | - * settings file. |
|
62 | - * @param Schema|null $schema Optional. Config that contains default |
|
63 | - * values that can get overwritten. |
|
64 | - * @param Validator|null $validator Optional. Validator class that does the |
|
65 | - * actual validation. |
|
66 | - * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
67 | - * configuration keys. Defaults to "\", "/" & ".". |
|
68 | - * |
|
69 | - * @throws InvalidConfigurationSourceException If the config source is not a string or array. |
|
70 | - * @throws FailedToInstantiateParentException If the parent class could not be instantiated. |
|
71 | - * @throws FailedToLoadConfigException If loading of the config source failed. |
|
72 | - * @throws FailedToResolveConfigException If the config file could not be resolved. |
|
73 | - * @throws InvalidConfigException If the config file is not valid. |
|
74 | - */ |
|
75 | - public function __construct( |
|
76 | - $config, |
|
77 | - ?Schema $schema = null, |
|
78 | - ?Validator $validator = null, |
|
79 | - $delimiter = null |
|
80 | - ) { |
|
81 | - $this->schema = $schema; |
|
82 | - $this->validator = $validator; |
|
83 | - |
|
84 | - // Make sure $config is either a string or array. |
|
85 | - if (! (is_string($config) || is_array($config))) { |
|
86 | - throw new InvalidConfigurationSourceException( |
|
87 | - sprintf( |
|
88 | - _('Invalid configuration source: %1$s'), |
|
89 | - print_r($config, true) |
|
90 | - ) |
|
91 | - ); |
|
92 | - } |
|
93 | - |
|
94 | - if (is_string($config)) { |
|
95 | - $config = Loader::load($config); |
|
96 | - } |
|
97 | - |
|
98 | - // Run the $config through the OptionsResolver. |
|
99 | - $config = $this->resolveOptions($config); |
|
100 | - |
|
101 | - // Instantiate the parent class. |
|
102 | - try { |
|
103 | - parent::__construct($config, $delimiter); |
|
104 | - } catch (Exception $exception) { |
|
105 | - throw new FailedToInstantiateParentException( |
|
106 | - sprintf( |
|
107 | - _('Could not instantiate the configuration through its parent. Reason: %1$s'), |
|
108 | - $exception->getMessage() |
|
109 | - ) |
|
110 | - ); |
|
111 | - } |
|
112 | - |
|
113 | - // Finally, validate the resulting config. |
|
114 | - if (! $this->isValid()) { |
|
115 | - throw new InvalidConfigException( |
|
116 | - sprintf( |
|
117 | - _('ConfigInterface file is not valid: %1$s'), |
|
118 | - print_r($config, true) |
|
119 | - ) |
|
120 | - ); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Validate the Config file. |
|
126 | - * |
|
127 | - * @since 0.1.0 |
|
128 | - * |
|
129 | - * @return boolean |
|
130 | - */ |
|
131 | - public function isValid() |
|
132 | - { |
|
133 | - if ($this->validator) { |
|
134 | - return $this->validator->isValid($this); |
|
135 | - } |
|
136 | - |
|
137 | - return true; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Process the passed-in defaults and merge them with the new values, while |
|
142 | - * checking that all required options are set. |
|
143 | - * |
|
144 | - * @since 0.1.0 |
|
145 | - * |
|
146 | - * @param array $config Configuration settings to resolve. |
|
147 | - * |
|
148 | - * @return array Resolved configuration settings. |
|
149 | - * @throws FailedToResolveConfigException If there are errors while resolving the options. |
|
150 | - */ |
|
151 | - protected function resolveOptions($config) |
|
152 | - { |
|
153 | - if (! $this->schema) { |
|
154 | - return $config; |
|
155 | - } |
|
156 | - |
|
157 | - try { |
|
158 | - $resolver = new OptionsResolver(); |
|
159 | - if ($this->configureOptions($resolver)) { |
|
160 | - $config = $resolver->resolve($config); |
|
161 | - } |
|
162 | - } catch (Exception $exception) { |
|
163 | - throw new FailedToResolveConfigException( |
|
164 | - sprintf( |
|
165 | - _('Error while resolving config options: %1$s'), |
|
166 | - $exception->getMessage() |
|
167 | - ) |
|
168 | - ); |
|
169 | - } |
|
170 | - |
|
171 | - return $config; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Configure the possible and required options for the Config. |
|
176 | - * |
|
177 | - * This should return a bool to let the resolve_options() know whether the |
|
178 | - * actual resolving needs to be done or not. |
|
179 | - * |
|
180 | - * @since 0.1.0 |
|
181 | - * |
|
182 | - * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
183 | - * instance. |
|
184 | - * |
|
185 | - * @return bool Whether to do the resolving. |
|
186 | - * @throws FailedToResolveConfigException If there are errors while processing. |
|
187 | - */ |
|
188 | - protected function configureOptions(OptionsResolver $resolver) |
|
189 | - { |
|
190 | - $defined = $this->schema->getDefinedOptions(); |
|
191 | - $defaults = $this->schema->getDefaultOptions(); |
|
192 | - $required = $this->schema->getRequiredOptions(); |
|
193 | - |
|
194 | - if (! $defined && ! $defaults && ! $required) { |
|
195 | - return false; |
|
196 | - } |
|
197 | - |
|
198 | - try { |
|
199 | - if ($defined) { |
|
200 | - $resolver->setDefined($defined); |
|
201 | - } |
|
202 | - if ($defaults) { |
|
203 | - $resolver->setDefaults($defaults); |
|
204 | - } |
|
205 | - if ($required) { |
|
206 | - $resolver->setRequired($required); |
|
207 | - } |
|
208 | - } catch (Exception $exception) { |
|
209 | - throw new FailedToResolveConfigException( |
|
210 | - sprintf( |
|
211 | - _('Error while processing config options: %1$s'), |
|
212 | - $exception->getMessage() |
|
213 | - ) |
|
214 | - ); |
|
215 | - } |
|
216 | - |
|
217 | - return true; |
|
218 | - } |
|
35 | + /** |
|
36 | + * The schema of the Config file. |
|
37 | + * |
|
38 | + * @var Schema |
|
39 | + */ |
|
40 | + protected $schema; |
|
41 | + |
|
42 | + /** |
|
43 | + * The Validator class that gets asked to do the validation of the config. |
|
44 | + * |
|
45 | + * @since 0.1.0 |
|
46 | + * |
|
47 | + * @var Validator |
|
48 | + */ |
|
49 | + protected $validator; |
|
50 | + |
|
51 | + /** |
|
52 | + * Instantiate the Config object. |
|
53 | + * |
|
54 | + * It accepts either an array with the configuration settings, or a |
|
55 | + * filename pointing to a PHP file it can include. |
|
56 | + * |
|
57 | + * @since 0.1.0 |
|
58 | + * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
|
59 | + * |
|
60 | + * @param array|string $config Array with settings or filename for the |
|
61 | + * settings file. |
|
62 | + * @param Schema|null $schema Optional. Config that contains default |
|
63 | + * values that can get overwritten. |
|
64 | + * @param Validator|null $validator Optional. Validator class that does the |
|
65 | + * actual validation. |
|
66 | + * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
|
67 | + * configuration keys. Defaults to "\", "/" & ".". |
|
68 | + * |
|
69 | + * @throws InvalidConfigurationSourceException If the config source is not a string or array. |
|
70 | + * @throws FailedToInstantiateParentException If the parent class could not be instantiated. |
|
71 | + * @throws FailedToLoadConfigException If loading of the config source failed. |
|
72 | + * @throws FailedToResolveConfigException If the config file could not be resolved. |
|
73 | + * @throws InvalidConfigException If the config file is not valid. |
|
74 | + */ |
|
75 | + public function __construct( |
|
76 | + $config, |
|
77 | + ?Schema $schema = null, |
|
78 | + ?Validator $validator = null, |
|
79 | + $delimiter = null |
|
80 | + ) { |
|
81 | + $this->schema = $schema; |
|
82 | + $this->validator = $validator; |
|
83 | + |
|
84 | + // Make sure $config is either a string or array. |
|
85 | + if (! (is_string($config) || is_array($config))) { |
|
86 | + throw new InvalidConfigurationSourceException( |
|
87 | + sprintf( |
|
88 | + _('Invalid configuration source: %1$s'), |
|
89 | + print_r($config, true) |
|
90 | + ) |
|
91 | + ); |
|
92 | + } |
|
93 | + |
|
94 | + if (is_string($config)) { |
|
95 | + $config = Loader::load($config); |
|
96 | + } |
|
97 | + |
|
98 | + // Run the $config through the OptionsResolver. |
|
99 | + $config = $this->resolveOptions($config); |
|
100 | + |
|
101 | + // Instantiate the parent class. |
|
102 | + try { |
|
103 | + parent::__construct($config, $delimiter); |
|
104 | + } catch (Exception $exception) { |
|
105 | + throw new FailedToInstantiateParentException( |
|
106 | + sprintf( |
|
107 | + _('Could not instantiate the configuration through its parent. Reason: %1$s'), |
|
108 | + $exception->getMessage() |
|
109 | + ) |
|
110 | + ); |
|
111 | + } |
|
112 | + |
|
113 | + // Finally, validate the resulting config. |
|
114 | + if (! $this->isValid()) { |
|
115 | + throw new InvalidConfigException( |
|
116 | + sprintf( |
|
117 | + _('ConfigInterface file is not valid: %1$s'), |
|
118 | + print_r($config, true) |
|
119 | + ) |
|
120 | + ); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Validate the Config file. |
|
126 | + * |
|
127 | + * @since 0.1.0 |
|
128 | + * |
|
129 | + * @return boolean |
|
130 | + */ |
|
131 | + public function isValid() |
|
132 | + { |
|
133 | + if ($this->validator) { |
|
134 | + return $this->validator->isValid($this); |
|
135 | + } |
|
136 | + |
|
137 | + return true; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Process the passed-in defaults and merge them with the new values, while |
|
142 | + * checking that all required options are set. |
|
143 | + * |
|
144 | + * @since 0.1.0 |
|
145 | + * |
|
146 | + * @param array $config Configuration settings to resolve. |
|
147 | + * |
|
148 | + * @return array Resolved configuration settings. |
|
149 | + * @throws FailedToResolveConfigException If there are errors while resolving the options. |
|
150 | + */ |
|
151 | + protected function resolveOptions($config) |
|
152 | + { |
|
153 | + if (! $this->schema) { |
|
154 | + return $config; |
|
155 | + } |
|
156 | + |
|
157 | + try { |
|
158 | + $resolver = new OptionsResolver(); |
|
159 | + if ($this->configureOptions($resolver)) { |
|
160 | + $config = $resolver->resolve($config); |
|
161 | + } |
|
162 | + } catch (Exception $exception) { |
|
163 | + throw new FailedToResolveConfigException( |
|
164 | + sprintf( |
|
165 | + _('Error while resolving config options: %1$s'), |
|
166 | + $exception->getMessage() |
|
167 | + ) |
|
168 | + ); |
|
169 | + } |
|
170 | + |
|
171 | + return $config; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Configure the possible and required options for the Config. |
|
176 | + * |
|
177 | + * This should return a bool to let the resolve_options() know whether the |
|
178 | + * actual resolving needs to be done or not. |
|
179 | + * |
|
180 | + * @since 0.1.0 |
|
181 | + * |
|
182 | + * @param OptionsResolver $resolver Reference to the OptionsResolver |
|
183 | + * instance. |
|
184 | + * |
|
185 | + * @return bool Whether to do the resolving. |
|
186 | + * @throws FailedToResolveConfigException If there are errors while processing. |
|
187 | + */ |
|
188 | + protected function configureOptions(OptionsResolver $resolver) |
|
189 | + { |
|
190 | + $defined = $this->schema->getDefinedOptions(); |
|
191 | + $defaults = $this->schema->getDefaultOptions(); |
|
192 | + $required = $this->schema->getRequiredOptions(); |
|
193 | + |
|
194 | + if (! $defined && ! $defaults && ! $required) { |
|
195 | + return false; |
|
196 | + } |
|
197 | + |
|
198 | + try { |
|
199 | + if ($defined) { |
|
200 | + $resolver->setDefined($defined); |
|
201 | + } |
|
202 | + if ($defaults) { |
|
203 | + $resolver->setDefaults($defaults); |
|
204 | + } |
|
205 | + if ($required) { |
|
206 | + $resolver->setRequired($required); |
|
207 | + } |
|
208 | + } catch (Exception $exception) { |
|
209 | + throw new FailedToResolveConfigException( |
|
210 | + sprintf( |
|
211 | + _('Error while processing config options: %1$s'), |
|
212 | + $exception->getMessage() |
|
213 | + ) |
|
214 | + ); |
|
215 | + } |
|
216 | + |
|
217 | + return true; |
|
218 | + } |
|
219 | 219 | } |