Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\View;
4
 
5
use ArrayAccess;
6
use BadMethodCallException;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Contracts\Support\Htmlable;
9
use Illuminate\Contracts\Support\MessageProvider;
10
use Illuminate\Contracts\Support\Renderable;
11
use Illuminate\Contracts\View\Engine;
12
use Illuminate\Contracts\View\View as ViewContract;
13
use Illuminate\Support\MessageBag;
14
use Illuminate\Support\Str;
15
use Illuminate\Support\Traits\Macroable;
16
use Illuminate\Support\ViewErrorBag;
17
use Throwable;
18
 
19
class View implements ArrayAccess, Htmlable, ViewContract
20
{
21
    use Macroable {
22
        __call as macroCall;
23
    }
24
 
25
    /**
26
     * The view factory instance.
27
     *
28
     * @var \Illuminate\View\Factory
29
     */
30
    protected $factory;
31
 
32
    /**
33
     * The engine implementation.
34
     *
35
     * @var \Illuminate\Contracts\View\Engine
36
     */
37
    protected $engine;
38
 
39
    /**
40
     * The name of the view.
41
     *
42
     * @var string
43
     */
44
    protected $view;
45
 
46
    /**
47
     * The array of view data.
48
     *
49
     * @var array
50
     */
51
    protected $data;
52
 
53
    /**
54
     * The path to the view file.
55
     *
56
     * @var string
57
     */
58
    protected $path;
59
 
60
    /**
61
     * Create a new view instance.
62
     *
63
     * @param  \Illuminate\View\Factory  $factory
64
     * @param  \Illuminate\Contracts\View\Engine  $engine
65
     * @param  string  $view
66
     * @param  string  $path
67
     * @param  mixed  $data
68
     * @return void
69
     */
70
    public function __construct(Factory $factory, Engine $engine, $view, $path, $data = [])
71
    {
72
        $this->view = $view;
73
        $this->path = $path;
74
        $this->engine = $engine;
75
        $this->factory = $factory;
76
 
77
        $this->data = $data instanceof Arrayable ? $data->toArray() : (array) $data;
78
    }
79
 
80
    /**
81
     * Get the evaluated contents of a given fragment.
82
     *
83
     * @param  string  $fragment
84
     * @return string
85
     */
86
    public function fragment($fragment)
87
    {
88
        return $this->render(function () use ($fragment) {
89
            return $this->factory->getFragment($fragment);
90
        });
91
    }
92
 
93
    /**
94
     * Get the string contents of the view.
95
     *
96
     * @param  callable|null  $callback
97
     * @return string
98
     *
99
     * @throws \Throwable
100
     */
101
    public function render(callable $callback = null)
102
    {
103
        try {
104
            $contents = $this->renderContents();
105
 
106
            $response = isset($callback) ? $callback($this, $contents) : null;
107
 
108
            // Once we have the contents of the view, we will flush the sections if we are
109
            // done rendering all views so that there is nothing left hanging over when
110
            // another view gets rendered in the future by the application developer.
111
            $this->factory->flushStateIfDoneRendering();
112
 
113
            return ! is_null($response) ? $response : $contents;
114
        } catch (Throwable $e) {
115
            $this->factory->flushState();
116
 
117
            throw $e;
118
        }
119
    }
120
 
121
    /**
122
     * Get the contents of the view instance.
123
     *
124
     * @return string
125
     */
126
    protected function renderContents()
127
    {
128
        // We will keep track of the number of views being rendered so we can flush
129
        // the section after the complete rendering operation is done. This will
130
        // clear out the sections for any separate views that may be rendered.
131
        $this->factory->incrementRender();
132
 
133
        $this->factory->callComposer($this);
134
 
135
        $contents = $this->getContents();
136
 
137
        // Once we've finished rendering the view, we'll decrement the render count
138
        // so that each section gets flushed out next time a view is created and
139
        // no old sections are staying around in the memory of an environment.
140
        $this->factory->decrementRender();
141
 
142
        return $contents;
143
    }
144
 
145
    /**
146
     * Get the evaluated contents of the view.
147
     *
148
     * @return string
149
     */
150
    protected function getContents()
151
    {
152
        return $this->engine->get($this->path, $this->gatherData());
153
    }
154
 
155
    /**
156
     * Get the data bound to the view instance.
157
     *
158
     * @return array
159
     */
160
    public function gatherData()
161
    {
162
        $data = array_merge($this->factory->getShared(), $this->data);
163
 
164
        foreach ($data as $key => $value) {
165
            if ($value instanceof Renderable) {
166
                $data[$key] = $value->render();
167
            }
168
        }
169
 
170
        return $data;
171
    }
172
 
173
    /**
174
     * Get the sections of the rendered view.
175
     *
176
     * @return array
177
     *
178
     * @throws \Throwable
179
     */
180
    public function renderSections()
181
    {
182
        return $this->render(function () {
183
            return $this->factory->getSections();
184
        });
185
    }
186
 
187
    /**
188
     * Add a piece of data to the view.
189
     *
190
     * @param  string|array  $key
191
     * @param  mixed  $value
192
     * @return $this
193
     */
194
    public function with($key, $value = null)
195
    {
196
        if (is_array($key)) {
197
            $this->data = array_merge($this->data, $key);
198
        } else {
199
            $this->data[$key] = $value;
200
        }
201
 
202
        return $this;
203
    }
204
 
205
    /**
206
     * Add a view instance to the view data.
207
     *
208
     * @param  string  $key
209
     * @param  string  $view
210
     * @param  array  $data
211
     * @return $this
212
     */
213
    public function nest($key, $view, array $data = [])
214
    {
215
        return $this->with($key, $this->factory->make($view, $data));
216
    }
217
 
218
    /**
219
     * Add validation errors to the view.
220
     *
221
     * @param  \Illuminate\Contracts\Support\MessageProvider|array  $provider
222
     * @param  string  $bag
223
     * @return $this
224
     */
225
    public function withErrors($provider, $bag = 'default')
226
    {
227
        return $this->with('errors', (new ViewErrorBag)->put(
228
            $bag, $this->formatErrors($provider)
229
        ));
230
    }
231
 
232
    /**
233
     * Parse the given errors into an appropriate value.
234
     *
235
     * @param  \Illuminate\Contracts\Support\MessageProvider|array|string  $provider
236
     * @return \Illuminate\Support\MessageBag
237
     */
238
    protected function formatErrors($provider)
239
    {
240
        return $provider instanceof MessageProvider
241
                        ? $provider->getMessageBag()
242
                        : new MessageBag((array) $provider);
243
    }
244
 
245
    /**
246
     * Get the name of the view.
247
     *
248
     * @return string
249
     */
250
    public function name()
251
    {
252
        return $this->getName();
253
    }
254
 
255
    /**
256
     * Get the name of the view.
257
     *
258
     * @return string
259
     */
260
    public function getName()
261
    {
262
        return $this->view;
263
    }
264
 
265
    /**
266
     * Get the array of view data.
267
     *
268
     * @return array
269
     */
270
    public function getData()
271
    {
272
        return $this->data;
273
    }
274
 
275
    /**
276
     * Get the path to the view file.
277
     *
278
     * @return string
279
     */
280
    public function getPath()
281
    {
282
        return $this->path;
283
    }
284
 
285
    /**
286
     * Set the path to the view.
287
     *
288
     * @param  string  $path
289
     * @return void
290
     */
291
    public function setPath($path)
292
    {
293
        $this->path = $path;
294
    }
295
 
296
    /**
297
     * Get the view factory instance.
298
     *
299
     * @return \Illuminate\View\Factory
300
     */
301
    public function getFactory()
302
    {
303
        return $this->factory;
304
    }
305
 
306
    /**
307
     * Get the view's rendering engine.
308
     *
309
     * @return \Illuminate\Contracts\View\Engine
310
     */
311
    public function getEngine()
312
    {
313
        return $this->engine;
314
    }
315
 
316
    /**
317
     * Determine if a piece of data is bound.
318
     *
319
     * @param  string  $key
320
     * @return bool
321
     */
322
    public function offsetExists($key): bool
323
    {
324
        return array_key_exists($key, $this->data);
325
    }
326
 
327
    /**
328
     * Get a piece of bound data to the view.
329
     *
330
     * @param  string  $key
331
     * @return mixed
332
     */
333
    public function offsetGet($key): mixed
334
    {
335
        return $this->data[$key];
336
    }
337
 
338
    /**
339
     * Set a piece of data on the view.
340
     *
341
     * @param  string  $key
342
     * @param  mixed  $value
343
     * @return void
344
     */
345
    public function offsetSet($key, $value): void
346
    {
347
        $this->with($key, $value);
348
    }
349
 
350
    /**
351
     * Unset a piece of data from the view.
352
     *
353
     * @param  string  $key
354
     * @return void
355
     */
356
    public function offsetUnset($key): void
357
    {
358
        unset($this->data[$key]);
359
    }
360
 
361
    /**
362
     * Get a piece of data from the view.
363
     *
364
     * @param  string  $key
365
     * @return mixed
366
     */
367
    public function &__get($key)
368
    {
369
        return $this->data[$key];
370
    }
371
 
372
    /**
373
     * Set a piece of data on the view.
374
     *
375
     * @param  string  $key
376
     * @param  mixed  $value
377
     * @return void
378
     */
379
    public function __set($key, $value)
380
    {
381
        $this->with($key, $value);
382
    }
383
 
384
    /**
385
     * Check if a piece of data is bound to the view.
386
     *
387
     * @param  string  $key
388
     * @return bool
389
     */
390
    public function __isset($key)
391
    {
392
        return isset($this->data[$key]);
393
    }
394
 
395
    /**
396
     * Remove a piece of bound data from the view.
397
     *
398
     * @param  string  $key
399
     * @return void
400
     */
401
    public function __unset($key)
402
    {
403
        unset($this->data[$key]);
404
    }
405
 
406
    /**
407
     * Dynamically bind parameters to the view.
408
     *
409
     * @param  string  $method
410
     * @param  array  $parameters
411
     * @return \Illuminate\View\View
412
     *
413
     * @throws \BadMethodCallException
414
     */
415
    public function __call($method, $parameters)
416
    {
417
        if (static::hasMacro($method)) {
418
            return $this->macroCall($method, $parameters);
419
        }
420
 
421
        if (! str_starts_with($method, 'with')) {
422
            throw new BadMethodCallException(sprintf(
423
                'Method %s::%s does not exist.', static::class, $method
424
            ));
425
        }
426
 
427
        return $this->with(Str::camel(substr($method, 4)), $parameters[0]);
428
    }
429
 
430
    /**
431
     * Get content as a string of HTML.
432
     *
433
     * @return string
434
     */
435
    public function toHtml()
436
    {
437
        return $this->render();
438
    }
439
 
440
    /**
441
     * Get the string contents of the view.
442
     *
443
     * @return string
444
     *
445
     * @throws \Throwable
446
     */
447
    public function __toString()
448
    {
449
        return $this->render();
450
    }
451
}