@@ -25,164 +25,164 @@ |
||
| 25 | 25 | trait ConfigTrait |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Reference to the Config object. |
|
| 30 | - * |
|
| 31 | - * @since 0.1.2 |
|
| 32 | - * |
|
| 33 | - * @var ConfigInterface |
|
| 34 | - */ |
|
| 35 | - protected $config; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Process the passed-in configuration file. |
|
| 39 | - * |
|
| 40 | - * @since 0.1.2 |
|
| 41 | - * |
|
| 42 | - * @param ConfigInterface $config The Config to process. |
|
| 43 | - * @param string ... List of keys. |
|
| 44 | - * |
|
| 45 | - * @throws FailedToProcessConfigException If the arguments could not be parsed into a Config. |
|
| 46 | - */ |
|
| 47 | - protected function processConfig(ConfigInterface $config) |
|
| 48 | - { |
|
| 49 | - if (func_num_args() > 1) { |
|
| 50 | - try { |
|
| 51 | - $keys = func_get_args(); |
|
| 52 | - array_shift($keys); |
|
| 53 | - $config = $config->getSubConfig($keys); |
|
| 54 | - } catch (Exception $exception) { |
|
| 55 | - throw new FailedToProcessConfigException( |
|
| 56 | - sprintf( |
|
| 57 | - _('Could not process the config with the arguments "%1$s".'), |
|
| 58 | - print_r(func_get_args(), true) |
|
| 59 | - ) |
|
| 60 | - ); |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - $this->config = $config; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Check whether the Config has a specific key. |
|
| 68 | - * |
|
| 69 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 70 | - * |
|
| 71 | - * @since 0.1.2 |
|
| 72 | - * @since 0.1.5 Accepts list of keys. |
|
| 73 | - * |
|
| 74 | - * @param string|array $_ List of keys. |
|
| 75 | - * |
|
| 76 | - * @return bool Whether the key is known. |
|
| 77 | - */ |
|
| 78 | - protected function hasConfigKey($_) |
|
| 79 | - { |
|
| 80 | - $keys = func_get_args(); |
|
| 81 | - |
|
| 82 | - return $this->config->hasKey($keys); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Get the Config value for a specific key. |
|
| 87 | - * |
|
| 88 | - * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 89 | - * |
|
| 90 | - * @since 0.1.2 |
|
| 91 | - * @since 0.1.5 Accepts list of keys. |
|
| 92 | - * |
|
| 93 | - * @param string|array $_ List of keys. |
|
| 94 | - * |
|
| 95 | - * @return mixed Value of the key. |
|
| 96 | - */ |
|
| 97 | - protected function getConfigKey($_) |
|
| 98 | - { |
|
| 99 | - $keys = func_get_args(); |
|
| 100 | - |
|
| 101 | - return $this->config->getKey($keys); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Get the callable Config value for a specific key. |
|
| 106 | - * |
|
| 107 | - * If the fetched value is indeed a callable, it will be executed with the provided arguments, and the resultant |
|
| 108 | - * value will be returned instead. |
|
| 109 | - * |
|
| 110 | - * @since 0.4.8 |
|
| 111 | - * |
|
| 112 | - * @param string|array $key Key or array of nested keys. |
|
| 113 | - * @param array $args Optional. Array of arguments to pass to the callable. |
|
| 114 | - * |
|
| 115 | - * @return mixed Resultant value of the key's callable. |
|
| 116 | - */ |
|
| 117 | - protected function getConfigCallable($key, array $args = []) |
|
| 118 | - { |
|
| 119 | - $value = $this->config->getKey($key); |
|
| 120 | - |
|
| 121 | - if (is_callable($value)) { |
|
| 122 | - $value = $value(...$args); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - return $value; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Get a (multi-dimensional) array of all the configuration settings. |
|
| 130 | - * |
|
| 131 | - * @since 0.1.4 |
|
| 132 | - * |
|
| 133 | - * @return array All the configuration settings. |
|
| 134 | - */ |
|
| 135 | - protected function getConfigArray() |
|
| 136 | - { |
|
| 137 | - return $this->config->getAll(); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Get an array of all the keys that are known by the Config. |
|
| 142 | - * |
|
| 143 | - * @since 0.1.2 |
|
| 144 | - * |
|
| 145 | - * @return array Array of strings containing all the keys. |
|
| 146 | - */ |
|
| 147 | - protected function getConfigKeys() |
|
| 148 | - { |
|
| 149 | - return $this->config->getKeys(); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Get a default configuration in case none was injected into the constructor. |
|
| 154 | - * |
|
| 155 | - * The name and path of the configuration needs to be set as a const called DEFAULT_CONFIG within the class |
|
| 156 | - * containing the trait. The path needs to be relative to the location of the containing class file. |
|
| 157 | - * |
|
| 158 | - * @since 0.4.2 |
|
| 159 | - * |
|
| 160 | - * @return ConfigInterface Configuration settings to use. |
|
| 161 | - */ |
|
| 162 | - protected function fetchDefaultConfig() |
|
| 163 | - { |
|
| 164 | - $configFile = method_exists($this, 'getDefaultConfigFile') |
|
| 165 | - ? $this->getDefaultConfigFile() |
|
| 166 | - : __DIR__ . '/../config/defaults.php'; |
|
| 167 | - |
|
| 168 | - return $this->fetchConfig($configFile); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Get a configuration from a specified $file. |
|
| 173 | - * |
|
| 174 | - * If file is not accessible or readable, returns an empty Config. |
|
| 175 | - * |
|
| 176 | - * @since 0.4.2 |
|
| 177 | - * |
|
| 178 | - * @return ConfigInterface Configuration settings to use. |
|
| 179 | - */ |
|
| 180 | - protected function fetchConfig($configFile) |
|
| 181 | - { |
|
| 182 | - if (is_string($configFile) && ! is_readable($configFile)) { |
|
| 183 | - $configFile = []; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - return ConfigFactory::create($configFile); |
|
| 187 | - } |
|
| 28 | + /** |
|
| 29 | + * Reference to the Config object. |
|
| 30 | + * |
|
| 31 | + * @since 0.1.2 |
|
| 32 | + * |
|
| 33 | + * @var ConfigInterface |
|
| 34 | + */ |
|
| 35 | + protected $config; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Process the passed-in configuration file. |
|
| 39 | + * |
|
| 40 | + * @since 0.1.2 |
|
| 41 | + * |
|
| 42 | + * @param ConfigInterface $config The Config to process. |
|
| 43 | + * @param string ... List of keys. |
|
| 44 | + * |
|
| 45 | + * @throws FailedToProcessConfigException If the arguments could not be parsed into a Config. |
|
| 46 | + */ |
|
| 47 | + protected function processConfig(ConfigInterface $config) |
|
| 48 | + { |
|
| 49 | + if (func_num_args() > 1) { |
|
| 50 | + try { |
|
| 51 | + $keys = func_get_args(); |
|
| 52 | + array_shift($keys); |
|
| 53 | + $config = $config->getSubConfig($keys); |
|
| 54 | + } catch (Exception $exception) { |
|
| 55 | + throw new FailedToProcessConfigException( |
|
| 56 | + sprintf( |
|
| 57 | + _('Could not process the config with the arguments "%1$s".'), |
|
| 58 | + print_r(func_get_args(), true) |
|
| 59 | + ) |
|
| 60 | + ); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + $this->config = $config; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Check whether the Config has a specific key. |
|
| 68 | + * |
|
| 69 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 70 | + * |
|
| 71 | + * @since 0.1.2 |
|
| 72 | + * @since 0.1.5 Accepts list of keys. |
|
| 73 | + * |
|
| 74 | + * @param string|array $_ List of keys. |
|
| 75 | + * |
|
| 76 | + * @return bool Whether the key is known. |
|
| 77 | + */ |
|
| 78 | + protected function hasConfigKey($_) |
|
| 79 | + { |
|
| 80 | + $keys = func_get_args(); |
|
| 81 | + |
|
| 82 | + return $this->config->hasKey($keys); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Get the Config value for a specific key. |
|
| 87 | + * |
|
| 88 | + * To get a value several levels deep, add the keys for each level as a comma-separated list. |
|
| 89 | + * |
|
| 90 | + * @since 0.1.2 |
|
| 91 | + * @since 0.1.5 Accepts list of keys. |
|
| 92 | + * |
|
| 93 | + * @param string|array $_ List of keys. |
|
| 94 | + * |
|
| 95 | + * @return mixed Value of the key. |
|
| 96 | + */ |
|
| 97 | + protected function getConfigKey($_) |
|
| 98 | + { |
|
| 99 | + $keys = func_get_args(); |
|
| 100 | + |
|
| 101 | + return $this->config->getKey($keys); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Get the callable Config value for a specific key. |
|
| 106 | + * |
|
| 107 | + * If the fetched value is indeed a callable, it will be executed with the provided arguments, and the resultant |
|
| 108 | + * value will be returned instead. |
|
| 109 | + * |
|
| 110 | + * @since 0.4.8 |
|
| 111 | + * |
|
| 112 | + * @param string|array $key Key or array of nested keys. |
|
| 113 | + * @param array $args Optional. Array of arguments to pass to the callable. |
|
| 114 | + * |
|
| 115 | + * @return mixed Resultant value of the key's callable. |
|
| 116 | + */ |
|
| 117 | + protected function getConfigCallable($key, array $args = []) |
|
| 118 | + { |
|
| 119 | + $value = $this->config->getKey($key); |
|
| 120 | + |
|
| 121 | + if (is_callable($value)) { |
|
| 122 | + $value = $value(...$args); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + return $value; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Get a (multi-dimensional) array of all the configuration settings. |
|
| 130 | + * |
|
| 131 | + * @since 0.1.4 |
|
| 132 | + * |
|
| 133 | + * @return array All the configuration settings. |
|
| 134 | + */ |
|
| 135 | + protected function getConfigArray() |
|
| 136 | + { |
|
| 137 | + return $this->config->getAll(); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Get an array of all the keys that are known by the Config. |
|
| 142 | + * |
|
| 143 | + * @since 0.1.2 |
|
| 144 | + * |
|
| 145 | + * @return array Array of strings containing all the keys. |
|
| 146 | + */ |
|
| 147 | + protected function getConfigKeys() |
|
| 148 | + { |
|
| 149 | + return $this->config->getKeys(); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Get a default configuration in case none was injected into the constructor. |
|
| 154 | + * |
|
| 155 | + * The name and path of the configuration needs to be set as a const called DEFAULT_CONFIG within the class |
|
| 156 | + * containing the trait. The path needs to be relative to the location of the containing class file. |
|
| 157 | + * |
|
| 158 | + * @since 0.4.2 |
|
| 159 | + * |
|
| 160 | + * @return ConfigInterface Configuration settings to use. |
|
| 161 | + */ |
|
| 162 | + protected function fetchDefaultConfig() |
|
| 163 | + { |
|
| 164 | + $configFile = method_exists($this, 'getDefaultConfigFile') |
|
| 165 | + ? $this->getDefaultConfigFile() |
|
| 166 | + : __DIR__ . '/../config/defaults.php'; |
|
| 167 | + |
|
| 168 | + return $this->fetchConfig($configFile); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Get a configuration from a specified $file. |
|
| 173 | + * |
|
| 174 | + * If file is not accessible or readable, returns an empty Config. |
|
| 175 | + * |
|
| 176 | + * @since 0.4.2 |
|
| 177 | + * |
|
| 178 | + * @return ConfigInterface Configuration settings to use. |
|
| 179 | + */ |
|
| 180 | + protected function fetchConfig($configFile) |
|
| 181 | + { |
|
| 182 | + if (is_string($configFile) && ! is_readable($configFile)) { |
|
| 183 | + $configFile = []; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + return ConfigFactory::create($configFile); |
|
| 187 | + } |
|
| 188 | 188 | } |
@@ -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 | } |
@@ -25,74 +25,74 @@ |
||
| 25 | 25 | class JSONLoader 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 'json' === mb_strtolower($path['extension']); |
|
| 42 | - } |
|
| 41 | + return 'json' === 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 | - ob_start(); |
|
| 58 | - $data = json_decode(file_get_contents($uri), true); |
|
| 59 | - 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 | + ob_start(); |
|
| 58 | + $data = json_decode(file_get_contents($uri), true); |
|
| 59 | + ob_end_clean(); |
|
| 60 | 60 | |
| 61 | - return (array)$data; |
|
| 62 | - } catch (Exception $exception) { |
|
| 63 | - throw new FailedToLoadConfigException( |
|
| 64 | - sprintf( |
|
| 65 | - _('Could not include JSON config file "%1$s". Reason: "%2$s".'), |
|
| 66 | - $uri, |
|
| 67 | - $exception->getMessage() |
|
| 68 | - ), |
|
| 69 | - $exception->getCode(), |
|
| 70 | - $exception |
|
| 71 | - ); |
|
| 72 | - } |
|
| 73 | - } |
|
| 61 | + return (array)$data; |
|
| 62 | + } catch (Exception $exception) { |
|
| 63 | + throw new FailedToLoadConfigException( |
|
| 64 | + sprintf( |
|
| 65 | + _('Could not include JSON config file "%1$s". Reason: "%2$s".'), |
|
| 66 | + $uri, |
|
| 67 | + $exception->getMessage() |
|
| 68 | + ), |
|
| 69 | + $exception->getCode(), |
|
| 70 | + $exception |
|
| 71 | + ); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Validate and return the URI. |
|
| 77 | - * |
|
| 78 | - * @since 0.4.0 |
|
| 79 | - * |
|
| 80 | - * @param string $uri URI of the resource to load. |
|
| 81 | - * |
|
| 82 | - * @return string Validated URI. |
|
| 83 | - * @throws FailedToLoadConfigException If the URI does not exist or is not readable. |
|
| 84 | - */ |
|
| 85 | - protected function validateUri($uri) |
|
| 86 | - { |
|
| 87 | - if (! is_readable($uri)) { |
|
| 88 | - throw new FailedToLoadConfigException( |
|
| 89 | - sprintf( |
|
| 90 | - _('The requested JSON config file "%1$s" does not exist or is not readable.'), |
|
| 91 | - $uri |
|
| 92 | - ) |
|
| 93 | - ); |
|
| 94 | - } |
|
| 75 | + /** |
|
| 76 | + * Validate and return the URI. |
|
| 77 | + * |
|
| 78 | + * @since 0.4.0 |
|
| 79 | + * |
|
| 80 | + * @param string $uri URI of the resource to load. |
|
| 81 | + * |
|
| 82 | + * @return string Validated URI. |
|
| 83 | + * @throws FailedToLoadConfigException If the URI does not exist or is not readable. |
|
| 84 | + */ |
|
| 85 | + protected function validateUri($uri) |
|
| 86 | + { |
|
| 87 | + if (! is_readable($uri)) { |
|
| 88 | + throw new FailedToLoadConfigException( |
|
| 89 | + sprintf( |
|
| 90 | + _('The requested JSON config file "%1$s" does not exist or is not readable.'), |
|
| 91 | + $uri |
|
| 92 | + ) |
|
| 93 | + ); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - return $uri; |
|
| 97 | - } |
|
| 96 | + return $uri; |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -22,26 +22,26 @@ |
||
| 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 | } |
@@ -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 | } |