<?php
namespace Core;

class View
{
    public static function buffer(string $view, array $data = []): string
    {
        $file = BASE_PATH . '/app/Views/' . str_replace('.', '/', $view) . '.php';
        if (!is_file($file)) {
            throw new \Exception("视图不存在: $view");
        }
        ob_start();
        extract($data, EXTR_SKIP);
        include $file;
        return ob_get_clean();
    }

    public static function make(string $view, array $data = [], ?string $layout = null): string
    {
        $content = self::buffer($view, $data);
        if ($layout) {
            return self::buffer($layout, array_merge($data, ['content' => $content]));
        }
        return $content;
    }
}
