Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\View\Concerns;
4
 
5
use InvalidArgumentException;
6
 
7
trait ManagesStacks
8
{
9
    /**
10
     * All of the finished, captured push sections.
11
     *
12
     * @var array
13
     */
14
    protected $pushes = [];
15
 
16
    /**
17
     * All of the finished, captured prepend sections.
18
     *
19
     * @var array
20
     */
21
    protected $prepends = [];
22
 
23
    /**
24
     * The stack of in-progress push sections.
25
     *
26
     * @var array
27
     */
28
    protected $pushStack = [];
29
 
30
    /**
31
     * Start injecting content into a push section.
32
     *
33
     * @param  string  $section
34
     * @param  string  $content
35
     * @return void
36
     */
37
    public function startPush($section, $content = '')
38
    {
39
        if ($content === '') {
40
            if (ob_start()) {
41
                $this->pushStack[] = $section;
42
            }
43
        } else {
44
            $this->extendPush($section, $content);
45
        }
46
    }
47
 
48
    /**
49
     * Stop injecting content into a push section.
50
     *
51
     * @return string
52
     *
53
     * @throws \InvalidArgumentException
54
     */
55
    public function stopPush()
56
    {
57
        if (empty($this->pushStack)) {
58
            throw new InvalidArgumentException('Cannot end a push stack without first starting one.');
59
        }
60
 
61
        return tap(array_pop($this->pushStack), function ($last) {
62
            $this->extendPush($last, ob_get_clean());
63
        });
64
    }
65
 
66
    /**
67
     * Append content to a given push section.
68
     *
69
     * @param  string  $section
70
     * @param  string  $content
71
     * @return void
72
     */
73
    protected function extendPush($section, $content)
74
    {
75
        if (! isset($this->pushes[$section])) {
76
            $this->pushes[$section] = [];
77
        }
78
 
79
        if (! isset($this->pushes[$section][$this->renderCount])) {
80
            $this->pushes[$section][$this->renderCount] = $content;
81
        } else {
82
            $this->pushes[$section][$this->renderCount] .= $content;
83
        }
84
    }
85
 
86
    /**
87
     * Start prepending content into a push section.
88
     *
89
     * @param  string  $section
90
     * @param  string  $content
91
     * @return void
92
     */
93
    public function startPrepend($section, $content = '')
94
    {
95
        if ($content === '') {
96
            if (ob_start()) {
97
                $this->pushStack[] = $section;
98
            }
99
        } else {
100
            $this->extendPrepend($section, $content);
101
        }
102
    }
103
 
104
    /**
105
     * Stop prepending content into a push section.
106
     *
107
     * @return string
108
     *
109
     * @throws \InvalidArgumentException
110
     */
111
    public function stopPrepend()
112
    {
113
        if (empty($this->pushStack)) {
114
            throw new InvalidArgumentException('Cannot end a prepend operation without first starting one.');
115
        }
116
 
117
        return tap(array_pop($this->pushStack), function ($last) {
118
            $this->extendPrepend($last, ob_get_clean());
119
        });
120
    }
121
 
122
    /**
123
     * Prepend content to a given stack.
124
     *
125
     * @param  string  $section
126
     * @param  string  $content
127
     * @return void
128
     */
129
    protected function extendPrepend($section, $content)
130
    {
131
        if (! isset($this->prepends[$section])) {
132
            $this->prepends[$section] = [];
133
        }
134
 
135
        if (! isset($this->prepends[$section][$this->renderCount])) {
136
            $this->prepends[$section][$this->renderCount] = $content;
137
        } else {
138
            $this->prepends[$section][$this->renderCount] = $content.$this->prepends[$section][$this->renderCount];
139
        }
140
    }
141
 
142
    /**
143
     * Get the string contents of a push section.
144
     *
145
     * @param  string  $section
146
     * @param  string  $default
147
     * @return string
148
     */
149
    public function yieldPushContent($section, $default = '')
150
    {
151
        if (! isset($this->pushes[$section]) && ! isset($this->prepends[$section])) {
152
            return $default;
153
        }
154
 
155
        $output = '';
156
 
157
        if (isset($this->prepends[$section])) {
158
            $output .= implode(array_reverse($this->prepends[$section]));
159
        }
160
 
161
        if (isset($this->pushes[$section])) {
162
            $output .= implode($this->pushes[$section]);
163
        }
164
 
165
        return $output;
166
    }
167
 
168
    /**
169
     * Flush all of the stacks.
170
     *
171
     * @return void
172
     */
173
    public function flushStacks()
174
    {
175
        $this->pushes = [];
176
        $this->prepends = [];
177
        $this->pushStack = [];
178
    }
179
}