@@ -32,7 +32,7 @@ |
||
| 32 | 32 | * |
| 33 | 33 | * @param string $uri URI of the resource to load. |
| 34 | 34 | * |
| 35 | - * @return array Data contained within the resource. |
|
| 35 | + * @return boolean Data contained within the resource. |
|
| 36 | 36 | */ |
| 37 | 37 | public static function canLoad($uri) |
| 38 | 38 | { |
@@ -25,74 +25,74 @@ |
||
| 25 | 25 | class JSONLoader extends AbstractLoader |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Load the configuration from an URI. |
|
| 30 | - * |
|
| 31 | - * @since 0.4.0 |
|
| 32 | - * |
|
| 33 | - * @param string $uri URI of the resource to load. |
|
| 34 | - * |
|
| 35 | - * @return array Data contained within the resource. |
|
| 36 | - */ |
|
| 37 | - public static function canLoad($uri) |
|
| 38 | - { |
|
| 39 | - $path = pathinfo($uri); |
|
| 28 | + /** |
|
| 29 | + * Load the configuration from an URI. |
|
| 30 | + * |
|
| 31 | + * @since 0.4.0 |
|
| 32 | + * |
|
| 33 | + * @param string $uri URI of the resource to load. |
|
| 34 | + * |
|
| 35 | + * @return array Data contained within the resource. |
|
| 36 | + */ |
|
| 37 | + public static function canLoad($uri) |
|
| 38 | + { |
|
| 39 | + $path = pathinfo($uri); |
|
| 40 | 40 | |
| 41 | - return '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 | } |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | $data = json_decode(file_get_contents($uri), true); |
| 59 | 59 | ob_end_clean(); |
| 60 | 60 | |
| 61 | - return (array)$data; |
|
| 61 | + return (array) $data; |
|
| 62 | 62 | } catch (Exception $exception) { |
| 63 | 63 | throw new FailedToLoadConfigException( |
| 64 | 64 | sprintf( |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | */ |
| 85 | 85 | protected function validateUri($uri) |
| 86 | 86 | { |
| 87 | - if (! is_readable($uri)) { |
|
| 87 | + if ( ! is_readable($uri)) { |
|
| 88 | 88 | throw new FailedToLoadConfigException( |
| 89 | 89 | sprintf( |
| 90 | 90 | _('The requested JSON config file "%1$s" does not exist or is not readable.'), |
@@ -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 | } |