container = new Container(); $this->router = new Router(); $this->middleware = new Middleware(); $this->security = new Security(); // Set the global container so helpers can use it app_set_container($this->container); $this->registerServices(); $this->loadRoutes(); $this->setupMiddleware(); } /** * Run the application */ public function run(): void { // Start session if (session_status() === PHP_SESSION_NONE) { session_start(); } // Initialize security $this->security->initialize(); // Get request method and URI $method = $_SERVER['REQUEST_METHOD'] ?? 'GET'; $uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH); // Run middleware pipeline $this->middleware->run($method, $uri); // Route the request $route = $this->router->match($method, $uri); if (!$route) { $this->handleNotFound(); return; } // Execute controller $this->executeController($route); } /** * Register services in container */ private function registerServices(): void { $this->container->singleton('request', function () { return new Request(); }); $this->container->singleton('response', function () { return new Response(); }); $this->container->singleton('view', function () { return new View(); }); $this->container->singleton('security', function () { return $this->security; }); } /** * Load routes from all modules */ private function loadRoutes(): void { $modulesPath = __DIR__ . '/../Modules'; if (is_dir($modulesPath)) { $modules = scandir($modulesPath); foreach ($modules as $module) { if ($module === '.' || $module === '..') continue; $routesFile = $modulesPath . '/' . $module . '/routes.php'; if (file_exists($routesFile)) { // Pass router instance to routes file $router = $this->router; require $routesFile; } } } } /** * Setup default middleware stack */ private function setupMiddleware(): void { $this->middleware->add(new \App\Core\Middleware\SecurityMiddleware()); $this->middleware->add(new \App\Core\Middleware\CsrfMiddleware()); } /** * Execute controller method */ private function executeController(array $route): void { [$controllerClass, $method] = explode('@', $route['handler']); // Normalize controller class to fully-qualified name if (!str_contains($controllerClass, '\\')) { // No backslash provided → assume default Controller in module $controllerClass = "App\\Modules\\{$route['module']}\\Controller"; } else { // Has backslash but may be relative like "Home\\Controller" if (strpos($controllerClass, 'App\\') !== 0) { $segments = explode('\\', $controllerClass); $moduleName = $segments[0] ?? $route['module']; $className = end($segments); $controllerClass = "App\\Modules\\{$moduleName}\\{$className}"; } } if (!class_exists($controllerClass)) { $this->handleNotFound(); return; } $controller = new $controllerClass(); if (!method_exists($controller, $method)) { $this->handleNotFound(); return; } // Inject dependencies $this->container->inject($controller); // Execute method $result = $controller->$method(); // Handle response if ($result instanceof Response) { $result->send(); } elseif (is_array($result) || is_object($result)) { $response = $this->container->get('response'); $response->json($result)->send(); } else { echo $result; } } /** * Handle 404 Not Found */ private function handleNotFound(): void { try { $errorController = new \App\Modules\Error\Controller(); $errorController->notFound(); } catch (\Throwable $e) { // Fallback to basic 404 http_response_code(404); echo "\n"; echo "\n\n"; echo "404 - Page Not Found\n"; echo "\n"; echo "\n"; echo "\n\n"; echo "
\n"; echo "
\n"; echo "
\n"; echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "
\n"; echo "

404

\n"; echo "

Page Not Found

\n"; echo "

The page you are looking for could not be found.

\n"; echo "
\n"; echo "Return to Home\n"; echo "\n"; echo "
\n"; echo "
If you believe this is an error, please contact support.
\n"; echo "
\n"; echo "
\n"; echo "
\n"; echo "\n\n"; } } }