-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.php
executable file
·395 lines (335 loc) · 12.4 KB
/
Tree.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
namespace bultonFr\DependencyTree;
use \Exception;
class Tree
{
/**
* @var array $dependenciesInfos : List of dependency with there infos;
*/
protected $dependenciesInfos = [];
/**
* @var array $listDepends : List the depends of each dependancy
*/
protected $listDepends = [];
/**
* @var array $dependenciesPositions : List of position in the tree for
* each dependancy
*/
protected $dependenciesPositions = [];
/**
* @var array $tree : The generate tree
*/
protected $tree = [];
/**
* @var \stdClass $genOrderSecurityLoop : Informations about dependency
* during the generateOrder.
* It's a security against infinite loop.
*/
protected $genOrderSecurityLoop;
/**
* Add a dependency to system
*
* @param string $name : Dependency's name
* @param int $order : Order to load dependency
* @param array $dependencies : All dependencies for this dependency
*
* @throws Exception : If dependency already declared
*
* @return Tree : Current instance
*/
public function addDependency($name, $order = 0, $dependencies = [])
{
//Check if dependency is already declared.
if (isset($this->dependenciesInfos[$name])) {
throw new Exception('Dependency '.$name.' already declared.');
}
if (!is_array($dependencies)) {
throw new Exception('Dependencies must be passed in a array.');
}
$dependencyInfos = new \stdClass;
$dependencyInfos->order = $order;
$dependencyInfos->dependencies = $dependencies;
$this->dependenciesInfos[$name] = $dependencyInfos;
//Create the key for list of depends if she doesn't exist
if (!isset($this->listDepends[$name])) {
$this->listDepends[$name] = [];
}
//Generate the list of depends
if ($dependencies !== []) {
foreach ($dependencies as $dependencyName) {
if(!isset($this->listDepends[$dependencyName])) {
$this->listDepends[$dependencyName] = [];
}
$this->listDepends[$dependencyName][] = $name;
}
}
return $this;
}
/**
* Generate the tree
*
* @return array
*/
public function generateTree()
{
//Read all depencies declared and positioned each dependency on
//the tree with they order value
foreach ($this->dependenciesInfos as $name => $dependency) {
$order = $dependency->order;
//If the line for this order not exist
if (!isset($this->tree[$order])) {
$this->tree[$order] = [];
}
//Add to the tree and save the current position
$this->tree[$order][] = $name;
$this->dependenciesPositions[$name] = [$order];
}
//Read the tree and check depends of each package.
//Move some package in the tree if need for depends.
foreach ($this->tree as $dependencies) {
foreach ($dependencies as $dependencyName) {
$this->checkDepend($dependencyName);
}
}
//Sort by key. Some keys should be unorganized
ksort($this->tree);
return $this->tree;
}
/**
* Check if all depends is correctly spoted in the tree
*
* @param string $dependencyName : The name of the dependency for which
* check depends
*
* @return void
*/
protected function checkDepend($dependencyName)
{
$listDepends = $this->listDepends[$dependencyName];
$dependencyInfos = $this->dependenciesInfos[$dependencyName];
$order = $dependencyInfos->order;
//No depends :)
if ($listDepends === []) {
return;
}
//Read all depends and check if they correctly spoted.
//If not, call the method to move the depend read.
foreach ($listDepends as $dependencyName) {
$dependencyPos = $this->dependenciesPositions[$dependencyName];
$dependencyOrder = $dependencyPos[0];
if ($dependencyOrder < $order) {
$this->moveDepend($dependencyName, $order);
}
}
}
/**
* Move a depend to a new position in the tree
*
* @param string $dependencyName : The dependency name
* @param int $newOrder : The new position in the tree
*
* @return void
*/
protected function moveDepend($dependencyName, $newOrder)
{
//Get old position in the tree for this dependency
$dependencyInfos = $this->dependenciesInfos[$dependencyName];
$oldOrder = $dependencyInfos->order;
//If the new position not already exist in the tree
if (!isset($this->tree[$newOrder])) {
$this->tree[$newOrder] = [];
}
//Add dependency to this new position
$this->tree[$newOrder][] = $dependencyName;
//Search the key corresponding to the old position in the array tree
$oldKey = array_search($dependencyName, $this->tree[$oldOrder]);
//Remove dependency from this old position
unset($this->tree[$oldOrder][$oldKey]);
//Call checkDepend for check all depends of this dependency
$this->checkDepend($dependencyName);
}
/**
* Generate the order position from the depends of each dependencies
*
* First we generate a reversed tree from depends
* Reverse the tree for have a tree in the correct depends order
* And update the order value of each dependency with the tree of depends
*
* @return void
*/
public function generateOrderFromDependencies()
{
$this->initGenOrderSecurityLoop(0);
$this->tree = [[]]; //generate a empty tree
//Read all depends of each dependencies
foreach ($this->listDepends as $dependencyName => $depends) {
//If the dependency in the depend's list is declared on this tree.
if (!isset($this->dependenciesInfos[$dependencyName])) {
continue;
}
//If the package have depends, we continue
if ($depends !== []) {
continue;
}
//Add the dependency to the first line of tree
$this->tree[0][$dependencyName] = $dependencyName;
//And generate the order for this depends
$this->genOrderSecurityLoopAddDepend($dependencyName, 0);
$this->generateOrderForADependency($dependencyName, 0);
}
//Here, $this->tree have a reversed array of depends
//So we reverse the array to have the array of depends
//in the correct order
$this->tree = array_reverse($this->tree);
//Some line could be empty because the dependency is in another tree
//So we define the order manually.
$treeOrder = 0;
//Read the tree for update the order of each dependency
foreach ($this->tree as $dependencies) {
if ($dependencies === []) {
continue;
}
foreach ($dependencies as $dependencyName) {
$dependencyInfos = &$this->dependenciesInfos[$dependencyName];
//If the order has not be already updated
if ($dependencyInfos->order > -1) {
continue;
}
$dependencyInfos->order = $treeOrder;
}
$treeOrder++;
}
//Reinit the tree for use the main system of tree generator.
$this->tree = [];
}
/**
* Define the order of all the dependencies of dependency
*
* @param string $dependencyName : The name of dependency for which we
* read the dependencies
* @param int $currentOrder : The current order of the dependency
*
* @return void
*/
protected function generateOrderForADependency($dependencyName, $currentOrder)
{
$depends = $this->dependenciesInfos[$dependencyName]->dependencies;
if ($depends === []) {
return;
}
$order = $currentOrder+1;
if (!isset($this->tree[$order])) {
$this->tree[$order] = [];
}
foreach ($depends as $dependName) {
//If the dependency of the dependency is in a other tree
if (!isset($this->dependenciesInfos[$dependName])) {
continue;
}
//Infinite dependency loop security
$this->checkReinitGenOrderSecurityLoop($order);
$this->genOrderCheckInfiniteLoop($dependName);
$this->tree[$order][$dependName] = $dependName;
$this->genOrderSecurityLoopAddDepend($dependName, $order);
$this->generateOrderForADependency(
$dependName,
$order
);
}
}
/**
* Add a dependency into list used by security infinite depend loop.
*
* @param string $dependencyName Dependency name
* @param int $order The current order of the dependency
*
* @return void
*/
protected function genOrderSecurityLoopAddDepend($dependencyName, $order)
{
$this->checkReinitGenOrderSecurityLoop($order);
$this->genOrderSecurityLoop->order = $order;
$this->genOrderSecurityLoop->list[] = (object) [
'dependName' => $dependencyName,
'dependList' => []
];
}
/**
* Check if we reinitialize the list used against dependency infinite loop
*
* @param int $order The current order of the dependency
*
* @return void
*/
protected function checkReinitGenOrderSecurityLoop($order)
{
if ($order <= $this->genOrderSecurityLoop->order) {
$this->initGenOrderSecurityLoop($order);
}
}
/**
* (re)Initialize the list used against dependency infinite loop
*
* @param int $order The current order of the dependency
*
* @return void
*/
protected function initGenOrderSecurityLoop($order)
{
$this->genOrderSecurityLoop = (object) [
'order' => $order,
'list' => []
];
}
/**
* Check if we allow to moved a dependency in the tree to protect
* against infine loop.
* It's for the case where a dependency is moved to be loaded before an
* another, but this another dependency depend on the first package
* who asked to be moved.
* So the system try to moved packages at the infine. We protect this.
*
* @see Issue #4 on the github repo.
*
* @param string $checkDependName : The name of dependency
*
* @return void
*
* @throws Exception The infinite loop security.
*/
protected function genOrderCheckInfiniteLoop($checkDependName)
{
$runException = false;
foreach ($this->genOrderSecurityLoop->list as &$checkInfos) {
if ($checkInfos->dependList === []) {
$checkInfos->dependList[] = $checkDependName;
continue;
}
if (
$checkInfos->dependList !== []
&& $checkInfos->dependName !== $checkDependName
) {
$checkInfos->dependList[] = $checkDependName;
continue;
}
$runException = true;
break;
}
if ($runException === false) {
unset($checkInfos); //Kill ref
return;
}
//Package is already moved for the original dependency : Loop error
$loopInfos = '';
foreach ($checkInfos->dependList as $packageName) {
if ($loopInfos !== '') {
$loopInfos .= ', ';
}
$loopInfos .= $packageName;
}
throw new Exception(
'Infinite depends loop find for package '.$checkInfos->dependName
.' - Loop info : '.$loopInfos
);
}
}