Files
michaelschiemer/src/Framework/Attributes/BeforeExecute.php
2025-11-24 21:28:25 +01:00

78 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Attributes;
use App\Framework\Attributes\Execution\AttributeExecutionContext;
use App\Framework\Attributes\Execution\CallbackExecutor;
use App\Framework\Attributes\Execution\CallbackMetadata;
use App\Framework\Attributes\Execution\CallbackMetadataExtractor;
use App\Framework\Attributes\Execution\ExecutableAttribute;
use Attribute;
use Closure;
/**
* BeforeExecute Attribute - Führt Logik VOR Handler-Ausführung aus
*
* Unterstützt alle drei Patterns:
* - Pattern A: Handler-Klasse: #[BeforeExecute(ValidationHandler::class)]
* - Pattern B: First-Class Callable: #[BeforeExecute(Validators::validateInput(...))]
* - Pattern C: Closure-Factory: #[BeforeExecute(Hooks::validateCommand())]
*
* Wird vor der Handler-Methode ausgeführt. Kann die Ausführung abbrechen.
*/
#[Attribute(Attribute::TARGET_METHOD)]
final readonly class BeforeExecute implements ExecutableAttribute
{
private ?CallbackMetadata $callbackMetadata;
public function __construct(
private Closure $handler
) {
// Extrahiere Metadata für Cache-Kompatibilität
$extractor = new CallbackMetadataExtractor();
try {
$this->callbackMetadata = $extractor->extract($handler);
} catch (\InvalidArgumentException) {
// Fallback: Closure (nicht cachebar)
$this->callbackMetadata = null;
}
}
public function execute(AttributeExecutionContext $context): mixed
{
$callbackExecutor = $context->container->get(CallbackExecutor::class);
// Wenn Metadata vorhanden, nutze sie für Ausführung
if ($this->callbackMetadata !== null) {
$result = $callbackExecutor->execute($this->callbackMetadata, $context);
// Wenn Factory-Pattern, führe Closure aus
if ($result instanceof Closure) {
return $result($context);
}
return $result;
}
// Fallback: Direkte Closure-Ausführung (nicht empfohlen)
if ($this->handler instanceof Closure) {
return ($this->handler)($context);
}
throw new \RuntimeException(
'BeforeExecute callback must be a Handler class, First-Class Callable, or Factory. Direct closures are not supported.'
);
}
/**
* Gibt die Callback-Metadata zurück (für Cache-Serialisierung)
*/
public function getCallbackMetadata(): ?CallbackMetadata
{
return $this->callbackMetadata;
}
}