1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: georg |
||
5 | * Date: 5/29/2018 |
||
6 | * Time: 3:42 PM |
||
7 | */ |
||
8 | |||
9 | namespace Ghaskell\Scaffold; |
||
10 | |||
11 | use Exception; |
||
12 | use ErrorException; |
||
13 | use Illuminate\View\Compilers\CompilerInterface; |
||
14 | use Illuminate\View\Engines\PhpEngine; |
||
15 | use Symfony\Component\Debug\Exception\FatalThrowableError; |
||
16 | |||
17 | class CodeCompilerEngine extends PhpEngine |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Create a new Blade view engine instance. |
||
22 | * |
||
23 | * @param \Illuminate\View\Compilers\CompilerInterface $compiler |
||
24 | * @return void |
||
25 | */ |
||
26 | public function __construct(CompilerInterface $compiler) |
||
27 | { |
||
28 | $this->compiler = $compiler; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * The Code compiler instance. |
||
33 | * |
||
34 | * @var \Illuminate\View\Compilers\CompilerInterface |
||
35 | */ |
||
36 | protected $compiler; |
||
37 | |||
38 | /** |
||
39 | * A stack of the last compiled templates. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $lastCompiled = []; |
||
44 | |||
45 | /** |
||
46 | * Get the evaluated contents of the view. |
||
47 | * |
||
48 | * @param string $path |
||
49 | * @param array $data |
||
50 | * @return string |
||
51 | */ |
||
52 | public function get($path, array $data = []) |
||
53 | { |
||
54 | $this->lastCompiled[] = $path; |
||
55 | |||
56 | // If this given view has expired, which means it has simply been edited since |
||
57 | // it was last compiled, we will re-compile the views so we can evaluate a |
||
58 | // fresh copy of the view. We'll pass the compiler the path of the view. |
||
59 | if ($this->compiler->isExpired($path)) { |
||
60 | $this->compiler->compile($path); |
||
61 | } |
||
62 | |||
63 | $compiled = $this->compiler->getCompiledPath($path); |
||
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 | $results = $this->evaluatePath($compiled, $data); |
||
69 | |||
70 | array_pop($this->lastCompiled); |
||
71 | |||
72 | return $results; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Handle a view exception. |
||
77 | * |
||
78 | * @param \Exception $e |
||
79 | * @param int $obLevel |
||
80 | * @return void |
||
81 | * |
||
82 | * @throws \Exception |
||
83 | */ |
||
84 | protected function handleViewException(Exception $e, $obLevel) |
||
85 | { |
||
86 | $e = new ErrorException($this->getMessage($e), 0, 1, $e->getFile(), $e->getLine(), $e); |
||
87 | |||
88 | parent::handleViewException($e, $obLevel); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get the exception message for an exception. |
||
93 | * |
||
94 | * @param \Exception $e |
||
95 | * @return string |
||
96 | */ |
||
97 | protected function getMessage(Exception $e) |
||
98 | { |
||
99 | return $e->getMessage().' (View: '.realpath(last($this->lastCompiled)).')'; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Get the compiler implementation. |
||
104 | * |
||
105 | * @return \Illuminate\View\Compilers\CompilerInterface |
||
106 | */ |
||
107 | public function getCompiler() |
||
108 | { |
||
109 | return $this->compiler; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Get the evaluated contents of the view at the given path. |
||
114 | * |
||
115 | * @param string $__path |
||
116 | * @param array $__data |
||
117 | * @return string |
||
118 | */ |
||
119 | protected function evaluatePath($__path, $__data) |
||
120 | { |
||
121 | $obLevel = ob_get_level(); |
||
122 | |||
123 | ob_start(); |
||
124 | |||
125 | extract($__data, EXTR_SKIP); |
||
126 | |||
127 | // We'll evaluate the contents of the view inside a try/catch block so we can |
||
128 | // flush out any stray output that might get out before an error occurs or |
||
129 | // an exception is thrown. This prevents any partial views from leaking. |
||
130 | try { |
||
131 | include $__path; |
||
132 | } catch (Exception $e) { |
||
133 | $this->handleViewException($e, $obLevel); |
||
134 | } catch (Throwable $e) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
135 | $this->handleViewException(new FatalThrowableError($e), $obLevel); |
||
136 | } |
||
137 | |||
138 | return ltrim(ob_get_clean()); |
||
139 | } |
||
140 | |||
141 | } |
||
142 |