1 | <?php |
||
16 | class Isolator |
||
17 | { |
||
18 | /** |
||
19 | * Found PHP blocks to be replaced. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $phpBlocks = []; |
||
24 | |||
25 | /** |
||
26 | * Isolation prefix. Use any values that will not corrupt HTML or |
||
27 | * other source. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | private $prefix = ''; |
||
32 | |||
33 | /** |
||
34 | * Isolation postfix. Use any values that will not corrupt HTML |
||
35 | * or other source. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $postfix = ''; |
||
40 | |||
41 | /** |
||
42 | * @param string $prefix Replaced block prefix, -php by default. |
||
43 | * @param string $postfix Replaced block postfix, block- by default. |
||
44 | */ |
||
45 | public function __construct($prefix = '-php-', $postfix = '-block-') |
||
50 | |||
51 | /** |
||
52 | * Isolates all returned PHP blocks with a defined pattern. Method uses |
||
53 | * token_get_all function. Resulted source have all php blocks replaced |
||
54 | * with non executable placeholder. |
||
55 | * |
||
56 | * @param string $source |
||
57 | * @return string |
||
58 | */ |
||
59 | public function isolatePHP($source) |
||
92 | |||
93 | /** |
||
94 | * Set block content by id. |
||
95 | * |
||
96 | * @param string $blockID |
||
97 | * @param string $source |
||
98 | * @return $this |
||
99 | * @throws IsolatorException |
||
100 | */ |
||
101 | public function setBlock($blockID, $source) |
||
111 | |||
112 | /** |
||
113 | * Replace every isolated block. |
||
114 | * |
||
115 | * @deprecated Use setBlock instead! |
||
116 | * @param array $blocks |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function setBlocks(array $blocks) |
||
125 | |||
126 | /** |
||
127 | * List of all found and replaced php blocks. |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | public function getBlocks() |
||
135 | |||
136 | /** |
||
137 | * Restore PHP blocks position in isolated source (isolatePHP() must |
||
138 | * be already called). |
||
139 | * |
||
140 | * @param string $source |
||
141 | * @return string |
||
142 | */ |
||
143 | public function repairPHP($source) |
||
157 | |||
158 | /** |
||
159 | * Remove PHP blocks from isolated source (isolatePHP() must be |
||
160 | * already called). |
||
161 | * |
||
162 | * @param string $isolatedSource |
||
163 | * @return string |
||
164 | */ |
||
165 | public function removePHP($isolatedSource) |
||
169 | |||
170 | /** |
||
171 | * Reset isolator state. |
||
172 | */ |
||
173 | public function reset() |
||
177 | |||
178 | /** |
||
179 | * @return string |
||
180 | */ |
||
181 | private function blockRegex() |
||
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | private function uniqueID() |
||
197 | |||
198 | /** |
||
199 | * @param int $blockID |
||
200 | * @return string |
||
201 | */ |
||
202 | private function placeholder($blockID) |
||
206 | |||
207 | /** |
||
208 | * @param mixed $token |
||
209 | * @return bool |
||
210 | */ |
||
211 | private function isOpenTag($token) |
||
224 | |||
225 | /** |
||
226 | * @param mixed $token |
||
227 | * @return bool |
||
228 | */ |
||
229 | public function isCloseTag($token) |
||
237 | } |