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
|
73 |
|
public function __construct(Vars $vars, $entity, $relative = true, $recursive = true) |
87
|
|
|
{ |
88
|
73 |
|
if (!is_string($entity) && !is_array($entity)) { |
89
|
1 |
|
throw new \InvalidArgumentException('You can only pass strings or arrays as Resources'); |
90
|
|
|
} |
91
|
|
|
|
92
|
72 |
|
$this->vars = $vars; |
93
|
72 |
|
$this->entity = $entity; |
94
|
72 |
|
$this->relative = $relative; |
95
|
72 |
|
$this->recursive = $recursive; |
96
|
72 |
|
$type = gettype($entity); |
97
|
|
|
|
98
|
72 |
|
$resources = $this->processEntity($entity, $type); |
99
|
|
|
|
100
|
69 |
|
if ($resources && !empty($resources)) { |
101
|
67 |
|
$this->createResources($resources, $type); |
102
|
57 |
|
} |
103
|
59 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Creates the FileResource|ResourceProvider from the resource |
107
|
|
|
* |
108
|
|
|
* @param array $resources The array of resources |
109
|
|
|
* @param string $type The type of the resource |
110
|
|
|
*/ |
111
|
67 |
|
private function createResources(array $resources, $type) |
112
|
|
|
{ |
113
|
67 |
|
foreach ($resources as $resource) { |
114
|
67 |
|
if ($type === "string") { |
115
|
67 |
|
$this->vars->pathsLoadedCheck($resource); |
116
|
|
|
|
117
|
67 |
|
if ($this->vars->cache->checkCache()) { |
118
|
1 |
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
67 |
|
if ($this->vars->resourceImported($resource)) { |
122
|
1 |
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
67 |
|
$pos = $this->vars->addResource($resource); |
126
|
67 |
|
$resource = new FileResource($this, $resource); |
127
|
57 |
|
$this->vars->updateResource($resource, $pos); |
128
|
57 |
|
} else { |
129
|
1 |
|
$resource = new ResourceProvider($this->vars, $resource); |
130
|
|
|
} |
131
|
|
|
|
132
|
57 |
|
$this->addContent($resource->getContent()); |
133
|
57 |
|
} |
134
|
57 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Creates the content from the entity |
138
|
|
|
* |
139
|
|
|
* @param string|array $entity The configuration entity |
140
|
|
|
* @param string $type The type of entity |
141
|
|
|
* |
142
|
|
|
* @throws \InvalidArgumentException If the entity is not array|file, is readable or exists |
143
|
|
|
* |
144
|
|
|
* @returns array The array of resources |
145
|
|
|
*/ |
146
|
72 |
|
private function processEntity($entity, $type) |
147
|
|
|
{ |
148
|
72 |
|
$resources = $entity; |
149
|
|
|
|
150
|
72 |
|
if ($type === 'string') { |
151
|
72 |
|
$entity = $this->parseEntity($entity); |
|
|
|
|
152
|
|
|
|
153
|
72 |
|
if (is_file($entity)) { |
154
|
67 |
|
$resources = array($entity); |
155
|
72 |
|
} elseif (is_dir($entity)) { |
156
|
5 |
|
$resources = $this->getSupportedFilesInDir(); |
157
|
12 |
|
} elseif ($this->suppress_file_exceptions) { |
158
|
2 |
|
$resources = false; |
159
|
2 |
|
} else { |
160
|
5 |
|
throw new \InvalidArgumentException(sprintf("'%s' does not exist or is not readable", $entity)); |
161
|
|
|
} |
162
|
69 |
|
} |
163
|
|
|
|
164
|
69 |
|
return $resources; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Creates the content from the entity |
169
|
|
|
* |
170
|
|
|
* @param string $entity The configuration entity |
171
|
|
|
* |
172
|
|
|
* @returns string The parsed entity |
173
|
|
|
*/ |
174
|
72 |
|
private function parseEntity($entity) |
175
|
|
|
{ |
176
|
72 |
|
$files = $this->explodeResourceIfElse($entity); |
177
|
|
|
|
178
|
72 |
|
foreach ($files as $f) { |
179
|
72 |
|
$this->suppress_file_exceptions = false; |
180
|
|
|
|
181
|
72 |
|
if ($this->checkSuppression($f)) { |
182
|
2 |
|
$f = trim($f, "@"); |
183
|
2 |
|
$this->suppress_file_exceptions = true; |
184
|
2 |
|
} |
185
|
|
|
|
186
|
72 |
|
if (file_exists($f) || !isset($files[1])) { |
187
|
72 |
|
return $f; |
188
|
|
|
} |
189
|
3 |
|
} |
190
|
|
|
|
191
|
2 |
|
return $f; |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Adds content to the parent contents |
196
|
|
|
* |
197
|
|
|
* @param array $content The content from the resource |
198
|
|
|
*/ |
199
|
57 |
|
private function addContent($content) |
200
|
|
|
{ |
201
|
57 |
|
if ($this->relative) { |
202
|
57 |
|
$this->content = $this->mergeContents($this->content, $content); |
203
|
57 |
|
} else { |
204
|
7 |
|
$this->parent_content = $this->mergeContents($this->parent_content, $content); |
205
|
|
|
} |
206
|
57 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns the supported files using the extensions from the loaders in the entity which is a directory |
210
|
|
|
* |
211
|
|
|
* @see \M1\Vars\Loader\LoaderProvider::getExtensions() \M1\Vars\Loader\LoaderProvider::getExtensions() |
212
|
|
|
* @see \M1\Vars\Loader\LoaderProvider::makeLoaders() \M1\Vars\Loader\LoaderProvider::makeLoaders() |
213
|
|
|
* |
214
|
|
|
* @return array|bool Returns the supported files or false if no files were found |
215
|
|
|
*/ |
216
|
5 |
|
private function getSupportedFilesInDir() |
217
|
|
|
{ |
218
|
5 |
|
$dir_loader = new DirectoryLoader($this->entity, $this->recursive); |
|
|
|
|
219
|
5 |
|
$dir_loader->setSupports($this->vars->loader->getExtensions()); |
220
|
5 |
|
$dir_loader->load(); |
221
|
|
|
|
222
|
5 |
|
return $dir_loader->getContent(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Merges various configuration contents into one array |
227
|
|
|
* |
228
|
|
|
* @param mixed $contents,... Optional number of arrays to merge |
|
|
|
|
229
|
|
|
* |
230
|
|
|
* @return array The merged contents |
231
|
|
|
*/ |
232
|
59 |
|
private function mergeContents() |
233
|
|
|
{ |
234
|
59 |
|
$contents = func_get_args(); |
235
|
59 |
|
return call_user_func_array('array_replace_recursive', $contents); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Adds content to the parent content array |
240
|
|
|
* |
241
|
|
|
* @param array $content The content to add |
242
|
|
|
*/ |
243
|
7 |
|
public function addParentContent(array $content) |
244
|
|
|
{ |
245
|
7 |
|
$this->parent_content = array_merge_recursive($this->parent_content, $content); |
246
|
7 |
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Merges the content and the parent content together |
250
|
|
|
* |
251
|
|
|
* @return \M1\Vars\Resource\ResourceProvider |
252
|
|
|
*/ |
253
|
59 |
|
public function mergeParentContent() |
254
|
|
|
{ |
255
|
59 |
|
$this->content = $this->mergeContents($this->content, $this->parent_content); |
256
|
|
|
|
257
|
59 |
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Returns the parent content |
262
|
|
|
* |
263
|
|
|
* @return mixed The parent content |
264
|
|
|
*/ |
265
|
44 |
|
public function getParentContent() |
266
|
|
|
{ |
267
|
44 |
|
return $this->parent_content; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.