53 lines
1.1 KiB
PHP
53 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Aspects;
|
|
|
|
use Attribute;
|
|
use IceFox\Aspect\AspectContext;
|
|
|
|
#[Attribute(Attribute::TARGET_METHOD)]
|
|
class TrackingAspect
|
|
{
|
|
public static array $calls = [];
|
|
|
|
public function __construct(
|
|
private readonly AspectContext $context,
|
|
) {
|
|
}
|
|
|
|
public function before(array $args): void
|
|
{
|
|
self::$calls[] = ['event' => 'before', 'args' => $args];
|
|
}
|
|
|
|
public function after(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;
|
|
}
|
|
}
|