1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: georg |
||
5 | * Date: 5/26/2018 |
||
6 | * Time: 1:30 PM |
||
7 | */ |
||
8 | |||
9 | namespace Ghaskell\Scaffold; |
||
10 | |||
11 | use Illuminate\Support\Arr; |
||
12 | use Illuminate\Support\Str; |
||
13 | use Illuminate\View\Compilers\BladeCompiler; |
||
14 | use Ghaskell\Scaffold\Facades\Code; |
||
15 | |||
16 | |||
17 | class VibroCompiler extends BladeCompiler |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Array of opening and closing tags for raw echos. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $rawTags = ['\*\|', '\|\*']; //pipes are escaped for regex, actual tags are #|, |# |
||
26 | |||
27 | |||
28 | /** |
||
29 | * Array of opening and closing tags for regular echos. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $contentTags = ['<%', '%>']; |
||
34 | |||
35 | /** |
||
36 | * Array of opening and closing tags for escaped echos. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $escapedTags = ['<%%%', '%%%>']; |
||
41 | |||
42 | /** |
||
43 | * The "regular" / legacy echo string format. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $echoFormat = 'e(%s)'; |
||
48 | |||
49 | /** |
||
50 | * Array of footer lines to be added to template. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $footer = []; |
||
55 | |||
56 | /** |
||
57 | * Array to temporary store the raw blocks found in the template. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $rawBlocks = []; |
||
62 | |||
63 | |||
64 | |||
65 | public function compileFileName($fileName, $data) { |
||
66 | extract($data); |
||
67 | $parsed = $this->compileString($fileName); |
||
68 | ob_start(); |
||
69 | eval( '?>' . $parsed ); |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
70 | $output = ob_get_contents(); |
||
71 | ob_end_clean(); |
||
72 | return $output; |
||
73 | } |
||
74 | |||
75 | public function compileFile($filePath, array $data) |
||
76 | { |
||
77 | $result = Code::file($filePath, $data)->with($data)->render(); |
||
78 | return str_replace("<?stub", "<?php", $result); |
||
79 | |||
80 | } |
||
81 | |||
82 | public function compileString($value) |
||
83 | { |
||
84 | if (strpos($value, '@verbatim') !== false) { |
||
85 | $value = $this->storeVerbatimBlocks($value); |
||
86 | } |
||
87 | |||
88 | $this->footer = []; |
||
89 | |||
90 | if (strpos($value, '@php') !== false) { |
||
91 | $value = $this->storePhpBlocks($value); |
||
92 | } |
||
93 | |||
94 | $result = ''; |
||
95 | |||
96 | // Here we will loop through all of the tokens returned by the Zend lexer and |
||
97 | // parse each one into the corresponding valid PHP. We will then have this |
||
98 | // template as the correctly rendered PHP that can be rendered natively. |
||
99 | foreach (token_get_all($value) as $token) { |
||
100 | $result .= is_array($token) ? $this->parseToken($token) : $token; |
||
101 | } |
||
102 | |||
103 | if (! empty($this->rawBlocks)) { |
||
104 | $result = $this->restoreRawContent($result); |
||
105 | } |
||
106 | |||
107 | // If there are any footer lines that need to get added to a template we will |
||
108 | // add them here at the end of the template. This gets used mainly for the |
||
109 | // template inheritance via the extends keyword that should be appended. |
||
110 | if (count($this->footer) > 0) { |
||
111 | $result = $this->addFooters($result); |
||
112 | } |
||
113 | |||
114 | return $result; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Compile Blade statements that start with "@%". |
||
119 | * |
||
120 | * @param string $value |
||
121 | * @return string |
||
122 | */ |
||
123 | protected function compileStatements($value) |
||
124 | { |
||
125 | return preg_replace_callback( |
||
126 | '/\B@%(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', function ($match) { |
||
127 | return $this->compileStatement($match); |
||
128 | }, $value |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Compile a single Blade @% statement. |
||
134 | * |
||
135 | * @param array $match |
||
136 | * @return string |
||
137 | */ |
||
138 | protected function compileStatement($match) |
||
139 | { |
||
140 | if (Str::contains($match[1], '@%')) { |
||
141 | $match[0] = isset($match[3]) ? $match[1].$match[3] : $match[1]; |
||
142 | } elseif (method_exists($this, $method = 'compile'.ucfirst($match[1]))) { |
||
143 | $match[0] = $this->$method(Arr::get($match, 3)); |
||
144 | } |
||
145 | |||
146 | return isset($match[3]) ? $match[0] : $match[0].$match[2]; |
||
147 | } |
||
148 | |||
149 | } |