diff --git a/src/AspectContext.php b/src/AspectContext.php new file mode 100644 index 0000000..45232a3 --- /dev/null +++ b/src/AspectContext.php @@ -0,0 +1,13 @@ + $aspectMetadata) { $aspectClass = $aspectMetadata->aspectClass; @@ -252,12 +260,11 @@ class AspectWeaver $args = $attribute->getArguments(); $aspectConstructorArguments = $this->buildConstructorArguments($args); - $code .= " \$aspect{$index} = new \\{$aspectClass}({$aspectConstructorArguments});\n"; + // Pass context as first parameter before other constructor arguments + $constructorParams = $aspectConstructorArguments ? "\$context, {$aspectConstructorArguments}" : "\$context"; + $code .= " \$aspect{$index} = new \\{$aspectClass}({$constructorParams});\n"; } - // Prepare target argument for aspect methods - $targetArg = $isStatic ? "static::class" : "\$this"; - // Call all before methods in a single try-catch $code .= " \$currentAspect = '';\n"; $code .= " try {\n"; @@ -266,8 +273,7 @@ class AspectWeaver $aspectReflection = new ReflectionClass($aspectClass); if ($aspectReflection->hasMethod('before')) { $code .= " \$currentAspect = \\{$aspectClass}::class;\n"; - $beforeParams = $paramNames ? "{$targetArg}, {$paramNames}" : $targetArg; - $code .= " \$aspect{$index}->before({$beforeParams});\n"; + $code .= " \$aspect{$index}->before({$paramNames});\n"; } } $code .= " } catch (\\Throwable \$e) {\n"; @@ -291,7 +297,7 @@ class AspectWeaver $aspectReflection = new ReflectionClass($aspectClass); if ($aspectReflection->hasMethod('after')) { $code .= " \$currentAspect = \\{$aspectClass}::class;\n"; - $code .= " \$result = \$aspect{$i}->after({$targetArg}, \$result);\n"; + $code .= " \$result = \$aspect{$i}->after(\$result);\n"; } } $code .= " } catch (\\Throwable \$e) {\n"; @@ -315,7 +321,7 @@ class AspectWeaver $aspectReflection = new ReflectionClass($aspectClass); if ($aspectReflection->hasMethod('after')) { $code .= " \$currentAspect = \\{$aspectClass}::class;\n"; - $code .= " \$aspect{$i}->after({$targetArg}, null);\n"; + $code .= " \$aspect{$i}->after(null);\n"; } } $code .= " } catch (\\Throwable \$e) {\n"; diff --git a/tests/Aspects/BasicAspect.php b/tests/Aspects/BasicAspect.php index 84abeb6..303d476 100644 --- a/tests/Aspects/BasicAspect.php +++ b/tests/Aspects/BasicAspect.php @@ -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; diff --git a/tests/Aspects/ConfigurableAspect.php b/tests/Aspects/ConfigurableAspect.php index 38a12a9..4774a45 100644 --- a/tests/Aspects/ConfigurableAspect.php +++ b/tests/Aspects/ConfigurableAspect.php @@ -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; diff --git a/tests/Aspects/LoggingAspect.php b/tests/Aspects/LoggingAspect.php index 70fa4c5..b10c8c9 100644 --- a/tests/Aspects/LoggingAspect.php +++ b/tests/Aspects/LoggingAspect.php @@ -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; diff --git a/tests/Aspects/ModifyingAspect.php b/tests/Aspects/ModifyingAspect.php index 90375f2..488c8af 100644 --- a/tests/Aspects/ModifyingAspect.php +++ b/tests/Aspects/ModifyingAspect.php @@ -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); diff --git a/tests/Aspects/ThrowingAspect.php b/tests/Aspects/ThrowingAspect.php index 61faf18..16fdeba 100644 --- a/tests/Aspects/ThrowingAspect.php +++ b/tests/Aspects/ThrowingAspect.php @@ -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()'); diff --git a/tests/Aspects/TrackingAspect.php b/tests/Aspects/TrackingAspect.php index fc84435..daccbfb 100644 --- a/tests/Aspects/TrackingAspect.php +++ b/tests/Aspects/TrackingAspect.php @@ -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;