mirror of
https://github.com/pestphp/pest.git
synced 2026-03-08 00:37:22 +01:00
implements decorators pipeline
This commit is contained in:
@ -17,6 +17,9 @@ trait Extendable
|
||||
*/
|
||||
private static $extends = [];
|
||||
|
||||
/** @var array<string, array<Closure>> */
|
||||
private static $decorators = [];
|
||||
|
||||
/**
|
||||
* Register a custom extend.
|
||||
*/
|
||||
@ -25,6 +28,11 @@ trait Extendable
|
||||
static::$extends[$name] = $extend;
|
||||
}
|
||||
|
||||
public static function decorate(string $name, Closure $decorator): void
|
||||
{
|
||||
static::$decorators[$name][] = $decorator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if extend is registered.
|
||||
*/
|
||||
@ -33,6 +41,32 @@ trait Extendable
|
||||
return array_key_exists($name, static::$extends);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if decorator are registered.
|
||||
*/
|
||||
public static function hasDecorators(string $name): bool
|
||||
{
|
||||
return array_key_exists($name, static::$decorators);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, Closure>
|
||||
*/
|
||||
public function decorators(string $name, object $context, string $scope): array
|
||||
{
|
||||
if (!self::hasDecorators($name)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$decorators = [];
|
||||
foreach (self::$decorators[$name] as $decorator) {
|
||||
//@phpstan-ignore-next-line
|
||||
$decorators[] = $decorator->bindTo($context, $scope);
|
||||
}
|
||||
|
||||
return $decorators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically handle calls to the class.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user