project($project); } return self::$instance[$key]; } /** * __construct * @param string $file * @param string $path * * @return mixed */ public function __construct($file, $path = '') { $this->file = $file; $this->path($path); $this->content = ''; } /** * project * @param string $project * * @return mixed */ public function project($project) { $this->project = $project; } /** * page * @param string $service * * @return \Maze\Template\View */ public function page($file, $path = '') { echo $this->load($file, $path); } /** * load service * @param string $service * * @return \Maze\Template\View */ public function load($file, $path = '', $project = false) { $view = View::getInstance($file, $path, $project); return $view->runing(); } /** * path * @param string $path * * @return \Maze\Template\View */ public function path($path = false) { if(isset(Config::$global['base']['maze_template']) && Config::$global['base']['maze_template'] == true) { $save = new Save(false, 'cookie'); $template = Input::get('maze_template'); if($template) { if($template == 'no') { $save->un('maze_template'); } else { $save->add('maze_template', $template); } } $template = $save->get('maze_template'); } if(isset($template) && $template) { $this->path = $template . DIRECTORY_SEPARATOR; } elseif($path) { $this->path = $path; } elseif(isset(Config::$global['base']['template'])) { if(is_array(Config::$global['base']['template'])) { $this->path = Maze::mobile() ? Config::$global['base']['template'][1] : Config::$global['base']['template'][0]; } else { $this->path = Config::$global['base']['template']; } $this->path .= DIRECTORY_SEPARATOR; } else { $temp = explode(DIRECTORY_SEPARATOR, MAZE_PROJECT_PATH); $this->path = $temp[count($temp)-2] . DIRECTORY_SEPARATOR; } return $this; } /** * parse * @param string $parse * * @return \Maze\Template\View */ public function parse($parse) { $parse = 'Maze\\Template\\Parse\\' . ucwords($parse); if(class_exists($parse)) { $this->parse = new $parse($this->compile); } return $this; } /** * runing * * @return \Maze\Template\View */ public function runing() { if($this->content) { return $this->content; } return $this->template()->compile(); } /** * file * * @return string */ public function file() { if(empty($this->compile)) { return ''; } return $this->compile->file(); } /** * compile * * @return \Maze\Template\View */ public function compile() { $this->compile = new Compile($this->file, $this->template, $this->service, $this->project, $this->path); $this->content = $this->compile->get(); if($this->content !== false) { return $this->content; } return $this->service(); } /** * service * * @return \Maze\Template\View */ public function service() { $view = $this; require $this->service; return $this->content; } /** * template * * @return \Maze\Template\View */ public function template($state = 1) { //$path = str_replace(MAZE_PROJECT_PATH, '', Config::$global['base']['assets']); //$path = $path == Config::$global['base']['assets'] ? str_replace(MAZE_PATH, '', Config::$global['base']['assets']) : $path; if($this->path && strpos(Config::$global['base']['assets'], DIRECTORY_SEPARATOR . $this->path) !== false) { $this->path = ''; } $html = isset(Config::$global['base']['html']) ? Config::$global['base']['html'] . DIRECTORY_SEPARATOR : self::TEMPLATE; $this->template = Config::$global['base']['assets'] . $this->path . $html; if($state == 1 && !is_file($this->template . $this->file . '.html') && isset(Config::$global['base']['template'][0])) { # 当前文件不存在,则跳转 $this->path(Config::$global['base']['template'][0] . DIRECTORY_SEPARATOR); return $this->template(2); } if($this->path && empty(Config::$global['host']['replace'])) { if(isset(Config::$global['base']['replace']) && is_array(Config::$global['base']['replace'])) { foreach(Config::$global['base']['replace'] as $k => $v) { if(isset(Config::$global['host'][$k])) { Config::$global['host'][$k] = str_replace('assets/', 'assets/' . $this->path, Config::$global['host'][$k]); } } } Config::$global['host']['replace'] = true; } //echo Config::$global['host']['css'];die; if($this->path && (MAZE_PROJECT_LIB . DIRECTORY_SEPARATOR == $this->path || MAZE_PROJECT_NAME . DIRECTORY_SEPARATOR == $this->path)) { $this->path = ''; } $this->service = (isset(Config::$global['base']['service']) && $this->path) ? Config::$global['base']['service'] . DIRECTORY_SEPARATOR : $this->path; $this->service = MAZE_PROJECT_PATH . self::SERVICE . $this->service . $this->file . '.php'; return $this; } /** * fetch * * @return \Maze\Template\View */ public function fetch() { $this->method[] = func_get_args(); return $this; } /** * set 设置变量 * @param $key 变量名 * @param $value 变量的值 * * @return \Maze\Template\View */ public function set($key, $value = false) { $this->compile->set($key, $value); return $this; } /** * hack * @param $key * * @return \Maze\Template\View */ public function hack($key, $value) { $this->compile->set('Maze::$global[\'hack\'][\'' . $key. '\']', $value); $value = '<{function(){echo 1;}}>'; $this->fetch($key, $value); return $this; } /** * display * @param $file 文件名 * @param $local 是否本地化 默认不抓取文件到本地 * * @return mixed */ public function display($file = false, $local = false) { if($file) { $this->compile->read($file, $this->template, $this->service, $local); } if($this->method) { if(is_string($this->parse)) { $this->parse($this->parse); } if(is_object($this->parse)) { # 为了更好的兼容大部分php配置,忍痛取消这里的匿名函数... foreach($this->method as $k => $v) { $this->parse->make($v); } /* $callback = function($param, $key) { $this->parse->make($param); }; array_walk($this->method, $callback); */ $this->content = $this->compile->create($this->parse->get()); } } else { $this->content = $this->compile->create($this->compile->template()); } } /** * result 获取当前视图模板的基本信息 * * @return array */ public function result() { $result['service'] = $this->service; $result['template'] = $this->template . $this->file . '.html'; $result['cmp'] = $this->compile->file(); //$content = file_get_contents($result['template']); //$rule = '