Add PatchBeforeBuild attribute

This commit is contained in:
crazywhalecc
2026-02-02 16:15:25 +08:00
parent 82ab14165e
commit 3d102363c4
3 changed files with 28 additions and 10 deletions

View File

@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace StaticPHP\Attribute\Package;
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class PatchBeforeBuild {}

View File

@@ -16,7 +16,7 @@ trait PackageCallbacksTrait
protected mixed $validate_callback = null; protected mixed $validate_callback = null;
protected mixed $patch_before_build_callback = null; protected mixed $patch_before_build_callbacks = null;
public function setInfoCallback(callable $callback): void public function setInfoCallback(callable $callback): void
{ {
@@ -48,9 +48,9 @@ trait PackageCallbacksTrait
$this->validate_callback = $callback; $this->validate_callback = $callback;
} }
public function setPatchBeforeBuildCallback(callable $callback): void public function addPatchBeforeBuildCallback(callable $callback): void
{ {
$this->patch_before_build_callback = $callback; $this->patch_before_build_callbacks[] = $callback;
} }
public function patchBeforeBuild(): void public function patchBeforeBuild(): void
@@ -58,16 +58,18 @@ trait PackageCallbacksTrait
if (file_exists("{$this->getSourceDir()}/.spc-patched")) { if (file_exists("{$this->getSourceDir()}/.spc-patched")) {
return; return;
} }
if ($this->patch_before_build_callback === null) { if ($this->patch_before_build_callbacks === null) {
return; return;
} }
// Use CallbackInvoker with current package as context // Use CallbackInvoker with current package as context
$ret = ApplicationContext::invoke($this->patch_before_build_callback, [ foreach ($this->patch_before_build_callbacks as $callback) {
Package::class => $this, $ret = ApplicationContext::invoke($callback, [
static::class => $this, Package::class => $this,
]); static::class => $this,
if ($ret === true) { ]);
FileSystem::writeFile("{$this->getSourceDir()}/.spc-patched", 'PATCHED!!!'); if ($ret === true) {
FileSystem::writeFile("{$this->getSourceDir()}/.spc-patched", 'PATCHED!!!');
}
} }
} }

View File

@@ -12,6 +12,7 @@ use StaticPHP\Attribute\Package\Extension;
use StaticPHP\Attribute\Package\Info; use StaticPHP\Attribute\Package\Info;
use StaticPHP\Attribute\Package\InitPackage; use StaticPHP\Attribute\Package\InitPackage;
use StaticPHP\Attribute\Package\Library; use StaticPHP\Attribute\Package\Library;
use StaticPHP\Attribute\Package\PatchBeforeBuild;
use StaticPHP\Attribute\Package\ResolveBuild; use StaticPHP\Attribute\Package\ResolveBuild;
use StaticPHP\Attribute\Package\Stage; use StaticPHP\Attribute\Package\Stage;
use StaticPHP\Attribute\Package\Target; use StaticPHP\Attribute\Package\Target;
@@ -196,6 +197,8 @@ class PackageLoader
match ($method_attribute->getName()) { match ($method_attribute->getName()) {
// #[BuildFor(PHP_OS_FAMILY)] // #[BuildFor(PHP_OS_FAMILY)]
BuildFor::class => self::addBuildFunction($pkg, $method_instance, [$instance_class, $method->getName()]), BuildFor::class => self::addBuildFunction($pkg, $method_instance, [$instance_class, $method->getName()]),
// #[BeforeBuild]
PatchBeforeBuild::class => self::addPatchBeforeBuildFunction($pkg, [$instance_class, $method->getName()]),
// #[CustomPhpConfigureArg(PHP_OS_FAMILY)] // #[CustomPhpConfigureArg(PHP_OS_FAMILY)]
CustomPhpConfigureArg::class => self::bindCustomPhpConfigureArg($pkg, $method_attribute->newInstance(), [$instance_class, $method->getName()]), CustomPhpConfigureArg::class => self::bindCustomPhpConfigureArg($pkg, $method_attribute->newInstance(), [$instance_class, $method->getName()]),
// #[Stage('stage_name')] // #[Stage('stage_name')]
@@ -332,6 +335,11 @@ class PackageLoader
$pkg->addBuildFunction($attr->os, $fn); $pkg->addBuildFunction($attr->os, $fn);
} }
private static function addPatchBeforeBuildFunction(Package $pkg, callable $fn): void
{
$pkg->addPatchBeforeBuildCallback($fn);
}
private static function addStage(\ReflectionMethod $method, Package $pkg, object $instance_class, object $method_instance): void private static function addStage(\ReflectionMethod $method, Package $pkg, object $instance_class, object $method_instance): void
{ {
$name = $method_instance->function; $name = $method_instance->function;