1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,314 +0,0 @@ |
1 |
-<?php declare(strict_types=1); |
|
2 |
-/** |
|
3 |
- * Part of Windwalker project. |
|
4 |
- * |
|
5 |
- * @copyright Copyright (C) 2019 LYRASOFT. |
|
6 |
- * @license LGPL-2.0-or-later |
|
7 |
- */ |
|
8 |
- |
|
9 |
-namespace Windwalker\Renderer; |
|
10 |
- |
|
11 |
-/** |
|
12 |
- * Class PhpRenderer |
|
13 |
- * |
|
14 |
- * @since 2.0 |
|
15 |
- */ |
|
16 |
-class PhpRenderer extends AbstractRenderer |
|
17 |
-{ |
|
18 |
- /** |
|
19 |
- * Property block. |
|
20 |
- * |
|
21 |
- * @var array |
|
22 |
- */ |
|
23 |
- protected $block = []; |
|
24 |
- |
|
25 |
- /** |
|
26 |
- * Property blockQueue. |
|
27 |
- * |
|
28 |
- * @var \SplQueue |
|
29 |
- */ |
|
30 |
- protected $blockQueue = null; |
|
31 |
- |
|
32 |
- /** |
|
33 |
- * Property currentBlock. |
|
34 |
- * |
|
35 |
- * @var string |
|
36 |
- */ |
|
37 |
- protected $currentBlock = null; |
|
38 |
- |
|
39 |
- /** |
|
40 |
- * Property extends. |
|
41 |
- * |
|
42 |
- * @var string |
|
43 |
- */ |
|
44 |
- protected $extend = null; |
|
45 |
- |
|
46 |
- /** |
|
47 |
- * Property parent. |
|
48 |
- * |
|
49 |
- * @var PhpRenderer |
|
50 |
- */ |
|
51 |
- protected $parent = null; |
|
52 |
- |
|
53 |
- /** |
|
54 |
- * Property data. |
|
55 |
- * |
|
56 |
- * @var array |
|
57 |
- */ |
|
58 |
- protected $data = null; |
|
59 |
- |
|
60 |
- /** |
|
61 |
- * Property file. |
|
62 |
- * |
|
63 |
- * @var string |
|
64 |
- */ |
|
65 |
- protected $file; |
|
66 |
- |
|
67 |
- /** |
|
68 |
- * render |
|
69 |
- * |
|
70 |
- * @param string $file |
|
71 |
- * @param array $__data |
|
72 |
- * |
|
73 |
- * @throws \UnexpectedValueException |
|
74 |
- * @return string |
|
75 |
- */ |
|
76 |
- public function render($file, $__data = null) |
|
77 |
- { |
|
78 |
- $this->data = $__data = (array) $__data; |
|
79 |
- |
|
80 |
- $this->prepareData($__data); |
|
81 |
- |
|
82 |
- $__filePath = $this->findFile($file); |
|
83 |
- |
|
84 |
- if (!$__filePath) { |
|
85 |
- $__paths = $this->dumpPaths(); |
|
86 |
- |
|
87 |
- $__paths = "\n " . implode(" |\n ", $__paths); |
|
88 |
- |
|
89 |
- throw new \UnexpectedValueException(sprintf('File: %s not found. Paths in queue: %s', $file, $__paths)); |
|
90 |
- } |
|
91 |
- |
|
92 |
- foreach ($__data as $key => $value) { |
|
93 |
- if ($key === 'data') { |
|
94 |
- $key = '_data'; |
|
95 |
- } |
|
96 |
- |
|
97 |
- $$key = $value; |
|
98 |
- } |
|
99 |
- |
|
100 |
- unset($__data); |
|
101 |
- |
|
102 |
- // Start an output buffer. |
|
103 |
- ob_start(); |
|
104 |
- |
|
105 |
- // Load the layout. |
|
106 |
- include $__filePath; |
|
107 |
- |
|
108 |
- // Get the layout contents. |
|
109 |
- $output = ob_get_clean(); |
|
110 |
- |
|
111 |
- // Handler extend |
|
112 |
- if (!$this->extend) { |
|
113 |
- return $output; |
|
114 |
- } |
|
115 |
- |
|
116 |
- /** @var $parent phpRenderer */ |
|
117 |
- $parent = $this->createSelf(); |
|
118 |
- |
|
119 |
- foreach ($this->block as $name => $block) { |
|
120 |
- $parent->setBlock($name, $block); |
|
121 |
- } |
|
122 |
- |
|
123 |
- $output = $parent->render($this->extend, $this->data); |
|
124 |
- |
|
125 |
- return $output; |
|
126 |
- } |
|
127 |
- |
|
128 |
- /** |
|
129 |
- * finFile |
|
130 |
- * |
|
131 |
- * @param string $file |
|
132 |
- * @param string $ext |
|
133 |
- * |
|
134 |
- * @return string |
|
135 |
- */ |
|
136 |
- public function findFile($file, $ext = 'php') |
|
137 |
- { |
|
138 |
- return parent::findFile($file, $ext); |
|
139 |
- } |
|
140 |
- |
|
141 |
- /** |
|
142 |
- * load |
|
143 |
- * |
|
144 |
- * @param string $file |
|
145 |
- * @param array $data |
|
146 |
- * |
|
147 |
- * @return string |
|
148 |
- */ |
|
149 |
- public function load($file, $data = null) |
|
150 |
- { |
|
151 |
- $data = array_merge($this->data, (array) $data); |
|
152 |
- |
|
153 |
- $renderer = $this->createSelf(); |
|
154 |
- |
|
155 |
- return $renderer->render($file, $data); |
|
156 |
- } |
|
157 |
- |
|
158 |
- /** |
|
159 |
- * prepareData |
|
160 |
- * |
|
161 |
- * @param array &$data |
|
162 |
- * |
|
163 |
- * @return void |
|
164 |
- */ |
|
165 |
- protected function prepareData(&$data) |
|
166 |
- { |
|
167 |
- } |
|
168 |
- |
|
169 |
- /** |
|
170 |
- * getParent |
|
171 |
- * |
|
172 |
- * @return mixed|null |
|
173 |
- */ |
|
174 |
- public function parent() |
|
175 |
- { |
|
176 |
- if (!$this->extend) { |
|
177 |
- return null; |
|
178 |
- } |
|
179 |
- |
|
180 |
- if (!$this->parent) { |
|
181 |
- $this->parent = $this->createSelf(); |
|
182 |
- |
|
183 |
- $this->parent->render($this->extend, $this->data); |
|
184 |
- } |
|
185 |
- |
|
186 |
- return $this->parent->getBlock($this->currentBlock); |
|
187 |
- } |
|
188 |
- |
|
189 |
- /** |
|
190 |
- * createSelf |
|
191 |
- * |
|
192 |
- * @return static |
|
193 |
- */ |
|
194 |
- protected function createSelf() |
|
195 |
- { |
|
196 |
- return new static($this->paths, $this->config); |
|
197 |
- } |
|
198 |
- |
|
199 |
- /** |
|
200 |
- * extend |
|
201 |
- * |
|
202 |
- * @param string $name |
|
203 |
- * |
|
204 |
- * @return void |
|
205 |
- * |
|
206 |
- * @throws \LogicException |
|
207 |
- */ |
|
208 |
- public function extend($name) |
|
209 |
- { |
|
210 |
- if ($this->extend) { |
|
211 |
- throw new \LogicException('Please just extend one file.'); |
|
212 |
- } |
|
213 |
- |
|
214 |
- $this->extend = $name; |
|
215 |
- } |
|
216 |
- |
|
217 |
- /** |
|
218 |
- * getBlock |
|
219 |
- * |
|
220 |
- * @param string $name |
|
221 |
- * |
|
222 |
- * @return mixed |
|
223 |
- */ |
|
224 |
- public function getBlock($name) |
|
225 |
- { |
|
226 |
- return !empty($this->block[$name]) ? $this->block[$name] : null; |
|
227 |
- } |
|
228 |
- |
|
229 |
- /** |
|
230 |
- * setBlock |
|
231 |
- * |
|
232 |
- * @param string $name |
|
233 |
- * @param string $content |
|
234 |
- * |
|
235 |
- * @return PhpRenderer Return self to support chaining. |
|
236 |
- */ |
|
237 |
- public function setBlock($name, $content = '') |
|
238 |
- { |
|
239 |
- $this->block[$name] = $content; |
|
240 |
- |
|
241 |
- return $this; |
|
242 |
- } |
|
243 |
- |
|
244 |
- /** |
|
245 |
- * setBlock |
|
246 |
- * |
|
247 |
- * @param string $name |
|
248 |
- * |
|
249 |
- * @return void |
|
250 |
- */ |
|
251 |
- public function block($name) |
|
252 |
- { |
|
253 |
- $this->currentBlock = $name; |
|
254 |
- |
|
255 |
- $this->getBlockQueue()->push($name); |
|
256 |
- |
|
257 |
- // Start an output buffer. |
|
258 |
- ob_start(); |
|
259 |
- } |
|
260 |
- |
|
261 |
- /** |
|
262 |
- * endblock |
|
263 |
- * |
|
264 |
- * @return void |
|
265 |
- */ |
|
266 |
- public function endblock() |
|
267 |
- { |
|
268 |
- $name = $this->getBlockQueue()->pop(); |
|
269 |
- |
|
270 |
- // If this block name not exists on parent level, we just echo inner content. |
|
271 |
- if (!empty($this->block[$name])) { |
|
272 |
- ob_get_clean(); |
|
273 |
- |
|
274 |
- echo $this->block[$name]; |
|
275 |
- |
|
276 |
- return; |
|
277 |
- } |
|
278 |
- |
|
279 |
- // Get the layout contents. |
|
280 |
- echo $this->block[$name] = ob_get_clean(); |
|
281 |
- } |
|
282 |
- |
|
283 |
- /** |
|
284 |
- * getBlockQueue |
|
285 |
- * |
|
286 |
- * @return \SplQueue |
|
287 |
- */ |
|
288 |
- public function getBlockQueue() |
|
289 |
- { |
|
290 |
- if (!$this->blockQueue) { |
|
291 |
- $this->blockQueue = new \SplStack(); |
|
292 |
- } |
|
293 |
- |
|
294 |
- return $this->blockQueue; |
|
295 |
- } |
|
296 |
- |
|
297 |
- /** |
|
298 |
- * reset |
|
299 |
- * |
|
300 |
- * @return static |
|
301 |
- */ |
|
302 |
- public function reset() |
|
303 |
- { |
|
304 |
- $this->file = null; |
|
305 |
- $this->extend = null; |
|
306 |
- $this->parent = null; |
|
307 |
- $this->data = null; |
|
308 |
- $this->block = []; |
|
309 |
- $this->blockQueue = null; |
|
310 |
- $this->currentBlock = null; |
|
311 |
- |
|
312 |
- return $this; |
|
313 |
- } |
|
314 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,314 @@ |
1 |
+<?php declare(strict_types=1); |
|
2 |
+/** |
|
3 |
+ * Part of Windwalker project. |
|
4 |
+ * |
|
5 |
+ * @copyright Copyright (C) 2019 LYRASOFT. |
|
6 |
+ * @license LGPL-2.0-or-later |
|
7 |
+ */ |
|
8 |
+ |
|
9 |
+namespace Windwalker\Renderer; |
|
10 |
+ |
|
11 |
+/** |
|
12 |
+ * Class PhpRenderer |
|
13 |
+ * |
|
14 |
+ * @since 2.0 |
|
15 |
+ */ |
|
16 |
+class PhpRenderer extends AbstractRenderer |
|
17 |
+{ |
|
18 |
+ /** |
|
19 |
+ * Property block. |
|
20 |
+ * |
|
21 |
+ * @var array |
|
22 |
+ */ |
|
23 |
+ protected $block = []; |
|
24 |
+ |
|
25 |
+ /** |
|
26 |
+ * Property blockQueue. |
|
27 |
+ * |
|
28 |
+ * @var \SplQueue |
|
29 |
+ */ |
|
30 |
+ protected $blockQueue = null; |
|
31 |
+ |
|
32 |
+ /** |
|
33 |
+ * Property currentBlock. |
|
34 |
+ * |
|
35 |
+ * @var string |
|
36 |
+ */ |
|
37 |
+ protected $currentBlock = null; |
|
38 |
+ |
|
39 |
+ /** |
|
40 |
+ * Property extends. |
|
41 |
+ * |
|
42 |
+ * @var string |
|
43 |
+ */ |
|
44 |
+ protected $extend = null; |
|
45 |
+ |
|
46 |
+ /** |
|
47 |
+ * Property parent. |
|
48 |
+ * |
|
49 |
+ * @var PhpRenderer |
|
50 |
+ */ |
|
51 |
+ protected $parent = null; |
|
52 |
+ |
|
53 |
+ /** |
|
54 |
+ * Property data. |
|
55 |
+ * |
|
56 |
+ * @var array |
|
57 |
+ */ |
|
58 |
+ protected $data = null; |
|
59 |
+ |
|
60 |
+ /** |
|
61 |
+ * Property file. |
|
62 |
+ * |
|
63 |
+ * @var string |
|
64 |
+ */ |
|
65 |
+ protected $file; |
|
66 |
+ |
|
67 |
+ /** |
|
68 |
+ * render |
|
69 |
+ * |
|
70 |
+ * @param string $file |
|
71 |
+ * @param array $__data |
|
72 |
+ * |
|
73 |
+ * @throws \UnexpectedValueException |
|
74 |
+ * @return string |
|
75 |
+ */ |
|
76 |
+ public function render($file, $__data = null) |
|
77 |
+ { |
|
78 |
+ $this->data = $__data = (array) $__data; |
|
79 |
+ |
|
80 |
+ $this->prepareData($__data); |
|
81 |
+ |
|
82 |
+ $__filePath = $this->findFile($file); |
|
83 |
+ |
|
84 |
+ if (!$__filePath) { |
|
85 |
+ $__paths = $this->dumpPaths(); |
|
86 |
+ |
|
87 |
+ $__paths = "\n " . implode(" |\n ", $__paths); |
|
88 |
+ |
|
89 |
+ throw new \UnexpectedValueException(sprintf('File: %s not found. Paths in queue: %s', $file, $__paths)); |
|
90 |
+ } |
|
91 |
+ |
|
92 |
+ foreach ($__data as $key => $value) { |
|
93 |
+ if ($key === 'data') { |
|
94 |
+ $key = '_data'; |
|
95 |
+ } |
|
96 |
+ |
|
97 |
+ $$key = $value; |
|
98 |
+ } |
|
99 |
+ |
|
100 |
+ unset($__data); |
|
101 |
+ |
|
102 |
+ // Start an output buffer. |
|
103 |
+ ob_start(); |
|
104 |
+ |
|
105 |
+ // Load the layout. |
|
106 |
+ include $__filePath; |
|
107 |
+ |
|
108 |
+ // Get the layout contents. |
|
109 |
+ $output = ob_get_clean(); |
|
110 |
+ |
|
111 |
+ // Handler extend |
|
112 |
+ if (!$this->extend) { |
|
113 |
+ return $output; |
|
114 |
+ } |
|
115 |
+ |
|
116 |
+ /** @var $parent phpRenderer */ |
|
117 |
+ $parent = $this->createSelf(); |
|
118 |
+ |
|
119 |
+ foreach ($this->block as $name => $block) { |
|
120 |
+ $parent->setBlock($name, $block); |
|
121 |
+ } |
|
122 |
+ |
|
123 |
+ $output = $parent->render($this->extend, $this->data); |
|
124 |
+ |
|
125 |
+ return $output; |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ /** |
|
129 |
+ * finFile |
|
130 |
+ * |
|
131 |
+ * @param string $file |
|
132 |
+ * @param string $ext |
|
133 |
+ * |
|
134 |
+ * @return string |
|
135 |
+ */ |
|
136 |
+ public function findFile($file, $ext = 'php') |
|
137 |
+ { |
|
138 |
+ return parent::findFile($file, $ext); |
|
139 |
+ } |
|
140 |
+ |
|
141 |
+ /** |
|
142 |
+ * load |
|
143 |
+ * |
|
144 |
+ * @param string $file |
|
145 |
+ * @param array $data |
|
146 |
+ * |
|
147 |
+ * @return string |
|
148 |
+ */ |
|
149 |
+ public function load($file, $data = null) |
|
150 |
+ { |
|
151 |
+ $data = array_merge($this->data, (array) $data); |
|
152 |
+ |
|
153 |
+ $renderer = $this->createSelf(); |
|
154 |
+ |
|
155 |
+ return $renderer->render($file, $data); |
|
156 |
+ } |
|
157 |
+ |
|
158 |
+ /** |
|
159 |
+ * prepareData |
|
160 |
+ * |
|
161 |
+ * @param array &$data |
|
162 |
+ * |
|
163 |
+ * @return void |
|
164 |
+ */ |
|
165 |
+ protected function prepareData(&$data) |
|
166 |
+ { |
|
167 |
+ } |
|
168 |
+ |
|
169 |
+ /** |
|
170 |
+ * getParent |
|
171 |
+ * |
|
172 |
+ * @return mixed|null |
|
173 |
+ */ |
|
174 |
+ public function parent() |
|
175 |
+ { |
|
176 |
+ if (!$this->extend) { |
|
177 |
+ return null; |
|
178 |
+ } |
|
179 |
+ |
|
180 |
+ if (!$this->parent) { |
|
181 |
+ $this->parent = $this->createSelf(); |
|
182 |
+ |
|
183 |
+ $this->parent->render($this->extend, $this->data); |
|
184 |
+ } |
|
185 |
+ |
|
186 |
+ return $this->parent->getBlock($this->currentBlock); |
|
187 |
+ } |
|
188 |
+ |
|
189 |
+ /** |
|
190 |
+ * createSelf |
|
191 |
+ * |
|
192 |
+ * @return static |
|
193 |
+ */ |
|
194 |
+ protected function createSelf() |
|
195 |
+ { |
|
196 |
+ return new static($this->paths, $this->config); |
|
197 |
+ } |
|
198 |
+ |
|
199 |
+ /** |
|
200 |
+ * extend |
|
201 |
+ * |
|
202 |
+ * @param string $name |
|
203 |
+ * |
|
204 |
+ * @return void |
|
205 |
+ * |
|
206 |
+ * @throws \LogicException |
|
207 |
+ */ |
|
208 |
+ public function extend($name) |
|
209 |
+ { |
|
210 |
+ if ($this->extend) { |
|
211 |
+ throw new \LogicException('Please just extend one file.'); |
|
212 |
+ } |
|
213 |
+ |
|
214 |
+ $this->extend = $name; |
|
215 |
+ } |
|
216 |
+ |
|
217 |
+ /** |
|
218 |
+ * getBlock |
|
219 |
+ * |
|
220 |
+ * @param string $name |
|
221 |
+ * |
|
222 |
+ * @return mixed |
|
223 |
+ */ |
|
224 |
+ public function getBlock($name) |
|
225 |
+ { |
|
226 |
+ return !empty($this->block[$name]) ? $this->block[$name] : null; |
|
227 |
+ } |
|
228 |
+ |
|
229 |
+ /** |
|
230 |
+ * setBlock |
|
231 |
+ * |
|
232 |
+ * @param string $name |
|
233 |
+ * @param string $content |
|
234 |
+ * |
|
235 |
+ * @return PhpRenderer Return self to support chaining. |
|
236 |
+ */ |
|
237 |
+ public function setBlock($name, $content = '') |
|
238 |
+ { |
|
239 |
+ $this->block[$name] = $content; |
|
240 |
+ |
|
241 |
+ return $this; |
|
242 |
+ } |
|
243 |
+ |
|
244 |
+ /** |
|
245 |
+ * setBlock |
|
246 |
+ * |
|
247 |
+ * @param string $name |
|
248 |
+ * |
|
249 |
+ * @return void |
|
250 |
+ */ |
|
251 |
+ public function block($name) |
|
252 |
+ { |
|
253 |
+ $this->currentBlock = $name; |
|
254 |
+ |
|
255 |
+ $this->getBlockQueue()->push($name); |
|
256 |
+ |
|
257 |
+ // Start an output buffer. |
|
258 |
+ ob_start(); |
|
259 |
+ } |
|
260 |
+ |
|
261 |
+ /** |
|
262 |
+ * endblock |
|
263 |
+ * |
|
264 |
+ * @return void |
|
265 |
+ */ |
|
266 |
+ public function endblock() |
|
267 |
+ { |
|
268 |
+ $name = $this->getBlockQueue()->pop(); |
|
269 |
+ |
|
270 |
+ // If this block name not exists on parent level, we just echo inner content. |
|
271 |
+ if (!empty($this->block[$name])) { |
|
272 |
+ ob_get_clean(); |
|
273 |
+ |
|
274 |
+ echo $this->block[$name]; |
|
275 |
+ |
|
276 |
+ return; |
|
277 |
+ } |
|
278 |
+ |
|
279 |
+ // Get the layout contents. |
|
280 |
+ echo $this->block[$name] = ob_get_clean(); |
|
281 |
+ } |
|
282 |
+ |
|
283 |
+ /** |
|
284 |
+ * getBlockQueue |
|
285 |
+ * |
|
286 |
+ * @return \SplQueue |
|
287 |
+ */ |
|
288 |
+ public function getBlockQueue() |
|
289 |
+ { |
|
290 |
+ if (!$this->blockQueue) { |
|
291 |
+ $this->blockQueue = new \SplStack(); |
|
292 |
+ } |
|
293 |
+ |
|
294 |
+ return $this->blockQueue; |
|
295 |
+ } |
|
296 |
+ |
|
297 |
+ /** |
|
298 |
+ * reset |
|
299 |
+ * |
|
300 |
+ * @return static |
|
301 |
+ */ |
|
302 |
+ public function reset() |
|
303 |
+ { |
|
304 |
+ $this->file = null; |
|
305 |
+ $this->extend = null; |
|
306 |
+ $this->parent = null; |
|
307 |
+ $this->data = null; |
|
308 |
+ $this->block = []; |
|
309 |
+ $this->blockQueue = null; |
|
310 |
+ $this->currentBlock = null; |
|
311 |
+ |
|
312 |
+ return $this; |
|
313 |
+ } |
|
314 |
+} |