|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the m1\vars library |
|
5
|
|
|
* |
|
6
|
|
|
* (c) m1 <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @package m1/vars |
|
12
|
|
|
* @version 0.3.0 |
|
13
|
|
|
* @author Miles Croxford <[email protected]> |
|
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
|
15
|
|
|
* @license http://github.com/m1/vars/blob/master/LICENSE |
|
16
|
|
|
* @link http://github.com/m1/vars/blob/master/README.MD Documentation |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace M1\Vars\Loader; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This file provides the loaders and extensions supported by Vars |
|
23
|
|
|
* |
|
24
|
|
|
* @since 0.1.1 |
|
25
|
|
|
*/ |
|
26
|
|
|
class LoaderProvider |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* The available extensions |
|
30
|
|
|
* |
|
31
|
|
|
* @var array $extensions |
|
32
|
|
|
*/ |
|
33
|
|
|
private $extensions = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The available loaders |
|
37
|
|
|
* |
|
38
|
|
|
* @var array $loaders |
|
39
|
|
|
*/ |
|
40
|
|
|
private $loaders = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The constructor for LoaderProvider, makes the loaders and extensions |
|
44
|
|
|
* |
|
45
|
|
|
* @param array|null $options The options being used for Vars |
|
46
|
|
|
* @param array $default_loaders The default loaders for Vars |
|
47
|
|
|
*/ |
|
48
|
78 |
|
public function __construct($options, $default_loaders) |
|
49
|
|
|
{ |
|
50
|
78 |
|
$this->makeLoaders($options, $default_loaders); |
|
51
|
76 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get loaders and make extensions for the loaders |
|
55
|
|
|
* |
|
56
|
|
|
* @param array|null $options The options being used for Vars |
|
57
|
|
|
* @param array $default_loaders The default loaders for Vars |
|
58
|
|
|
*/ |
|
59
|
78 |
|
private function makeLoaders($options, $default_loaders) |
|
60
|
|
|
{ |
|
61
|
78 |
|
$loaders = $this->preParseLoaders($options, $default_loaders); |
|
62
|
78 |
|
$parsed_loaders = array(); |
|
63
|
|
|
|
|
64
|
78 |
|
foreach ($loaders as $loader) { |
|
65
|
78 |
|
if ($loader === 'default') { |
|
66
|
2 |
|
$parsed_loaders = array_merge($parsed_loaders, $default_loaders); |
|
67
|
2 |
|
} else { |
|
68
|
77 |
|
$parsed_loaders[] = $loader; |
|
69
|
|
|
} |
|
70
|
78 |
|
} |
|
71
|
|
|
|
|
72
|
78 |
|
$parsed_loaders = array_unique($parsed_loaders); |
|
73
|
|
|
|
|
74
|
78 |
|
$this->loaders = $this->makeNameSpaceLoaders($parsed_loaders, $default_loaders); |
|
75
|
77 |
|
$this->extensions = $this->makeExtensions($this->loaders); |
|
76
|
76 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Pre parse the loaders for use in make loaders |
|
80
|
|
|
* |
|
81
|
|
|
* @param array|null $options The options being used for Vars |
|
82
|
|
|
* @param array $default_loaders The default loaders for Vars |
|
83
|
|
|
* |
|
84
|
|
|
* @return array The pre parsed loaders |
|
85
|
|
|
*/ |
|
86
|
78 |
|
private function preParseLoaders($options, $default_loaders) |
|
87
|
|
|
{ |
|
88
|
78 |
|
$loaders = array(); |
|
89
|
|
|
|
|
90
|
78 |
|
if (is_array($options['loaders']) && !empty($options['loaders'])) { |
|
91
|
73 |
|
$loaders = $options['loaders']; |
|
92
|
78 |
|
} elseif (is_string($options['loaders'])) { |
|
93
|
4 |
|
$loaders[] = $options['loaders']; |
|
94
|
4 |
|
} else { |
|
95
|
1 |
|
$loaders = $default_loaders; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
78 |
|
return $loaders; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Makes namespace loaders from loader strings |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $loaders The options being used for Vars |
|
105
|
|
|
* @param array $default_loaders The default loaders for Vars |
|
106
|
|
|
* |
|
107
|
|
|
* @throws \InvalidArgumentException If a loader from options isn't found |
|
108
|
|
|
* |
|
109
|
|
|
* @return array The namespace loaders |
|
110
|
|
|
*/ |
|
111
|
78 |
|
private function makeNameSpaceLoaders($loaders, $default_loaders) |
|
112
|
|
|
{ |
|
113
|
78 |
|
$parsed_loaders = array(); |
|
114
|
|
|
|
|
115
|
78 |
|
foreach ($loaders as $loader) { |
|
116
|
78 |
|
if (in_array($loader, $default_loaders)) { |
|
117
|
75 |
|
$loader = sprintf('%s\%sLoader', __NAMESPACE__, ucfirst(strtolower($loader))); |
|
118
|
75 |
|
} |
|
119
|
|
|
|
|
120
|
78 |
|
if (!class_exists($loader)) { |
|
121
|
1 |
|
throw new \InvalidArgumentException(sprintf("'%s' loader class does not exist", $loader)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
77 |
|
$parsed_loaders[] = $loader; |
|
125
|
77 |
|
} |
|
126
|
|
|
|
|
127
|
77 |
|
return $parsed_loaders; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get and make extensions for loaders made from makeLoaders() |
|
132
|
|
|
* |
|
133
|
|
|
* @see \M1\Vars\Vars::makeLoaders() \M1\Vars\Vars::makeLoaders() |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $loaders File loaders |
|
136
|
|
|
* |
|
137
|
|
|
* @throws \RuntimeException If no loader extensions were found |
|
138
|
|
|
* |
|
139
|
|
|
* @return array File loader supported extensions |
|
140
|
|
|
*/ |
|
141
|
77 |
|
private function makeExtensions(array $loaders) |
|
142
|
|
|
{ |
|
143
|
77 |
|
$extensions = array(); |
|
144
|
|
|
|
|
145
|
77 |
|
foreach ($loaders as $loader) { |
|
146
|
77 |
|
$extensions = array_merge($extensions, $loader::$supported); |
|
147
|
77 |
|
} |
|
148
|
|
|
|
|
149
|
77 |
|
if (empty($extensions)) { |
|
150
|
1 |
|
throw new \RuntimeException('No loader extensions were found'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
76 |
|
return $extensions; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get the Vars file loaders |
|
158
|
|
|
* |
|
159
|
|
|
* @return array The Vars file loaders |
|
160
|
|
|
*/ |
|
161
|
69 |
|
public function getLoaders() |
|
162
|
|
|
{ |
|
163
|
69 |
|
return $this->loaders; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get the Vars file loaders extensions |
|
168
|
|
|
* |
|
169
|
|
|
* @return array The Vars file loader extensions |
|
170
|
|
|
*/ |
|
171
|
7 |
|
public function getExtensions() |
|
172
|
|
|
{ |
|
173
|
7 |
|
return $this->extensions; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|