22
33namespace Pre \Plugin ;
44
5- use PhpCsFixer \Console \Application ;
6- use Symfony \Component \Console \Input \ArrayInput ;
7- use Symfony \Component \Console \Output \BufferedOutput ;
8- use Tuupola \Base62 ;
9- use Yay \Engine ;
10-
115require_once __DIR__ . "/environment.php " ;
126
137define ("COMMENT " , trim ("
@@ -163,21 +157,12 @@ function compile($from, $to, $format = true, $comment = true)
163157 */
164158function expand ($ code , $ includeStaticPaths = true , $ includeStaticCompilers = true )
165159{
166- static $ engine ;
167-
168- if (is_null ($ engine )) {
169- $ engine = new Engine ;
170- }
160+ $ base = getenv ("PRE_BASE_DIR " );
161+ $ vendor = realpath ("{$ base }/vendor/autoload.php " );
171162
172- static $ staticCompilers ;
163+ static $ staticCompilers = [] ;
173164
174165 if ($ includeStaticCompilers ) {
175- if (!is_array ($ staticCompilers )) {
176- $ staticCompilers = [];
177- }
178-
179- $ base = getenv ("PRE_BASE_DIR " );
180-
181166 if (file_exists ("{$ base }/pre.compilers " )) {
182167 $ staticCompilers = json_decode (file_get_contents ("{$ base }/pre.compilers " ), true );
183168 }
@@ -189,27 +174,40 @@ function expand($code, $includeStaticPaths = true, $includeStaticCompilers = tru
189174 $ code = $ compiler ($ code );
190175 }
191176
192- static $ staticPaths ;
177+ static $ staticPaths = [] ;
193178
194179 if ($ includeStaticPaths ) {
195- if (!is_array ($ staticPaths )) {
196- $ staticPaths = [];
197- }
198-
199- $ base = getenv ("PRE_BASE_DIR " );
200-
201180 if (file_exists ("{$ base }/pre.paths " )) {
202181 $ data = json_decode (file_get_contents ("{$ base }/pre.paths " ), true );
203-
204- static $ base62 ;
205-
206- if (!$ base62 ) {
207- $ base62 = new Base62 ();
182+ $ data = $ data ?? [];
183+
184+ if ((int ) getenv ("PRE_ISOLATE_DEPENDENCIES " ) === 1 ) {
185+ $ defer = '
186+ require " ' . $ vendor . '";
187+
188+ $data = unserialize(base64_decode(" ' . base64_encode (serialize ($ data )) . '"));
189+ $base62 = new \Tuupola\Base62();
190+
191+ print base64_encode(serialize(
192+ array_map(function($key) use ($base62) {
193+ return $base62->decode($key);
194+ }, array_keys($data))
195+ ));
196+ ' ;
197+
198+ $ result = exec ("php -r 'eval(base64_decode( \"" . base64_encode ($ defer ) . "\"));' " );
199+ $ staticPaths = unserialize (base64_decode ($ result ));
200+ } else {
201+ static $ base62 ;
202+
203+ if (!$ base62 ) {
204+ $ base62 = new \Tuupola \Base62 ();
205+ }
206+
207+ $ staticPaths = array_map (function ($ key ) use ($ base62 ) {
208+ return $ base62 ->decode ($ key );
209+ }, array_keys ($ data ));
208210 }
209-
210- $ staticPaths = array_map (function ($ key ) use ($ base62 ) {
211- return $ base62 ->decode ($ key );
212- }, array_keys ($ data ));
213211 }
214212 }
215213
@@ -225,9 +223,33 @@ function expand($code, $includeStaticPaths = true, $includeStaticCompilers = tru
225223 }
226224 }
227225
228- gc_disable ();
229- $ parsed = $ engine ->expand ($ code );
230- gc_enable ();
226+ if ((int ) getenv ("PRE_ISOLATE_DEPENDENCIES " ) === 1 ) {
227+ $ defer = '
228+ require " ' . $ vendor . '";
229+
230+ $code = base64_decode(" ' . base64_encode ($ code ) . '");
231+ $engine = new \Yay\Engine;
232+
233+ gc_disable();
234+ $parsed = $engine->expand($code);
235+ gc_enable();
236+
237+ print base64_encode(serialize($parsed));
238+ ' ;
239+
240+ $ result = exec ("php -r 'eval(base64_decode( \"" . base64_encode ($ defer ) . "\"));' " );
241+ $ parsed = unserialize (base64_decode ($ result ));
242+ } else {
243+ static $ engine ;
244+
245+ if (is_null ($ engine )) {
246+ $ engine = new \Yay \Engine ;
247+ }
248+
249+ gc_disable ();
250+ $ parsed = $ engine ->expand ($ code );
251+ gc_enable ();
252+ }
231253
232254 return preg_replace ('/\n\s+\n/ ' , "\n\n" , $ parsed );
233255}
@@ -256,23 +278,54 @@ function formatCode($code)
256278 */
257279function formatFile ($ path )
258280{
259- $ application = new Application ();
260- $ application ->setAutoExit (false );
261-
262- if (!is_array ($ path )) {
263- $ path = [$ path ];
264- }
281+ $ base = getenv ("PRE_BASE_DIR " );
282+ $ vendor = realpath ("{$ base }/vendor/autoload.php " );
265283
266- $ input = new ArrayInput ([
267- "command " => "fix " ,
268- "path " => $ path ,
269- "--using-cache " => "no " ,
270- "--quiet " ,
271- ]);
284+ if ((int ) getenv ("PRE_ISOLATE_DEPENDENCIES " ) === 1 ) {
285+ $ defer = '
286+ require " ' . $ vendor . '";
272287
273- $ output = new BufferedOutput ( );
288+ $path = base64_decode(" ' . base64_encode ( $ path ) . ' " );
274289
275- $ application ->run ($ input , $ output );
290+ $application = new PhpCsFixer\Console\Application();
291+ $application->setAutoExit(false);
292+
293+ if (!is_array($path)) {
294+ $path = [$path];
295+ }
296+
297+ $input = new Symfony\Component\Console\Input\ArrayInput([
298+ "command" => "fix",
299+ "path" => $path,
300+ "--using-cache" => "no",
301+ "--quiet",
302+ ]);
303+
304+ $output = new Symfony\Component\Console\Output\BufferedOutput();
305+
306+ $application->run($input, $output);
307+ ' ;
308+
309+ exec ("php -r 'eval(base64_decode( \"" . base64_encode ($ defer ) . "\"));' " );
310+ } else {
311+ $ application = new \PhpCsFixer \Console \Application ();
312+ $ application ->setAutoExit (false );
313+
314+ if (!is_array ($ path )) {
315+ $ path = [$ path ];
316+ }
317+
318+ $ input = new \Symfony \Component \Console \Input \ArrayInput ([
319+ "command " => "fix " ,
320+ "path " => $ path ,
321+ "--using-cache " => "no " ,
322+ "--quiet " ,
323+ ]);
324+
325+ $ output = new \Symfony \Component \Console \Output \BufferedOutput ();
326+
327+ $ application ->run ($ input , $ output );
328+ }
276329}
277330
278331/**
0 commit comments