60 lines
1.4 KiB
PHP
60 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Aspects;
|
|
|
|
use Attribute;
|
|
|
|
#[Attribute(Attribute::TARGET_METHOD)]
|
|
class ConfigurableAspect
|
|
{
|
|
public static array $executionLog = [];
|
|
|
|
public function __construct(
|
|
public readonly string $prefix = 'default',
|
|
public readonly int $multiplier = 1,
|
|
public readonly bool $enabled = true,
|
|
) {
|
|
}
|
|
|
|
public function before(object|string $target, mixed ...$args): void
|
|
{
|
|
if ($this->enabled) {
|
|
self::$executionLog[] = [
|
|
'event' => 'before',
|
|
'prefix' => $this->prefix,
|
|
'multiplier' => $this->multiplier,
|
|
'args' => $args,
|
|
];
|
|
}
|
|
}
|
|
|
|
public function after(object|string $target, mixed $result): mixed
|
|
{
|
|
if (!$this->enabled) {
|
|
return $result;
|
|
}
|
|
|
|
self::$executionLog[] = [
|
|
'event' => 'after',
|
|
'prefix' => $this->prefix,
|
|
'multiplier' => $this->multiplier,
|
|
'result' => $result,
|
|
];
|
|
|
|
// Modify result based on constructor parameters
|
|
if (is_int($result)) {
|
|
return $result * $this->multiplier;
|
|
}
|
|
|
|
if (is_string($result)) {
|
|
return $this->prefix . $result;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function reset(): void
|
|
{
|
|
self::$executionLog = [];
|
|
}
|
|
}
|