Skip to content

Commit f68f060

Browse files
committed
Refactor all (except command) modules using new exceptions
1 parent 722bb31 commit f68f060

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+334
-290
lines changed

src/SPC/builder/Extension.php

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
namespace SPC\builder;
66

7-
use SPC\exception\FileSystemException;
8-
use SPC\exception\RuntimeException;
7+
use SPC\exception\EnvironmentException;
8+
use SPC\exception\SPCException;
9+
use SPC\exception\ValidationException;
910
use SPC\exception\WrongUsageException;
1011
use SPC\store\Config;
1112
use SPC\store\FileSystem;
@@ -30,18 +31,18 @@ public function __construct(protected string $name, protected BuilderBase $build
3031
$unix_only = Config::getExt($this->name, 'unix-only', false);
3132
$windows_only = Config::getExt($this->name, 'windows-only', false);
3233
if (PHP_OS_FAMILY !== 'Windows' && $windows_only) {
33-
throw new RuntimeException("{$ext_type} extension {$name} is not supported on Linux and macOS platform");
34+
throw new EnvironmentException("{$ext_type} extension {$name} is not supported on Linux and macOS platform");
3435
}
3536
if (PHP_OS_FAMILY === 'Windows' && $unix_only) {
36-
throw new RuntimeException("{$ext_type} extension {$name} is not supported on Windows platform");
37+
throw new EnvironmentException("{$ext_type} extension {$name} is not supported on Windows platform");
3738
}
3839
// set source_dir for builtin
3940
if ($ext_type === 'builtin') {
4041
$this->source_dir = SOURCE_PATH . '/php-src/ext/' . $this->name;
4142
} elseif ($ext_type === 'external') {
4243
$source = Config::getExt($this->name, 'source');
4344
if ($source === null) {
44-
throw new RuntimeException("{$ext_type} extension {$name} source not found");
45+
throw new ValidationException("{$ext_type} extension {$name} source not found", validation_module: "Extension [{$name}] loader");
4546
}
4647
$source_path = Config::getSource($source)['path'] ?? null;
4748
$source_path = $source_path === null ? SOURCE_PATH . '/' . $source : SOURCE_PATH . '/' . $source_path;
@@ -287,13 +288,12 @@ public function runCliCheckUnix(): void
287288
{
288289
// Run compile check if build target is cli
289290
// If you need to run some check, overwrite this or add your assert in src/globals/ext-tests/{extension_name}.php
290-
// If check failed, throw RuntimeException
291291
$sharedExtensions = $this->getSharedExtensionLoadString();
292292
[$ret, $out] = shell()->execWithResult(BUILD_BIN_PATH . '/php -n' . $sharedExtensions . ' --ri "' . $this->getDistName() . '"');
293293
if ($ret !== 0) {
294-
throw new RuntimeException(
295-
'extension ' . $this->getName() . ' failed runtime check: php-cli returned ' . $ret . "\n" .
296-
join("\n", $out)
294+
throw new ValidationException(
295+
"extension {$this->getName()} failed compile check: php-cli returned {$ret}",
296+
validation_module: 'Extension ' . $this->getName() . ' sanity check'
297297
);
298298
}
299299

@@ -307,8 +307,10 @@ public function runCliCheckUnix(): void
307307

308308
[$ret, $out] = shell()->execWithResult(BUILD_BIN_PATH . '/php -n' . $sharedExtensions . ' -r "' . trim($test) . '"');
309309
if ($ret !== 0) {
310-
var_dump($out);
311-
throw new RuntimeException('extension ' . $this->getName() . ' failed sanity check');
310+
throw new ValidationException(
311+
"extension {$this->getName()} failed sanity check. Code: {$ret}, output: " . implode("\n", $out),
312+
validation_module: 'Extension ' . $this->getName() . ' function check'
313+
);
312314
}
313315
}
314316
}
@@ -317,10 +319,9 @@ public function runCliCheckWindows(): void
317319
{
318320
// Run compile check if build target is cli
319321
// If you need to run some check, overwrite this or add your assert in src/globals/ext-tests/{extension_name}.php
320-
// If check failed, throw RuntimeException
321322
[$ret] = cmd()->execWithResult(BUILD_ROOT_PATH . '/bin/php.exe -n --ri "' . $this->getDistName() . '"', false);
322323
if ($ret !== 0) {
323-
throw new RuntimeException('extension ' . $this->getName() . ' failed compile check: php-cli returned ' . $ret);
324+
throw new ValidationException("extension {$this->getName()} failed compile check: php-cli returned {$ret}", validation_module: "Extension {$this->getName()} sanity check");
324325
}
325326

326327
if (file_exists(FileSystem::convertPath(ROOT_DIR . '/src/globals/ext-tests/' . $this->getName() . '.php'))) {
@@ -333,7 +334,10 @@ public function runCliCheckWindows(): void
333334

334335
[$ret] = cmd()->execWithResult(BUILD_ROOT_PATH . '/bin/php.exe -n -r "' . trim($test) . '"');
335336
if ($ret !== 0) {
336-
throw new RuntimeException('extension ' . $this->getName() . ' failed sanity check');
337+
throw new ValidationException(
338+
"extension {$this->getName()} failed function check",
339+
validation_module: "Extension {$this->getName()} function check"
340+
);
337341
}
338342
}
339343
}
@@ -348,34 +352,39 @@ public function validate(): void
348352
*/
349353
public function buildShared(): void
350354
{
351-
if (Config::getExt($this->getName(), 'type') === 'builtin' || Config::getExt($this->getName(), 'build-with-php') === true) {
352-
if (file_exists(BUILD_MODULES_PATH . '/' . $this->getName() . '.so')) {
353-
logger()->info('Shared extension [' . $this->getName() . '] was already built by php-src/configure (' . $this->getName() . '.so)');
354-
return;
355-
}
356-
if (Config::getExt($this->getName(), 'build-with-php') === true) {
357-
logger()->warning('Shared extension [' . $this->getName() . '] did not build with php-src/configure (' . $this->getName() . '.so)');
358-
logger()->warning('Try deleting your build and source folders and running `spc build`` again.');
359-
return;
355+
try {
356+
if (Config::getExt($this->getName(), 'type') === 'builtin' || Config::getExt($this->getName(), 'build-with-php') === true) {
357+
if (file_exists(BUILD_MODULES_PATH . '/' . $this->getName() . '.so')) {
358+
logger()->info('Shared extension [' . $this->getName() . '] was already built by php-src/configure (' . $this->getName() . '.so)');
359+
return;
360+
}
361+
if (Config::getExt($this->getName(), 'build-with-php') === true) {
362+
logger()->warning('Shared extension [' . $this->getName() . '] did not build with php-src/configure (' . $this->getName() . '.so)');
363+
logger()->warning('Try deleting your build and source folders and running `spc build`` again.');
364+
return;
365+
}
360366
}
361-
}
362-
if (file_exists(BUILD_MODULES_PATH . '/' . $this->getName() . '.so')) {
363-
logger()->info('Shared extension [' . $this->getName() . '] was already built, skipping (' . $this->getName() . '.so)');
364-
}
365-
logger()->info('Building extension [' . $this->getName() . '] as shared extension (' . $this->getName() . '.so)');
366-
foreach ($this->dependencies as $dependency) {
367-
if (!$dependency instanceof Extension) {
368-
continue;
367+
if (file_exists(BUILD_MODULES_PATH . '/' . $this->getName() . '.so')) {
368+
logger()->info('Shared extension [' . $this->getName() . '] was already built, skipping (' . $this->getName() . '.so)');
369369
}
370-
if (!$dependency->isBuildStatic()) {
371-
logger()->info('extension ' . $this->getName() . ' requires extension ' . $dependency->getName());
372-
$dependency->buildShared();
370+
logger()->info('Building extension [' . $this->getName() . '] as shared extension (' . $this->getName() . '.so)');
371+
foreach ($this->dependencies as $dependency) {
372+
if (!$dependency instanceof Extension) {
373+
continue;
374+
}
375+
if (!$dependency->isBuildStatic()) {
376+
logger()->info('extension ' . $this->getName() . ' requires extension ' . $dependency->getName());
377+
$dependency->buildShared();
378+
}
373379
}
380+
match (PHP_OS_FAMILY) {
381+
'Darwin', 'Linux' => $this->buildUnixShared(),
382+
default => throw new WrongUsageException(PHP_OS_FAMILY . ' build shared extensions is not supported yet'),
383+
};
384+
} catch (SPCException $e) {
385+
$e->bindExtensionInfo(['extension_name' => $this->getName()]);
386+
throw $e;
374387
}
375-
match (PHP_OS_FAMILY) {
376-
'Darwin', 'Linux' => $this->buildUnixShared(),
377-
default => throw new WrongUsageException(PHP_OS_FAMILY . ' build shared extensions is not supported yet'),
378-
};
379388
}
380389

381390
/**
@@ -479,7 +488,7 @@ protected function addLibraryDependency(string $name, bool $optional = false): v
479488
$depLib = $this->builder->getLib($name);
480489
if (!$depLib) {
481490
if (!$optional) {
482-
throw new RuntimeException("extension {$this->name} requires library {$name}");
491+
throw new WrongUsageException("extension {$this->name} requires library {$name}");
483492
}
484493
logger()->info("enabling {$this->name} without library {$name}");
485494
} else {
@@ -492,7 +501,7 @@ protected function addExtensionDependency(string $name, bool $optional = false):
492501
$depExt = $this->builder->getExt($name);
493502
if (!$depExt) {
494503
if (!$optional) {
495-
throw new RuntimeException("{$this->name} requires extension {$name}");
504+
throw new WrongUsageException("{$this->name} requires extension {$name} which is not included");
496505
}
497506
logger()->info("enabling {$this->name} without extension {$name}");
498507
} else {

src/SPC/builder/LibraryBase.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace SPC\builder;
66

7-
use SPC\exception\FileSystemException;
8-
use SPC\exception\RuntimeException;
7+
use SPC\exception\SPCException;
8+
use SPC\exception\SPCInternalException;
99
use SPC\exception\WrongUsageException;
1010
use SPC\store\Config;
1111
use SPC\store\Downloader;
@@ -30,9 +30,9 @@ abstract class LibraryBase
3030
public function __construct(?string $source_dir = null)
3131
{
3232
if (static::NAME === 'unknown') {
33-
throw new RuntimeException('no unknown!!!!!');
33+
throw new SPCInternalException('Please set the NAME constant in ' . static::class);
3434
}
35-
$this->source_dir = $source_dir ?? (SOURCE_PATH . '/' . Config::getLib(static::NAME, 'source'));
35+
$this->source_dir = $source_dir ?? (SOURCE_PATH . DIRECTORY_SEPARATOR . Config::getLib(static::NAME, 'source'));
3636
}
3737

3838
/**
@@ -154,7 +154,7 @@ public function tryInstall(array $lock, bool $force_install = false): int
154154
FileSystem::extractPackage($install_file, $lock['source_type'], DOWNLOAD_PATH . '/' . $install_file, BUILD_ROOT_PATH);
155155
$this->install();
156156
return LIB_STATUS_OK;
157-
} catch (FileSystemException|RuntimeException $e) {
157+
} catch (SPCException $e) {
158158
logger()->error('Failed to extract pre-built library [' . static::NAME . ']: ' . $e->getMessage());
159159
return LIB_STATUS_INSTALL_FAILED;
160160
}
@@ -304,7 +304,7 @@ protected function install(): void
304304
}
305305
$replace_items = json_decode(file_get_contents($replace_item_file), true);
306306
if (!is_array($replace_items)) {
307-
throw new RuntimeException('Invalid placeholder file: ' . $replace_item_file);
307+
throw new SPCInternalException("Invalid placeholder file: {$replace_item_file}");
308308
}
309309
$placeholders = get_pack_replace();
310310
// replace placeholders in BUILD_ROOT_PATH
@@ -331,7 +331,7 @@ protected function addLibraryDependency(string $name, bool $optional = false): v
331331
return;
332332
}
333333
if (!$optional) {
334-
throw new RuntimeException(static::NAME . " requires library {$name}");
334+
throw new WrongUsageException(static::NAME . " requires library {$name} but it is not included");
335335
}
336336
logger()->debug('enabling ' . static::NAME . " without {$name}");
337337
}

src/SPC/builder/extension/curl.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use SPC\builder\linux\LinuxBuilder;
99
use SPC\builder\macos\MacOSBuilder;
1010
use SPC\builder\windows\WindowsBuilder;
11-
use SPC\exception\FileSystemException;
12-
use SPC\exception\WrongUsageException;
11+
use SPC\exception\PatchException;
1312
use SPC\store\FileSystem;
1413
use SPC\util\CustomExt;
1514

@@ -98,7 +97,7 @@ public function patchBeforeSharedConfigure(): bool
9897
);
9998

10099
if ($patched === null) {
101-
throw new \RuntimeException('Failed to patch config.m4 due to a regex error');
100+
throw new PatchException('shared extension curl patcher', 'Failed to patch config.m4 due to a regex error');
102101
}
103102

104103
FileSystem::writeFile($file, $patched);

src/SPC/builder/extension/grpc.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use SPC\builder\Extension;
88
use SPC\builder\windows\WindowsBuilder;
9+
use SPC\exception\ValidationException;
910
use SPC\store\FileSystem;
1011
use SPC\util\CustomExt;
1112
use SPC\util\GlobalEnvManager;
@@ -18,7 +19,7 @@ class grpc extends Extension
1819
public function patchBeforeBuildconf(): bool
1920
{
2021
if ($this->builder instanceof WindowsBuilder) {
21-
throw new \RuntimeException('grpc extension does not support windows yet');
22+
throw new ValidationException('grpc extension does not support windows yet');
2223
}
2324
if (file_exists(SOURCE_PATH . '/php-src/ext/grpc')) {
2425
return false;
@@ -27,7 +28,7 @@ public function patchBeforeBuildconf(): bool
2728
if (is_dir($this->source_dir . '/src/php/ext/grpc')) {
2829
shell()->exec('ln -s ' . $this->source_dir . '/src/php/ext/grpc ' . SOURCE_PATH . '/php-src/ext/grpc');
2930
} else {
30-
throw new \RuntimeException('Cannot find grpc source code');
31+
throw new ValidationException('Cannot find grpc source code in ' . $this->source_dir . '/src/php/ext/grpc');
3132
}
3233
if (SPCTarget::getTargetOS() === 'Darwin') {
3334
FileSystem::replaceFileRegex(

src/SPC/builder/extension/mbregex.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SPC\builder\extension;
66

77
use SPC\builder\Extension;
8-
use SPC\exception\RuntimeException;
8+
use SPC\exception\ValidationException;
99
use SPC\util\CustomExt;
1010

1111
#[CustomExt('mbregex')]
@@ -16,11 +16,6 @@ public function getDistName(): string
1616
return 'mbstring';
1717
}
1818

19-
public function getConfigureArg(bool $shared = false): string
20-
{
21-
return '';
22-
}
23-
2419
/**
2520
* mbregex is not an extension, we need to overwrite the default check.
2621
*/
@@ -29,19 +24,19 @@ public function runCliCheckUnix(): void
2924
$sharedext = $this->builder->getExt('mbstring')->isBuildShared() ? '-d "extension_dir=' . BUILD_MODULES_PATH . '" -d "extension=mbstring"' : '';
3025
[$ret] = shell()->execWithResult(BUILD_ROOT_PATH . '/bin/php -n' . $sharedext . ' --ri "mbstring" | grep regex', false);
3126
if ($ret !== 0) {
32-
throw new RuntimeException('extension ' . $this->getName() . ' failed compile check: compiled php-cli mbstring extension does not contain regex !');
27+
throw new ValidationException("Extension {$this->getName()} failed compile check: compiled php-cli mbstring extension does not contain regex !");
3328
}
3429
}
3530

3631
public function runCliCheckWindows(): void
3732
{
3833
[$ret, $out] = cmd()->execWithResult(BUILD_ROOT_PATH . '/bin/php -n --ri "mbstring"', false);
3934
if ($ret !== 0) {
40-
throw new RuntimeException('extension ' . $this->getName() . ' failed compile check: compiled php-cli does not contain mbstring !');
35+
throw new ValidationException("extension {$this->getName()} failed compile check: compiled php-cli does not contain mbstring !");
4136
}
4237
$out = implode("\n", $out);
4338
if (!str_contains($out, 'regex')) {
44-
throw new RuntimeException('extension ' . $this->getName() . ' failed compile check: compiled php-cli mbstring extension does not contain regex !');
39+
throw new ValidationException("extension {$this->getName()} failed compile check: compiled php-cli mbstring extension does not contain regex !");
4540
}
4641
}
4742
}

src/SPC/builder/extension/opentelemetry.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SPC\builder\extension;
66

77
use SPC\builder\Extension;
8+
use SPC\exception\ValidationException;
89
use SPC\store\FileSystem;
910
use SPC\util\CustomExt;
1011
use SPC\util\GlobalEnvManager;
@@ -15,7 +16,7 @@ class opentelemetry extends Extension
1516
public function validate(): void
1617
{
1718
if ($this->builder->getPHPVersionID() < 80000 && getenv('SPC_SKIP_PHP_VERSION_CHECK') !== 'yes') {
18-
throw new \RuntimeException('The opentelemetry extension requires PHP 8.0 or later');
19+
throw new ValidationException('The opentelemetry extension requires PHP 8.0 or later');
1920
}
2021
}
2122

src/SPC/builder/extension/password_argon2.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SPC\builder\extension;
66

77
use SPC\builder\Extension;
8-
use SPC\exception\RuntimeException;
8+
use SPC\exception\ValidationException;
99
use SPC\util\CustomExt;
1010

1111
#[CustomExt('password-argon2')]
@@ -20,7 +20,7 @@ public function runCliCheckUnix(): void
2020
{
2121
[$ret] = shell()->execWithResult(BUILD_ROOT_PATH . '/bin/php -n -r "assert(defined(\'PASSWORD_ARGON2I\'));"');
2222
if ($ret !== 0) {
23-
throw new RuntimeException('extension ' . $this->getName() . ' failed sanity check');
23+
throw new ValidationException('extension ' . $this->getName() . ' failed sanity check', validation_module: 'password_argon2 function check');
2424
}
2525
}
2626

src/SPC/builder/extension/protobuf.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SPC\builder\extension;
66

77
use SPC\builder\Extension;
8+
use SPC\exception\ValidationException;
89
use SPC\util\CustomExt;
910

1011
#[CustomExt('protobuf')]
@@ -13,12 +14,12 @@ class protobuf extends Extension
1314
public function validate(): void
1415
{
1516
if ($this->builder->getPHPVersionID() < 80000 && getenv('SPC_SKIP_PHP_VERSION_CHECK') !== 'yes') {
16-
throw new \RuntimeException('The latest protobuf extension requires PHP 8.0 or later');
17+
throw new ValidationException('The latest protobuf extension requires PHP 8.0 or later');
1718
}
1819
$grpc = $this->builder->getExt('grpc');
1920
// protobuf conflicts with grpc
2021
if ($grpc?->isBuildStatic()) {
21-
throw new \RuntimeException('protobuf conflicts with grpc, please remove grpc or protobuf extension');
22+
throw new ValidationException('protobuf conflicts with grpc, please remove grpc or protobuf extension');
2223
}
2324
}
2425
}

src/SPC/builder/extension/swoole_hook_mysql.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace SPC\builder\extension;
66

77
use SPC\builder\Extension;
8-
use SPC\exception\RuntimeException;
8+
use SPC\exception\ValidationException;
99
use SPC\util\CustomExt;
1010

1111
#[CustomExt('swoole-hook-mysql')]
@@ -32,10 +32,10 @@ public function runCliCheckUnix(): void
3232
[$ret, $out] = shell()->execWithResult(BUILD_ROOT_PATH . '/bin/php -n' . $this->getSharedExtensionLoadString() . ' --ri "swoole"', false);
3333
$out = implode('', $out);
3434
if ($ret !== 0) {
35-
throw new RuntimeException('extension ' . $this->getName() . ' failed compile check: php-cli returned ' . $ret);
35+
throw new ValidationException("extension {$this->getName()} failed compile check: php-cli returned {$ret}", validation_module: 'extension swoole_hook_mysql sanity check');
3636
}
3737
if (!str_contains($out, 'mysqlnd')) {
38-
throw new RuntimeException('swoole mysql hook is not enabled correctly.');
38+
throw new ValidationException('swoole mysql hook is not enabled correctly.', validation_module: 'Extension swoole mysql hook avilability check');
3939
}
4040
}
4141
}

0 commit comments

Comments
 (0)