ManagesStacks.php
4.2 KB
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
<?php
namespace Illuminate\View\Concerns;
use InvalidArgumentException;
trait ManagesStacks
{
/**
* All of the finished, captured push sections.
*
* @var array
*/
protected $pushes = [];
/**
* All of the finished, captured prepend sections.
*
* @var array
*/
protected $prepends = [];
/**
* The stack of in-progress push sections.
*
* @var array
*/
protected $pushStack = [];
/**
* Start injecting content into a push section.
*
* @param string $section
* @param string $content
* @return void
*/
public function startPush($section, $content = '')
{
if ($content === '') {
if (ob_start()) {
$this->pushStack[] = $section;
}
} else {
$this->extendPush($section, $content);
}
}
/**
* Stop injecting content into a push section.
*
* @return string
*
* @throws \InvalidArgumentException
*/
public function stopPush()
{
if (empty($this->pushStack)) {
throw new InvalidArgumentException('Cannot end a push stack without first starting one.');
}
return tap(array_pop($this->pushStack), function ($last) {
$this->extendPush($last, ob_get_clean());
});
}
/**
* Append content to a given push section.
*
* @param string $section
* @param string $content
* @return void
*/
protected function extendPush($section, $content)
{
if (! isset($this->pushes[$section])) {
$this->pushes[$section] = [];
}
if (! isset($this->pushes[$section][$this->renderCount])) {
$this->pushes[$section][$this->renderCount] = $content;
} else {
$this->pushes[$section][$this->renderCount] .= $content;
}
}
/**
* Start prepending content into a push section.
*
* @param string $section
* @param string $content
* @return void
*/
public function startPrepend($section, $content = '')
{
if ($content === '') {
if (ob_start()) {
$this->pushStack[] = $section;
}
} else {
$this->extendPrepend($section, $content);
}
}
/**
* Stop prepending content into a push section.
*
* @return string
*
* @throws \InvalidArgumentException
*/
public function stopPrepend()
{
if (empty($this->pushStack)) {
throw new InvalidArgumentException('Cannot end a prepend operation without first starting one.');
}
return tap(array_pop($this->pushStack), function ($last) {
$this->extendPrepend($last, ob_get_clean());
});
}
/**
* Prepend content to a given stack.
*
* @param string $section
* @param string $content
* @return void
*/
protected function extendPrepend($section, $content)
{
if (! isset($this->prepends[$section])) {
$this->prepends[$section] = [];
}
if (! isset($this->prepends[$section][$this->renderCount])) {
$this->prepends[$section][$this->renderCount] = $content;
} else {
$this->prepends[$section][$this->renderCount] = $content.$this->prepends[$section][$this->renderCount];
}
}
/**
* Get the string contents of a push section.
*
* @param string $section
* @param string $default
* @return string
*/
public function yieldPushContent($section, $default = '')
{
if (! isset($this->pushes[$section]) && ! isset($this->prepends[$section])) {
return $default;
}
$output = '';
if (isset($this->prepends[$section])) {
$output .= implode(array_reverse($this->prepends[$section]));
}
if (isset($this->pushes[$section])) {
$output .= implode($this->pushes[$section]);
}
return $output;
}
/**
* Flush all of the stacks.
*
* @return void
*/
public function flushStacks()
{
$this->pushes = [];
$this->prepends = [];
$this->pushStack = [];
}
}