<?php
declare(strict_types=1);
namespace Framework;
// @responsibility: Applikations-Bootstrap und Service-Registrierung
use Framework\Middleware\CorrelationMiddleware;
class App
{
protected Router $router;
protected Container $container;
public function __construct()
{
$this->container = new Container();
$this->router = new Router();
$this->router->setContainer($this->container);
// Register core services
$this->registerServices();
}
public function run(): void
{
// Run middleware chain
$middleware = new CorrelationMiddleware();
$middleware->handle(fn () => $this->dispatch());
}
/**
* Dispatch the request to the router.
*/
protected function dispatch(): void
{
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$this->router->dispatch($uri ?? '/', $method);
}
public function router(): Router
{
return $this->router;
}
public function container(): Container
{
return $this->container;
}
/**
* Register core services in the container.
*
* Explicit registrations override autowiring.
* Services without registration will be autowired.
*/
protected function registerServices(): void
{
// Load service definitions if file exists
$servicesFile = dirname(__DIR__) . '/services.php';
if (file_exists($servicesFile)) {
$configure = require $servicesFile;
if (is_callable($configure)) {
$configure($this->container);
}
}
// Eager-load services that use static facades
$this->initializeEagerServices();
}
/**
* Initialize services that must be loaded before any request handling.
* Required for static facades like ModelConfig that depend on singletons.
*/
protected function initializeEagerServices(): void
{
// ModelRegistry must be initialized for ModelConfig static methods
$this->container->get(\Infrastructure\AI\ModelRegistry::class);
}
}