Add PatchBeforeBuild attribute

This commit is contained in:
crazywhalecc 2026-02-02 16:15:25 +08:00
parent 82ab14165e
commit 3d102363c4
No known key found for this signature in database
GPG Key ID: 1F4BDD59391F2680
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 $patch_before_build_callback = null;
protected mixed $patch_before_build_callbacks = null;
public function setInfoCallback(callable $callback): void
{
@ -48,9 +48,9 @@ trait PackageCallbacksTrait
$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
@ -58,16 +58,18 @@ trait PackageCallbacksTrait
if (file_exists("{$this->getSourceDir()}/.spc-patched")) {
return;
}
if ($this->patch_before_build_callback === null) {
if ($this->patch_before_build_callbacks === null) {
return;
}
// Use CallbackInvoker with current package as context
$ret = ApplicationContext::invoke($this->patch_before_build_callback, [
Package::class => $this,
static::class => $this,
]);
if ($ret === true) {
FileSystem::writeFile("{$this->getSourceDir()}/.spc-patched", 'PATCHED!!!');
foreach ($this->patch_before_build_callbacks as $callback) {
$ret = ApplicationContext::invoke($callback, [
Package::class => $this,
static::class => $this,
]);
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\InitPackage;
use StaticPHP\Attribute\Package\Library;
use StaticPHP\Attribute\Package\PatchBeforeBuild;
use StaticPHP\Attribute\Package\ResolveBuild;
use StaticPHP\Attribute\Package\Stage;
use StaticPHP\Attribute\Package\Target;
@ -196,6 +197,8 @@ class PackageLoader
match ($method_attribute->getName()) {
// #[BuildFor(PHP_OS_FAMILY)]
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::class => self::bindCustomPhpConfigureArg($pkg, $method_attribute->newInstance(), [$instance_class, $method->getName()]),
// #[Stage('stage_name')]
@ -332,6 +335,11 @@ class PackageLoader
$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
{
$name = $method_instance->function;