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 handleHttp(Request $request): Response { return $this->http->handle($request); } 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; } }