<?php

namespace app;

class View
{
    public $assign; #用于判断类是否存在，节约性能

    /**
     * 变量名替换
     * @param $name
     * @param $value
     */
    public function assign($name, $value)
    {
        $this->assign[$name] = $value;
    }

    /**
     * 显示视图
     * @param $file
     */
    // public function display($file)
    // {
    //     $file = APP.'/views/'.$file;
    //     if (is_file($file)){
    //         extract($this->assign);#从数组中将变量导入到当前的符号表
    //         include $file;
    //     }
    // }

    public function fetch($file = "")
    {
        $request = Request::getInstance();

        if (!empty($this->assign)) {
            extract($this->assign);
        }

        if ($file) {
            $filename = ROOT_PATH . DIRECTORY_SEPARATOR . "view" . DIRECTORY_SEPARATOR . $file;
        } else {
            $filename = ROOT_PATH . DIRECTORY_SEPARATOR . "view" . DIRECTORY_SEPARATOR . $request->actionName . '.php';
        }

        return require($filename);
        die();
    }
}
