1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,568 +0,0 @@ |
1 |
-<?php |
|
2 |
- |
|
3 |
-namespace Illuminate\View; |
|
4 |
- |
|
5 |
-use Illuminate\Support\Arr; |
|
6 |
-use Illuminate\Support\Str; |
|
7 |
-use InvalidArgumentException; |
|
8 |
-use Illuminate\Support\Traits\Macroable; |
|
9 |
-use Illuminate\Contracts\Events\Dispatcher; |
|
10 |
-use Illuminate\Contracts\Support\Arrayable; |
|
11 |
-use Illuminate\View\Engines\EngineResolver; |
|
12 |
-use Illuminate\Contracts\Container\Container; |
|
13 |
-use Illuminate\Contracts\View\Factory as FactoryContract; |
|
14 |
- |
|
15 |
-class Factory implements FactoryContract |
|
16 |
-{ |
|
17 |
- use Macroable, |
|
18 |
- Concerns\ManagesComponents, |
|
19 |
- Concerns\ManagesEvents, |
|
20 |
- Concerns\ManagesLayouts, |
|
21 |
- Concerns\ManagesLoops, |
|
22 |
- Concerns\ManagesStacks, |
|
23 |
- Concerns\ManagesTranslations; |
|
24 |
- |
|
25 |
- /** |
|
26 |
- * The engine implementation. |
|
27 |
- * |
|
28 |
- * @var \Illuminate\View\Engines\EngineResolver |
|
29 |
- */ |
|
30 |
- protected $engines; |
|
31 |
- |
|
32 |
- /** |
|
33 |
- * The view finder implementation. |
|
34 |
- * |
|
35 |
- * @var \Illuminate\View\ViewFinderInterface |
|
36 |
- */ |
|
37 |
- protected $finder; |
|
38 |
- |
|
39 |
- /** |
|
40 |
- * The event dispatcher instance. |
|
41 |
- * |
|
42 |
- * @var \Illuminate\Contracts\Events\Dispatcher |
|
43 |
- */ |
|
44 |
- protected $events; |
|
45 |
- |
|
46 |
- /** |
|
47 |
- * The IoC container instance. |
|
48 |
- * |
|
49 |
- * @var \Illuminate\Contracts\Container\Container |
|
50 |
- */ |
|
51 |
- protected $container; |
|
52 |
- |
|
53 |
- /** |
|
54 |
- * Data that should be available to all templates. |
|
55 |
- * |
|
56 |
- * @var array |
|
57 |
- */ |
|
58 |
- protected $shared = []; |
|
59 |
- |
|
60 |
- /** |
|
61 |
- * The extension to engine bindings. |
|
62 |
- * |
|
63 |
- * @var array |
|
64 |
- */ |
|
65 |
- protected $extensions = [ |
|
66 |
- 'blade.php' => 'blade', |
|
67 |
- 'php' => 'php', |
|
68 |
- 'css' => 'file', |
|
69 |
- 'html' => 'file', |
|
70 |
- ]; |
|
71 |
- |
|
72 |
- /** |
|
73 |
- * The view composer events. |
|
74 |
- * |
|
75 |
- * @var array |
|
76 |
- */ |
|
77 |
- protected $composers = []; |
|
78 |
- |
|
79 |
- /** |
|
80 |
- * The number of active rendering operations. |
|
81 |
- * |
|
82 |
- * @var int |
|
83 |
- */ |
|
84 |
- protected $renderCount = 0; |
|
85 |
- |
|
86 |
- /** |
|
87 |
- * Create a new view factory instance. |
|
88 |
- * |
|
89 |
- * @param \Illuminate\View\Engines\EngineResolver $engines |
|
90 |
- * @param \Illuminate\View\ViewFinderInterface $finder |
|
91 |
- * @param \Illuminate\Contracts\Events\Dispatcher $events |
|
92 |
- * @return void |
|
93 |
- */ |
|
94 |
- public function __construct(EngineResolver $engines, ViewFinderInterface $finder, Dispatcher $events) |
|
95 |
- { |
|
96 |
- $this->finder = $finder; |
|
97 |
- $this->events = $events; |
|
98 |
- $this->engines = $engines; |
|
99 |
- |
|
100 |
- $this->share('__env', $this); |
|
101 |
- } |
|
102 |
- |
|
103 |
- /** |
|
104 |
- * Get the evaluated view contents for the given view. |
|
105 |
- * |
|
106 |
- * @param string $path |
|
107 |
- * @param \Illuminate\Contracts\Support\Arrayable|array $data |
|
108 |
- * @param array $mergeData |
|
109 |
- * @return \Illuminate\Contracts\View\View |
|
110 |
- */ |
|
111 |
- public function file($path, $data = [], $mergeData = []) |
|
112 |
- { |
|
113 |
- $data = array_merge($mergeData, $this->parseData($data)); |
|
114 |
- |
|
115 |
- return tap($this->viewInstance($path, $path, $data), function ($view) { |
|
116 |
- $this->callCreator($view); |
|
117 |
- }); |
|
118 |
- } |
|
119 |
- |
|
120 |
- /** |
|
121 |
- * Get the evaluated view contents for the given view. |
|
122 |
- * |
|
123 |
- * @param string $view |
|
124 |
- * @param \Illuminate\Contracts\Support\Arrayable|array $data |
|
125 |
- * @param array $mergeData |
|
126 |
- * @return \Illuminate\Contracts\View\View |
|
127 |
- */ |
|
128 |
- public function make($view, $data = [], $mergeData = []) |
|
129 |
- { |
|
130 |
- $path = $this->finder->find( |
|
131 |
- $view = $this->normalizeName($view) |
|
132 |
- ); |
|
133 |
- |
|
134 |
- // Next, we will create the view instance and call the view creator for the view |
|
135 |
- // which can set any data, etc. Then we will return the view instance back to |
|
136 |
- // the caller for rendering or performing other view manipulations on this. |
|
137 |
- $data = array_merge($mergeData, $this->parseData($data)); |
|
138 |
- |
|
139 |
- return tap($this->viewInstance($view, $path, $data), function ($view) { |
|
140 |
- $this->callCreator($view); |
|
141 |
- }); |
|
142 |
- } |
|
143 |
- |
|
144 |
- /** |
|
145 |
- * Get the first view that actually exists from the given list. |
|
146 |
- * |
|
147 |
- * @param array $views |
|
148 |
- * @param \Illuminate\Contracts\Support\Arrayable|array $data |
|
149 |
- * @param array $mergeData |
|
150 |
- * @return \Illuminate\Contracts\View\View |
|
151 |
- * |
|
152 |
- * @throws \InvalidArgumentException |
|
153 |
- */ |
|
154 |
- public function first(array $views, $data = [], $mergeData = []) |
|
155 |
- { |
|
156 |
- $view = Arr::first($views, function ($view) { |
|
157 |
- return $this->exists($view); |
|
158 |
- }); |
|
159 |
- |
|
160 |
- if (! $view) { |
|
161 |
- throw new InvalidArgumentException('None of the views in the given array exist.'); |
|
162 |
- } |
|
163 |
- |
|
164 |
- return $this->make($view, $data, $mergeData); |
|
165 |
- } |
|
166 |
- |
|
167 |
- /** |
|
168 |
- * Get the rendered content of the view based on a given condition. |
|
169 |
- * |
|
170 |
- * @param bool $condition |
|
171 |
- * @param string $view |
|
172 |
- * @param \Illuminate\Contracts\Support\Arrayable|array $data |
|
173 |
- * @param array $mergeData |
|
174 |
- * @return string |
|
175 |
- */ |
|
176 |
- public function renderWhen($condition, $view, $data = [], $mergeData = []) |
|
177 |
- { |
|
178 |
- if (! $condition) { |
|
179 |
- return ''; |
|
180 |
- } |
|
181 |
- |
|
182 |
- return $this->make($view, $this->parseData($data), $mergeData)->render(); |
|
183 |
- } |
|
184 |
- |
|
185 |
- /** |
|
186 |
- * Get the rendered contents of a partial from a loop. |
|
187 |
- * |
|
188 |
- * @param string $view |
|
189 |
- * @param array $data |
|
190 |
- * @param string $iterator |
|
191 |
- * @param string $empty |
|
192 |
- * @return string |
|
193 |
- */ |
|
194 |
- public function renderEach($view, $data, $iterator, $empty = 'raw|') |
|
195 |
- { |
|
196 |
- $result = ''; |
|
197 |
- |
|
198 |
- // If is actually data in the array, we will loop through the data and append |
|
199 |
- // an instance of the partial view to the final result HTML passing in the |
|
200 |
- // iterated value of this data array, allowing the views to access them. |
|
201 |
- if (count($data) > 0) { |
|
202 |
- foreach ($data as $key => $value) { |
|
203 |
- $result .= $this->make( |
|
204 |
- $view, ['key' => $key, $iterator => $value] |
|
205 |
- )->render(); |
|
206 |
- } |
|
207 |
- } |
|
208 |
- |
|
209 |
- // If there is no data in the array, we will render the contents of the empty |
|
210 |
- // view. Alternatively, the "empty view" could be a raw string that begins |
|
211 |
- // with "raw|" for convenience and to let this know that it is a string. |
|
212 |
- else { |
|
213 |
- $result = Str::startsWith($empty, 'raw|') |
|
214 |
- ? substr($empty, 4) |
|
215 |
- : $this->make($empty)->render(); |
|
216 |
- } |
|
217 |
- |
|
218 |
- return $result; |
|
219 |
- } |
|
220 |
- |
|
221 |
- /** |
|
222 |
- * Normalize a view name. |
|
223 |
- * |
|
224 |
- * @param string $name |
|
225 |
- * @return string |
|
226 |
- */ |
|
227 |
- protected function normalizeName($name) |
|
228 |
- { |
|
229 |
- return ViewName::normalize($name); |
|
230 |
- } |
|
231 |
- |
|
232 |
- /** |
|
233 |
- * Parse the given data into a raw array. |
|
234 |
- * |
|
235 |
- * @param mixed $data |
|
236 |
- * @return array |
|
237 |
- */ |
|
238 |
- protected function parseData($data) |
|
239 |
- { |
|
240 |
- return $data instanceof Arrayable ? $data->toArray() : $data; |
|
241 |
- } |
|
242 |
- |
|
243 |
- /** |
|
244 |
- * Create a new view instance from the given arguments. |
|
245 |
- * |
|
246 |
- * @param string $view |
|
247 |
- * @param string $path |
|
248 |
- * @param \Illuminate\Contracts\Support\Arrayable|array $data |
|
249 |
- * @return \Illuminate\Contracts\View\View |
|
250 |
- */ |
|
251 |
- protected function viewInstance($view, $path, $data) |
|
252 |
- { |
|
253 |
- return new View($this, $this->getEngineFromPath($path), $view, $path, $data); |
|
254 |
- } |
|
255 |
- |
|
256 |
- /** |
|
257 |
- * Determine if a given view exists. |
|
258 |
- * |
|
259 |
- * @param string $view |
|
260 |
- * @return bool |
|
261 |
- */ |
|
262 |
- public function exists($view) |
|
263 |
- { |
|
264 |
- try { |
|
265 |
- $this->finder->find($view); |
|
266 |
- } catch (InvalidArgumentException $e) { |
|
267 |
- return false; |
|
268 |
- } |
|
269 |
- |
|
270 |
- return true; |
|
271 |
- } |
|
272 |
- |
|
273 |
- /** |
|
274 |
- * Get the appropriate view engine for the given path. |
|
275 |
- * |
|
276 |
- * @param string $path |
|
277 |
- * @return \Illuminate\Contracts\View\Engine |
|
278 |
- * |
|
279 |
- * @throws \InvalidArgumentException |
|
280 |
- */ |
|
281 |
- public function getEngineFromPath($path) |
|
282 |
- { |
|
283 |
- if (! $extension = $this->getExtension($path)) { |
|
284 |
- throw new InvalidArgumentException("Unrecognized extension in file: {$path}"); |
|
285 |
- } |
|
286 |
- |
|
287 |
- $engine = $this->extensions[$extension]; |
|
288 |
- |
|
289 |
- return $this->engines->resolve($engine); |
|
290 |
- } |
|
291 |
- |
|
292 |
- /** |
|
293 |
- * Get the extension used by the view file. |
|
294 |
- * |
|
295 |
- * @param string $path |
|
296 |
- * @return string |
|
297 |
- */ |
|
298 |
- protected function getExtension($path) |
|
299 |
- { |
|
300 |
- $extensions = array_keys($this->extensions); |
|
301 |
- |
|
302 |
- return Arr::first($extensions, function ($value) use ($path) { |
|
303 |
- return Str::endsWith($path, '.'.$value); |
|
304 |
- }); |
|
305 |
- } |
|
306 |
- |
|
307 |
- /** |
|
308 |
- * Add a piece of shared data to the environment. |
|
309 |
- * |
|
310 |
- * @param array|string $key |
|
311 |
- * @param mixed|null $value |
|
312 |
- * @return mixed |
|
313 |
- */ |
|
314 |
- public function share($key, $value = null) |
|
315 |
- { |
|
316 |
- $keys = is_array($key) ? $key : [$key => $value]; |
|
317 |
- |
|
318 |
- foreach ($keys as $key => $value) { |
|
319 |
- $this->shared[$key] = $value; |
|
320 |
- } |
|
321 |
- |
|
322 |
- return $value; |
|
323 |
- } |
|
324 |
- |
|
325 |
- /** |
|
326 |
- * Increment the rendering counter. |
|
327 |
- * |
|
328 |
- * @return void |
|
329 |
- */ |
|
330 |
- public function incrementRender() |
|
331 |
- { |
|
332 |
- $this->renderCount++; |
|
333 |
- } |
|
334 |
- |
|
335 |
- /** |
|
336 |
- * Decrement the rendering counter. |
|
337 |
- * |
|
338 |
- * @return void |
|
339 |
- */ |
|
340 |
- public function decrementRender() |
|
341 |
- { |
|
342 |
- $this->renderCount--; |
|
343 |
- } |
|
344 |
- |
|
345 |
- /** |
|
346 |
- * Check if there are no active render operations. |
|
347 |
- * |
|
348 |
- * @return bool |
|
349 |
- */ |
|
350 |
- public function doneRendering() |
|
351 |
- { |
|
352 |
- return $this->renderCount == 0; |
|
353 |
- } |
|
354 |
- |
|
355 |
- /** |
|
356 |
- * Add a location to the array of view locations. |
|
357 |
- * |
|
358 |
- * @param string $location |
|
359 |
- * @return void |
|
360 |
- */ |
|
361 |
- public function addLocation($location) |
|
362 |
- { |
|
363 |
- $this->finder->addLocation($location); |
|
364 |
- } |
|
365 |
- |
|
366 |
- /** |
|
367 |
- * Add a new namespace to the loader. |
|
368 |
- * |
|
369 |
- * @param string $namespace |
|
370 |
- * @param string|array $hints |
|
371 |
- * @return $this |
|
372 |
- */ |
|
373 |
- public function addNamespace($namespace, $hints) |
|
374 |
- { |
|
375 |
- $this->finder->addNamespace($namespace, $hints); |
|
376 |
- |
|
377 |
- return $this; |
|
378 |
- } |
|
379 |
- |
|
380 |
- /** |
|
381 |
- * Prepend a new namespace to the loader. |
|
382 |
- * |
|
383 |
- * @param string $namespace |
|
384 |
- * @param string|array $hints |
|
385 |
- * @return $this |
|
386 |
- */ |
|
387 |
- public function prependNamespace($namespace, $hints) |
|
388 |
- { |
|
389 |
- $this->finder->prependNamespace($namespace, $hints); |
|
390 |
- |
|
391 |
- return $this; |
|
392 |
- } |
|
393 |
- |
|
394 |
- /** |
|
395 |
- * Replace the namespace hints for the given namespace. |
|
396 |
- * |
|
397 |
- * @param string $namespace |
|
398 |
- * @param string|array $hints |
|
399 |
- * @return $this |
|
400 |
- */ |
|
401 |
- public function replaceNamespace($namespace, $hints) |
|
402 |
- { |
|
403 |
- $this->finder->replaceNamespace($namespace, $hints); |
|
404 |
- |
|
405 |
- return $this; |
|
406 |
- } |
|
407 |
- |
|
408 |
- /** |
|
409 |
- * Register a valid view extension and its engine. |
|
410 |
- * |
|
411 |
- * @param string $extension |
|
412 |
- * @param string $engine |
|
413 |
- * @param \Closure|null $resolver |
|
414 |
- * @return void |
|
415 |
- */ |
|
416 |
- public function addExtension($extension, $engine, $resolver = null) |
|
417 |
- { |
|
418 |
- $this->finder->addExtension($extension); |
|
419 |
- |
|
420 |
- if (isset($resolver)) { |
|
421 |
- $this->engines->register($engine, $resolver); |
|
422 |
- } |
|
423 |
- |
|
424 |
- unset($this->extensions[$extension]); |
|
425 |
- |
|
426 |
- $this->extensions = array_merge([$extension => $engine], $this->extensions); |
|
427 |
- } |
|
428 |
- |
|
429 |
- /** |
|
430 |
- * Flush all of the factory state like sections and stacks. |
|
431 |
- * |
|
432 |
- * @return void |
|
433 |
- */ |
|
434 |
- public function flushState() |
|
435 |
- { |
|
436 |
- $this->renderCount = 0; |
|
437 |
- |
|
438 |
- $this->flushSections(); |
|
439 |
- $this->flushStacks(); |
|
440 |
- } |
|
441 |
- |
|
442 |
- /** |
|
443 |
- * Flush all of the section contents if done rendering. |
|
444 |
- * |
|
445 |
- * @return void |
|
446 |
- */ |
|
447 |
- public function flushStateIfDoneRendering() |
|
448 |
- { |
|
449 |
- if ($this->doneRendering()) { |
|
450 |
- $this->flushState(); |
|
451 |
- } |
|
452 |
- } |
|
453 |
- |
|
454 |
- /** |
|
455 |
- * Get the extension to engine bindings. |
|
456 |
- * |
|
457 |
- * @return array |
|
458 |
- */ |
|
459 |
- public function getExtensions() |
|
460 |
- { |
|
461 |
- return $this->extensions; |
|
462 |
- } |
|
463 |
- |
|
464 |
- /** |
|
465 |
- * Get the engine resolver instance. |
|
466 |
- * |
|
467 |
- * @return \Illuminate\View\Engines\EngineResolver |
|
468 |
- */ |
|
469 |
- public function getEngineResolver() |
|
470 |
- { |
|
471 |
- return $this->engines; |
|
472 |
- } |
|
473 |
- |
|
474 |
- /** |
|
475 |
- * Get the view finder instance. |
|
476 |
- * |
|
477 |
- * @return \Illuminate\View\ViewFinderInterface |
|
478 |
- */ |
|
479 |
- public function getFinder() |
|
480 |
- { |
|
481 |
- return $this->finder; |
|
482 |
- } |
|
483 |
- |
|
484 |
- /** |
|
485 |
- * Set the view finder instance. |
|
486 |
- * |
|
487 |
- * @param \Illuminate\View\ViewFinderInterface $finder |
|
488 |
- * @return void |
|
489 |
- */ |
|
490 |
- public function setFinder(ViewFinderInterface $finder) |
|
491 |
- { |
|
492 |
- $this->finder = $finder; |
|
493 |
- } |
|
494 |
- |
|
495 |
- /** |
|
496 |
- * Flush the cache of views located by the finder. |
|
497 |
- * |
|
498 |
- * @return void |
|
499 |
- */ |
|
500 |
- public function flushFinderCache() |
|
501 |
- { |
|
502 |
- $this->getFinder()->flush(); |
|
503 |
- } |
|
504 |
- |
|
505 |
- /** |
|
506 |
- * Get the event dispatcher instance. |
|
507 |
- * |
|
508 |
- * @return \Illuminate\Contracts\Events\Dispatcher |
|
509 |
- */ |
|
510 |
- public function getDispatcher() |
|
511 |
- { |
|
512 |
- return $this->events; |
|
513 |
- } |
|
514 |
- |
|
515 |
- /** |
|
516 |
- * Set the event dispatcher instance. |
|
517 |
- * |
|
518 |
- * @param \Illuminate\Contracts\Events\Dispatcher $events |
|
519 |
- * @return void |
|
520 |
- */ |
|
521 |
- public function setDispatcher(Dispatcher $events) |
|
522 |
- { |
|
523 |
- $this->events = $events; |
|
524 |
- } |
|
525 |
- |
|
526 |
- /** |
|
527 |
- * Get the IoC container instance. |
|
528 |
- * |
|
529 |
- * @return \Illuminate\Contracts\Container\Container |
|
530 |
- */ |
|
531 |
- public function getContainer() |
|
532 |
- { |
|
533 |
- return $this->container; |
|
534 |
- } |
|
535 |
- |
|
536 |
- /** |
|
537 |
- * Set the IoC container instance. |
|
538 |
- * |
|
539 |
- * @param \Illuminate\Contracts\Container\Container $container |
|
540 |
- * @return void |
|
541 |
- */ |
|
542 |
- public function setContainer(Container $container) |
|
543 |
- { |
|
544 |
- $this->container = $container; |
|
545 |
- } |
|
546 |
- |
|
547 |
- /** |
|
548 |
- * Get an item from the shared data. |
|
549 |
- * |
|
550 |
- * @param string $key |
|
551 |
- * @param mixed $default |
|
552 |
- * @return mixed |
|
553 |
- */ |
|
554 |
- public function shared($key, $default = null) |
|
555 |
- { |
|
556 |
- return Arr::get($this->shared, $key, $default); |
|
557 |
- } |
|
558 |
- |
|
559 |
- /** |
|
560 |
- * Get all of the shared data for the environment. |
|
561 |
- * |
|
562 |
- * @return array |
|
563 |
- */ |
|
564 |
- public function getShared() |
|
565 |
- { |
|
566 |
- return $this->shared; |
|
567 |
- } |
|
568 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,568 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace Illuminate\View; |
|
4 |
+ |
|
5 |
+use Illuminate\Support\Arr; |
|
6 |
+use Illuminate\Support\Str; |
|
7 |
+use InvalidArgumentException; |
|
8 |
+use Illuminate\Support\Traits\Macroable; |
|
9 |
+use Illuminate\Contracts\Events\Dispatcher; |
|
10 |
+use Illuminate\Contracts\Support\Arrayable; |
|
11 |
+use Illuminate\View\Engines\EngineResolver; |
|
12 |
+use Illuminate\Contracts\Container\Container; |
|
13 |
+use Illuminate\Contracts\View\Factory as FactoryContract; |
|
14 |
+ |
|
15 |
+class Factory implements FactoryContract |
|
16 |
+{ |
|
17 |
+ use Macroable, |
|
18 |
+ Concerns\ManagesComponents, |
|
19 |
+ Concerns\ManagesEvents, |
|
20 |
+ Concerns\ManagesLayouts, |
|
21 |
+ Concerns\ManagesLoops, |
|
22 |
+ Concerns\ManagesStacks, |
|
23 |
+ Concerns\ManagesTranslations; |
|
24 |
+ |
|
25 |
+ /** |
|
26 |
+ * The engine implementation. |
|
27 |
+ * |
|
28 |
+ * @var \Illuminate\View\Engines\EngineResolver |
|
29 |
+ */ |
|
30 |
+ protected $engines; |
|
31 |
+ |
|
32 |
+ /** |
|
33 |
+ * The view finder implementation. |
|
34 |
+ * |
|
35 |
+ * @var \Illuminate\View\ViewFinderInterface |
|
36 |
+ */ |
|
37 |
+ protected $finder; |
|
38 |
+ |
|
39 |
+ /** |
|
40 |
+ * The event dispatcher instance. |
|
41 |
+ * |
|
42 |
+ * @var \Illuminate\Contracts\Events\Dispatcher |
|
43 |
+ */ |
|
44 |
+ protected $events; |
|
45 |
+ |
|
46 |
+ /** |
|
47 |
+ * The IoC container instance. |
|
48 |
+ * |
|
49 |
+ * @var \Illuminate\Contracts\Container\Container |
|
50 |
+ */ |
|
51 |
+ protected $container; |
|
52 |
+ |
|
53 |
+ /** |
|
54 |
+ * Data that should be available to all templates. |
|
55 |
+ * |
|
56 |
+ * @var array |
|
57 |
+ */ |
|
58 |
+ protected $shared = []; |
|
59 |
+ |
|
60 |
+ /** |
|
61 |
+ * The extension to engine bindings. |
|
62 |
+ * |
|
63 |
+ * @var array |
|
64 |
+ */ |
|
65 |
+ protected $extensions = [ |
|
66 |
+ 'blade.php' => 'blade', |
|
67 |
+ 'php' => 'php', |
|
68 |
+ 'css' => 'file', |
|
69 |
+ 'html' => 'file', |
|
70 |
+ ]; |
|
71 |
+ |
|
72 |
+ /** |
|
73 |
+ * The view composer events. |
|
74 |
+ * |
|
75 |
+ * @var array |
|
76 |
+ */ |
|
77 |
+ protected $composers = []; |
|
78 |
+ |
|
79 |
+ /** |
|
80 |
+ * The number of active rendering operations. |
|
81 |
+ * |
|
82 |
+ * @var int |
|
83 |
+ */ |
|
84 |
+ protected $renderCount = 0; |
|
85 |
+ |
|
86 |
+ /** |
|
87 |
+ * Create a new view factory instance. |
|
88 |
+ * |
|
89 |
+ * @param \Illuminate\View\Engines\EngineResolver $engines |
|
90 |
+ * @param \Illuminate\View\ViewFinderInterface $finder |
|
91 |
+ * @param \Illuminate\Contracts\Events\Dispatcher $events |
|
92 |
+ * @return void |
|
93 |
+ */ |
|
94 |
+ public function __construct(EngineResolver $engines, ViewFinderInterface $finder, Dispatcher $events) |
|
95 |
+ { |
|
96 |
+ $this->finder = $finder; |
|
97 |
+ $this->events = $events; |
|
98 |
+ $this->engines = $engines; |
|
99 |
+ |
|
100 |
+ $this->share('__env', $this); |
|
101 |
+ } |
|
102 |
+ |
|
103 |
+ /** |
|
104 |
+ * Get the evaluated view contents for the given view. |
|
105 |
+ * |
|
106 |
+ * @param string $path |
|
107 |
+ * @param \Illuminate\Contracts\Support\Arrayable|array $data |
|
108 |
+ * @param array $mergeData |
|
109 |
+ * @return \Illuminate\Contracts\View\View |
|
110 |
+ */ |
|
111 |
+ public function file($path, $data = [], $mergeData = []) |
|
112 |
+ { |
|
113 |
+ $data = array_merge($mergeData, $this->parseData($data)); |
|
114 |
+ |
|
115 |
+ return tap($this->viewInstance($path, $path, $data), function ($view) { |
|
116 |
+ $this->callCreator($view); |
|
117 |
+ }); |
|
118 |
+ } |
|
119 |
+ |
|
120 |
+ /** |
|
121 |
+ * Get the evaluated view contents for the given view. |
|
122 |
+ * |
|
123 |
+ * @param string $view |
|
124 |
+ * @param \Illuminate\Contracts\Support\Arrayable|array $data |
|
125 |
+ * @param array $mergeData |
|
126 |
+ * @return \Illuminate\Contracts\View\View |
|
127 |
+ */ |
|
128 |
+ public function make($view, $data = [], $mergeData = []) |
|
129 |
+ { |
|
130 |
+ $path = $this->finder->find( |
|
131 |
+ $view = $this->normalizeName($view) |
|
132 |
+ ); |
|
133 |
+ |
|
134 |
+ // Next, we will create the view instance and call the view creator for the view |
|
135 |
+ // which can set any data, etc. Then we will return the view instance back to |
|
136 |
+ // the caller for rendering or performing other view manipulations on this. |
|
137 |
+ $data = array_merge($mergeData, $this->parseData($data)); |
|
138 |
+ |
|
139 |
+ return tap($this->viewInstance($view, $path, $data), function ($view) { |
|
140 |
+ $this->callCreator($view); |
|
141 |
+ }); |
|
142 |
+ } |
|
143 |
+ |
|
144 |
+ /** |
|
145 |
+ * Get the first view that actually exists from the given list. |
|
146 |
+ * |
|
147 |
+ * @param array $views |
|
148 |
+ * @param \Illuminate\Contracts\Support\Arrayable|array $data |
|
149 |
+ * @param array $mergeData |
|
150 |
+ * @return \Illuminate\Contracts\View\View |
|
151 |
+ * |
|
152 |
+ * @throws \InvalidArgumentException |
|
153 |
+ */ |
|
154 |
+ public function first(array $views, $data = [], $mergeData = []) |
|
155 |
+ { |
|
156 |
+ $view = Arr::first($views, function ($view) { |
|
157 |
+ return $this->exists($view); |
|
158 |
+ }); |
|
159 |
+ |
|
160 |
+ if (! $view) { |
|
161 |
+ throw new InvalidArgumentException('None of the views in the given array exist.'); |
|
162 |
+ } |
|
163 |
+ |
|
164 |
+ return $this->make($view, $data, $mergeData); |
|
165 |
+ } |
|
166 |
+ |
|
167 |
+ /** |
|
168 |
+ * Get the rendered content of the view based on a given condition. |
|
169 |
+ * |
|
170 |
+ * @param bool $condition |
|
171 |
+ * @param string $view |
|
172 |
+ * @param \Illuminate\Contracts\Support\Arrayable|array $data |
|
173 |
+ * @param array $mergeData |
|
174 |
+ * @return string |
|
175 |
+ */ |
|
176 |
+ public function renderWhen($condition, $view, $data = [], $mergeData = []) |
|
177 |
+ { |
|
178 |
+ if (! $condition) { |
|
179 |
+ return ''; |
|
180 |
+ } |
|
181 |
+ |
|
182 |
+ return $this->make($view, $this->parseData($data), $mergeData)->render(); |
|
183 |
+ } |
|
184 |
+ |
|
185 |
+ /** |
|
186 |
+ * Get the rendered contents of a partial from a loop. |
|
187 |
+ * |
|
188 |
+ * @param string $view |
|
189 |
+ * @param array $data |
|
190 |
+ * @param string $iterator |
|
191 |
+ * @param string $empty |
|
192 |
+ * @return string |
|
193 |
+ */ |
|
194 |
+ public function renderEach($view, $data, $iterator, $empty = 'raw|') |
|
195 |
+ { |
|
196 |
+ $result = ''; |
|
197 |
+ |
|
198 |
+ // If is actually data in the array, we will loop through the data and append |
|
199 |
+ // an instance of the partial view to the final result HTML passing in the |
|
200 |
+ // iterated value of this data array, allowing the views to access them. |
|
201 |
+ if (count($data) > 0) { |
|
202 |
+ foreach ($data as $key => $value) { |
|
203 |
+ $result .= $this->make( |
|
204 |
+ $view, ['key' => $key, $iterator => $value] |
|
205 |
+ )->render(); |
|
206 |
+ } |
|
207 |
+ } |
|
208 |
+ |
|
209 |
+ // If there is no data in the array, we will render the contents of the empty |
|
210 |
+ // view. Alternatively, the "empty view" could be a raw string that begins |
|
211 |
+ // with "raw|" for convenience and to let this know that it is a string. |
|
212 |
+ else { |
|
213 |
+ $result = Str::startsWith($empty, 'raw|') |
|
214 |
+ ? substr($empty, 4) |
|
215 |
+ : $this->make($empty)->render(); |
|
216 |
+ } |
|
217 |
+ |
|
218 |
+ return $result; |
|
219 |
+ } |
|
220 |
+ |
|
221 |
+ /** |
|
222 |
+ * Normalize a view name. |
|
223 |
+ * |
|
224 |
+ * @param string $name |
|
225 |
+ * @return string |
|
226 |
+ */ |
|
227 |
+ protected function normalizeName($name) |
|
228 |
+ { |
|
229 |
+ return ViewName::normalize($name); |
|
230 |
+ } |
|
231 |
+ |
|
232 |
+ /** |
|
233 |
+ * Parse the given data into a raw array. |
|
234 |
+ * |
|
235 |
+ * @param mixed $data |
|
236 |
+ * @return array |
|
237 |
+ */ |
|
238 |
+ protected function parseData($data) |
|
239 |
+ { |
|
240 |
+ return $data instanceof Arrayable ? $data->toArray() : $data; |
|
241 |
+ } |
|
242 |
+ |
|
243 |
+ /** |
|
244 |
+ * Create a new view instance from the given arguments. |
|
245 |
+ * |
|
246 |
+ * @param string $view |
|
247 |
+ * @param string $path |
|
248 |
+ * @param \Illuminate\Contracts\Support\Arrayable|array $data |
|
249 |
+ * @return \Illuminate\Contracts\View\View |
|
250 |
+ */ |
|
251 |
+ protected function viewInstance($view, $path, $data) |
|
252 |
+ { |
|
253 |
+ return new View($this, $this->getEngineFromPath($path), $view, $path, $data); |
|
254 |
+ } |
|
255 |
+ |
|
256 |
+ /** |
|
257 |
+ * Determine if a given view exists. |
|
258 |
+ * |
|
259 |
+ * @param string $view |
|
260 |
+ * @return bool |
|
261 |
+ */ |
|
262 |
+ public function exists($view) |
|
263 |
+ { |
|
264 |
+ try { |
|
265 |
+ $this->finder->find($view); |
|
266 |
+ } catch (InvalidArgumentException $e) { |
|
267 |
+ return false; |
|
268 |
+ } |
|
269 |
+ |
|
270 |
+ return true; |
|
271 |
+ } |
|
272 |
+ |
|
273 |
+ /** |
|
274 |
+ * Get the appropriate view engine for the given path. |
|
275 |
+ * |
|
276 |
+ * @param string $path |
|
277 |
+ * @return \Illuminate\Contracts\View\Engine |
|
278 |
+ * |
|
279 |
+ * @throws \InvalidArgumentException |
|
280 |
+ */ |
|
281 |
+ public function getEngineFromPath($path) |
|
282 |
+ { |
|
283 |
+ if (! $extension = $this->getExtension($path)) { |
|
284 |
+ throw new InvalidArgumentException("Unrecognized extension in file: {$path}"); |
|
285 |
+ } |
|
286 |
+ |
|
287 |
+ $engine = $this->extensions[$extension]; |
|
288 |
+ |
|
289 |
+ return $this->engines->resolve($engine); |
|
290 |
+ } |
|
291 |
+ |
|
292 |
+ /** |
|
293 |
+ * Get the extension used by the view file. |
|
294 |
+ * |
|
295 |
+ * @param string $path |
|
296 |
+ * @return string |
|
297 |
+ */ |
|
298 |
+ protected function getExtension($path) |
|
299 |
+ { |
|
300 |
+ $extensions = array_keys($this->extensions); |
|
301 |
+ |
|
302 |
+ return Arr::first($extensions, function ($value) use ($path) { |
|
303 |
+ return Str::endsWith($path, '.'.$value); |
|
304 |
+ }); |
|
305 |
+ } |
|
306 |
+ |
|
307 |
+ /** |
|
308 |
+ * Add a piece of shared data to the environment. |
|
309 |
+ * |
|
310 |
+ * @param array|string $key |
|
311 |
+ * @param mixed|null $value |
|
312 |
+ * @return mixed |
|
313 |
+ */ |
|
314 |
+ public function share($key, $value = null) |
|
315 |
+ { |
|
316 |
+ $keys = is_array($key) ? $key : [$key => $value]; |
|
317 |
+ |
|
318 |
+ foreach ($keys as $key => $value) { |
|
319 |
+ $this->shared[$key] = $value; |
|
320 |
+ } |
|
321 |
+ |
|
322 |
+ return $value; |
|
323 |
+ } |
|
324 |
+ |
|
325 |
+ /** |
|
326 |
+ * Increment the rendering counter. |
|
327 |
+ * |
|
328 |
+ * @return void |
|
329 |
+ */ |
|
330 |
+ public function incrementRender() |
|
331 |
+ { |
|
332 |
+ $this->renderCount++; |
|
333 |
+ } |
|
334 |
+ |
|
335 |
+ /** |
|
336 |
+ * Decrement the rendering counter. |
|
337 |
+ * |
|
338 |
+ * @return void |
|
339 |
+ */ |
|
340 |
+ public function decrementRender() |
|
341 |
+ { |
|
342 |
+ $this->renderCount--; |
|
343 |
+ } |
|
344 |
+ |
|
345 |
+ /** |
|
346 |
+ * Check if there are no active render operations. |
|
347 |
+ * |
|
348 |
+ * @return bool |
|
349 |
+ */ |
|
350 |
+ public function doneRendering() |
|
351 |
+ { |
|
352 |
+ return $this->renderCount == 0; |
|
353 |
+ } |
|
354 |
+ |
|
355 |
+ /** |
|
356 |
+ * Add a location to the array of view locations. |
|
357 |
+ * |
|
358 |
+ * @param string $location |
|
359 |
+ * @return void |
|
360 |
+ */ |
|
361 |
+ public function addLocation($location) |
|
362 |
+ { |
|
363 |
+ $this->finder->addLocation($location); |
|
364 |
+ } |
|
365 |
+ |
|
366 |
+ /** |
|
367 |
+ * Add a new namespace to the loader. |
|
368 |
+ * |
|
369 |
+ * @param string $namespace |
|
370 |
+ * @param string|array $hints |
|
371 |
+ * @return $this |
|
372 |
+ */ |
|
373 |
+ public function addNamespace($namespace, $hints) |
|
374 |
+ { |
|
375 |
+ $this->finder->addNamespace($namespace, $hints); |
|
376 |
+ |
|
377 |
+ return $this; |
|
378 |
+ } |
|
379 |
+ |
|
380 |
+ /** |
|
381 |
+ * Prepend a new namespace to the loader. |
|
382 |
+ * |
|
383 |
+ * @param string $namespace |
|
384 |
+ * @param string|array $hints |
|
385 |
+ * @return $this |
|
386 |
+ */ |
|
387 |
+ public function prependNamespace($namespace, $hints) |
|
388 |
+ { |
|
389 |
+ $this->finder->prependNamespace($namespace, $hints); |
|
390 |
+ |
|
391 |
+ return $this; |
|
392 |
+ } |
|
393 |
+ |
|
394 |
+ /** |
|
395 |
+ * Replace the namespace hints for the given namespace. |
|
396 |
+ * |
|
397 |
+ * @param string $namespace |
|
398 |
+ * @param string|array $hints |
|
399 |
+ * @return $this |
|
400 |
+ */ |
|
401 |
+ public function replaceNamespace($namespace, $hints) |
|
402 |
+ { |
|
403 |
+ $this->finder->replaceNamespace($namespace, $hints); |
|
404 |
+ |
|
405 |
+ return $this; |
|
406 |
+ } |
|
407 |
+ |
|
408 |
+ /** |
|
409 |
+ * Register a valid view extension and its engine. |
|
410 |
+ * |
|
411 |
+ * @param string $extension |
|
412 |
+ * @param string $engine |
|
413 |
+ * @param \Closure|null $resolver |
|
414 |
+ * @return void |
|
415 |
+ */ |
|
416 |
+ public function addExtension($extension, $engine, $resolver = null) |
|
417 |
+ { |
|
418 |
+ $this->finder->addExtension($extension); |
|
419 |
+ |
|
420 |
+ if (isset($resolver)) { |
|
421 |
+ $this->engines->register($engine, $resolver); |
|
422 |
+ } |
|
423 |
+ |
|
424 |
+ unset($this->extensions[$extension]); |
|
425 |
+ |
|
426 |
+ $this->extensions = array_merge([$extension => $engine], $this->extensions); |
|
427 |
+ } |
|
428 |
+ |
|
429 |
+ /** |
|
430 |
+ * Flush all of the factory state like sections and stacks. |
|
431 |
+ * |
|
432 |
+ * @return void |
|
433 |
+ */ |
|
434 |
+ public function flushState() |
|
435 |
+ { |
|
436 |
+ $this->renderCount = 0; |
|
437 |
+ |
|
438 |
+ $this->flushSections(); |
|
439 |
+ $this->flushStacks(); |
|
440 |
+ } |
|
441 |
+ |
|
442 |
+ /** |
|
443 |
+ * Flush all of the section contents if done rendering. |
|
444 |
+ * |
|
445 |
+ * @return void |
|
446 |
+ */ |
|
447 |
+ public function flushStateIfDoneRendering() |
|
448 |
+ { |
|
449 |
+ if ($this->doneRendering()) { |
|
450 |
+ $this->flushState(); |
|
451 |
+ } |
|
452 |
+ } |
|
453 |
+ |
|
454 |
+ /** |
|
455 |
+ * Get the extension to engine bindings. |
|
456 |
+ * |
|
457 |
+ * @return array |
|
458 |
+ */ |
|
459 |
+ public function getExtensions() |
|
460 |
+ { |
|
461 |
+ return $this->extensions; |
|
462 |
+ } |
|
463 |
+ |
|
464 |
+ /** |
|
465 |
+ * Get the engine resolver instance. |
|
466 |
+ * |
|
467 |
+ * @return \Illuminate\View\Engines\EngineResolver |
|
468 |
+ */ |
|
469 |
+ public function getEngineResolver() |
|
470 |
+ { |
|
471 |
+ return $this->engines; |
|
472 |
+ } |
|
473 |
+ |
|
474 |
+ /** |
|
475 |
+ * Get the view finder instance. |
|
476 |
+ * |
|
477 |
+ * @return \Illuminate\View\ViewFinderInterface |
|
478 |
+ */ |
|
479 |
+ public function getFinder() |
|
480 |
+ { |
|
481 |
+ return $this->finder; |
|
482 |
+ } |
|
483 |
+ |
|
484 |
+ /** |
|
485 |
+ * Set the view finder instance. |
|
486 |
+ * |
|
487 |
+ * @param \Illuminate\View\ViewFinderInterface $finder |
|
488 |
+ * @return void |
|
489 |
+ */ |
|
490 |
+ public function setFinder(ViewFinderInterface $finder) |
|
491 |
+ { |
|
492 |
+ $this->finder = $finder; |
|
493 |
+ } |
|
494 |
+ |
|
495 |
+ /** |
|
496 |
+ * Flush the cache of views located by the finder. |
|
497 |
+ * |
|
498 |
+ * @return void |
|
499 |
+ */ |
|
500 |
+ public function flushFinderCache() |
|
501 |
+ { |
|
502 |
+ $this->getFinder()->flush(); |
|
503 |
+ } |
|
504 |
+ |
|
505 |
+ /** |
|
506 |
+ * Get the event dispatcher instance. |
|
507 |
+ * |
|
508 |
+ * @return \Illuminate\Contracts\Events\Dispatcher |
|
509 |
+ */ |
|
510 |
+ public function getDispatcher() |
|
511 |
+ { |
|
512 |
+ return $this->events; |
|
513 |
+ } |
|
514 |
+ |
|
515 |
+ /** |
|
516 |
+ * Set the event dispatcher instance. |
|
517 |
+ * |
|
518 |
+ * @param \Illuminate\Contracts\Events\Dispatcher $events |
|
519 |
+ * @return void |
|
520 |
+ */ |
|
521 |
+ public function setDispatcher(Dispatcher $events) |
|
522 |
+ { |
|
523 |
+ $this->events = $events; |
|
524 |
+ } |
|
525 |
+ |
|
526 |
+ /** |
|
527 |
+ * Get the IoC container instance. |
|
528 |
+ * |
|
529 |
+ * @return \Illuminate\Contracts\Container\Container |
|
530 |
+ */ |
|
531 |
+ public function getContainer() |
|
532 |
+ { |
|
533 |
+ return $this->container; |
|
534 |
+ } |
|
535 |
+ |
|
536 |
+ /** |
|
537 |
+ * Set the IoC container instance. |
|
538 |
+ * |
|
539 |
+ * @param \Illuminate\Contracts\Container\Container $container |
|
540 |
+ * @return void |
|
541 |
+ */ |
|
542 |
+ public function setContainer(Container $container) |
|
543 |
+ { |
|
544 |
+ $this->container = $container; |
|
545 |
+ } |
|
546 |
+ |
|
547 |
+ /** |
|
548 |
+ * Get an item from the shared data. |
|
549 |
+ * |
|
550 |
+ * @param string $key |
|
551 |
+ * @param mixed $default |
|
552 |
+ * @return mixed |
|
553 |
+ */ |
|
554 |
+ public function shared($key, $default = null) |
|
555 |
+ { |
|
556 |
+ return Arr::get($this->shared, $key, $default); |
|
557 |
+ } |
|
558 |
+ |
|
559 |
+ /** |
|
560 |
+ * Get all of the shared data for the environment. |
|
561 |
+ * |
|
562 |
+ * @return array |
|
563 |
+ */ |
|
564 |
+ public function getShared() |
|
565 |
+ { |
|
566 |
+ return $this->shared; |
|
567 |
+ } |
|
568 |
+} |