1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,191 +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 |
-use Windwalker\Structure\Structure; |
|
12 |
- |
|
13 |
-/** |
|
14 |
- * Class AbstractRenderer |
|
15 |
- * |
|
16 |
- * @property-read Structure $config Config data. |
|
17 |
- * |
|
18 |
- * @since 2.0 |
|
19 |
- */ |
|
20 |
-abstract class AbstractRenderer implements RendererInterface |
|
21 |
-{ |
|
22 |
- /** |
|
23 |
- * Property paths. |
|
24 |
- * |
|
25 |
- * @var \SplPriorityQueue |
|
26 |
- */ |
|
27 |
- protected $paths = null; |
|
28 |
- |
|
29 |
- /** |
|
30 |
- * Property config. |
|
31 |
- * |
|
32 |
- * @var Structure |
|
33 |
- */ |
|
34 |
- protected $config = []; |
|
35 |
- |
|
36 |
- /** |
|
37 |
- * Class init. |
|
38 |
- * |
|
39 |
- * @param \SplPriorityQueue $paths |
|
40 |
- * @param array $config |
|
41 |
- */ |
|
42 |
- public function __construct($paths = null, $config = []) |
|
43 |
- { |
|
44 |
- $this->setPaths($paths); |
|
45 |
- |
|
46 |
- $this->config = new Structure($this->config); |
|
47 |
- |
|
48 |
- $this->config->load($config); |
|
49 |
- } |
|
50 |
- |
|
51 |
- /** |
|
52 |
- * Method to escape output. |
|
53 |
- * |
|
54 |
- * @param string $output The output to escape. |
|
55 |
- * |
|
56 |
- * @return string The escaped output. |
|
57 |
- * |
|
58 |
- * @see ViewInterface::escape() |
|
59 |
- * @since 2.0 |
|
60 |
- */ |
|
61 |
- public function escape($output) |
|
62 |
- { |
|
63 |
- // Escape the output. |
|
64 |
- return htmlspecialchars((string) $output, ENT_COMPAT, 'UTF-8'); |
|
65 |
- } |
|
66 |
- |
|
67 |
- /** |
|
68 |
- * finFile |
|
69 |
- * |
|
70 |
- * @param string $file |
|
71 |
- * @param string $ext |
|
72 |
- * |
|
73 |
- * @return string |
|
74 |
- */ |
|
75 |
- public function findFile($file, $ext = '') |
|
76 |
- { |
|
77 |
- $paths = clone $this->getPaths(); |
|
78 |
- |
|
79 |
- $file = str_replace('.', '/', $file); |
|
80 |
- |
|
81 |
- $ext = $ext ? '.' . trim($ext, '.') : ''; |
|
82 |
- |
|
83 |
- foreach ($paths as $path) { |
|
84 |
- $filePath = $path . '/' . $file . $ext; |
|
85 |
- |
|
86 |
- if (is_file($filePath)) { |
|
87 |
- return realpath($filePath); |
|
88 |
- } |
|
89 |
- } |
|
90 |
- |
|
91 |
- return null; |
|
92 |
- } |
|
93 |
- |
|
94 |
- /** |
|
95 |
- * has |
|
96 |
- * |
|
97 |
- * @param string $file |
|
98 |
- * @param string $ext |
|
99 |
- * |
|
100 |
- * @return bool |
|
101 |
- * |
|
102 |
- * @since 3.5.2 |
|
103 |
- */ |
|
104 |
- public function has(string $file, string $ext = ''): bool |
|
105 |
- { |
|
106 |
- return $this->findFile($file, $ext) !== null; |
|
107 |
- } |
|
108 |
- |
|
109 |
- /** |
|
110 |
- * getPaths |
|
111 |
- * |
|
112 |
- * @return \SplPriorityQueue |
|
113 |
- */ |
|
114 |
- public function getPaths() |
|
115 |
- { |
|
116 |
- return $this->paths; |
|
117 |
- } |
|
118 |
- |
|
119 |
- /** |
|
120 |
- * setPaths |
|
121 |
- * |
|
122 |
- * @param \SplPriorityQueue $paths |
|
123 |
- * |
|
124 |
- * @return AbstractRenderer Return self to support chaining. |
|
125 |
- */ |
|
126 |
- public function setPaths($paths) |
|
127 |
- { |
|
128 |
- if (!($paths instanceof \SplPriorityQueue)) { |
|
129 |
- $priority = new \SplPriorityQueue(); |
|
130 |
- |
|
131 |
- foreach ((array) $paths as $i => $path) { |
|
132 |
- $priority->insert($path, 100 - ($i * 10)); |
|
133 |
- } |
|
134 |
- |
|
135 |
- $paths = $priority; |
|
136 |
- } |
|
137 |
- |
|
138 |
- $this->paths = $paths; |
|
139 |
- |
|
140 |
- return $this; |
|
141 |
- } |
|
142 |
- |
|
143 |
- /** |
|
144 |
- * addPath |
|
145 |
- * |
|
146 |
- * @param string $path |
|
147 |
- * @param integer $priority |
|
148 |
- * |
|
149 |
- * @return static |
|
150 |
- */ |
|
151 |
- public function addPath($path, $priority = 100) |
|
152 |
- { |
|
153 |
- $this->paths->insert($path, $priority); |
|
154 |
- |
|
155 |
- return $this; |
|
156 |
- } |
|
157 |
- |
|
158 |
- /** |
|
159 |
- * dumpPaths |
|
160 |
- * |
|
161 |
- * @return array |
|
162 |
- */ |
|
163 |
- public function dumpPaths() |
|
164 |
- { |
|
165 |
- $paths = clone $this->paths; |
|
166 |
- |
|
167 |
- $return = []; |
|
168 |
- |
|
169 |
- foreach ($paths as $path) { |
|
170 |
- $return[] = $path; |
|
171 |
- } |
|
172 |
- |
|
173 |
- return $return; |
|
174 |
- } |
|
175 |
- |
|
176 |
- /** |
|
177 |
- * __get |
|
178 |
- * |
|
179 |
- * @param string $name |
|
180 |
- * |
|
181 |
- * @return mixed |
|
182 |
- */ |
|
183 |
- public function __get($name) |
|
184 |
- { |
|
185 |
- if ($name === 'config') { |
|
186 |
- return $this->$name; |
|
187 |
- } |
|
188 |
- |
|
189 |
- throw new \UnexpectedValueException('Property ' . $name . ' not extists.'); |
|
190 |
- } |
|
191 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,191 @@ |
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 |
+use Windwalker\Structure\Structure; |
|
12 |
+ |
|
13 |
+/** |
|
14 |
+ * Class AbstractRenderer |
|
15 |
+ * |
|
16 |
+ * @property-read Structure $config Config data. |
|
17 |
+ * |
|
18 |
+ * @since 2.0 |
|
19 |
+ */ |
|
20 |
+abstract class AbstractRenderer implements RendererInterface |
|
21 |
+{ |
|
22 |
+ /** |
|
23 |
+ * Property paths. |
|
24 |
+ * |
|
25 |
+ * @var \SplPriorityQueue |
|
26 |
+ */ |
|
27 |
+ protected $paths = null; |
|
28 |
+ |
|
29 |
+ /** |
|
30 |
+ * Property config. |
|
31 |
+ * |
|
32 |
+ * @var Structure |
|
33 |
+ */ |
|
34 |
+ protected $config = []; |
|
35 |
+ |
|
36 |
+ /** |
|
37 |
+ * Class init. |
|
38 |
+ * |
|
39 |
+ * @param \SplPriorityQueue $paths |
|
40 |
+ * @param array $config |
|
41 |
+ */ |
|
42 |
+ public function __construct($paths = null, $config = []) |
|
43 |
+ { |
|
44 |
+ $this->setPaths($paths); |
|
45 |
+ |
|
46 |
+ $this->config = new Structure($this->config); |
|
47 |
+ |
|
48 |
+ $this->config->load($config); |
|
49 |
+ } |
|
50 |
+ |
|
51 |
+ /** |
|
52 |
+ * Method to escape output. |
|
53 |
+ * |
|
54 |
+ * @param string $output The output to escape. |
|
55 |
+ * |
|
56 |
+ * @return string The escaped output. |
|
57 |
+ * |
|
58 |
+ * @see ViewInterface::escape() |
|
59 |
+ * @since 2.0 |
|
60 |
+ */ |
|
61 |
+ public function escape($output) |
|
62 |
+ { |
|
63 |
+ // Escape the output. |
|
64 |
+ return htmlspecialchars((string) $output, ENT_COMPAT, 'UTF-8'); |
|
65 |
+ } |
|
66 |
+ |
|
67 |
+ /** |
|
68 |
+ * finFile |
|
69 |
+ * |
|
70 |
+ * @param string $file |
|
71 |
+ * @param string $ext |
|
72 |
+ * |
|
73 |
+ * @return string |
|
74 |
+ */ |
|
75 |
+ public function findFile($file, $ext = '') |
|
76 |
+ { |
|
77 |
+ $paths = clone $this->getPaths(); |
|
78 |
+ |
|
79 |
+ $file = str_replace('.', '/', $file); |
|
80 |
+ |
|
81 |
+ $ext = $ext ? '.' . trim($ext, '.') : ''; |
|
82 |
+ |
|
83 |
+ foreach ($paths as $path) { |
|
84 |
+ $filePath = $path . '/' . $file . $ext; |
|
85 |
+ |
|
86 |
+ if (is_file($filePath)) { |
|
87 |
+ return realpath($filePath); |
|
88 |
+ } |
|
89 |
+ } |
|
90 |
+ |
|
91 |
+ return null; |
|
92 |
+ } |
|
93 |
+ |
|
94 |
+ /** |
|
95 |
+ * has |
|
96 |
+ * |
|
97 |
+ * @param string $file |
|
98 |
+ * @param string $ext |
|
99 |
+ * |
|
100 |
+ * @return bool |
|
101 |
+ * |
|
102 |
+ * @since 3.5.2 |
|
103 |
+ */ |
|
104 |
+ public function has(string $file, string $ext = ''): bool |
|
105 |
+ { |
|
106 |
+ return $this->findFile($file, $ext) !== null; |
|
107 |
+ } |
|
108 |
+ |
|
109 |
+ /** |
|
110 |
+ * getPaths |
|
111 |
+ * |
|
112 |
+ * @return \SplPriorityQueue |
|
113 |
+ */ |
|
114 |
+ public function getPaths() |
|
115 |
+ { |
|
116 |
+ return $this->paths; |
|
117 |
+ } |
|
118 |
+ |
|
119 |
+ /** |
|
120 |
+ * setPaths |
|
121 |
+ * |
|
122 |
+ * @param \SplPriorityQueue $paths |
|
123 |
+ * |
|
124 |
+ * @return AbstractRenderer Return self to support chaining. |
|
125 |
+ */ |
|
126 |
+ public function setPaths($paths) |
|
127 |
+ { |
|
128 |
+ if (!($paths instanceof \SplPriorityQueue)) { |
|
129 |
+ $priority = new \SplPriorityQueue(); |
|
130 |
+ |
|
131 |
+ foreach ((array) $paths as $i => $path) { |
|
132 |
+ $priority->insert($path, 100 - ($i * 10)); |
|
133 |
+ } |
|
134 |
+ |
|
135 |
+ $paths = $priority; |
|
136 |
+ } |
|
137 |
+ |
|
138 |
+ $this->paths = $paths; |
|
139 |
+ |
|
140 |
+ return $this; |
|
141 |
+ } |
|
142 |
+ |
|
143 |
+ /** |
|
144 |
+ * addPath |
|
145 |
+ * |
|
146 |
+ * @param string $path |
|
147 |
+ * @param integer $priority |
|
148 |
+ * |
|
149 |
+ * @return static |
|
150 |
+ */ |
|
151 |
+ public function addPath($path, $priority = 100) |
|
152 |
+ { |
|
153 |
+ $this->paths->insert($path, $priority); |
|
154 |
+ |
|
155 |
+ return $this; |
|
156 |
+ } |
|
157 |
+ |
|
158 |
+ /** |
|
159 |
+ * dumpPaths |
|
160 |
+ * |
|
161 |
+ * @return array |
|
162 |
+ */ |
|
163 |
+ public function dumpPaths() |
|
164 |
+ { |
|
165 |
+ $paths = clone $this->paths; |
|
166 |
+ |
|
167 |
+ $return = []; |
|
168 |
+ |
|
169 |
+ foreach ($paths as $path) { |
|
170 |
+ $return[] = $path; |
|
171 |
+ } |
|
172 |
+ |
|
173 |
+ return $return; |
|
174 |
+ } |
|
175 |
+ |
|
176 |
+ /** |
|
177 |
+ * __get |
|
178 |
+ * |
|
179 |
+ * @param string $name |
|
180 |
+ * |
|
181 |
+ * @return mixed |
|
182 |
+ */ |
|
183 |
+ public function __get($name) |
|
184 |
+ { |
|
185 |
+ if ($name === 'config') { |
|
186 |
+ return $this->$name; |
|
187 |
+ } |
|
188 |
+ |
|
189 |
+ throw new \UnexpectedValueException('Property ' . $name . ' not extists.'); |
|
190 |
+ } |
|
191 |
+} |