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