<?php

namespace Korkz\Demirteknoloji\Router;
class View
{
    protected static array $data = [];

    public static function bind($key, $value): void
    {
        static::$data[$key] = $value;
    }

    public static function render(string $view, array $layout = []): void
    {
        if (str_starts_with($_SERVER["HTTP_HOST"], "localhost")) {
            $dirPath = $_SERVER["DOCUMENT_ROOT"] . "/demirteknoloji/views/";
        } else {
            $dirPath = $_SERVER["DOCUMENT_ROOT"] . "/views/";
        }
        extract(static::$data);
        $path = $dirPath . $view . '.php';
        if (isset($layout) && !empty($layout["header"]) && !empty($layout["footer"])) {
            $header = $dirPath . $layout["header"] . '.php';
            $footer = $dirPath . $layout["footer"] . '.php';
        }
        if (file_exists($path)) {
            if (isset($layout) && !empty($layout["header"]) && !empty($layout["footer"])) {
                require_once $header;
                require_once $path;
                require_once $footer;
            } else {
                require_once $path;
            }

        } else {
            echo 'View not found: ' . $path;
        }
    }
}
