eb99eb5a2e
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
155 lines
3.6 KiB
PHP
155 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXC;
|
|
|
|
use Composer\Autoload\ClassLoader;
|
|
use KTXC\Application\KernelOptions;
|
|
use KTXC\Application\ProjectPaths;
|
|
use KTXC\Http\Request\Request;
|
|
use KTXC\Http\Response\Response;
|
|
use KTXC\Module\ModuleAutoloader;
|
|
use KTXC\Runtime\Console\ConsoleRuntime;
|
|
use KTXC\Runtime\Http\HttpRuntime;
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
final class Application
|
|
{
|
|
private readonly ProjectPaths $paths;
|
|
private readonly array $config;
|
|
private readonly Kernel $kernel;
|
|
private readonly HttpRuntime $http;
|
|
private readonly ConsoleRuntime $console;
|
|
|
|
public function __construct(
|
|
string $projectDir,
|
|
?ClassLoader $composerLoader = null,
|
|
?string $environment = null,
|
|
?bool $debug = null,
|
|
) {
|
|
$this->paths = ProjectPaths::resolve($projectDir);
|
|
$this->config = $this->loadConfig();
|
|
|
|
$environment ??= $this->config['environment'] ?? 'prod';
|
|
$debug ??= (bool) ($this->config['debug'] ?? false);
|
|
|
|
$this->kernel = new Kernel(
|
|
new KernelOptions(
|
|
$this->paths,
|
|
$environment,
|
|
$debug,
|
|
),
|
|
$this->config,
|
|
);
|
|
|
|
(new ModuleAutoloader(
|
|
$this->moduleDir(),
|
|
$composerLoader,
|
|
))->register();
|
|
|
|
$this->http = new HttpRuntime($this->kernel, $debug);
|
|
$this->console = new ConsoleRuntime($this->kernel);
|
|
}
|
|
|
|
public static function create(
|
|
string $projectDir,
|
|
?ClassLoader $composerLoader = null,
|
|
?string $environment = null,
|
|
?bool $debug = null,
|
|
): self {
|
|
return new self($projectDir, $composerLoader, $environment, $debug);
|
|
}
|
|
|
|
public function runHttp(): void
|
|
{
|
|
try {
|
|
$this->http->run();
|
|
} finally {
|
|
$this->kernel->shutdown();
|
|
}
|
|
}
|
|
|
|
public function runHttpRequest(Request $request): Response
|
|
{
|
|
return $this->http->run($request, send: false);
|
|
}
|
|
|
|
public function runConsole(): int
|
|
{
|
|
try {
|
|
return $this->console->run();
|
|
} finally {
|
|
$this->kernel->shutdown();
|
|
}
|
|
}
|
|
|
|
public function shutdown(): void
|
|
{
|
|
$this->kernel->shutdown();
|
|
}
|
|
|
|
public function kernel(): KernelInterface
|
|
{
|
|
return $this->kernel;
|
|
}
|
|
|
|
public function container(): ContainerInterface
|
|
{
|
|
return $this->kernel->container();
|
|
}
|
|
|
|
public function environment(): string
|
|
{
|
|
return $this->kernel->environment();
|
|
}
|
|
|
|
public function debug(): bool
|
|
{
|
|
return $this->kernel->debug();
|
|
}
|
|
|
|
public function projectDir(): string
|
|
{
|
|
return $this->paths->project;
|
|
}
|
|
|
|
public function moduleDir(): string
|
|
{
|
|
return $this->paths->modules();
|
|
}
|
|
|
|
public function config(?string $key = null, mixed $default = null): mixed
|
|
{
|
|
if ($key === null) {
|
|
return $this->config;
|
|
}
|
|
|
|
$value = $this->config;
|
|
foreach (explode('.', $key) as $part) {
|
|
if (!is_array($value) || !array_key_exists($part, $value)) {
|
|
return $default;
|
|
}
|
|
$value = $value[$part];
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
private function loadConfig(): array
|
|
{
|
|
$path = $this->paths->configuration() . '/system.php';
|
|
if (!is_file($path)) {
|
|
return [];
|
|
}
|
|
|
|
$config = require $path;
|
|
if (!is_array($config)) {
|
|
throw new \RuntimeException("Configuration file must return an array: {$path}");
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
}
|