Skip to content

Commit 6b4163d

Browse files
committed
Refactor to isolate dependencies
1 parent 7587dc7 commit 6b4163d

File tree

6 files changed

+127
-63
lines changed

6 files changed

+127
-63
lines changed

composer.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
"type": "composer-plugin",
44
"license": "MIT",
55
"require": {
6-
"composer-plugin-api": "^1.1",
7-
"friendsofphp/php-cs-fixer": "^2.0",
8-
"yay/yay": "^0.2",
9-
"tuupola/base62": "^0.8"
6+
"composer-plugin-api": "^1.1"
107
},
118
"autoload": {
129
"files": [
@@ -21,16 +18,17 @@
2118
},
2219
"require-dev": {
2320
"composer/composer": "^1.3",
24-
"phpunit/phpunit": "^5.0|^6.0"
21+
"phpunit/phpunit": "^5.0|^6.0",
22+
"friendsofphp/php-cs-fixer": "^2.0",
23+
"yay/yay": "^0.2",
24+
"tuupola/base62": "^0.8"
2525
},
2626
"autoload-dev": {
2727
"psr-4": {
2828
"Pre\\Plugin\\": "tests"
2929
}
3030
},
3131
"extra": {
32-
"class": [
33-
"Pre\\Plugin\\Integration\\Plugin"
34-
]
32+
"class": ["Pre\\Plugin\\Integration\\Plugin"]
3533
}
3634
}

src/Integration/Installer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private function addMacroPath($path)
6363
static $base62;
6464

6565
if (!$base62) {
66-
$base62 = new Base62();
66+
$base62 = new Base62();
6767
}
6868

6969
$paths[$base62->encode($path)] = true;
@@ -94,7 +94,7 @@ private function addCompiler($compiler)
9494
$compilers = json_decode(file_get_contents($file), true);
9595
}
9696

97-
$compilers = array_filter($compilers, function($next) use ($compiler) {
97+
$compilers = array_filter($compilers, function ($next) use ($compiler) {
9898
return $next !== $compiler;
9999
});
100100

src/Testing/Runner.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ private function specs()
3535
$directories = new RecursiveDirectoryIterator($this->path());
3636

3737
$files = new RegexIterator(
38-
new RecursiveIteratorIterator($directories), "/spec$/", RegexIterator::MATCH
38+
new RecursiveIteratorIterator($directories),
39+
"/spec$/",
40+
RegexIterator::MATCH
3941
);
4042

4143
$specs = [];

src/environment.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
if (empty(getenv("PRE_ISOLATE_DEPENDENCIES"))) {
4+
putenv("PRE_ISOLATE_DEPENDENCIES=1");
5+
}
6+
37
if (empty(getenv("PRE_BASE_DIR"))) {
48
putenv("PRE_BASE_DIR=" . realpath(__DIR__ . "/../../../../"));
59
}

src/functions.php

Lines changed: 104 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
namespace 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-
115
require_once __DIR__ . "/environment.php";
126

137
define("COMMENT", trim("
@@ -163,21 +157,12 @@ function compile($from, $to, $format = true, $comment = true)
163157
*/
164158
function 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
*/
257279
function 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
/**

tests/FunctionTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use PHPUnit\Framework\TestCase;
66

7-
function compiler($code) {
7+
function compiler($code)
8+
{
89
return "{$code} COMPILED";
910
}
1011

@@ -67,7 +68,13 @@ public function can_register_custom_compilers()
6768
addCompiler("Pre\\Plugin\\compiler");
6869

6970
$expected = "hello world COMPILED";
71+
72+
$actual = expand("hello world");
73+
74+
// try again without dependency isolation
75+
putenv("PRE_ISOLATE_DEPENDENCIES=0");
7076
$actual = expand("hello world");
77+
putenv("PRE_ISOLATE_DEPENDENCIES=1");
7178

7279
$this->assertEquals($expected, $actual);
7380

0 commit comments

Comments
 (0)