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 Illuminate\Contracts\View\View;
6
use Illuminate\Support\Str;
7
use InvalidArgumentException;
8
 
9
trait ManagesLayouts
10
{
11
    /**
12
     * All of the finished, captured sections.
13
     *
14
     * @var array
15
     */
16
    protected $sections = [];
17
 
18
    /**
19
     * The stack of in-progress sections.
20
     *
21
     * @var array
22
     */
23
    protected $sectionStack = [];
24
 
25
    /**
26
     * The parent placeholder for the request.
27
     *
28
     * @var mixed
29
     */
30
    protected static $parentPlaceholder = [];
31
 
32
    /**
33
     * The parent placeholder salt for the request.
34
     *
35
     * @var string
36
     */
37
    protected static $parentPlaceholderSalt;
38
 
39
    /**
40
     * Start injecting content into a section.
41
     *
42
     * @param  string  $section
43
     * @param  string|null  $content
44
     * @return void
45
     */
46
    public function startSection($section, $content = null)
47
    {
48
        if ($content === null) {
49
            if (ob_start()) {
50
                $this->sectionStack[] = $section;
51
            }
52
        } else {
53
            $this->extendSection($section, $content instanceof View ? $content : e($content));
54
        }
55
    }
56
 
57
    /**
58
     * Inject inline content into a section.
59
     *
60
     * @param  string  $section
61
     * @param  string  $content
62
     * @return void
63
     */
64
    public function inject($section, $content)
65
    {
66
        $this->startSection($section, $content);
67
    }
68
 
69
    /**
70
     * Stop injecting content into a section and return its contents.
71
     *
72
     * @return string
73
     */
74
    public function yieldSection()
75
    {
76
        if (empty($this->sectionStack)) {
77
            return '';
78
        }
79
 
80
        return $this->yieldContent($this->stopSection());
81
    }
82
 
83
    /**
84
     * Stop injecting content into a section.
85
     *
86
     * @param  bool  $overwrite
87
     * @return string
88
     *
89
     * @throws \InvalidArgumentException
90
     */
91
    public function stopSection($overwrite = false)
92
    {
93
        if (empty($this->sectionStack)) {
94
            throw new InvalidArgumentException('Cannot end a section without first starting one.');
95
        }
96
 
97
        $last = array_pop($this->sectionStack);
98
 
99
        if ($overwrite) {
100
            $this->sections[$last] = ob_get_clean();
101
        } else {
102
            $this->extendSection($last, ob_get_clean());
103
        }
104
 
105
        return $last;
106
    }
107
 
108
    /**
109
     * Stop injecting content into a section and append it.
110
     *
111
     * @return string
112
     *
113
     * @throws \InvalidArgumentException
114
     */
115
    public function appendSection()
116
    {
117
        if (empty($this->sectionStack)) {
118
            throw new InvalidArgumentException('Cannot end a section without first starting one.');
119
        }
120
 
121
        $last = array_pop($this->sectionStack);
122
 
123
        if (isset($this->sections[$last])) {
124
            $this->sections[$last] .= ob_get_clean();
125
        } else {
126
            $this->sections[$last] = ob_get_clean();
127
        }
128
 
129
        return $last;
130
    }
131
 
132
    /**
133
     * Append content to a given section.
134
     *
135
     * @param  string  $section
136
     * @param  string  $content
137
     * @return void
138
     */
139
    protected function extendSection($section, $content)
140
    {
141
        if (isset($this->sections[$section])) {
142
            $content = str_replace(static::parentPlaceholder($section), $content, $this->sections[$section]);
143
        }
144
 
145
        $this->sections[$section] = $content;
146
    }
147
 
148
    /**
149
     * Get the string contents of a section.
150
     *
151
     * @param  string  $section
152
     * @param  string  $default
153
     * @return string
154
     */
155
    public function yieldContent($section, $default = '')
156
    {
157
        $sectionContent = $default instanceof View ? $default : e($default);
158
 
159
        if (isset($this->sections[$section])) {
160
            $sectionContent = $this->sections[$section];
161
        }
162
 
163
        $sectionContent = str_replace('@@parent', '--parent--holder--', $sectionContent);
164
 
165
        return str_replace(
166
            '--parent--holder--', '@parent', str_replace(static::parentPlaceholder($section), '', $sectionContent)
167
        );
168
    }
169
 
170
    /**
171
     * Get the parent placeholder for the current request.
172
     *
173
     * @param  string  $section
174
     * @return string
175
     */
176
    public static function parentPlaceholder($section = '')
177
    {
178
        if (! isset(static::$parentPlaceholder[$section])) {
179
            $salt = static::parentPlaceholderSalt();
180
 
181
            static::$parentPlaceholder[$section] = '##parent-placeholder-'.sha1($salt.$section).'##';
182
        }
183
 
184
        return static::$parentPlaceholder[$section];
185
    }
186
 
187
    /**
188
     * Get the parent placeholder salt.
189
     *
190
     * @return string
191
     */
192
    protected static function parentPlaceholderSalt()
193
    {
194
        if (! static::$parentPlaceholderSalt) {
195
            return static::$parentPlaceholderSalt = Str::random(40);
196
        }
197
 
198
        return static::$parentPlaceholderSalt;
199
    }
200
 
201
    /**
202
     * Check if section exists.
203
     *
204
     * @param  string  $name
205
     * @return bool
206
     */
207
    public function hasSection($name)
208
    {
209
        return array_key_exists($name, $this->sections);
210
    }
211
 
212
    /**
213
     * Check if section does not exist.
214
     *
215
     * @param  string  $name
216
     * @return bool
217
     */
218
    public function sectionMissing($name)
219
    {
220
        return ! $this->hasSection($name);
221
    }
222
 
223
    /**
224
     * Get the contents of a section.
225
     *
226
     * @param  string  $name
227
     * @param  string|null  $default
228
     * @return mixed
229
     */
230
    public function getSection($name, $default = null)
231
    {
232
        return $this->getSections()[$name] ?? $default;
233
    }
234
 
235
    /**
236
     * Get the entire array of sections.
237
     *
238
     * @return array
239
     */
240
    public function getSections()
241
    {
242
        return $this->sections;
243
    }
244
 
245
    /**
246
     * Flush all of the sections.
247
     *
248
     * @return void
249
     */
250
    public function flushSections()
251
    {
252
        $this->sections = [];
253
        $this->sectionStack = [];
254
    }
255
}