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.1.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\Vars; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Vars Resource Provider to separate the different formats of config (array/file/dir) into one resource |
26
|
|
|
* |
27
|
|
|
* @since 0.1.0 |
28
|
|
|
*/ |
29
|
|
|
class ResourceProvider extends AbstractResource |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The configuration entity -- could be a file, array or dir |
34
|
|
|
* |
35
|
|
|
* @var array|string |
36
|
|
|
*/ |
37
|
|
|
private $entity; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* If the import was not relative then the content will be stored in this array |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $parent_content = array(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Is the import relative |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
private $relative; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The parent Vars class |
55
|
|
|
* |
56
|
|
|
* @var \M1\Vars\Vars |
57
|
|
|
*/ |
58
|
|
|
public $vars; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The ResourceProvider constructor creates the content from the entity |
62
|
|
|
* |
63
|
|
|
* @param \M1\Vars\Vars $vars The calling Vars class |
64
|
|
|
* @param string|array $entity The configuration entity |
65
|
|
|
* @param bool $relative Is the entity relative to the calling entity or class |
66
|
|
|
* |
67
|
|
|
* @throws \InvalidArgumentException If the entity passed is not a string or array |
68
|
|
|
*/ |
69
|
62 |
|
public function __construct(Vars $vars, $entity, $relative = true) |
70
|
|
|
{ |
71
|
62 |
|
if (!is_string($entity) && !is_array($entity)) { |
72
|
1 |
|
throw new \InvalidArgumentException('You can only pass strings or arrays as Resources'); |
73
|
|
|
} |
74
|
|
|
|
75
|
61 |
|
$this->vars = $vars; |
76
|
61 |
|
$this->entity = $entity; |
77
|
61 |
|
$this->relative = $relative; |
78
|
|
|
|
79
|
61 |
|
$this->createContent($entity); |
80
|
50 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Creates the content from the entity |
84
|
|
|
* |
85
|
|
|
* @param string|array $entity The configuration entity |
86
|
|
|
*/ |
87
|
61 |
|
private function createContent($entity) |
88
|
|
|
{ |
89
|
61 |
|
$type = gettype($entity); |
90
|
61 |
|
$resources = $this->processEntity($entity, $type); |
91
|
|
|
|
92
|
58 |
|
if ($resources && !empty($resources)) { |
93
|
57 |
|
foreach ($resources as $resource) { |
|
|
|
|
94
|
57 |
|
if ($type === "string") { |
95
|
57 |
|
$this->vars->pathsLoadedCheck($resource); |
96
|
|
|
|
97
|
57 |
|
if ($this->vars->cache->checkCache()) { |
98
|
1 |
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
57 |
|
if ($this->vars->resourceImported($resource)) { |
102
|
1 |
|
continue; |
103
|
|
|
} |
104
|
57 |
|
$pos = $this->vars->addResource($resource); |
105
|
57 |
|
$resource = new FileResource($this, $resource); |
106
|
49 |
|
$this->vars->updateResource($resource, $pos); |
107
|
49 |
|
} else { |
108
|
1 |
|
$resource = new ResourceProvider($this->vars, $resource); |
109
|
|
|
} |
110
|
|
|
|
111
|
49 |
|
$this->addContent($resource->getContent()); |
112
|
49 |
|
} |
113
|
49 |
|
} |
114
|
50 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Creates the content from the entity |
118
|
|
|
* |
119
|
|
|
* @param string|array $entity The configuration entity |
120
|
|
|
* @param string $type The type of entity |
121
|
|
|
* |
122
|
|
|
* @throws \InvalidArgumentException If the entity is not array|file, is readable or exists |
123
|
|
|
* |
124
|
|
|
* @returns array The array of resources |
125
|
|
|
*/ |
126
|
61 |
|
private function processEntity($entity, $type) |
127
|
|
|
{ |
128
|
61 |
|
$resources = $entity; |
129
|
|
|
|
130
|
61 |
|
if ($type === 'string') { |
131
|
61 |
|
if (is_file($entity)) { |
132
|
57 |
|
$resources = array($entity); |
133
|
61 |
|
} elseif (is_dir($entity)) { |
134
|
4 |
|
$resources = $this->getSupportedFilesInDir(); |
135
|
4 |
|
} else { |
136
|
4 |
|
throw new \InvalidArgumentException(sprintf("'%s' does not exist or is not readable", $entity)); |
137
|
|
|
} |
138
|
58 |
|
} |
139
|
|
|
|
140
|
58 |
|
return $resources; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Adds content to the parent contents |
145
|
|
|
* |
146
|
|
|
* @param array $content The content from the resource |
147
|
|
|
*/ |
148
|
49 |
|
private function addContent($content) |
149
|
|
|
{ |
150
|
49 |
|
if ($this->relative) { |
151
|
49 |
|
$this->content = $this->mergeContents($this->content, $content); |
152
|
49 |
|
} else { |
153
|
7 |
|
$this->parent_content = $this->mergeContents($this->parent_content, $content); |
154
|
|
|
} |
155
|
49 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns the supported files using the extensions from the loaders in the entity which is a directory |
159
|
|
|
* |
160
|
|
|
* @see \M1\Vars\Loader\LoaderProvider::getExtensions() \M1\Vars\Loader\LoaderProvider::getExtensions() |
161
|
|
|
* @see \M1\Vars\Loader\LoaderProvider::makeLoaders() \M1\Vars\Loader\LoaderProvider::makeLoaders() |
162
|
|
|
* |
163
|
|
|
* @return array|bool Returns the supported files or false if no files were found |
164
|
|
|
*/ |
165
|
4 |
|
private function getSupportedFilesInDir() |
166
|
|
|
{ |
167
|
4 |
|
$dir_loader = new DirectoryLoader($this->entity); |
|
|
|
|
168
|
4 |
|
$dir_loader->setSupports($this->vars->loader->getExtensions()); |
169
|
4 |
|
$dir_loader->load(); |
170
|
|
|
|
171
|
4 |
|
return $dir_loader->getContent(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Merges various configuration contents into one array |
176
|
|
|
* |
177
|
|
|
* @param mixed $contents,... Optional number of arrays to merge |
|
|
|
|
178
|
|
|
* |
179
|
|
|
* @return array The merged contents |
180
|
|
|
*/ |
181
|
50 |
|
private function mergeContents() |
182
|
|
|
{ |
183
|
50 |
|
$contents = func_get_args(); |
184
|
50 |
|
return call_user_func_array('array_replace_recursive', $contents); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Adds content to the parent content array |
189
|
|
|
* |
190
|
|
|
* @param array $content The content to add |
191
|
|
|
*/ |
192
|
7 |
|
public function addParentContent(array $content) |
193
|
|
|
{ |
194
|
7 |
|
$this->parent_content = array_merge_recursive($this->parent_content, $content); |
195
|
7 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Merges the content and the parent content together |
199
|
|
|
* |
200
|
|
|
* @return \M1\Vars\Resource\ResourceProvider |
201
|
|
|
*/ |
202
|
50 |
|
public function mergeParentContent() |
203
|
|
|
{ |
204
|
50 |
|
$this->content = $this->mergeContents($this->content, $this->parent_content); |
205
|
|
|
|
206
|
50 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Returns the parent content |
211
|
|
|
* |
212
|
|
|
* @return mixed The parent content |
213
|
|
|
*/ |
214
|
38 |
|
public function getParentContent() |
215
|
|
|
{ |
216
|
38 |
|
return $this->parent_content; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
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.