Skip to content

Commit b34b659

Browse files
committed
Suggestions
1 parent 2fba61e commit b34b659

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

src/SPC/builder/BuilderProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function makeBuilderByInput(InputInterface $input): BuilderBase
3232
};
3333

3434
// bind the builder to ExceptionHandler
35-
ExceptionHandler::$bind_builder = self::$builder;
35+
ExceptionHandler::setBindBuilder(self::$builder);
3636

3737
return self::$builder;
3838
}

src/SPC/command/BuildPHPCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function handle(): int
167167
$this->printFormatInfo($indent_texts);
168168

169169
// bind extra info to exception handler
170-
ExceptionHandler::$bind_build_php_extra_info = $indent_texts;
170+
ExceptionHandler::setBindBuildPhpExtraInfo($indent_texts);
171171

172172
logger()->notice('Build will start after 2s ...');
173173
sleep(2);

src/SPC/exception/ExceptionHandler.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
namespace SPC\exception;
66

77
use SPC\builder\BuilderBase;
8+
use SPC\builder\freebsd\BSDBuilder;
9+
use SPC\builder\linux\LinuxBuilder;
10+
use SPC\builder\macos\MacOSBuilder;
11+
use SPC\builder\windows\WindowsBuilder;
812
use ZM\Logger\ConsoleColor;
913

1014
class ExceptionHandler
@@ -28,10 +32,10 @@ class ExceptionHandler
2832
];
2933

3034
/** @var null|BuilderBase Builder binding */
31-
public static ?BuilderBase $bind_builder = null;
35+
private static ?BuilderBase $bind_builder = null;
3236

3337
/** @var array<string, mixed> Build PHP extra info binding */
34-
public static array $bind_build_php_extra_info = [];
38+
private static array $bind_build_php_extra_info = [];
3539

3640
public static function handleSPCException(SPCException $e): void
3741
{
@@ -47,7 +51,7 @@ public static function handleSPCException(SPCException $e): void
4751
SPCInternalException::class => "✗ SPC internal error: {$e->getMessage()}",
4852
ValidationException::class => "⚠ Validation failed: {$e->getMessage()}",
4953
WrongUsageException::class => $e->getMessage(),
50-
default => "✗Unknown SPC exception {$class}: {$e->getMessage()}",
54+
default => " Unknown SPC exception {$class}: {$e->getMessage()}",
5155
};
5256
self::logError($head_msg);
5357

@@ -61,12 +65,19 @@ public static function handleSPCException(SPCException $e): void
6165
self::logError("----------------------------------------\n");
6266

6367
// get the SPCException module
64-
if ($php_info = $e->getBuildPHPInfo()) {
65-
self::logError('Failed module: ' . ConsoleColor::yellow("Builder for {$php_info['os']}"));
66-
} elseif ($lib_info = $e->getLibraryInfo()) {
68+
if ($lib_info = $e->getLibraryInfo()) {
6769
self::logError('Failed module: ' . ConsoleColor::yellow("library {$lib_info['library_name']} builder for {$lib_info['os']}"));
6870
} elseif ($ext_info = $e->getExtensionInfo()) {
6971
self::logError('Failed module: ' . ConsoleColor::yellow("shared extension {$ext_info['extension_name']} builder"));
72+
} elseif (self::$bind_builder) {
73+
$os = match (get_class(self::$bind_builder)) {
74+
WindowsBuilder::class => 'Windows',
75+
MacOSBuilder::class => 'macOS',
76+
LinuxBuilder::class => 'Linux',
77+
BSDBuilder::class => 'FreeBSD',
78+
default => 'Unknown OS',
79+
};
80+
self::logError('Failed module: ' . ConsoleColor::yellow("Builder for {$os}"));
7081
} elseif (!in_array($class, self::KNOWN_EXCEPTIONS)) {
7182
self::logError('Failed From: ' . ConsoleColor::yellow('Unknown SPC module ' . $class));
7283
}
@@ -118,12 +129,14 @@ public static function handleSPCException(SPCException $e): void
118129
}
119130

120131
// get the full builder options if possible
121-
if (self::$bind_builder && $e->getBuildPHPInfo()) {
132+
if ($e->getBuildPHPInfo()) {
122133
$info = $e->getBuildPHPInfo();
123134
self::logError('', output_log: defined('DEBUG_MODE'));
124135
self::logError('Builder function: ' . ConsoleColor::yellow($info['builder_function']), output_log: defined('DEBUG_MODE'));
125-
self::logError('Builder options:', output_log: defined('DEBUG_MODE'));
126-
self::printArrayInfo(self::$bind_builder->getOptions());
136+
if (self::$bind_builder) {
137+
self::logError('Builder options:', output_log: defined('DEBUG_MODE'));
138+
self::printArrayInfo(self::$bind_builder->getOptions());
139+
}
127140
}
128141

129142
self::logError("\n----------------------------------------\n");
@@ -151,6 +164,16 @@ public static function handleDefaultException(\Throwable $e): void
151164
self::logError('⚠ Please report this exception to: https://github.com/crazywhalecc/static-php-cli/issues');
152165
}
153166

167+
public static function setBindBuilder(?BuilderBase $bind_builder): void
168+
{
169+
self::$bind_builder = $bind_builder;
170+
}
171+
172+
public static function setBindBuildPhpExtraInfo(array $bind_build_php_extra_info): void
173+
{
174+
self::$bind_build_php_extra_info = $bind_build_php_extra_info;
175+
}
176+
154177
private static function logError($message, int $indent_space = 0, bool $output_log = true): void
155178
{
156179
$spc_log = fopen(SPC_OUTPUT_LOG, 'a');

src/SPC/exception/SPCException.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
namespace SPC\exception;
66

77
use SPC\builder\BuilderBase;
8-
use SPC\builder\freebsd\BSDBuilder;
98
use SPC\builder\freebsd\library\BSDLibraryBase;
109
use SPC\builder\LibraryBase;
1110
use SPC\builder\linux\library\LinuxLibraryBase;
12-
use SPC\builder\linux\LinuxBuilder;
1311
use SPC\builder\macos\library\MacOSLibraryBase;
14-
use SPC\builder\macos\MacOSBuilder;
1512
use SPC\builder\windows\library\WindowsLibraryBase;
16-
use SPC\builder\windows\WindowsBuilder;
1713

1814
/**
1915
* Base class for SPC exceptions.
@@ -70,10 +66,7 @@ public function getLibraryInfo(): ?array
7066
* Returns an array containing information about the PHP build process.
7167
*
7268
* @return null|array{
73-
* builder_class: string,
74-
* builder_options: array<string, mixed>,
7569
* builder_function: string,
76-
* os: string,
7770
* file: null|string,
7871
* line: null|int,
7972
* } an array containing PHP build information
@@ -143,19 +136,8 @@ private function loadStackTraceInfo(): void
143136

144137
// Check if the class is a subclass of BuilderBase and the method is buildPHP
145138
if (!$this->build_php_info && is_a($frame['class'], BuilderBase::class, true)) {
146-
$options = ExceptionHandler::$bind_builder?->getOptions() ?? [];
147-
$os = match (get_class(ExceptionHandler::$bind_builder ?? $frame['class'])) {
148-
BSDBuilder::class => 'BSD',
149-
LinuxBuilder::class => 'Linux',
150-
MacOSBuilder::class => 'macOS',
151-
WindowsBuilder::class => 'Windows',
152-
default => 'Unknown',
153-
};
154139
$this->build_php_info = [
155-
'builder_class' => $frame['class'],
156-
'builder_options' => $options,
157140
'builder_function' => $frame['function'],
158-
'os' => $os,
159141
'file' => $frame['file'] ?? null,
160142
'line' => $frame['line'] ?? null,
161143
];

0 commit comments

Comments
 (0)