1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bright Nucleus Config Component. |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus\Config |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2016-2017 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\Config\Loader; |
13
|
|
|
|
14
|
|
|
use BrightNucleus\Config\Exception\FailedToLoadConfigException; |
15
|
|
|
use Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class LoaderFactory. |
19
|
|
|
* |
20
|
|
|
* @since 0.4.0 |
21
|
|
|
* |
22
|
|
|
* @package BrightNucleus\Config\Loader |
23
|
|
|
* @author Alain Schlesser <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class LoaderFactory |
26
|
|
|
{ |
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
|
|
|
]; |
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 = []; |
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
|
2 |
|
public static function createFromUri($uri) |
62
|
|
|
{ |
63
|
2 |
|
foreach (static::$loaders as $loader) { |
64
|
2 |
|
if ($loader::canLoad($uri)) { |
65
|
2 |
|
return static::getLoader($loader); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new FailedToLoadConfigException( |
70
|
|
|
sprintf( |
71
|
|
|
_('Could not find a suitable loader for URI "%1$s".'), |
72
|
|
|
$uri |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
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
|
2 |
|
public static function getLoader($loaderClass) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
2 |
|
if (! array_key_exists($loaderClass, static::$loaderInstances)) { |
93
|
|
|
static::$loaderInstances[$loaderClass] = new $loaderClass; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
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
|
|
|
|
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
|
|
|
|
120
|
|
|
static::$loaders [] = $loader; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|