From b34b6594debdbb18d8147f82a023979c999acb4a Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Mon, 11 Aug 2025 12:05:41 +0800 Subject: [PATCH] Suggestions --- src/SPC/builder/BuilderProvider.php | 2 +- src/SPC/command/BuildPHPCommand.php | 2 +- src/SPC/exception/ExceptionHandler.php | 41 ++++++++++++++++++++------ src/SPC/exception/SPCException.php | 18 ----------- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/SPC/builder/BuilderProvider.php b/src/SPC/builder/BuilderProvider.php index 6fbec300..3c52903a 100644 --- a/src/SPC/builder/BuilderProvider.php +++ b/src/SPC/builder/BuilderProvider.php @@ -32,7 +32,7 @@ class BuilderProvider }; // bind the builder to ExceptionHandler - ExceptionHandler::$bind_builder = self::$builder; + ExceptionHandler::setBindBuilder(self::$builder); return self::$builder; } diff --git a/src/SPC/command/BuildPHPCommand.php b/src/SPC/command/BuildPHPCommand.php index 5f57653d..f4f1f478 100644 --- a/src/SPC/command/BuildPHPCommand.php +++ b/src/SPC/command/BuildPHPCommand.php @@ -167,7 +167,7 @@ class BuildPHPCommand extends BuildCommand $this->printFormatInfo($indent_texts); // bind extra info to exception handler - ExceptionHandler::$bind_build_php_extra_info = $indent_texts; + ExceptionHandler::setBindBuildPhpExtraInfo($indent_texts); logger()->notice('Build will start after 2s ...'); sleep(2); diff --git a/src/SPC/exception/ExceptionHandler.php b/src/SPC/exception/ExceptionHandler.php index d41b3f08..3966f71e 100644 --- a/src/SPC/exception/ExceptionHandler.php +++ b/src/SPC/exception/ExceptionHandler.php @@ -5,6 +5,10 @@ declare(strict_types=1); namespace SPC\exception; use SPC\builder\BuilderBase; +use SPC\builder\freebsd\BSDBuilder; +use SPC\builder\linux\LinuxBuilder; +use SPC\builder\macos\MacOSBuilder; +use SPC\builder\windows\WindowsBuilder; use ZM\Logger\ConsoleColor; class ExceptionHandler @@ -28,10 +32,10 @@ class ExceptionHandler ]; /** @var null|BuilderBase Builder binding */ - public static ?BuilderBase $bind_builder = null; + private static ?BuilderBase $bind_builder = null; /** @var array Build PHP extra info binding */ - public static array $bind_build_php_extra_info = []; + private static array $bind_build_php_extra_info = []; public static function handleSPCException(SPCException $e): void { @@ -47,7 +51,7 @@ class ExceptionHandler SPCInternalException::class => "✗ SPC internal error: {$e->getMessage()}", ValidationException::class => "⚠ Validation failed: {$e->getMessage()}", WrongUsageException::class => $e->getMessage(), - default => "✗Unknown SPC exception {$class}: {$e->getMessage()}", + default => "✗ Unknown SPC exception {$class}: {$e->getMessage()}", }; self::logError($head_msg); @@ -61,12 +65,19 @@ class ExceptionHandler self::logError("----------------------------------------\n"); // get the SPCException module - if ($php_info = $e->getBuildPHPInfo()) { - self::logError('Failed module: ' . ConsoleColor::yellow("Builder for {$php_info['os']}")); - } elseif ($lib_info = $e->getLibraryInfo()) { + if ($lib_info = $e->getLibraryInfo()) { self::logError('Failed module: ' . ConsoleColor::yellow("library {$lib_info['library_name']} builder for {$lib_info['os']}")); } elseif ($ext_info = $e->getExtensionInfo()) { self::logError('Failed module: ' . ConsoleColor::yellow("shared extension {$ext_info['extension_name']} builder")); + } elseif (self::$bind_builder) { + $os = match (get_class(self::$bind_builder)) { + WindowsBuilder::class => 'Windows', + MacOSBuilder::class => 'macOS', + LinuxBuilder::class => 'Linux', + BSDBuilder::class => 'FreeBSD', + default => 'Unknown OS', + }; + self::logError('Failed module: ' . ConsoleColor::yellow("Builder for {$os}")); } elseif (!in_array($class, self::KNOWN_EXCEPTIONS)) { self::logError('Failed From: ' . ConsoleColor::yellow('Unknown SPC module ' . $class)); } @@ -118,12 +129,14 @@ class ExceptionHandler } // get the full builder options if possible - if (self::$bind_builder && $e->getBuildPHPInfo()) { + if ($e->getBuildPHPInfo()) { $info = $e->getBuildPHPInfo(); self::logError('', output_log: defined('DEBUG_MODE')); self::logError('Builder function: ' . ConsoleColor::yellow($info['builder_function']), output_log: defined('DEBUG_MODE')); - self::logError('Builder options:', output_log: defined('DEBUG_MODE')); - self::printArrayInfo(self::$bind_builder->getOptions()); + if (self::$bind_builder) { + self::logError('Builder options:', output_log: defined('DEBUG_MODE')); + self::printArrayInfo(self::$bind_builder->getOptions()); + } } self::logError("\n----------------------------------------\n"); @@ -151,6 +164,16 @@ class ExceptionHandler self::logError('⚠ Please report this exception to: https://github.com/crazywhalecc/static-php-cli/issues'); } + public static function setBindBuilder(?BuilderBase $bind_builder): void + { + self::$bind_builder = $bind_builder; + } + + public static function setBindBuildPhpExtraInfo(array $bind_build_php_extra_info): void + { + self::$bind_build_php_extra_info = $bind_build_php_extra_info; + } + private static function logError($message, int $indent_space = 0, bool $output_log = true): void { $spc_log = fopen(SPC_OUTPUT_LOG, 'a'); diff --git a/src/SPC/exception/SPCException.php b/src/SPC/exception/SPCException.php index 8a101cd1..e8b23361 100644 --- a/src/SPC/exception/SPCException.php +++ b/src/SPC/exception/SPCException.php @@ -5,15 +5,11 @@ declare(strict_types=1); namespace SPC\exception; use SPC\builder\BuilderBase; -use SPC\builder\freebsd\BSDBuilder; use SPC\builder\freebsd\library\BSDLibraryBase; use SPC\builder\LibraryBase; use SPC\builder\linux\library\LinuxLibraryBase; -use SPC\builder\linux\LinuxBuilder; use SPC\builder\macos\library\MacOSLibraryBase; -use SPC\builder\macos\MacOSBuilder; use SPC\builder\windows\library\WindowsLibraryBase; -use SPC\builder\windows\WindowsBuilder; /** * Base class for SPC exceptions. @@ -70,10 +66,7 @@ abstract class SPCException extends \Exception * Returns an array containing information about the PHP build process. * * @return null|array{ - * builder_class: string, - * builder_options: array, * builder_function: string, - * os: string, * file: null|string, * line: null|int, * } an array containing PHP build information @@ -143,19 +136,8 @@ abstract class SPCException extends \Exception // Check if the class is a subclass of BuilderBase and the method is buildPHP if (!$this->build_php_info && is_a($frame['class'], BuilderBase::class, true)) { - $options = ExceptionHandler::$bind_builder?->getOptions() ?? []; - $os = match (get_class(ExceptionHandler::$bind_builder ?? $frame['class'])) { - BSDBuilder::class => 'BSD', - LinuxBuilder::class => 'Linux', - MacOSBuilder::class => 'macOS', - WindowsBuilder::class => 'Windows', - default => 'Unknown', - }; $this->build_php_info = [ - 'builder_class' => $frame['class'], - 'builder_options' => $options, 'builder_function' => $frame['function'], - 'os' => $os, 'file' => $frame['file'] ?? null, 'line' => $frame['line'] ?? null, ];