Passed
Push — main ( 3251c5...37e5f4 )
by
unknown
13:46
created
src/Loader/LoaderInterface.php 1 patch
Indentation   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -22,26 +22,26 @@
 block discarded – undo
22 22
 interface LoaderInterface
23 23
 {
24 24
 
25
-    /**
26
-     * Check whether the loader is able to load a given URI.
27
-     *
28
-     * @since 0.4.0
29
-     *
30
-     * @param string $uri URI to check.
31
-     *
32
-     * @return bool Whether the loader can load the given URI.
33
-     */
34
-    public static function canLoad($uri);
25
+	/**
26
+	 * Check whether the loader is able to load a given URI.
27
+	 *
28
+	 * @since 0.4.0
29
+	 *
30
+	 * @param string $uri URI to check.
31
+	 *
32
+	 * @return bool Whether the loader can load the given URI.
33
+	 */
34
+	public static function canLoad($uri);
35 35
 
36
-    /**
37
-     * Load the configuration from an URI.
38
-     *
39
-     * @since 0.4.0
40
-     *
41
-     * @param string $uri URI of the resource to load.
42
-     *
43
-     * @return array|null Data contained within the resource. Null if no data could be loaded/parsed.
44
-     * @throws FailedToLoadConfigException If the configuration could not be loaded.
45
-     */
46
-    public function load($uri);
36
+	/**
37
+	 * Load the configuration from an URI.
38
+	 *
39
+	 * @since 0.4.0
40
+	 *
41
+	 * @param string $uri URI of the resource to load.
42
+	 *
43
+	 * @return array|null Data contained within the resource. Null if no data could be loaded/parsed.
44
+	 * @throws FailedToLoadConfigException If the configuration could not be loaded.
45
+	 */
46
+	public function load($uri);
47 47
 }
Please login to merge, or discard this patch.
src/Loader/PHPLoader.php 1 patch
Indentation   +67 added lines, -67 removed lines patch added patch discarded remove patch
@@ -25,76 +25,76 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/ConfigFactory.php 1 patch
Indentation   +225 added lines, -225 removed lines patch added patch discarded remove patch
@@ -24,230 +24,230 @@
 block discarded – undo
24 24
 class ConfigFactory
25 25
 {
26 26
 
27
-    /**
28
-     * Cached contents of the config files.
29
-     *
30
-     * @since 0.4.3
31
-     *
32
-     * @var array
33
-     */
34
-    protected static $configFilesCache = [];
35
-
36
-    /**
37
-     * Create a new ConfigInterface object from a file.
38
-     *
39
-     * If a comma-separated list of files is provided, they are checked in sequence until the first one could be loaded
40
-     * successfully.
41
-     *
42
-     * @since 0.3.0
43
-     *
44
-     * @param string|array $_ List of files.
45
-     *
46
-     * @return ConfigInterface Instance of a ConfigInterface implementation.
47
-     */
48
-    public static function createFromFile($_)
49
-    {
50
-        $files = array_reverse(func_get_args());
51
-
52
-        if (is_array($files[0])) {
53
-            $files = $files[0];
54
-        }
55
-
56
-        while (count($files) > 0) {
57
-            try {
58
-                $file = array_pop($files);
59
-
60
-                if (! is_string($file)) {
61
-                    continue;
62
-                }
27
+	/**
28
+	 * Cached contents of the config files.
29
+	 *
30
+	 * @since 0.4.3
31
+	 *
32
+	 * @var array
33
+	 */
34
+	protected static $configFilesCache = [];
35
+
36
+	/**
37
+	 * Create a new ConfigInterface object from a file.
38
+	 *
39
+	 * If a comma-separated list of files is provided, they are checked in sequence until the first one could be loaded
40
+	 * successfully.
41
+	 *
42
+	 * @since 0.3.0
43
+	 *
44
+	 * @param string|array $_ List of files.
45
+	 *
46
+	 * @return ConfigInterface Instance of a ConfigInterface implementation.
47
+	 */
48
+	public static function createFromFile($_)
49
+	{
50
+		$files = array_reverse(func_get_args());
51
+
52
+		if (is_array($files[0])) {
53
+			$files = $files[0];
54
+		}
55
+
56
+		while (count($files) > 0) {
57
+			try {
58
+				$file = array_pop($files);
59
+
60
+				if (! is_string($file)) {
61
+					continue;
62
+				}
63 63
                 
64
-                if (! is_readable($file)) {
65
-                    continue;
66
-                }
67
-
68
-                $config = static::createFromArray(
69
-                    static::getFromCache($file, function ($file) {
70
-                        return Loader::load($file);
71
-                    })
72
-                );
73
-
74
-                if (null === $config) {
75
-                    continue;
76
-                }
77
-
78
-                return $config;
79
-            } catch (Exception $exception) {
80
-                // Fail silently and try next file.
81
-            }
82
-        }
83
-
84
-        return static::createFromArray([]);
85
-    }
86
-
87
-    /**
88
-     * Create a new ConfigInterface object from an array.
89
-     *
90
-     * @since 0.3.0
91
-     *
92
-     * @param array $array Array with configuration values.
93
-     *
94
-     * @return ConfigInterface Instance of a ConfigInterface implementation.
95
-     */
96
-    public static function createFromArray(array $array)
97
-    {
98
-        try {
99
-            return new Config($array);
100
-        } catch (Exception $exception) {
101
-            // Fail silently and try next file.
102
-        }
103
-
104
-        return null;
105
-    }
106
-
107
-    /**
108
-     * Create a new ConfigInterface object.
109
-     *
110
-     * Tries to deduce the correct creation method by inspecting the provided arguments.
111
-     *
112
-     * @since 0.3.0
113
-     *
114
-     * @param mixed $_ Array with configuration values.
115
-     *
116
-     * @return ConfigInterface Instance of a ConfigInterface implementation.
117
-     */
118
-    public static function create($_)
119
-    {
120
-        if (func_num_args() < 1) {
121
-            return static::createFromArray([]);
122
-        }
123
-
124
-        $arguments = func_get_args();
125
-
126
-        if (is_array($arguments[0]) && func_num_args() === 1) {
127
-            return static::createFromArray($arguments[0]);
128
-        }
129
-
130
-        return static::createFromFile($arguments);
131
-    }
132
-
133
-    /**
134
-     * Create a new ConfigInterface object, by merging several files together.
135
-     *
136
-     * Duplicate keys in later files will override those in earlier files.
137
-     *
138
-     * @since 0.4.6
139
-     *
140
-     * @param mixed $_ Array with configuration values.
141
-     *
142
-     * @return ConfigInterface Instance of a ConfigInterface implementation.
143
-     */
144
-    public static function merge($_)
145
-    {
146
-        if (func_num_args() < 1) {
147
-            return static::createFromArray([]);
148
-        }
149
-
150
-        $arguments = func_get_args();
151
-
152
-        if (is_array($arguments[0]) && func_num_args() === 1) {
153
-            return static::createFromArray($arguments[0]);
154
-        }
155
-
156
-        return static::mergeFromFiles($arguments);
157
-    }
158
-
159
-    /**
160
-     * Create a new ConfigInterface object by merging data from several files.
161
-     *
162
-     * If a comma-separated list of files is provided, they are loaded in sequence and later files override settings in
163
-     * earlier files.
164
-     *
165
-     * @since 0.4.6
166
-     *
167
-     * @param string|array $_ List of files.
168
-     *
169
-     * @return ConfigInterface Instance of a ConfigInterface implementation.
170
-     */
171
-    public static function mergeFromFiles($_)
172
-    {
173
-        $files = array_reverse(func_get_args());
174
-        $data  = [];
175
-
176
-        if (is_array($files[0])) {
177
-            $files = array_reverse($files[0]);
178
-        }
179
-
180
-        while (count($files) > 0) {
181
-            try {
182
-                $file = array_pop($files);
183
-
184
-                if (! is_readable($file)) {
185
-                    continue;
186
-                }
187
-
188
-                $new_data = static::getFromCache($file, function ($file) {
189
-                    return Loader::load($file);
190
-                });
191
-
192
-                if (null === $data) {
193
-                    continue;
194
-                }
195
-
196
-                $data = array_replace_recursive($data, $new_data);
197
-            } catch (Exception $exception) {
198
-                // Fail silently and try next file.
199
-            }
200
-        }
201
-
202
-        return static::createFromArray($data);
203
-    }
204
-
205
-    /**
206
-     * Create a new ConfigInterface object from a file and return a sub-portion of it.
207
-     *
208
-     * The first argument needs to be the file name to load, and the subsequent arguments will be passed on to
209
-     * `Config::getSubConfig()`.
210
-     *
211
-     * @since 0.4.5
212
-     *
213
-     * @param mixed $_ File name of the config to load as a string, followed by an array of keys to pass to
214
-     *                 `Config::getSubConfig()`.
215
-     *
216
-     * @return ConfigInterface Instance of a ConfigInterface implementation.
217
-     */
218
-    public static function createSubConfig($_)
219
-    {
220
-        if (func_num_args() < 2) {
221
-            return static::createFromArray([]);
222
-        }
223
-
224
-        $arguments = func_get_args();
225
-        $file      = array_shift($arguments);
226
-
227
-        $config = static::createFromFile($file);
228
-
229
-        return $config->getSubConfig($arguments);
230
-    }
231
-
232
-    /**
233
-     * Get a config file from the config file cache.
234
-     *
235
-     * @since 0.4.4
236
-     *
237
-     * @param string $identifier Identifier to look for in the cache.
238
-     * @param mixed  $fallback   Fallback to use to fill the cache. If $fallback is a callable, it will be executed
239
-     *                           with $identifier as an argument.
240
-     *
241
-     * @return mixed The latest content of the cache for the given identifier.
242
-     */
243
-    protected static function getFromCache($identifier, $fallback)
244
-    {
245
-        if (! array_key_exists($identifier, static::$configFilesCache)) {
246
-            static::$configFilesCache[$identifier] = is_callable($fallback)
247
-                ? $fallback($identifier)
248
-                : $fallback;
249
-        }
250
-
251
-        return static::$configFilesCache[$identifier];
252
-    }
64
+				if (! is_readable($file)) {
65
+					continue;
66
+				}
67
+
68
+				$config = static::createFromArray(
69
+					static::getFromCache($file, function ($file) {
70
+						return Loader::load($file);
71
+					})
72
+				);
73
+
74
+				if (null === $config) {
75
+					continue;
76
+				}
77
+
78
+				return $config;
79
+			} catch (Exception $exception) {
80
+				// Fail silently and try next file.
81
+			}
82
+		}
83
+
84
+		return static::createFromArray([]);
85
+	}
86
+
87
+	/**
88
+	 * Create a new ConfigInterface object from an array.
89
+	 *
90
+	 * @since 0.3.0
91
+	 *
92
+	 * @param array $array Array with configuration values.
93
+	 *
94
+	 * @return ConfigInterface Instance of a ConfigInterface implementation.
95
+	 */
96
+	public static function createFromArray(array $array)
97
+	{
98
+		try {
99
+			return new Config($array);
100
+		} catch (Exception $exception) {
101
+			// Fail silently and try next file.
102
+		}
103
+
104
+		return null;
105
+	}
106
+
107
+	/**
108
+	 * Create a new ConfigInterface object.
109
+	 *
110
+	 * Tries to deduce the correct creation method by inspecting the provided arguments.
111
+	 *
112
+	 * @since 0.3.0
113
+	 *
114
+	 * @param mixed $_ Array with configuration values.
115
+	 *
116
+	 * @return ConfigInterface Instance of a ConfigInterface implementation.
117
+	 */
118
+	public static function create($_)
119
+	{
120
+		if (func_num_args() < 1) {
121
+			return static::createFromArray([]);
122
+		}
123
+
124
+		$arguments = func_get_args();
125
+
126
+		if (is_array($arguments[0]) && func_num_args() === 1) {
127
+			return static::createFromArray($arguments[0]);
128
+		}
129
+
130
+		return static::createFromFile($arguments);
131
+	}
132
+
133
+	/**
134
+	 * Create a new ConfigInterface object, by merging several files together.
135
+	 *
136
+	 * Duplicate keys in later files will override those in earlier files.
137
+	 *
138
+	 * @since 0.4.6
139
+	 *
140
+	 * @param mixed $_ Array with configuration values.
141
+	 *
142
+	 * @return ConfigInterface Instance of a ConfigInterface implementation.
143
+	 */
144
+	public static function merge($_)
145
+	{
146
+		if (func_num_args() < 1) {
147
+			return static::createFromArray([]);
148
+		}
149
+
150
+		$arguments = func_get_args();
151
+
152
+		if (is_array($arguments[0]) && func_num_args() === 1) {
153
+			return static::createFromArray($arguments[0]);
154
+		}
155
+
156
+		return static::mergeFromFiles($arguments);
157
+	}
158
+
159
+	/**
160
+	 * Create a new ConfigInterface object by merging data from several files.
161
+	 *
162
+	 * If a comma-separated list of files is provided, they are loaded in sequence and later files override settings in
163
+	 * earlier files.
164
+	 *
165
+	 * @since 0.4.6
166
+	 *
167
+	 * @param string|array $_ List of files.
168
+	 *
169
+	 * @return ConfigInterface Instance of a ConfigInterface implementation.
170
+	 */
171
+	public static function mergeFromFiles($_)
172
+	{
173
+		$files = array_reverse(func_get_args());
174
+		$data  = [];
175
+
176
+		if (is_array($files[0])) {
177
+			$files = array_reverse($files[0]);
178
+		}
179
+
180
+		while (count($files) > 0) {
181
+			try {
182
+				$file = array_pop($files);
183
+
184
+				if (! is_readable($file)) {
185
+					continue;
186
+				}
187
+
188
+				$new_data = static::getFromCache($file, function ($file) {
189
+					return Loader::load($file);
190
+				});
191
+
192
+				if (null === $data) {
193
+					continue;
194
+				}
195
+
196
+				$data = array_replace_recursive($data, $new_data);
197
+			} catch (Exception $exception) {
198
+				// Fail silently and try next file.
199
+			}
200
+		}
201
+
202
+		return static::createFromArray($data);
203
+	}
204
+
205
+	/**
206
+	 * Create a new ConfigInterface object from a file and return a sub-portion of it.
207
+	 *
208
+	 * The first argument needs to be the file name to load, and the subsequent arguments will be passed on to
209
+	 * `Config::getSubConfig()`.
210
+	 *
211
+	 * @since 0.4.5
212
+	 *
213
+	 * @param mixed $_ File name of the config to load as a string, followed by an array of keys to pass to
214
+	 *                 `Config::getSubConfig()`.
215
+	 *
216
+	 * @return ConfigInterface Instance of a ConfigInterface implementation.
217
+	 */
218
+	public static function createSubConfig($_)
219
+	{
220
+		if (func_num_args() < 2) {
221
+			return static::createFromArray([]);
222
+		}
223
+
224
+		$arguments = func_get_args();
225
+		$file      = array_shift($arguments);
226
+
227
+		$config = static::createFromFile($file);
228
+
229
+		return $config->getSubConfig($arguments);
230
+	}
231
+
232
+	/**
233
+	 * Get a config file from the config file cache.
234
+	 *
235
+	 * @since 0.4.4
236
+	 *
237
+	 * @param string $identifier Identifier to look for in the cache.
238
+	 * @param mixed  $fallback   Fallback to use to fill the cache. If $fallback is a callable, it will be executed
239
+	 *                           with $identifier as an argument.
240
+	 *
241
+	 * @return mixed The latest content of the cache for the given identifier.
242
+	 */
243
+	protected static function getFromCache($identifier, $fallback)
244
+	{
245
+		if (! array_key_exists($identifier, static::$configFilesCache)) {
246
+			static::$configFilesCache[$identifier] = is_callable($fallback)
247
+				? $fallback($identifier)
248
+				: $fallback;
249
+		}
250
+
251
+		return static::$configFilesCache[$identifier];
252
+	}
253 253
 }
Please login to merge, or discard this patch.
src/Config.php 1 patch
Indentation   +184 added lines, -184 removed lines patch added patch discarded remove patch
@@ -32,188 +32,188 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/ConfigInterface.php 1 patch
Indentation   +88 added lines, -88 removed lines patch added patch discarded remove patch
@@ -27,100 +27,100 @@
 block discarded – undo
27 27
 interface ConfigInterface extends IteratorAggregate, ArrayAccess, Serializable, Countable
28 28
 {
29 29
 
30
-    /**
31
-     * Creates a copy of the ArrayObject.
32
-     *
33
-     * Returns a copy of the array. When the ArrayObject refers to an object an
34
-     * array of the public properties of that object will be returned.
35
-     * This is implemented by \ArrayObject.
36
-     *
37
-     * @since 0.1.0
38
-     *
39
-     * @return array Copy of the array.
40
-     */
41
-    public function getArrayCopy();
30
+	/**
31
+	 * Creates a copy of the ArrayObject.
32
+	 *
33
+	 * Returns a copy of the array. When the ArrayObject refers to an object an
34
+	 * array of the public properties of that object will be returned.
35
+	 * This is implemented by \ArrayObject.
36
+	 *
37
+	 * @since 0.1.0
38
+	 *
39
+	 * @return array Copy of the array.
40
+	 */
41
+	public function getArrayCopy();
42 42
 
43
-    /**
44
-     * Check whether the Config has a specific key.
45
-     *
46
-     * To check a value several levels deep, add the keys for each level as a comma-separated list.
47
-     *
48
-     * @since 0.1.0
49
-     * @since 0.1.4 Accepts list of keys.
50
-     *
51
-     * @param string $_ List of keys.
52
-     *
53
-     * @return bool
54
-     */
55
-    public function hasKey($_);
43
+	/**
44
+	 * Check whether the Config has a specific key.
45
+	 *
46
+	 * To check a value several levels deep, add the keys for each level as a comma-separated list.
47
+	 *
48
+	 * @since 0.1.0
49
+	 * @since 0.1.4 Accepts list of keys.
50
+	 *
51
+	 * @param string $_ List of keys.
52
+	 *
53
+	 * @return bool
54
+	 */
55
+	public function hasKey($_);
56 56
 
57
-    /**
58
-     * Get the value of a specific key.
59
-     *
60
-     * To get a value several levels deep, add the keys for each level as a comma-separated list.
61
-     *
62
-     * @since 0.1.0
63
-     * @since 0.1.4 Accepts list of keys.
64
-     *
65
-     * @param string $_ List of keys.
66
-     *
67
-     * @return mixed
68
-     */
69
-    public function getKey($_);
57
+	/**
58
+	 * Get the value of a specific key.
59
+	 *
60
+	 * To get a value several levels deep, add the keys for each level as a comma-separated list.
61
+	 *
62
+	 * @since 0.1.0
63
+	 * @since 0.1.4 Accepts list of keys.
64
+	 *
65
+	 * @param string $_ List of keys.
66
+	 *
67
+	 * @return mixed
68
+	 */
69
+	public function getKey($_);
70 70
 
71
-    /**
72
-     * Get a (multi-dimensional) array of all the configuration settings.
73
-     *
74
-     * @since 0.1.4
75
-     *
76
-     * @return array
77
-     */
78
-    public function getAll();
71
+	/**
72
+	 * Get a (multi-dimensional) array of all the configuration settings.
73
+	 *
74
+	 * @since 0.1.4
75
+	 *
76
+	 * @return array
77
+	 */
78
+	public function getAll();
79 79
 
80
-    /**
81
-     * Get the an array with all the keys
82
-     *
83
-     * @since 0.1.0
84
-     *
85
-     * @return mixed
86
-     */
87
-    public function getKeys();
80
+	/**
81
+	 * Get the an array with all the keys
82
+	 *
83
+	 * @since 0.1.0
84
+	 *
85
+	 * @return mixed
86
+	 */
87
+	public function getKeys();
88 88
 
89
-    /**
90
-     * Is the Config valid?
91
-     *
92
-     * @since 0.1.0
93
-     *
94
-     * @return boolean
95
-     */
96
-    public function isValid();
89
+	/**
90
+	 * Is the Config valid?
91
+	 *
92
+	 * @since 0.1.0
93
+	 *
94
+	 * @return boolean
95
+	 */
96
+	public function isValid();
97 97
 
98
-    /**
99
-     * Get a new config at a specific sub-level.
100
-     *
101
-     * @since 0.1.13
102
-     *
103
-     * @param string $_ List of keys.
104
-     *
105
-     * @return ConfigInterface
106
-     */
107
-    public function getSubConfig($_);
98
+	/**
99
+	 * Get a new config at a specific sub-level.
100
+	 *
101
+	 * @since 0.1.13
102
+	 *
103
+	 * @param string $_ List of keys.
104
+	 *
105
+	 * @return ConfigInterface
106
+	 */
107
+	public function getSubConfig($_);
108 108
 
109
-    /**
110
-     * Serialize the config.
111
-     *
112
-     * @since 0.1.13
113
-     *
114
-     * @return array
115
-     */
116
-    public function __serialize(): array;
109
+	/**
110
+	 * Serialize the config.
111
+	 *
112
+	 * @since 0.1.13
113
+	 *
114
+	 * @return array
115
+	 */
116
+	public function __serialize(): array;
117 117
 
118
-    /**
119
-     * Unserialize the config.
120
-     *
121
-     * @since 0.1.13
122
-     *
123
-     * @param array $data The data to unserialize.
124
-     */
125
-    public function __unserialize(array $data): void;
118
+	/**
119
+	 * Unserialize the config.
120
+	 *
121
+	 * @since 0.1.13
122
+	 *
123
+	 * @param array $data The data to unserialize.
124
+	 */
125
+	public function __unserialize(array $data): void;
126 126
 }
Please login to merge, or discard this patch.