Refactor package stage handling and update class structures for improved flexibility

This commit is contained in:
crazywhalecc
2025-12-09 14:58:11 +08:00
parent e004d10861
commit 808aed2a66
29 changed files with 416 additions and 115 deletions

View File

@@ -5,10 +5,11 @@ declare(strict_types=1);
namespace StaticPHP\Package;
use StaticPHP\Artifact\Artifact;
use StaticPHP\Artifact\ArtifactLoader;
use StaticPHP\Config\PackageConfig;
use StaticPHP\DI\ApplicationContext;
use StaticPHP\Exception\SPCInternalException;
use StaticPHP\Registry\ArtifactLoader;
use StaticPHP\Registry\PackageLoader;
abstract class Package
{
@@ -32,15 +33,25 @@ abstract class Package
* Run a defined stage of the package.
* If the stage is not defined, an exception should be thrown.
*
* @param string $name Name of the stage to run
* @param array $context Additional context to pass to the stage callback
* @return mixed Based on the stage definition, return the result of the stage
* @param array|callable|string $name Name of the stage to run (can be callable)
* @param array $context Additional context to pass to the stage callback
* @return mixed Based on the stage definition, return the result of the stage
*/
public function runStage(string $name, array $context = []): mixed
public function runStage(mixed $name, array $context = []): mixed
{
if (!isset($this->stages[$name])) {
if (!$this->hasStage($name)) {
$name = match (true) {
is_string($name) => $name,
is_array($name) && count($name) === 2 => $name[1], // use function name
default => '{' . gettype($name) . '}',
};
throw new SPCInternalException("Stage '{$name}' is not defined for package '{$this->name}'.");
}
$name = match (true) {
is_string($name) => $name,
is_array($name) && count($name) === 2 => $name[1], // use function name
default => throw new SPCInternalException('Invalid stage name type: ' . gettype($name)),
};
// Merge package context with provided context
/** @noinspection PhpDuplicateArrayKeysInspection */
@@ -80,9 +91,6 @@ abstract class Package
/**
* Add a stage to the package.
*
* @param string $name Stage name
* @param callable $stage Stage callable
*/
public function addStage(string $name, callable $stage): void
{
@@ -92,11 +100,17 @@ abstract class Package
/**
* Check if the package has a specific stage defined.
*
* @param string $name Stage name
* @param mixed $name Stage name
*/
public function hasStage(string $name): bool
public function hasStage(mixed $name): bool
{
return isset($this->stages[$name]);
if (is_array($name) && count($name) === 2) {
return isset($this->stages[$name[1]]); // use function name
}
if (is_string($name)) {
return isset($this->stages[$name]); // use defined name
}
return false;
}
/**