1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Parser\Template\Abstracts; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Spl\Traits\Collectors\ConfigCollectorTrait; |
||
19 | |||
20 | /** |
||
21 | * Class AbstractAdapter |
||
22 | * |
||
23 | * @package O2System\Parser\Template\Abstracts |
||
24 | */ |
||
25 | abstract class AbstractAdapter |
||
26 | { |
||
27 | use ConfigCollectorTrait; |
||
28 | |||
29 | /** |
||
30 | * AbstractAdapter::$engine |
||
31 | * |
||
32 | * Driver Engine |
||
33 | * |
||
34 | * @var object |
||
35 | */ |
||
36 | protected $engine; |
||
37 | |||
38 | /** |
||
39 | * AbstractAdapter::$string |
||
40 | * |
||
41 | * Driver Raw String |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $string; |
||
46 | |||
47 | // ------------------------------------------------------------------------ |
||
48 | |||
49 | /** |
||
50 | * AbstractAdapter::__construct |
||
51 | */ |
||
52 | public function __construct() |
||
53 | { |
||
54 | $this->setConfig([ |
||
55 | 'allowPhpScripts' => true, |
||
56 | 'allowPhpGlobals' => true, |
||
57 | 'allowPhpFunctions' => true, |
||
58 | 'allowPhpConstants' => true, |
||
59 | ]); |
||
60 | } |
||
61 | |||
62 | // ------------------------------------------------------------------------ |
||
63 | |||
64 | /** |
||
65 | * AbstractAdapter::initialize |
||
66 | * |
||
67 | * @param array $config |
||
68 | */ |
||
69 | abstract public function initialize(array $config = []); |
||
70 | |||
71 | // ------------------------------------------------------------------------ |
||
72 | |||
73 | /** |
||
74 | * AbstractAdapter::isInitialize |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function isInitialize() |
||
79 | { |
||
80 | return (bool)(empty($this->engine) ? false : true); |
||
81 | } |
||
82 | |||
83 | // -------------------------------------------------------------------------------------- |
||
84 | |||
85 | /** |
||
86 | * AbstractAdapter::loadFile |
||
87 | * |
||
88 | * @param string $filePath |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function loadFile($filePath) |
||
93 | { |
||
94 | if ($filePath instanceof \SplFileInfo) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
95 | $filePath = $filePath->getRealPath(); |
||
96 | } |
||
97 | |||
98 | if (is_file($filePath)) { |
||
99 | return $this->loadString(file_get_contents($filePath)); |
||
100 | } |
||
101 | |||
102 | return false; |
||
103 | } |
||
104 | |||
105 | // ------------------------------------------------------------------------ |
||
106 | |||
107 | /** |
||
108 | * AbstractAdapter::loadString |
||
109 | * |
||
110 | * @param string $string |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function loadString($string) |
||
115 | { |
||
116 | $this->string = htmlspecialchars_decode($string); |
||
117 | |||
118 | if ($this->config[ 'allowPhpScripts' ] === false) { |
||
119 | $this->string = preg_replace( |
||
120 | '/<\\?.*(\\?>|$)/Us', |
||
121 | '', |
||
122 | str_replace('<?=', '<?php echo ', $this->string) |
||
123 | ); |
||
124 | } |
||
125 | |||
126 | return (bool)empty($this->string); |
||
127 | } |
||
128 | |||
129 | // ------------------------------------------------------------------------ |
||
130 | |||
131 | /** |
||
132 | * AbstractAdapter::getEngine |
||
133 | * |
||
134 | * @return object |
||
135 | */ |
||
136 | public function &getEngine() |
||
137 | { |
||
138 | return $this->engine; |
||
139 | } |
||
140 | |||
141 | // ------------------------------------------------------------------------ |
||
142 | |||
143 | /** |
||
144 | * AbstractAdapter::setEngine |
||
145 | * |
||
146 | * @param object $engine |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function setEngine($engine) |
||
151 | { |
||
152 | if ($this->isValidEngine($engine)) { |
||
153 | $this->engine =& $engine; |
||
154 | |||
155 | return true; |
||
156 | } |
||
157 | |||
158 | return false; |
||
159 | } |
||
160 | |||
161 | // ------------------------------------------------------------------------ |
||
162 | |||
163 | /** |
||
164 | * AbstractAdapter::isValidEngine |
||
165 | * |
||
166 | * @param object $engine |
||
167 | * |
||
168 | * @return mixed |
||
169 | */ |
||
170 | abstract protected function isValidEngine($engine); |
||
171 | |||
172 | // ------------------------------------------------------------------------ |
||
173 | |||
174 | /** |
||
175 | * AbstractAdapter::__call |
||
176 | * |
||
177 | * @param string $method |
||
178 | * @param array $arguments |
||
179 | * |
||
180 | * @return mixed|null |
||
181 | */ |
||
182 | public function __call($method, array $arguments = []) |
||
183 | { |
||
184 | if (method_exists($this, $method)) { |
||
185 | return call_user_func_array([&$this, $method], $arguments); |
||
186 | } elseif (method_exists($this->engine, $method)) { |
||
187 | return call_user_func_array([&$this->engine, $method], $arguments); |
||
188 | } |
||
189 | |||
190 | return null; |
||
191 | } |
||
192 | |||
193 | // ------------------------------------------------------------------------ |
||
194 | |||
195 | /** |
||
196 | * AbstractAdapter::isSupported |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | abstract public function isSupported(); |
||
201 | |||
202 | // ------------------------------------------------------------------------ |
||
203 | |||
204 | /** |
||
205 | * AbstractAdapter::parse |
||
206 | * |
||
207 | * @param array $vars Variable to be parsed. |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | abstract public function parse(array $vars = []); |
||
212 | } |