Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
    /**
150 lars 94
     * Get the evaluated contents for a given array of fragments.
95
     *
96
     * @param  array  $fragments
97
     * @return string
98
     */
99
    public function fragments(array $fragments)
100
    {
101
        return collect($fragments)->map(fn ($f) => $this->fragment($f))->implode('');
102
    }
103
 
104
    /**
105
     * Get the evaluated contents of a given fragment if the given condition is true.
106
     *
107
     * @param  bool  $boolean
108
     * @param  string  $fragment
109
     * @return string
110
     */
111
    public function fragmentIf($boolean, $fragment)
112
    {
113
        if (value($boolean)) {
114
            return $this->fragment($fragment);
115
        }
116
 
117
        return $this->render();
118
    }
119
 
120
    /**
121
     * Get the evaluated contents for a given array of fragments if the given condition is true.
122
     *
123
     * @param  bool  $boolean
124
     * @param  array  $fragments
125
     * @return string
126
     */
127
    public function fragmentsIf($boolean, array $fragments)
128
    {
129
        if (value($boolean)) {
130
            return $this->fragments($fragments);
131
        }
132
 
133
        return $this->render();
134
    }
135
 
136
    /**
148 lars 137
     * Get the string contents of the view.
138
     *
139
     * @param  callable|null  $callback
140
     * @return string
141
     *
142
     * @throws \Throwable
143
     */
144
    public function render(callable $callback = null)
145
    {
146
        try {
147
            $contents = $this->renderContents();
148
 
149
            $response = isset($callback) ? $callback($this, $contents) : null;
150
 
151
            // Once we have the contents of the view, we will flush the sections if we are
152
            // done rendering all views so that there is nothing left hanging over when
153
            // another view gets rendered in the future by the application developer.
154
            $this->factory->flushStateIfDoneRendering();
155
 
156
            return ! is_null($response) ? $response : $contents;
157
        } catch (Throwable $e) {
158
            $this->factory->flushState();
159
 
160
            throw $e;
161
        }
162
    }
163
 
164
    /**
165
     * Get the contents of the view instance.
166
     *
167
     * @return string
168
     */
169
    protected function renderContents()
170
    {
171
        // We will keep track of the number of views being rendered so we can flush
172
        // the section after the complete rendering operation is done. This will
173
        // clear out the sections for any separate views that may be rendered.
174
        $this->factory->incrementRender();
175
 
176
        $this->factory->callComposer($this);
177
 
178
        $contents = $this->getContents();
179
 
180
        // Once we've finished rendering the view, we'll decrement the render count
181
        // so that each section gets flushed out next time a view is created and
182
        // no old sections are staying around in the memory of an environment.
183
        $this->factory->decrementRender();
184
 
185
        return $contents;
186
    }
187
 
188
    /**
189
     * Get the evaluated contents of the view.
190
     *
191
     * @return string
192
     */
193
    protected function getContents()
194
    {
195
        return $this->engine->get($this->path, $this->gatherData());
196
    }
197
 
198
    /**
199
     * Get the data bound to the view instance.
200
     *
201
     * @return array
202
     */
203
    public function gatherData()
204
    {
205
        $data = array_merge($this->factory->getShared(), $this->data);
206
 
207
        foreach ($data as $key => $value) {
208
            if ($value instanceof Renderable) {
209
                $data[$key] = $value->render();
210
            }
211
        }
212
 
213
        return $data;
214
    }
215
 
216
    /**
217
     * Get the sections of the rendered view.
218
     *
219
     * @return array
220
     *
221
     * @throws \Throwable
222
     */
223
    public function renderSections()
224
    {
225
        return $this->render(function () {
226
            return $this->factory->getSections();
227
        });
228
    }
229
 
230
    /**
231
     * Add a piece of data to the view.
232
     *
233
     * @param  string|array  $key
234
     * @param  mixed  $value
235
     * @return $this
236
     */
237
    public function with($key, $value = null)
238
    {
239
        if (is_array($key)) {
240
            $this->data = array_merge($this->data, $key);
241
        } else {
242
            $this->data[$key] = $value;
243
        }
244
 
245
        return $this;
246
    }
247
 
248
    /**
249
     * Add a view instance to the view data.
250
     *
251
     * @param  string  $key
252
     * @param  string  $view
253
     * @param  array  $data
254
     * @return $this
255
     */
256
    public function nest($key, $view, array $data = [])
257
    {
258
        return $this->with($key, $this->factory->make($view, $data));
259
    }
260
 
261
    /**
262
     * Add validation errors to the view.
263
     *
264
     * @param  \Illuminate\Contracts\Support\MessageProvider|array  $provider
265
     * @param  string  $bag
266
     * @return $this
267
     */
268
    public function withErrors($provider, $bag = 'default')
269
    {
270
        return $this->with('errors', (new ViewErrorBag)->put(
271
            $bag, $this->formatErrors($provider)
272
        ));
273
    }
274
 
275
    /**
276
     * Parse the given errors into an appropriate value.
277
     *
278
     * @param  \Illuminate\Contracts\Support\MessageProvider|array|string  $provider
279
     * @return \Illuminate\Support\MessageBag
280
     */
281
    protected function formatErrors($provider)
282
    {
283
        return $provider instanceof MessageProvider
284
                        ? $provider->getMessageBag()
285
                        : new MessageBag((array) $provider);
286
    }
287
 
288
    /**
289
     * Get the name of the view.
290
     *
291
     * @return string
292
     */
293
    public function name()
294
    {
295
        return $this->getName();
296
    }
297
 
298
    /**
299
     * Get the name of the view.
300
     *
301
     * @return string
302
     */
303
    public function getName()
304
    {
305
        return $this->view;
306
    }
307
 
308
    /**
309
     * Get the array of view data.
310
     *
311
     * @return array
312
     */
313
    public function getData()
314
    {
315
        return $this->data;
316
    }
317
 
318
    /**
319
     * Get the path to the view file.
320
     *
321
     * @return string
322
     */
323
    public function getPath()
324
    {
325
        return $this->path;
326
    }
327
 
328
    /**
329
     * Set the path to the view.
330
     *
331
     * @param  string  $path
332
     * @return void
333
     */
334
    public function setPath($path)
335
    {
336
        $this->path = $path;
337
    }
338
 
339
    /**
340
     * Get the view factory instance.
341
     *
342
     * @return \Illuminate\View\Factory
343
     */
344
    public function getFactory()
345
    {
346
        return $this->factory;
347
    }
348
 
349
    /**
350
     * Get the view's rendering engine.
351
     *
352
     * @return \Illuminate\Contracts\View\Engine
353
     */
354
    public function getEngine()
355
    {
356
        return $this->engine;
357
    }
358
 
359
    /**
360
     * Determine if a piece of data is bound.
361
     *
362
     * @param  string  $key
363
     * @return bool
364
     */
365
    public function offsetExists($key): bool
366
    {
367
        return array_key_exists($key, $this->data);
368
    }
369
 
370
    /**
371
     * Get a piece of bound data to the view.
372
     *
373
     * @param  string  $key
374
     * @return mixed
375
     */
376
    public function offsetGet($key): mixed
377
    {
378
        return $this->data[$key];
379
    }
380
 
381
    /**
382
     * Set a piece of data on the view.
383
     *
384
     * @param  string  $key
385
     * @param  mixed  $value
386
     * @return void
387
     */
388
    public function offsetSet($key, $value): void
389
    {
390
        $this->with($key, $value);
391
    }
392
 
393
    /**
394
     * Unset a piece of data from the view.
395
     *
396
     * @param  string  $key
397
     * @return void
398
     */
399
    public function offsetUnset($key): void
400
    {
401
        unset($this->data[$key]);
402
    }
403
 
404
    /**
405
     * Get a piece of data from the view.
406
     *
407
     * @param  string  $key
408
     * @return mixed
409
     */
410
    public function &__get($key)
411
    {
412
        return $this->data[$key];
413
    }
414
 
415
    /**
416
     * Set a piece of data on the view.
417
     *
418
     * @param  string  $key
419
     * @param  mixed  $value
420
     * @return void
421
     */
422
    public function __set($key, $value)
423
    {
424
        $this->with($key, $value);
425
    }
426
 
427
    /**
428
     * Check if a piece of data is bound to the view.
429
     *
430
     * @param  string  $key
431
     * @return bool
432
     */
433
    public function __isset($key)
434
    {
435
        return isset($this->data[$key]);
436
    }
437
 
438
    /**
439
     * Remove a piece of bound data from the view.
440
     *
441
     * @param  string  $key
442
     * @return void
443
     */
444
    public function __unset($key)
445
    {
446
        unset($this->data[$key]);
447
    }
448
 
449
    /**
450
     * Dynamically bind parameters to the view.
451
     *
452
     * @param  string  $method
453
     * @param  array  $parameters
454
     * @return \Illuminate\View\View
455
     *
456
     * @throws \BadMethodCallException
457
     */
458
    public function __call($method, $parameters)
459
    {
460
        if (static::hasMacro($method)) {
461
            return $this->macroCall($method, $parameters);
462
        }
463
 
464
        if (! str_starts_with($method, 'with')) {
465
            throw new BadMethodCallException(sprintf(
466
                'Method %s::%s does not exist.', static::class, $method
467
            ));
468
        }
469
 
470
        return $this->with(Str::camel(substr($method, 4)), $parameters[0]);
471
    }
472
 
473
    /**
474
     * Get content as a string of HTML.
475
     *
476
     * @return string
477
     */
478
    public function toHtml()
479
    {
480
        return $this->render();
481
    }
482
 
483
    /**
484
     * Get the string contents of the view.
485
     *
486
     * @return string
487
     *
488
     * @throws \Throwable
489
     */
490
    public function __toString()
491
    {
492
        return $this->render();
493
    }
494
}