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\Resource; |
20
|
|
|
|
21
|
|
|
use M1\Vars\Loader\DirectoryLoader; |
22
|
|
|
use M1\Vars\Traits\ResourceFlagsTrait; |
23
|
|
|
use M1\Vars\Vars; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Vars Resource Provider to separate the different formats of config (array/file/dir) into one resource |
27
|
|
|
* |
28
|
|
|
* @since 0.1.0 |
29
|
|
|
*/ |
30
|
|
|
class ResourceProvider extends AbstractResource |
31
|
|
|
{ |
32
|
|
|
use ResourceFlagsTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The configuration entity -- could be a file, array or dir |
36
|
|
|
* |
37
|
|
|
* @var array|string |
38
|
|
|
*/ |
39
|
|
|
private $entity; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* If the import was not relative then the content will be stored in this array |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $parent_content = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Are dirs wanting to be recursively searched |
50
|
|
|
* |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
private $recursive; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Is the import relative |
57
|
|
|
* |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
private $relative; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Suppress file not found exceptions |
64
|
|
|
* |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
private $suppress_file_exceptions = false; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The parent Vars class |
71
|
|
|
* |
72
|
|
|
* @var \M1\Vars\Vars |
73
|
|
|
*/ |
74
|
|
|
public $vars; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The ResourceProvider constructor creates the content from the entity |
78
|
|
|
* |
79
|
|
|
* @param \M1\Vars\Vars $vars The calling Vars class |
80
|
|
|
* @param string|array $entity The configuration entity |
81
|
|
|
* @param bool $relative Is the entity relative to the calling entity or class |
82
|
|
|
* @param bool $recursive If entity a dir, do you want to recursively check directories |
83
|
|
|
* |
84
|
|
|
* @throws \InvalidArgumentException If the entity passed is not a string or array |
85
|
|
|
*/ |
86
|
72 |
|
public function __construct(Vars $vars, $entity, $relative = true, $recursive = true) |
87
|
|
|
{ |
88
|
72 |
|
if (!is_string($entity) && !is_array($entity)) { |
89
|
1 |
|
throw new \InvalidArgumentException('You can only pass strings or arrays as Resources'); |
90
|
|
|
} |
91
|
|
|
|
92
|
71 |
|
$this->vars = $vars; |
93
|
71 |
|
$this->entity = $entity; |
94
|
71 |
|
$this->relative = $relative; |
95
|
71 |
|
$this->recursive = $recursive; |
96
|
|
|
|
97
|
71 |
|
$this->createContent($entity); |
98
|
59 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Creates the content from the entity |
102
|
|
|
* |
103
|
|
|
* @param string|array $entity The configuration entity |
104
|
|
|
*/ |
105
|
71 |
|
private function createContent($entity) |
106
|
|
|
{ |
107
|
71 |
|
$type = gettype($entity); |
108
|
71 |
|
$resources = $this->processEntity($entity, $type); |
109
|
|
|
|
110
|
68 |
|
if ($resources && !empty($resources)) { |
111
|
66 |
|
foreach ($resources as $resource) { |
|
|
|
|
112
|
66 |
|
if ($type === "string") { |
113
|
66 |
|
$this->vars->pathsLoadedCheck($resource); |
114
|
|
|
|
115
|
66 |
|
if ($this->vars->cache->checkCache()) { |
116
|
1 |
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
66 |
|
if ($this->vars->resourceImported($resource)) { |
120
|
1 |
|
continue; |
121
|
|
|
} |
122
|
|
|
|
123
|
66 |
|
$pos = $this->vars->addResource($resource); |
124
|
66 |
|
$resource = new FileResource($this, $resource); |
125
|
57 |
|
$this->vars->updateResource($resource, $pos); |
126
|
57 |
|
} else { |
127
|
1 |
|
$resource = new ResourceProvider($this->vars, $resource); |
128
|
|
|
} |
129
|
|
|
|
130
|
57 |
|
$this->addContent($resource->getContent()); |
131
|
57 |
|
} |
132
|
57 |
|
} |
133
|
59 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Creates the content from the entity |
137
|
|
|
* |
138
|
|
|
* @param string|array $entity The configuration entity |
139
|
|
|
* @param string $type The type of entity |
140
|
|
|
* |
141
|
|
|
* @throws \InvalidArgumentException If the entity is not array|file, is readable or exists |
142
|
|
|
* |
143
|
|
|
* @returns array The array of resources |
144
|
|
|
*/ |
145
|
71 |
|
private function processEntity($entity, $type) |
146
|
|
|
{ |
147
|
71 |
|
$resources = $entity; |
148
|
|
|
|
149
|
71 |
|
if ($type === 'string') { |
150
|
71 |
|
$entity = $this->parseEntity($entity); |
|
|
|
|
151
|
|
|
|
152
|
71 |
|
if (is_file($entity)) { |
153
|
66 |
|
$resources = array($entity); |
154
|
71 |
|
} elseif (is_dir($entity)) { |
155
|
5 |
|
$resources = $this->getSupportedFilesInDir(); |
156
|
12 |
|
} elseif ($this->suppress_file_exceptions) { |
157
|
2 |
|
$resources = false; |
158
|
2 |
|
} else { |
159
|
5 |
|
throw new \InvalidArgumentException(sprintf("'%s' does not exist or is not readable", $entity)); |
160
|
|
|
} |
161
|
68 |
|
} |
162
|
|
|
|
163
|
68 |
|
return $resources; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Creates the content from the entity |
168
|
|
|
* |
169
|
|
|
* @param string $entity The configuration entity |
170
|
|
|
* |
171
|
|
|
* @returns string The parsed entity |
172
|
|
|
*/ |
173
|
71 |
|
private function parseEntity($entity) |
174
|
|
|
{ |
175
|
71 |
|
$files = $this->explodeResourceIfElse($entity); |
176
|
|
|
|
177
|
71 |
|
foreach ($files as $f) { |
178
|
71 |
|
$this->suppress_file_exceptions = false; |
179
|
|
|
|
180
|
71 |
|
if ($this->checkSuppression($f)) { |
181
|
2 |
|
$f = trim($f, "@"); |
182
|
2 |
|
$this->suppress_file_exceptions = true; |
183
|
2 |
|
} |
184
|
|
|
|
185
|
71 |
|
if (file_exists($f) || !isset($files[1])) { |
186
|
71 |
|
return $f; |
187
|
|
|
} |
188
|
3 |
|
} |
189
|
|
|
|
190
|
2 |
|
return $f; |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Adds content to the parent contents |
195
|
|
|
* |
196
|
|
|
* @param array $content The content from the resource |
197
|
|
|
*/ |
198
|
57 |
|
private function addContent($content) |
199
|
|
|
{ |
200
|
57 |
|
if ($this->relative) { |
201
|
57 |
|
$this->content = $this->mergeContents($this->content, $content); |
202
|
57 |
|
} else { |
203
|
7 |
|
$this->parent_content = $this->mergeContents($this->parent_content, $content); |
204
|
|
|
} |
205
|
57 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns the supported files using the extensions from the loaders in the entity which is a directory |
209
|
|
|
* |
210
|
|
|
* @see \M1\Vars\Loader\LoaderProvider::getExtensions() \M1\Vars\Loader\LoaderProvider::getExtensions() |
211
|
|
|
* @see \M1\Vars\Loader\LoaderProvider::makeLoaders() \M1\Vars\Loader\LoaderProvider::makeLoaders() |
212
|
|
|
* |
213
|
|
|
* @return array|bool Returns the supported files or false if no files were found |
214
|
|
|
*/ |
215
|
5 |
|
private function getSupportedFilesInDir() |
216
|
|
|
{ |
217
|
5 |
|
$dir_loader = new DirectoryLoader($this->entity, $this->recursive); |
|
|
|
|
218
|
5 |
|
$dir_loader->setSupports($this->vars->loader->getExtensions()); |
219
|
5 |
|
$dir_loader->load(); |
220
|
|
|
|
221
|
5 |
|
return $dir_loader->getContent(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Merges various configuration contents into one array |
226
|
|
|
* |
227
|
|
|
* @param mixed $contents,... Optional number of arrays to merge |
|
|
|
|
228
|
|
|
* |
229
|
|
|
* @return array The merged contents |
230
|
|
|
*/ |
231
|
59 |
|
private function mergeContents() |
232
|
|
|
{ |
233
|
59 |
|
$contents = func_get_args(); |
234
|
59 |
|
return call_user_func_array('array_replace_recursive', $contents); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Adds content to the parent content array |
239
|
|
|
* |
240
|
|
|
* @param array $content The content to add |
241
|
|
|
*/ |
242
|
7 |
|
public function addParentContent(array $content) |
243
|
|
|
{ |
244
|
7 |
|
$this->parent_content = array_merge_recursive($this->parent_content, $content); |
245
|
7 |
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Merges the content and the parent content together |
249
|
|
|
* |
250
|
|
|
* @return \M1\Vars\Resource\ResourceProvider |
251
|
|
|
*/ |
252
|
59 |
|
public function mergeParentContent() |
253
|
|
|
{ |
254
|
59 |
|
$this->content = $this->mergeContents($this->content, $this->parent_content); |
255
|
|
|
|
256
|
59 |
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Returns the parent content |
261
|
|
|
* |
262
|
|
|
* @return mixed The parent content |
263
|
|
|
*/ |
264
|
44 |
|
public function getParentContent() |
265
|
|
|
{ |
266
|
44 |
|
return $this->parent_content; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.