| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\View\Engines;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Filesystem\Filesystem;
|
|
|
6 |
use Illuminate\View\Compilers\CompilerInterface;
|
|
|
7 |
use Illuminate\View\ViewException;
|
|
|
8 |
use Throwable;
|
|
|
9 |
|
|
|
10 |
class CompilerEngine extends PhpEngine
|
|
|
11 |
{
|
|
|
12 |
/**
|
|
|
13 |
* The Blade compiler instance.
|
|
|
14 |
*
|
|
|
15 |
* @var \Illuminate\View\Compilers\CompilerInterface
|
|
|
16 |
*/
|
|
|
17 |
protected $compiler;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* A stack of the last compiled templates.
|
|
|
21 |
*
|
|
|
22 |
* @var array
|
|
|
23 |
*/
|
|
|
24 |
protected $lastCompiled = [];
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* The view paths that were compiled or are not expired, keyed by the path.
|
|
|
28 |
*
|
|
|
29 |
* @var array<string, true>
|
|
|
30 |
*/
|
|
|
31 |
protected $compiledOrNotExpired = [];
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Create a new compiler engine instance.
|
|
|
35 |
*
|
|
|
36 |
* @param \Illuminate\View\Compilers\CompilerInterface $compiler
|
|
|
37 |
* @param \Illuminate\Filesystem\Filesystem|null $files
|
|
|
38 |
* @return void
|
|
|
39 |
*/
|
|
|
40 |
public function __construct(CompilerInterface $compiler, Filesystem $files = null)
|
|
|
41 |
{
|
|
|
42 |
parent::__construct($files ?: new Filesystem);
|
|
|
43 |
|
|
|
44 |
$this->compiler = $compiler;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Get the evaluated contents of the view.
|
|
|
49 |
*
|
|
|
50 |
* @param string $path
|
|
|
51 |
* @param array $data
|
|
|
52 |
* @return string
|
|
|
53 |
*/
|
|
|
54 |
public function get($path, array $data = [])
|
|
|
55 |
{
|
|
|
56 |
$this->lastCompiled[] = $path;
|
|
|
57 |
|
|
|
58 |
// If this given view has expired, which means it has simply been edited since
|
|
|
59 |
// it was last compiled, we will re-compile the views so we can evaluate a
|
|
|
60 |
// fresh copy of the view. We'll pass the compiler the path of the view.
|
|
|
61 |
if (! isset($this->compiledOrNotExpired[$path]) && $this->compiler->isExpired($path)) {
|
|
|
62 |
$this->compiler->compile($path);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// Once we have the path to the compiled file, we will evaluate the paths with
|
|
|
66 |
// typical PHP just like any other templates. We also keep a stack of views
|
|
|
67 |
// which have been rendered for right exception messages to be generated.
|
|
|
68 |
|
|
|
69 |
try {
|
|
|
70 |
$results = $this->evaluatePath($this->compiler->getCompiledPath($path), $data);
|
|
|
71 |
} catch (ViewException $e) {
|
|
|
72 |
if (! str($e->getMessage())->contains(['No such file or directory', 'File does not exist at path'])) {
|
|
|
73 |
throw $e;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
if (! isset($this->compiledOrNotExpired[$path])) {
|
|
|
77 |
throw $e;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$this->compiler->compile($path);
|
|
|
81 |
|
|
|
82 |
$results = $this->evaluatePath($this->compiler->getCompiledPath($path), $data);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
$this->compiledOrNotExpired[$path] = true;
|
|
|
86 |
|
|
|
87 |
array_pop($this->lastCompiled);
|
|
|
88 |
|
|
|
89 |
return $results;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Handle a view exception.
|
|
|
94 |
*
|
|
|
95 |
* @param \Throwable $e
|
|
|
96 |
* @param int $obLevel
|
|
|
97 |
* @return void
|
|
|
98 |
*
|
|
|
99 |
* @throws \Throwable
|
|
|
100 |
*/
|
|
|
101 |
protected function handleViewException(Throwable $e, $obLevel)
|
|
|
102 |
{
|
|
|
103 |
$e = new ViewException($this->getMessage($e), 0, 1, $e->getFile(), $e->getLine(), $e);
|
|
|
104 |
|
|
|
105 |
parent::handleViewException($e, $obLevel);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Get the exception message for an exception.
|
|
|
110 |
*
|
|
|
111 |
* @param \Throwable $e
|
|
|
112 |
* @return string
|
|
|
113 |
*/
|
|
|
114 |
protected function getMessage(Throwable $e)
|
|
|
115 |
{
|
|
|
116 |
return $e->getMessage().' (View: '.realpath(last($this->lastCompiled)).')';
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Get the compiler implementation.
|
|
|
121 |
*
|
|
|
122 |
* @return \Illuminate\View\Compilers\CompilerInterface
|
|
|
123 |
*/
|
|
|
124 |
public function getCompiler()
|
|
|
125 |
{
|
|
|
126 |
return $this->compiler;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Clear the cache of views that were compiled or not expired.
|
|
|
131 |
*
|
|
|
132 |
* @return void
|
|
|
133 |
*/
|
|
|
134 |
public function forgetCompiledOrNotExpired()
|
|
|
135 |
{
|
|
|
136 |
$this->compiledOrNotExpired = [];
|
|
|
137 |
}
|
|
|
138 |
}
|