implements decorators pipeline

This commit is contained in:
Fabio Ivona
2021-10-08 15:29:35 +02:00
parent d802e88148
commit e92d9bfaae
5 changed files with 202 additions and 10 deletions

View File

@ -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.
*