Skip to content

Commit 70e559b

Browse files
committed
Reduce the number of unprefixed whitelisted functions in the PHAR
1 parent 0baf6de commit 70e559b

4 files changed

Lines changed: 111 additions & 1 deletion

File tree

compiler/patches/Consistency.diff

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--- ../vendor/hoa/consistency/Consistency.php 2017-05-02 14:18:12.000000000 +0200
2+
+++ ../vendor/hoa/consistency/Consistency2.php 2020-05-05 08:28:35.000000000 +0200
3+
@@ -319,42 +319,6 @@
4+
$define('STREAM_CRYPTO_METHOD_ANY_CLIENT', 63);
5+
}
6+
7+
-if (!function_exists('curry')) {
8+
- /**
9+
- * Curry.
10+
- * Example:
11+
- * $c = curry('str_replace', …, …, 'foobar');
12+
- * var_dump($c('foo', 'baz')); // bazbar
13+
- * $c = curry('str_replace', 'foo', 'baz', …);
14+
- * var_dump($c('foobarbaz')); // bazbarbaz
15+
- * Nested curries also work:
16+
- * $c1 = curry('str_replace', …, …, 'foobar');
17+
- * $c2 = curry($c1, 'foo', …);
18+
- * var_dump($c2('baz')); // bazbar
19+
- * Obviously, as the first argument is a callable, we can combine this with
20+
- * \Hoa\Consistency\Xcallable ;-).
21+
- * The “…” character is the HORIZONTAL ELLIPSIS Unicode character (Unicode:
22+
- * 2026, UTF-8: E2 80 A6).
23+
- *
24+
- * @param mixed $callable Callable (two parts).
25+
- * @param ... ... Arguments.
26+
- * @return \Closure
27+
- */
28+
- function curry($callable)
29+
- {
30+
- $arguments = func_get_args();
31+
- array_shift($arguments);
32+
- $ii = array_keys($arguments, …, true);
33+
-
34+
- return function () use ($callable, $arguments, $ii) {
35+
- return call_user_func_array(
36+
- $callable,
37+
- array_replace($arguments, array_combine($ii, func_get_args()))
38+
- );
39+
- };
40+
- }
41+
-}
42+
-
43+
/**
44+
* Flex entity.
45+
*/

compiler/patches/Wrapper.diff

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--- ../vendor/hoa/protocol/Wrapper.php 2017-01-14 13:26:10.000000000 +0100
2+
+++ ../vendor/hoa/protocol/Wrapper2.php 2020-05-05 08:39:18.000000000 +0200
3+
@@ -582,24 +582,3 @@
4+
stream_wrapper_register('hoa', Wrapper::class);
5+
6+
}
7+
-
8+
-namespace
9+
-{
10+
-
11+
-/**
12+
- * Alias of `Hoa\Protocol::resolve` method.
13+
- *
14+
- * @param string $path Path to resolve.
15+
- * @param bool $exists If `true`, try to find the first that exists,
16+
- * else return the first solution.
17+
- * @param bool $unfold Return all solutions instead of one.
18+
- * @return mixed
19+
- */
20+
-if (!function_exists('resolve')) {
21+
- function resolve($path, $exists = true, $unfold = false)
22+
- {
23+
- return Hoa\Protocol::getInstance()->resolve($path, $exists, $unfold);
24+
- }
25+
-}
26+
-
27+
-}

compiler/src/Console/CompileCommand.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Component\Console\Command\Command;
88
use Symfony\Component\Console\Input\InputInterface;
99
use Symfony\Component\Console\Output\OutputInterface;
10+
use function escapeshellarg;
1011

1112
final class CompileCommand extends Command
1213
{
@@ -48,6 +49,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4849
$this->processFactory->setOutput($output);
4950

5051
$this->buildPreloadScript();
52+
$this->deleteUnnecessaryVendorCode();
53+
$this->patchFile(
54+
$output,
55+
'vendor/hoa/consistency/Consistency.php',
56+
'compiler/patches/Consistency.diff'
57+
);
58+
$this->patchFile(
59+
$output,
60+
'vendor/hoa/protocol/Wrapper.php',
61+
'compiler/patches/Wrapper.diff'
62+
);
5163
$this->fixComposerJson($this->buildDir);
5264
$this->renamePhpStormStubs();
5365

@@ -145,4 +157,30 @@ private function buildPreloadScript(): void
145157
file_put_contents($preloadScript, sprintf($template, $output));
146158
}
147159

160+
private function deleteUnnecessaryVendorCode(): void
161+
{
162+
$vendorDir = $this->buildDir . '/vendor';
163+
if (!is_dir($vendorDir . '/nikic/php-parser')) {
164+
return;
165+
}
166+
167+
@unlink($vendorDir . '/nikic/php-parser/grammar/rebuildParsers.php');
168+
@unlink($vendorDir . '/nikic/php-parser/bin/php-parse');
169+
}
170+
171+
private function patchFile(OutputInterface $output, string $originalFile, string $patchFile): void
172+
{
173+
exec(sprintf(
174+
'patch -d %s %s %s',
175+
escapeshellarg(realpath(__DIR__ . '/../../..')),
176+
escapeshellarg($originalFile),
177+
escapeshellarg($patchFile)
178+
), $outputLines, $exitCode);
179+
if ($exitCode === 0) {
180+
return;
181+
}
182+
183+
$output->writeln(sprintf('Patching failed: %s', implode("\n", $outputLines)));
184+
}
185+
148186
}

compiler/tests/Console/CompileCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testCommand(): void
4848
'command' => $command->getName(),
4949
]);
5050

51-
self::assertSame('', $commandTester->getDisplay());
51+
self::assertStringContainsString('Patching failed', $commandTester->getDisplay());
5252
}
5353

5454
}

0 commit comments

Comments
 (0)