@@ -32,7 +32,7 @@ |
||
32 | 32 | * |
33 | 33 | * @param string $uri URI of the resource to load. |
34 | 34 | * |
35 | - * @return array Data contained within the resource. |
|
35 | + * @return boolean Data contained within the resource. |
|
36 | 36 | */ |
37 | 37 | public static function canLoad($uri) |
38 | 38 | { |
@@ -25,76 +25,76 @@ |
||
25 | 25 | class 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 | - // Try to load the file through PHP's include(). |
|
58 | - // Make sure we don't accidentally create output. |
|
59 | - ob_start(); |
|
60 | - $data = json_decode(file_get_contents($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 = json_decode(file_get_contents($uri)); |
|
61 | + ob_end_clean(); |
|
62 | 62 | |
63 | - return (array)$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 (array)$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 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | $data = json_decode(file_get_contents($uri)); |
61 | 61 | ob_end_clean(); |
62 | 62 | |
63 | - return (array)$data; |
|
63 | + return (array) $data; |
|
64 | 64 | } catch (Exception $exception) { |
65 | 65 | throw new FailedToLoadConfigException( |
66 | 66 | sprintf( |
@@ -86,7 +86,7 @@ discard block |
||
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,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 | } |