AspectContext
This commit is contained in:
parent
6050e3bb72
commit
d35c3df06d
8 changed files with 70 additions and 19 deletions
|
|
@ -3,19 +3,25 @@
|
|||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
use IceFox\Aspect\AspectContext;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class BasicAspect
|
||||
{
|
||||
public ?object $object = null;
|
||||
|
||||
public function before(object|string $target, mixed ...$args): void
|
||||
public function __construct(
|
||||
private readonly AspectContext $context,
|
||||
) {
|
||||
}
|
||||
|
||||
public function before(mixed ...$args): void
|
||||
{
|
||||
$this->object = end($args);
|
||||
$this->object->before = true;
|
||||
}
|
||||
|
||||
public function after(object|string $target, mixed $return): mixed
|
||||
public function after(mixed $return): mixed
|
||||
{
|
||||
$this->object->after = true;
|
||||
return $return;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
use IceFox\Aspect\AspectContext;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class ConfigurableAspect
|
||||
|
|
@ -10,13 +11,14 @@ class ConfigurableAspect
|
|||
public static array $executionLog = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly AspectContext $context,
|
||||
public readonly string $prefix = 'default',
|
||||
public readonly int $multiplier = 1,
|
||||
public readonly bool $enabled = true,
|
||||
) {
|
||||
}
|
||||
|
||||
public function before(object|string $target, mixed ...$args): void
|
||||
public function before(mixed ...$args): void
|
||||
{
|
||||
if ($this->enabled) {
|
||||
self::$executionLog[] = [
|
||||
|
|
@ -28,7 +30,7 @@ class ConfigurableAspect
|
|||
}
|
||||
}
|
||||
|
||||
public function after(object|string $target, mixed $result): mixed
|
||||
public function after(mixed $result): mixed
|
||||
{
|
||||
if (!$this->enabled) {
|
||||
return $result;
|
||||
|
|
|
|||
|
|
@ -3,18 +3,24 @@
|
|||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
use IceFox\Aspect\AspectContext;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class LoggingAspect
|
||||
{
|
||||
public static array $logs = [];
|
||||
|
||||
public function before(object|string $target, mixed ...$args): void
|
||||
public function __construct(
|
||||
private readonly AspectContext $context,
|
||||
) {
|
||||
}
|
||||
|
||||
public function before(mixed ...$args): void
|
||||
{
|
||||
self::$logs[] = ['type' => 'logging_before', 'args' => $args];
|
||||
}
|
||||
|
||||
public function after(object|string $target, mixed $result): mixed
|
||||
public function after(mixed $result): mixed
|
||||
{
|
||||
self::$logs[] = ['type' => 'logging_after', 'result' => $result];
|
||||
return $result;
|
||||
|
|
|
|||
|
|
@ -3,13 +3,19 @@
|
|||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
use IceFox\Aspect\AspectContext;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class ModifyingAspect
|
||||
{
|
||||
public static mixed $modifier = null;
|
||||
|
||||
public function after(object|string $target, mixed $result): mixed
|
||||
public function __construct(
|
||||
private readonly AspectContext $context,
|
||||
) {
|
||||
}
|
||||
|
||||
public function after(mixed $result): mixed
|
||||
{
|
||||
if (self::$modifier !== null) {
|
||||
return (self::$modifier)($result);
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
use IceFox\Aspect\AspectContext;
|
||||
use RuntimeException;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
|
|
@ -11,14 +12,19 @@ class ThrowingAspect
|
|||
public static bool $throwInBefore = false;
|
||||
public static bool $throwInAfter = false;
|
||||
|
||||
public function before(object|string $target, mixed ...$args): void
|
||||
public function __construct(
|
||||
private readonly AspectContext $context,
|
||||
) {
|
||||
}
|
||||
|
||||
public function before(mixed ...$args): void
|
||||
{
|
||||
if (self::$throwInBefore) {
|
||||
throw new RuntimeException('Exception thrown in before()');
|
||||
}
|
||||
}
|
||||
|
||||
public function after(object|string $target, mixed $result): mixed
|
||||
public function after(mixed $result): mixed
|
||||
{
|
||||
if (self::$throwInAfter) {
|
||||
throw new RuntimeException('Exception thrown in after()');
|
||||
|
|
|
|||
|
|
@ -3,18 +3,24 @@
|
|||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
use IceFox\Aspect\AspectContext;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class TrackingAspect
|
||||
{
|
||||
public static array $calls = [];
|
||||
|
||||
public function before(object|string $target, mixed ...$args): void
|
||||
public function __construct(
|
||||
private readonly AspectContext $context,
|
||||
) {
|
||||
}
|
||||
|
||||
public function before(mixed ...$args): void
|
||||
{
|
||||
self::$calls[] = ['event' => 'before', 'args' => $args];
|
||||
}
|
||||
|
||||
public function after(object|string $target, mixed $result): mixed
|
||||
public function after(mixed $result): mixed
|
||||
{
|
||||
self::$calls[] = ['event' => 'after', 'result' => $result];
|
||||
return $result;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue