123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?php namespace Maze\Template;
- use Maze;
- use Maze\Http\Input;
- use Maze\Session\Save;
- use Maze\Config\Load as Config;
- class View
- {
- /**
- * templatePath
- *
- * @var string
- */
- const TEMPLATE = 'html/';
- /**
- * servicePath
- *
- * @var string
- */
- const SERVICE = 'template/';
-
- /**
- * template
- *
- * @var string
- */
- protected $template;
- /**
- * template
- *
- * @var string
- */
- protected $service;
- /**
- * method
- *
- * @var string
- */
- protected $method;
- /**
- * content
- *
- * @var string
- */
- protected $content;
- /**
- * path
- *
- * @var string
- */
- protected $path;
- /**
- * parse (default value is dom)
- *
- * @var string
- */
- protected $parse = 'Dom';
- /**
- * file
- *
- * @var string
- */
- protected $file;
- /**
- * project
- *
- * @var string
- */
- protected $project;
- /**
- * compile
- *
- * @var \Maze\Template\Compile
- */
- protected $compile;
- /**
- * instance
- *
- * @var string
- */
- static protected $instance;
- /**
- * load file
- * @param string $file
- * @param string $path
- *
- * @return \Maze\Template\View
- */
- static public function getInstance($file, $path = '', $project = false)
- {
- $key = $path . DIRECTORY_SEPARATOR . $file;
- if(empty(self::$instance[$key]))
- {
- self::$instance[$key] = new self($file, $path);
- self::$instance[$key]->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();
- $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 = '<script class="include"(.*?)path="(.*?)"(.*?)file="(.*?)"(.*?)><\/script>';
- //preg_match_all(DIRECTORY_SEPARATOR . $rule . '/i', $content, $result);
-
- return $result;
- }
- }
|