fix phar patch

This commit is contained in:
crazywhalecc 2023-05-01 12:50:01 +08:00
parent 85ac553ded
commit 280284e4c2
No known key found for this signature in database
GPG Key ID: 1F4BDD59391F2680
5 changed files with 17 additions and 29 deletions

View File

@ -221,7 +221,7 @@
"type": "git", "type": "git",
"path": "php-src/sapi/micro", "path": "php-src/sapi/micro",
"rev": "master", "rev": "master",
"url": "https://github.com/dixyes/phpmicro", "url": "https://github.com/crazywhalecc/phpmicro",
"license": { "license": {
"type": "file", "type": "file",
"path": "LICENSE" "path": "LICENSE"

View File

@ -218,7 +218,7 @@ class LinuxBuilder extends BuilderBase
} }
if ($this->phar_patched) { if ($this->phar_patched) {
shell()->cd(SOURCE_PATH . '/php-src')->exec('patch -p1 -R < sapi/micro/patches/phar.patch'); SourcePatcher::patchMicro(['phar'], true);
} }
} }
@ -255,12 +255,7 @@ class LinuxBuilder extends BuilderBase
} }
if ($this->getExt('phar')) { if ($this->getExt('phar')) {
$this->phar_patched = true; $this->phar_patched = true;
try { SourcePatcher::patchMicro(['phar']);
shell()->cd(SOURCE_PATH . '/php-src')->exec('patch -p1 < sapi/micro/patches/phar.patch');
} catch (RuntimeException $e) {
logger()->error('failed to patch phat due to patch exit with code ' . $e->getCode());
$this->phar_patched = false;
}
} }
shell()->cd(SOURCE_PATH . '/php-src') shell()->cd(SOURCE_PATH . '/php-src')

View File

@ -187,7 +187,7 @@ class MacOSBuilder extends BuilderBase
} }
if ($this->phar_patched) { if ($this->phar_patched) {
f_passthru('cd ' . SOURCE_PATH . '/php-src && patch -p1 -R < sapi/micro/patches/phar.patch'); SourcePatcher::patchMicro(['phar'], true);
} }
} }
@ -218,13 +218,7 @@ class MacOSBuilder extends BuilderBase
} }
if ($this->getExt('phar')) { if ($this->getExt('phar')) {
$this->phar_patched = true; $this->phar_patched = true;
try { SourcePatcher::patchMicro(['phar']);
// TODO: 未来改进一下 patch让除了这种 patch 类型的文件以外可以恢复原文件
shell()->cd(SOURCE_PATH . '/php-src')->exec('patch -p1 < sapi/micro/patches/phar.patch');
} catch (RuntimeException $e) {
logger()->error('failed to patch phat due to patch exit with code ' . $e->getCode());
$this->phar_patched = false;
}
} }
shell()->cd(SOURCE_PATH . '/php-src') shell()->cd(SOURCE_PATH . '/php-src')

View File

@ -442,7 +442,7 @@ class FileSystem
private static function emitSourceExtractHook(string $name) private static function emitSourceExtractHook(string $name)
{ {
foreach ((self::$_extract_hook[$name] ?? []) as $hook) { foreach ((self::$_extract_hook[$name] ?? []) as $hook) {
if ($hook($name) === true) { if ($hook() === true) {
logger()->info('Patched source [' . $name . '] after extracted'); logger()->info('Patched source [' . $name . '] after extracted');
} }
} }

View File

@ -117,7 +117,7 @@ class SourcePatcher
); );
} }
public static function patchMicro(): bool public static function patchMicro(?array $list = null, bool $reverse = false): bool
{ {
if (!file_exists(SOURCE_PATH . '/php-src/sapi/micro/php_micro.c')) { if (!file_exists(SOURCE_PATH . '/php-src/sapi/micro/php_micro.c')) {
return false; return false;
@ -137,7 +137,7 @@ class SourcePatcher
$check = !defined('DEBUG_MODE') ? ' -q' : ''; $check = !defined('DEBUG_MODE') ? ' -q' : '';
// f_passthru('cd ' . SOURCE_PATH . '/php-src && git checkout' . $check . ' HEAD'); // f_passthru('cd ' . SOURCE_PATH . '/php-src && git checkout' . $check . ' HEAD');
$patch_list = [ $default = [
'static_opcache', 'static_opcache',
'static_extensions_win32', 'static_extensions_win32',
'cli_checks', 'cli_checks',
@ -146,15 +146,13 @@ class SourcePatcher
'win32', 'win32',
'zend_stream', 'zend_stream',
]; ];
$patch_list = array_merge($patch_list, match (PHP_OS_FAMILY) { if (PHP_OS_FAMILY === 'Windows') {
'Windows' => [ $default[] = 'cli_static';
'cli_static', }
], if (PHP_OS_FAMILY === 'Darwin') {
'Darwin' => [ $default[] = 'macos_iconv';
'macos_iconv', }
], $patch_list = $list ?? $default;
default => [],
});
$patches = []; $patches = [];
$serial = ['80', '81', '82']; $serial = ['80', '81', '82'];
foreach ($patch_list as $patchName) { foreach ($patch_list as $patchName) {
@ -177,8 +175,9 @@ class SourcePatcher
f_passthru( f_passthru(
'cd ' . SOURCE_PATH . '/php-src && ' . 'cd ' . SOURCE_PATH . '/php-src && ' .
(PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . $patchesStr . ' | patch -p1' (PHP_OS_FAMILY === 'Windows' ? 'type' : 'cat') . ' ' . $patchesStr . ' | patch -p1 ' . ($reverse ? '-R' : '')
); );
return true; return true;
} }