eb99eb5a2e
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
51 lines
1.3 KiB
PHP
51 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXC\Application\Execution;
|
|
|
|
use KTXC\KernelInterface;
|
|
|
|
final readonly class ExecutionRunner implements ExecutionRunnerInterface
|
|
{
|
|
public function __construct(
|
|
private KernelInterface $kernel,
|
|
) {
|
|
}
|
|
|
|
public function execute(
|
|
ExecutionDescriptor $descriptor,
|
|
callable $execution,
|
|
?callable $failure = null,
|
|
): mixed {
|
|
$scope = $this->kernel->beginExecution($descriptor);
|
|
$outcome = ExecutionOutcome::incomplete();
|
|
|
|
try {
|
|
$result = $execution($scope);
|
|
$outcome = ExecutionOutcome::success($result);
|
|
|
|
return $result;
|
|
} catch (\Throwable $error) {
|
|
if ($failure === null) {
|
|
$outcome = ExecutionOutcome::failure($error);
|
|
|
|
throw $error;
|
|
}
|
|
|
|
try {
|
|
$result = $failure($error, $scope);
|
|
$outcome = ExecutionOutcome::failure($error, $result);
|
|
|
|
return $result;
|
|
} catch (\Throwable $failureError) {
|
|
$outcome = ExecutionOutcome::failure($failureError);
|
|
|
|
throw $failureError;
|
|
}
|
|
} finally {
|
|
$this->kernel->terminateExecution($scope, $outcome);
|
|
}
|
|
}
|
|
}
|