CollectsViewExceptions.php
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Facade\Ignition\Views\Concerns;
use Illuminate\Foundation\Application;
use Illuminate\Support\Collection;
use Illuminate\View\Engines\CompilerEngine;
trait CollectsViewExceptions
{
protected $lastCompiledData = [];
public function collectViewData($path, array $data): void
{
$this->lastCompiledData[] = [
'path' => $path,
'compiledPath' => $this->getCompiledPath($path),
'data' => $this->filterViewData($data),
];
}
public function filterViewData(array $data): array
{
// By default, Laravel views get two shared data keys:
// __env and app. We try to filter them out.
return array_filter($data, function ($value, $key) {
if ($key === 'app') {
return ! $value instanceof Application;
}
return $key !== '__env';
}, ARRAY_FILTER_USE_BOTH);
}
public function getCompiledViewData($compiledPath): array
{
$compiledView = $this->findCompiledView($compiledPath);
return $compiledView['data'] ?? [];
}
public function getCompiledViewName($compiledPath): string
{
$compiledView = $this->findCompiledView($compiledPath);
return $compiledView['path'] ?? $compiledPath;
}
protected function findCompiledView($compiledPath): ?array
{
return Collection::make($this->lastCompiledData)
->first(function ($compiledData) use ($compiledPath) {
$comparePath = $compiledData['compiledPath'];
return realpath(dirname($comparePath)).DIRECTORY_SEPARATOR.basename($comparePath) === $compiledPath;
});
}
protected function getCompiledPath($path): string
{
if ($this instanceof CompilerEngine) {
return $this->getCompiler()->getCompiledPath($path);
}
return $path;
}
}