<?php
namespace App\Controllers;

use App\Core\Controller;

class ErrorController extends Controller {
    
    public function notFound() {
        header("HTTP/1.0 404 Not Found");
        $this->renderError('errors/404', ['title' => 'Page Not Found']);
    }
    
    public function forbidden() {
        header("HTTP/1.0 403 Forbidden");
        $this->renderError('errors/403', ['title' => 'Access Denied']);
    }
    
    public function serverError() {
        header("HTTP/1.0 500 Internal Server Error");
        $this->renderError('errors/500', ['title' => 'Server Error']);
    }
    
    private function renderError($view, $data = []) {
        extract($data);
        $viewFile = APP_PATH . '/views/' . str_replace('.', '/', $view) . '.php';
        if (file_exists($viewFile)) {
            include $viewFile;
        } else {
            echo "<h1>Error</h1><p>Page not found.</p>";
        }
        exit;
    }
}