This commit is contained in:
icefox 2025-12-22 17:54:16 -03:00
commit a5ce423afe
No known key found for this signature in database
30 changed files with 1807 additions and 0 deletions

View file

@ -0,0 +1,23 @@
<?php
namespace Tests\Aspects;
use Attribute;
#[Attribute(Attribute::TARGET_METHOD)]
class BasicAspect
{
public ?object $object = null;
public function before(object|string $target, mixed ...$args): void
{
$this->object = end($args);
$this->object->before = true;
}
public function after(object|string $target, mixed $return): mixed
{
$this->object->after = true;
return $return;
}
}

View file

@ -0,0 +1,60 @@
<?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 = [];
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace Tests\Aspects;
use Attribute;
#[Attribute(Attribute::TARGET_METHOD)]
class LoggingAspect
{
public static array $logs = [];
public function before(object|string $target, mixed ...$args): void
{
self::$logs[] = ['type' => 'logging_before', 'args' => $args];
}
public function after(object|string $target, mixed $result): mixed
{
self::$logs[] = ['type' => 'logging_after', 'result' => $result];
return $result;
}
public static function clearLogs(): void
{
self::$logs = [];
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace Tests\Aspects;
use Attribute;
#[Attribute(Attribute::TARGET_METHOD)]
class ModifyingAspect
{
public static mixed $modifier = null;
public function after(object|string $target, mixed $result): mixed
{
if (self::$modifier !== null) {
return (self::$modifier)($result);
}
return $result; // Return original value when not modifying
}
public static function reset(): void
{
self::$modifier = null;
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace Tests\Aspects;
use Attribute;
use RuntimeException;
#[Attribute(Attribute::TARGET_METHOD)]
class ThrowingAspect
{
public static bool $throwInBefore = false;
public static bool $throwInAfter = false;
public function before(object|string $target, mixed ...$args): void
{
if (self::$throwInBefore) {
throw new RuntimeException('Exception thrown in before()');
}
}
public function after(object|string $target, mixed $result): mixed
{
if (self::$throwInAfter) {
throw new RuntimeException('Exception thrown in after()');
}
return $result;
}
public static function reset(): void
{
self::$throwInBefore = false;
self::$throwInAfter = false;
}
}

View file

@ -0,0 +1,47 @@
<?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;
}
}