From 280284e4c2f498949f497532fda75bfa9ec36b12 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Mon, 1 May 2023 12:50:01 +0800 Subject: [PATCH] fix phar patch --- config/source.json | 2 +- src/SPC/builder/linux/LinuxBuilder.php | 9 ++------- src/SPC/builder/macos/MacOSBuilder.php | 10 ++-------- src/SPC/store/FileSystem.php | 2 +- src/SPC/store/SourcePatcher.php | 23 +++++++++++------------ 5 files changed, 17 insertions(+), 29 deletions(-) diff --git a/config/source.json b/config/source.json index 144b7015..e37039fe 100644 --- a/config/source.json +++ b/config/source.json @@ -221,7 +221,7 @@ "type": "git", "path": "php-src/sapi/micro", "rev": "master", - "url": "https://github.com/dixyes/phpmicro", + "url": "https://github.com/crazywhalecc/phpmicro", "license": { "type": "file", "path": "LICENSE" diff --git a/src/SPC/builder/linux/LinuxBuilder.php b/src/SPC/builder/linux/LinuxBuilder.php index 93a6fe5d..d9e83d24 100644 --- a/src/SPC/builder/linux/LinuxBuilder.php +++ b/src/SPC/builder/linux/LinuxBuilder.php @@ -218,7 +218,7 @@ class LinuxBuilder extends BuilderBase } 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')) { $this->phar_patched = true; - try { - 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; - } + SourcePatcher::patchMicro(['phar']); } shell()->cd(SOURCE_PATH . '/php-src') diff --git a/src/SPC/builder/macos/MacOSBuilder.php b/src/SPC/builder/macos/MacOSBuilder.php index bcd7c9f4..e13ac814 100644 --- a/src/SPC/builder/macos/MacOSBuilder.php +++ b/src/SPC/builder/macos/MacOSBuilder.php @@ -187,7 +187,7 @@ class MacOSBuilder extends BuilderBase } 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')) { $this->phar_patched = true; - try { - // 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; - } + SourcePatcher::patchMicro(['phar']); } shell()->cd(SOURCE_PATH . '/php-src') diff --git a/src/SPC/store/FileSystem.php b/src/SPC/store/FileSystem.php index bc0d024f..6c4b515a 100644 --- a/src/SPC/store/FileSystem.php +++ b/src/SPC/store/FileSystem.php @@ -442,7 +442,7 @@ class FileSystem private static function emitSourceExtractHook(string $name) { foreach ((self::$_extract_hook[$name] ?? []) as $hook) { - if ($hook($name) === true) { + if ($hook() === true) { logger()->info('Patched source [' . $name . '] after extracted'); } } diff --git a/src/SPC/store/SourcePatcher.php b/src/SPC/store/SourcePatcher.php index 222fa7b1..117b5666 100644 --- a/src/SPC/store/SourcePatcher.php +++ b/src/SPC/store/SourcePatcher.php @@ -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')) { return false; @@ -137,7 +137,7 @@ class SourcePatcher $check = !defined('DEBUG_MODE') ? ' -q' : ''; // f_passthru('cd ' . SOURCE_PATH . '/php-src && git checkout' . $check . ' HEAD'); - $patch_list = [ + $default = [ 'static_opcache', 'static_extensions_win32', 'cli_checks', @@ -146,15 +146,13 @@ class SourcePatcher 'win32', 'zend_stream', ]; - $patch_list = array_merge($patch_list, match (PHP_OS_FAMILY) { - 'Windows' => [ - 'cli_static', - ], - 'Darwin' => [ - 'macos_iconv', - ], - default => [], - }); + if (PHP_OS_FAMILY === 'Windows') { + $default[] = 'cli_static'; + } + if (PHP_OS_FAMILY === 'Darwin') { + $default[] = 'macos_iconv'; + } + $patch_list = $list ?? $default; $patches = []; $serial = ['80', '81', '82']; foreach ($patch_list as $patchName) { @@ -177,8 +175,9 @@ class SourcePatcher f_passthru( '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; }