App.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 1

Klassen 1

Funktionen 6

Versionen 7

Code

<?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 ModelRegistry for deprecated static ModelConfig facade (used in Views)
        $this->container->get(\Infrastructure\AI\ModelRegistry::class);
    }
}
← Übersicht Graph