47 lines
1 KiB
PHP
47 lines
1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Aspects;
|
|
|
|
use Attribute;
|
|
|
|
#[Attribute(Attribute::TARGET_METHOD)]
|
|
class TrackingAspect
|
|
{
|
|
public static array $calls = [];
|
|
|
|
public function before(object|string $target, mixed ...$args): void
|
|
{
|
|
self::$calls[] = ['event' => 'before', 'args' => $args];
|
|
}
|
|
|
|
public function after(object|string $target, mixed $result): mixed
|
|
{
|
|
self::$calls[] = ['event' => 'after', 'result' => $result];
|
|
return $result;
|
|
}
|
|
|
|
public static function clearCalls(): void
|
|
{
|
|
self::$calls = [];
|
|
}
|
|
|
|
public static function getLastBefore(): ?array
|
|
{
|
|
foreach (array_reverse(self::$calls) as $call) {
|
|
if ($call['event'] === 'before') {
|
|
return $call;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function getLastAfter(): ?array
|
|
{
|
|
foreach (array_reverse(self::$calls) as $call) {
|
|
if ($call['event'] === 'after') {
|
|
return $call;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|