1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Advmaker\BladeLoop; |
4
|
|
|
|
5
|
|
|
class LoopFactory |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The stack of Loop instances |
10
|
|
|
* |
11
|
|
|
* @var array $stack |
12
|
|
|
*/ |
13
|
|
|
protected $stack = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Creates a new loop with the given array and adds it to the stack |
17
|
|
|
* |
18
|
|
|
* @param array $items The array that will be iterated |
19
|
|
|
*/ |
20
|
|
|
public function newLoop($items) |
21
|
|
|
{ |
22
|
|
|
$this->addLoopStack(new Loop($this, $items)); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Adds a Loop to the stack |
27
|
|
|
* |
28
|
|
|
* @param Loop $stackItem |
29
|
|
|
*/ |
30
|
|
|
protected function addLoopStack(Loop $stackItem) |
31
|
|
|
{ |
32
|
|
|
// Check stack for parent loop to register it with this loop |
33
|
|
|
if (count($this->stack) > 0) { |
34
|
|
|
$stackItem->setParentLoop(last($this->stack)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
array_push($this->stack, $stackItem); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the stack |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function getStack() |
46
|
|
|
{ |
47
|
|
|
return $this->stack; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* getLastStack method |
52
|
|
|
* |
53
|
|
|
* @return Loop |
54
|
|
|
*/ |
55
|
|
|
public function getLastStack() |
56
|
|
|
{ |
57
|
|
|
return end($this->stack); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Resets the stack |
62
|
|
|
*/ |
63
|
|
|
public function reset() |
64
|
|
|
{ |
65
|
|
|
$this->stack = []; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* To be called first inside the foreach loop. Returns the current loop |
70
|
|
|
* |
71
|
|
|
* @return Loop $current The current loop data |
72
|
|
|
*/ |
73
|
|
|
public function loop() |
74
|
|
|
{ |
75
|
|
|
$current = end($this->stack); |
76
|
|
|
$current->before(); |
77
|
|
|
|
78
|
|
|
return $current; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* To be called before the end of the loop |
83
|
|
|
*/ |
84
|
|
|
public function looped() |
85
|
|
|
{ |
86
|
|
|
if (!empty($this->stack)) { |
87
|
|
|
end($this->stack)->after(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Should be called after the loop has finished |
93
|
|
|
* |
94
|
|
|
* @param $loop |
95
|
|
|
*/ |
96
|
|
|
public function endLoop(&$loop) |
97
|
|
|
{ |
98
|
|
|
array_pop($this->stack); |
99
|
|
|
if (count($this->stack) > 0) { |
100
|
|
|
// This loop was inside another loop. We persist the loop variable and assign back the parent loop |
101
|
|
|
$loop = end($this->stack); |
102
|
|
|
} else { |
103
|
|
|
// This loop was not inside another loop. We remove the var |
104
|
|
|
//echo "l:(" . count($this->stack) . ") "; |
|
|
|
|
105
|
|
|
$loop = null; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.