refactor: unified kernal logic clean up
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||
namespace KTXC\Runtime\Console;
|
||||
|
||||
use KTXC\Application\Execution\ExecutionDescriptor;
|
||||
use KTXC\Application\Execution\ExecutionOutcome;
|
||||
use KTXC\Kernel;
|
||||
use KTXC\KernelInterface;
|
||||
use KTXC\Module\ModuleManager;
|
||||
@@ -24,57 +23,37 @@ final class ConsoleRuntime
|
||||
) {
|
||||
}
|
||||
|
||||
public function run(
|
||||
?InputInterface $input = null,
|
||||
?OutputInterface $output = null,
|
||||
): int
|
||||
public function run(?InputInterface $input = null, ?OutputInterface $output = null): int
|
||||
{
|
||||
$scope = $this->kernel->beginExecution(ExecutionDescriptor::cli());
|
||||
$outcome = ExecutionOutcome::incomplete();
|
||||
return $this->kernel->executionRunner()->execute(
|
||||
ExecutionDescriptor::cli(),
|
||||
function () use ($input, $output): int {
|
||||
$container = $this->kernel->container();
|
||||
$console = new ConsoleApplication('Vallarx Console', Kernel::VERSION);
|
||||
$console->setAutoExit(false);
|
||||
|
||||
try {
|
||||
$exitCode = $this->application()->run($input, $output);
|
||||
$outcome = ExecutionOutcome::success($exitCode);
|
||||
/** @var ModuleManager $moduleManager */
|
||||
$moduleManager = $container->get(ModuleManager::class);
|
||||
foreach ($moduleManager->list() as $module) {
|
||||
$instance = $module->instance();
|
||||
if (!$instance instanceof ModuleConsoleInterface) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $exitCode;
|
||||
} catch (\Throwable $error) {
|
||||
$outcome = ExecutionOutcome::failure($error);
|
||||
throw $error;
|
||||
} finally {
|
||||
$this->kernel->terminateExecution($scope, $outcome);
|
||||
}
|
||||
}
|
||||
foreach ($instance->registerCI() as $commandClass) {
|
||||
$this->registerCommand($console, $container, $commandClass);
|
||||
}
|
||||
}
|
||||
|
||||
private function application(): ConsoleApplication
|
||||
{
|
||||
$container = $this->kernel->container();
|
||||
$console = new ConsoleApplication('Vallarx Console', Kernel::VERSION);
|
||||
$console->setAutoExit(false);
|
||||
|
||||
/** @var ModuleManager $moduleManager */
|
||||
$moduleManager = $container->get(ModuleManager::class);
|
||||
foreach ($moduleManager->list() as $module) {
|
||||
$instance = $module->instance();
|
||||
if (!$instance instanceof ModuleConsoleInterface) {
|
||||
continue;
|
||||
return $console->run($input, $output);
|
||||
}
|
||||
|
||||
foreach ($instance->registerCI() as $commandClass) {
|
||||
$this->registerLazyCommand($console, $container, $commandClass);
|
||||
}
|
||||
}
|
||||
|
||||
return $console;
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string $commandClass
|
||||
*/
|
||||
private function registerLazyCommand(
|
||||
ConsoleApplication $console,
|
||||
ContainerInterface $container,
|
||||
string $commandClass,
|
||||
): void {
|
||||
private function registerCommand(ConsoleApplication $console, ContainerInterface $container, string $commandClass): void {
|
||||
if (!class_exists($commandClass)) {
|
||||
throw new \RuntimeException("Command class not found: {$commandClass}");
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||
namespace KTXC\Runtime\Http;
|
||||
|
||||
use KTXC\Application\Execution\ExecutionDescriptor;
|
||||
use KTXC\Application\Execution\ExecutionOutcome;
|
||||
use KTXC\Http\Middleware\AuthenticationMiddleware;
|
||||
use KTXC\Http\Middleware\FirewallMiddleware;
|
||||
use KTXC\Http\Middleware\MiddlewarePipeline;
|
||||
@@ -23,48 +22,31 @@ final class HttpRuntime
|
||||
) {
|
||||
}
|
||||
|
||||
public function run(?Request $request = null): Response
|
||||
public function run(?Request $request = null, bool $send = true): Response
|
||||
{
|
||||
return $this->execute(
|
||||
$request ?? Request::createFromGlobals(),
|
||||
send: true,
|
||||
$request ??= Request::createFromGlobals();
|
||||
|
||||
return $this->kernel->executionRunner()->execute(
|
||||
ExecutionDescriptor::http(),
|
||||
function () use ($request, $send): Response {
|
||||
$response = $this->pipeline()->handle($request);
|
||||
if ($send) {
|
||||
$response->send();
|
||||
}
|
||||
|
||||
return $response;
|
||||
},
|
||||
function (\Throwable $error) use ($send): Response {
|
||||
$response = $this->errorResponse($error);
|
||||
if ($send) {
|
||||
$response->send();
|
||||
}
|
||||
|
||||
return $response;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
return $this->execute($request, send: false);
|
||||
}
|
||||
|
||||
private function execute(Request $request, bool $send): Response
|
||||
{
|
||||
$scope = null;
|
||||
$outcome = ExecutionOutcome::incomplete();
|
||||
|
||||
try {
|
||||
$scope = $this->kernel->beginExecution(ExecutionDescriptor::http());
|
||||
$response = $this->pipeline()->handle($request);
|
||||
$outcome = ExecutionOutcome::success($response);
|
||||
if ($send) {
|
||||
$response->send();
|
||||
}
|
||||
|
||||
return $response;
|
||||
} catch (\Throwable $error) {
|
||||
$response = $this->errorResponse($error);
|
||||
$outcome = ExecutionOutcome::failure($error, $response);
|
||||
if ($send) {
|
||||
$response->send();
|
||||
}
|
||||
|
||||
return $response;
|
||||
} finally {
|
||||
if ($scope !== null) {
|
||||
$this->kernel->terminateExecution($scope, $outcome);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function pipeline(): MiddlewarePipeline
|
||||
{
|
||||
$pipeline = new MiddlewarePipeline($this->kernel->container());
|
||||
|
||||
Reference in New Issue
Block a user