CollectsViewExceptions::getCompiledViewData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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