1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,275 +0,0 @@ |
1 |
-<?php declare(strict_types=1); |
|
2 |
-/** |
|
3 |
- * Part of Windwalker project. |
|
4 |
- * |
|
5 |
- * @copyright Copyright (C) 2019 LYRASOFT. |
|
6 |
- * @license GNU General Public License version 2 or later. |
|
7 |
- */ |
|
8 |
- |
|
9 |
-namespace Windwalker\Renderer; |
|
10 |
- |
|
11 |
-use Windwalker\Edge\Cache\EdgeArrayCache; |
|
12 |
-use Windwalker\Edge\Cache\EdgeCacheInterface; |
|
13 |
-use Windwalker\Edge\Cache\EdgeFileCache; |
|
14 |
-use Windwalker\Edge\Compiler\EdgeCompiler; |
|
15 |
-use Windwalker\Edge\Compiler\EdgeCompilerInterface; |
|
16 |
-use Windwalker\Edge\Edge; |
|
17 |
-use Windwalker\Edge\Exception\LayoutNotFoundException; |
|
18 |
-use Windwalker\Edge\Extension\EdgeExtensionInterface; |
|
19 |
-use Windwalker\Edge\Loader\EdgeFileLoader; |
|
20 |
-use Windwalker\Edge\Loader\EdgeLoaderInterface; |
|
21 |
-use Windwalker\Renderer\Edge\GlobalContainer; |
|
22 |
- |
|
23 |
-/** |
|
24 |
- * The EdgeRenderer class. |
|
25 |
- * |
|
26 |
- * @since 3.0 |
|
27 |
- */ |
|
28 |
-class EdgeRenderer extends AbstractEngineRenderer |
|
29 |
-{ |
|
30 |
- /** |
|
31 |
- * Property compiler. |
|
32 |
- * |
|
33 |
- * @var EdgeCompilerInterface |
|
34 |
- */ |
|
35 |
- protected $compiler; |
|
36 |
- |
|
37 |
- /** |
|
38 |
- * Property loader. |
|
39 |
- * |
|
40 |
- * @var EdgeLoaderInterface |
|
41 |
- */ |
|
42 |
- protected $loader; |
|
43 |
- |
|
44 |
- /** |
|
45 |
- * Property cache. |
|
46 |
- * |
|
47 |
- * @var EdgeCacheInterface |
|
48 |
- */ |
|
49 |
- protected $cache; |
|
50 |
- |
|
51 |
- /** |
|
52 |
- * Property extensions. |
|
53 |
- * |
|
54 |
- * @var callable[] |
|
55 |
- */ |
|
56 |
- protected $extensions = []; |
|
57 |
- |
|
58 |
- /** |
|
59 |
- * Method to get property Engine |
|
60 |
- * |
|
61 |
- * @param boolean $new |
|
62 |
- * |
|
63 |
- * @return Edge |
|
64 |
- */ |
|
65 |
- public function getEngine($new = false) |
|
66 |
- { |
|
67 |
- if (!$this->engine || $new) { |
|
68 |
- $this->loader = null; |
|
69 |
- $this->compiler = null; |
|
70 |
- $this->cache = null; |
|
71 |
- |
|
72 |
- $edge = new Edge($this->getLoader(), $this->getCompiler(), $this->getCache()); |
|
73 |
- |
|
74 |
- foreach (GlobalContainer::getExtensions() as $name => $extension) { |
|
75 |
- $edge->addExtension($extension, $name); |
|
76 |
- } |
|
77 |
- |
|
78 |
- foreach ($this->getExtensions() as $name => $extension) { |
|
79 |
- $edge->addExtension($extension, $name); |
|
80 |
- } |
|
81 |
- |
|
82 |
- foreach (GlobalContainer::getGlobals() as $key => $value) { |
|
83 |
- $edge->addGlobal($key, $value); |
|
84 |
- } |
|
85 |
- |
|
86 |
- $this->engine = $edge; |
|
87 |
- } |
|
88 |
- |
|
89 |
- return $this->engine; |
|
90 |
- } |
|
91 |
- |
|
92 |
- /** |
|
93 |
- * Method to set property engine |
|
94 |
- * |
|
95 |
- * @param Edge $engine |
|
96 |
- * |
|
97 |
- * @return static Return self to support chaining. |
|
98 |
- */ |
|
99 |
- public function setEngine($engine) |
|
100 |
- { |
|
101 |
- if (!$this->engine instanceof Edge) { |
|
102 |
- throw new \InvalidArgumentException('Engine should be instance of Edge'); |
|
103 |
- } |
|
104 |
- |
|
105 |
- $this->engine = $engine; |
|
106 |
- |
|
107 |
- return $this; |
|
108 |
- } |
|
109 |
- |
|
110 |
- /** |
|
111 |
- * render |
|
112 |
- * |
|
113 |
- * @param string $file |
|
114 |
- * @param array $data |
|
115 |
- * |
|
116 |
- * @return string |
|
117 |
- * @throws \Windwalker\Edge\Exception\EdgeException |
|
118 |
- */ |
|
119 |
- public function render($file, $data = []) |
|
120 |
- { |
|
121 |
- if ($data instanceof \Traversable) { |
|
122 |
- $data = iterator_to_array($data); |
|
123 |
- } |
|
124 |
- |
|
125 |
- if (is_object($data)) { |
|
126 |
- $data = get_object_vars($data); |
|
127 |
- } |
|
128 |
- |
|
129 |
- return $this->getEngine(true)->render($file, (array) $data); |
|
130 |
- } |
|
131 |
- |
|
132 |
- /** |
|
133 |
- * finFile |
|
134 |
- * |
|
135 |
- * @param string $file |
|
136 |
- * @param string $ext |
|
137 |
- * |
|
138 |
- * @return string |
|
139 |
- */ |
|
140 |
- public function findFile($file, $ext = '') |
|
141 |
- { |
|
142 |
- try { |
|
143 |
- return $this->getEngine()->getLoader()->find($file); |
|
144 |
- } catch (LayoutNotFoundException $e) { |
|
145 |
- return null; |
|
146 |
- } |
|
147 |
- } |
|
148 |
- |
|
149 |
- /** |
|
150 |
- * Method to get property Compiler |
|
151 |
- * |
|
152 |
- * @return EdgeCompilerInterface |
|
153 |
- */ |
|
154 |
- public function getCompiler() |
|
155 |
- { |
|
156 |
- if (!$this->compiler) { |
|
157 |
- $this->compiler = new EdgeCompiler(); |
|
158 |
- } |
|
159 |
- |
|
160 |
- return $this->compiler; |
|
161 |
- } |
|
162 |
- |
|
163 |
- /** |
|
164 |
- * Method to set property compiler |
|
165 |
- * |
|
166 |
- * @param EdgeCompilerInterface $compiler |
|
167 |
- * |
|
168 |
- * @return static Return self to support chaining. |
|
169 |
- */ |
|
170 |
- public function setCompiler($compiler) |
|
171 |
- { |
|
172 |
- $this->compiler = $compiler; |
|
173 |
- |
|
174 |
- return $this; |
|
175 |
- } |
|
176 |
- |
|
177 |
- /** |
|
178 |
- * Method to get property Loader |
|
179 |
- * |
|
180 |
- * @return EdgeLoaderInterface |
|
181 |
- */ |
|
182 |
- public function getLoader() |
|
183 |
- { |
|
184 |
- if (!$this->loader) { |
|
185 |
- $this->loader = new EdgeFileLoader($this->dumpPaths()); |
|
186 |
- } |
|
187 |
- |
|
188 |
- return $this->loader; |
|
189 |
- } |
|
190 |
- |
|
191 |
- /** |
|
192 |
- * Method to set property loader |
|
193 |
- * |
|
194 |
- * @param EdgeLoaderInterface $loader |
|
195 |
- * |
|
196 |
- * @return static Return self to support chaining. |
|
197 |
- */ |
|
198 |
- public function setLoader($loader) |
|
199 |
- { |
|
200 |
- $this->loader = $loader; |
|
201 |
- |
|
202 |
- return $this; |
|
203 |
- } |
|
204 |
- |
|
205 |
- /** |
|
206 |
- * Method to get property Cache |
|
207 |
- * |
|
208 |
- * @return EdgeCacheInterface |
|
209 |
- */ |
|
210 |
- public function getCache() |
|
211 |
- { |
|
212 |
- if (!$this->cache) { |
|
213 |
- if ($this->config->exists('cache_path')) { |
|
214 |
- $this->cache = new EdgeFileCache($this->config->get('cache_path')); |
|
215 |
- } else { |
|
216 |
- $this->cache = new EdgeArrayCache(); |
|
217 |
- } |
|
218 |
- } |
|
219 |
- |
|
220 |
- return $this->cache; |
|
221 |
- } |
|
222 |
- |
|
223 |
- /** |
|
224 |
- * Method to set property cache |
|
225 |
- * |
|
226 |
- * @param EdgeCacheInterface $cache |
|
227 |
- * |
|
228 |
- * @return static Return self to support chaining. |
|
229 |
- */ |
|
230 |
- public function setCache($cache) |
|
231 |
- { |
|
232 |
- $this->cache = $cache; |
|
233 |
- |
|
234 |
- return $this; |
|
235 |
- } |
|
236 |
- |
|
237 |
- /** |
|
238 |
- * Method to get property Extensions |
|
239 |
- * |
|
240 |
- * @return EdgeExtensionInterface[] |
|
241 |
- */ |
|
242 |
- public function getExtensions() |
|
243 |
- { |
|
244 |
- return $this->extensions; |
|
245 |
- } |
|
246 |
- |
|
247 |
- /** |
|
248 |
- * Method to set property extensions |
|
249 |
- * |
|
250 |
- * @param EdgeExtensionInterface[] $extensions |
|
251 |
- * |
|
252 |
- * @return static Return self to support chaining. |
|
253 |
- */ |
|
254 |
- public function setExtensions($extensions) |
|
255 |
- { |
|
256 |
- $this->extensions = $extensions; |
|
257 |
- |
|
258 |
- return $this; |
|
259 |
- } |
|
260 |
- |
|
261 |
- /** |
|
262 |
- * addExtension |
|
263 |
- * |
|
264 |
- * @param EdgeExtensionInterface $extension |
|
265 |
- * @param string $name |
|
266 |
- * |
|
267 |
- * @return static |
|
268 |
- */ |
|
269 |
- public function addExtension(EdgeExtensionInterface $extension, $name = null) |
|
270 |
- { |
|
271 |
- $this->extensions[$name ?: $extension->getName()] = $extension; |
|
272 |
- |
|
273 |
- return $this; |
|
274 |
- } |
|
275 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,275 @@ |
1 |
+<?php declare(strict_types=1); |
|
2 |
+/** |
|
3 |
+ * Part of Windwalker project. |
|
4 |
+ * |
|
5 |
+ * @copyright Copyright (C) 2019 LYRASOFT. |
|
6 |
+ * @license GNU General Public License version 2 or later. |
|
7 |
+ */ |
|
8 |
+ |
|
9 |
+namespace Windwalker\Renderer; |
|
10 |
+ |
|
11 |
+use Windwalker\Edge\Cache\EdgeArrayCache; |
|
12 |
+use Windwalker\Edge\Cache\EdgeCacheInterface; |
|
13 |
+use Windwalker\Edge\Cache\EdgeFileCache; |
|
14 |
+use Windwalker\Edge\Compiler\EdgeCompiler; |
|
15 |
+use Windwalker\Edge\Compiler\EdgeCompilerInterface; |
|
16 |
+use Windwalker\Edge\Edge; |
|
17 |
+use Windwalker\Edge\Exception\LayoutNotFoundException; |
|
18 |
+use Windwalker\Edge\Extension\EdgeExtensionInterface; |
|
19 |
+use Windwalker\Edge\Loader\EdgeFileLoader; |
|
20 |
+use Windwalker\Edge\Loader\EdgeLoaderInterface; |
|
21 |
+use Windwalker\Renderer\Edge\GlobalContainer; |
|
22 |
+ |
|
23 |
+/** |
|
24 |
+ * The EdgeRenderer class. |
|
25 |
+ * |
|
26 |
+ * @since 3.0 |
|
27 |
+ */ |
|
28 |
+class EdgeRenderer extends AbstractEngineRenderer |
|
29 |
+{ |
|
30 |
+ /** |
|
31 |
+ * Property compiler. |
|
32 |
+ * |
|
33 |
+ * @var EdgeCompilerInterface |
|
34 |
+ */ |
|
35 |
+ protected $compiler; |
|
36 |
+ |
|
37 |
+ /** |
|
38 |
+ * Property loader. |
|
39 |
+ * |
|
40 |
+ * @var EdgeLoaderInterface |
|
41 |
+ */ |
|
42 |
+ protected $loader; |
|
43 |
+ |
|
44 |
+ /** |
|
45 |
+ * Property cache. |
|
46 |
+ * |
|
47 |
+ * @var EdgeCacheInterface |
|
48 |
+ */ |
|
49 |
+ protected $cache; |
|
50 |
+ |
|
51 |
+ /** |
|
52 |
+ * Property extensions. |
|
53 |
+ * |
|
54 |
+ * @var callable[] |
|
55 |
+ */ |
|
56 |
+ protected $extensions = []; |
|
57 |
+ |
|
58 |
+ /** |
|
59 |
+ * Method to get property Engine |
|
60 |
+ * |
|
61 |
+ * @param boolean $new |
|
62 |
+ * |
|
63 |
+ * @return Edge |
|
64 |
+ */ |
|
65 |
+ public function getEngine($new = false) |
|
66 |
+ { |
|
67 |
+ if (!$this->engine || $new) { |
|
68 |
+ $this->loader = null; |
|
69 |
+ $this->compiler = null; |
|
70 |
+ $this->cache = null; |
|
71 |
+ |
|
72 |
+ $edge = new Edge($this->getLoader(), $this->getCompiler(), $this->getCache()); |
|
73 |
+ |
|
74 |
+ foreach (GlobalContainer::getExtensions() as $name => $extension) { |
|
75 |
+ $edge->addExtension($extension, $name); |
|
76 |
+ } |
|
77 |
+ |
|
78 |
+ foreach ($this->getExtensions() as $name => $extension) { |
|
79 |
+ $edge->addExtension($extension, $name); |
|
80 |
+ } |
|
81 |
+ |
|
82 |
+ foreach (GlobalContainer::getGlobals() as $key => $value) { |
|
83 |
+ $edge->addGlobal($key, $value); |
|
84 |
+ } |
|
85 |
+ |
|
86 |
+ $this->engine = $edge; |
|
87 |
+ } |
|
88 |
+ |
|
89 |
+ return $this->engine; |
|
90 |
+ } |
|
91 |
+ |
|
92 |
+ /** |
|
93 |
+ * Method to set property engine |
|
94 |
+ * |
|
95 |
+ * @param Edge $engine |
|
96 |
+ * |
|
97 |
+ * @return static Return self to support chaining. |
|
98 |
+ */ |
|
99 |
+ public function setEngine($engine) |
|
100 |
+ { |
|
101 |
+ if (!$this->engine instanceof Edge) { |
|
102 |
+ throw new \InvalidArgumentException('Engine should be instance of Edge'); |
|
103 |
+ } |
|
104 |
+ |
|
105 |
+ $this->engine = $engine; |
|
106 |
+ |
|
107 |
+ return $this; |
|
108 |
+ } |
|
109 |
+ |
|
110 |
+ /** |
|
111 |
+ * render |
|
112 |
+ * |
|
113 |
+ * @param string $file |
|
114 |
+ * @param array $data |
|
115 |
+ * |
|
116 |
+ * @return string |
|
117 |
+ * @throws \Windwalker\Edge\Exception\EdgeException |
|
118 |
+ */ |
|
119 |
+ public function render($file, $data = []) |
|
120 |
+ { |
|
121 |
+ if ($data instanceof \Traversable) { |
|
122 |
+ $data = iterator_to_array($data); |
|
123 |
+ } |
|
124 |
+ |
|
125 |
+ if (is_object($data)) { |
|
126 |
+ $data = get_object_vars($data); |
|
127 |
+ } |
|
128 |
+ |
|
129 |
+ return $this->getEngine(true)->render($file, (array) $data); |
|
130 |
+ } |
|
131 |
+ |
|
132 |
+ /** |
|
133 |
+ * finFile |
|
134 |
+ * |
|
135 |
+ * @param string $file |
|
136 |
+ * @param string $ext |
|
137 |
+ * |
|
138 |
+ * @return string |
|
139 |
+ */ |
|
140 |
+ public function findFile($file, $ext = '') |
|
141 |
+ { |
|
142 |
+ try { |
|
143 |
+ return $this->getEngine()->getLoader()->find($file); |
|
144 |
+ } catch (LayoutNotFoundException $e) { |
|
145 |
+ return null; |
|
146 |
+ } |
|
147 |
+ } |
|
148 |
+ |
|
149 |
+ /** |
|
150 |
+ * Method to get property Compiler |
|
151 |
+ * |
|
152 |
+ * @return EdgeCompilerInterface |
|
153 |
+ */ |
|
154 |
+ public function getCompiler() |
|
155 |
+ { |
|
156 |
+ if (!$this->compiler) { |
|
157 |
+ $this->compiler = new EdgeCompiler(); |
|
158 |
+ } |
|
159 |
+ |
|
160 |
+ return $this->compiler; |
|
161 |
+ } |
|
162 |
+ |
|
163 |
+ /** |
|
164 |
+ * Method to set property compiler |
|
165 |
+ * |
|
166 |
+ * @param EdgeCompilerInterface $compiler |
|
167 |
+ * |
|
168 |
+ * @return static Return self to support chaining. |
|
169 |
+ */ |
|
170 |
+ public function setCompiler($compiler) |
|
171 |
+ { |
|
172 |
+ $this->compiler = $compiler; |
|
173 |
+ |
|
174 |
+ return $this; |
|
175 |
+ } |
|
176 |
+ |
|
177 |
+ /** |
|
178 |
+ * Method to get property Loader |
|
179 |
+ * |
|
180 |
+ * @return EdgeLoaderInterface |
|
181 |
+ */ |
|
182 |
+ public function getLoader() |
|
183 |
+ { |
|
184 |
+ if (!$this->loader) { |
|
185 |
+ $this->loader = new EdgeFileLoader($this->dumpPaths()); |
|
186 |
+ } |
|
187 |
+ |
|
188 |
+ return $this->loader; |
|
189 |
+ } |
|
190 |
+ |
|
191 |
+ /** |
|
192 |
+ * Method to set property loader |
|
193 |
+ * |
|
194 |
+ * @param EdgeLoaderInterface $loader |
|
195 |
+ * |
|
196 |
+ * @return static Return self to support chaining. |
|
197 |
+ */ |
|
198 |
+ public function setLoader($loader) |
|
199 |
+ { |
|
200 |
+ $this->loader = $loader; |
|
201 |
+ |
|
202 |
+ return $this; |
|
203 |
+ } |
|
204 |
+ |
|
205 |
+ /** |
|
206 |
+ * Method to get property Cache |
|
207 |
+ * |
|
208 |
+ * @return EdgeCacheInterface |
|
209 |
+ */ |
|
210 |
+ public function getCache() |
|
211 |
+ { |
|
212 |
+ if (!$this->cache) { |
|
213 |
+ if ($this->config->exists('cache_path')) { |
|
214 |
+ $this->cache = new EdgeFileCache($this->config->get('cache_path')); |
|
215 |
+ } else { |
|
216 |
+ $this->cache = new EdgeArrayCache(); |
|
217 |
+ } |
|
218 |
+ } |
|
219 |
+ |
|
220 |
+ return $this->cache; |
|
221 |
+ } |
|
222 |
+ |
|
223 |
+ /** |
|
224 |
+ * Method to set property cache |
|
225 |
+ * |
|
226 |
+ * @param EdgeCacheInterface $cache |
|
227 |
+ * |
|
228 |
+ * @return static Return self to support chaining. |
|
229 |
+ */ |
|
230 |
+ public function setCache($cache) |
|
231 |
+ { |
|
232 |
+ $this->cache = $cache; |
|
233 |
+ |
|
234 |
+ return $this; |
|
235 |
+ } |
|
236 |
+ |
|
237 |
+ /** |
|
238 |
+ * Method to get property Extensions |
|
239 |
+ * |
|
240 |
+ * @return EdgeExtensionInterface[] |
|
241 |
+ */ |
|
242 |
+ public function getExtensions() |
|
243 |
+ { |
|
244 |
+ return $this->extensions; |
|
245 |
+ } |
|
246 |
+ |
|
247 |
+ /** |
|
248 |
+ * Method to set property extensions |
|
249 |
+ * |
|
250 |
+ * @param EdgeExtensionInterface[] $extensions |
|
251 |
+ * |
|
252 |
+ * @return static Return self to support chaining. |
|
253 |
+ */ |
|
254 |
+ public function setExtensions($extensions) |
|
255 |
+ { |
|
256 |
+ $this->extensions = $extensions; |
|
257 |
+ |
|
258 |
+ return $this; |
|
259 |
+ } |
|
260 |
+ |
|
261 |
+ /** |
|
262 |
+ * addExtension |
|
263 |
+ * |
|
264 |
+ * @param EdgeExtensionInterface $extension |
|
265 |
+ * @param string $name |
|
266 |
+ * |
|
267 |
+ * @return static |
|
268 |
+ */ |
|
269 |
+ public function addExtension(EdgeExtensionInterface $extension, $name = null) |
|
270 |
+ { |
|
271 |
+ $this->extensions[$name ?: $extension->getName()] = $extension; |
|
272 |
+ |
|
273 |
+ return $this; |
|
274 |
+ } |
|
275 |
+} |