5d588b725a8f781d288aee6f0f8984456aa56864.svn-base 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php namespace Maze\Template;
  2. use Maze;
  3. use Maze\Http\Input;
  4. use Maze\Session\Save;
  5. use Maze\Config\Load as Config;
  6. class View
  7. {
  8. /**
  9. * templatePath
  10. *
  11. * @var string
  12. */
  13. const TEMPLATE = 'html/';
  14. /**
  15. * servicePath
  16. *
  17. * @var string
  18. */
  19. const SERVICE = 'template/';
  20. /**
  21. * template
  22. *
  23. * @var string
  24. */
  25. protected $template;
  26. /**
  27. * template
  28. *
  29. * @var string
  30. */
  31. protected $service;
  32. /**
  33. * method
  34. *
  35. * @var string
  36. */
  37. protected $method;
  38. /**
  39. * content
  40. *
  41. * @var string
  42. */
  43. protected $content;
  44. /**
  45. * path
  46. *
  47. * @var string
  48. */
  49. protected $path;
  50. /**
  51. * parse (default value is dom)
  52. *
  53. * @var string
  54. */
  55. protected $parse = 'Dom';
  56. /**
  57. * file
  58. *
  59. * @var string
  60. */
  61. protected $file;
  62. /**
  63. * project
  64. *
  65. * @var string
  66. */
  67. protected $project;
  68. /**
  69. * compile
  70. *
  71. * @var \Maze\Template\Compile
  72. */
  73. protected $compile;
  74. /**
  75. * instance
  76. *
  77. * @var string
  78. */
  79. static protected $instance;
  80. /**
  81. * load file
  82. * @param string $file
  83. * @param string $path
  84. *
  85. * @return \Maze\Template\View
  86. */
  87. static public function getInstance($file, $path = '', $project = false)
  88. {
  89. $key = $path . DIRECTORY_SEPARATOR . $file;
  90. if(empty(self::$instance[$key]))
  91. {
  92. self::$instance[$key] = new self($file, $path);
  93. self::$instance[$key]->project($project);
  94. }
  95. return self::$instance[$key];
  96. }
  97. /**
  98. * __construct
  99. * @param string $file
  100. * @param string $path
  101. *
  102. * @return mixed
  103. */
  104. public function __construct($file, $path = '')
  105. {
  106. $this->file = $file;
  107. $this->path($path);
  108. $this->content = '';
  109. }
  110. /**
  111. * project
  112. * @param string $project
  113. *
  114. * @return mixed
  115. */
  116. public function project($project)
  117. {
  118. $this->project = $project;
  119. }
  120. /**
  121. * page
  122. * @param string $service
  123. *
  124. * @return \Maze\Template\View
  125. */
  126. public function page($file, $path = '')
  127. {
  128. echo $this->load($file, $path);
  129. }
  130. /**
  131. * load service
  132. * @param string $service
  133. *
  134. * @return \Maze\Template\View
  135. */
  136. public function load($file, $path = '', $project = false)
  137. {
  138. $view = View::getInstance($file, $path, $project);
  139. return $view->runing();
  140. }
  141. /**
  142. * path
  143. * @param string $path
  144. *
  145. * @return \Maze\Template\View
  146. */
  147. public function path($path = false)
  148. {
  149. if(isset(Config::$global['base']['maze_template']) && Config::$global['base']['maze_template'] == true)
  150. {
  151. $save = new Save();
  152. $template = Input::get('maze_template');
  153. if($template)
  154. {
  155. if($template == 'no')
  156. {
  157. $save->un('maze_template');
  158. }
  159. else
  160. {
  161. $save->add('maze_template', $template);
  162. }
  163. }
  164. $template = $save->get('maze_template');
  165. }
  166. if(isset($template) && $template)
  167. {
  168. $this->path = $template . DIRECTORY_SEPARATOR;
  169. }
  170. elseif($path)
  171. {
  172. $this->path = $path;
  173. }
  174. elseif(isset(Config::$global['base']['template']))
  175. {
  176. if(is_array(Config::$global['base']['template']))
  177. {
  178. $this->path = Maze::mobile() ? Config::$global['base']['template'][1] : Config::$global['base']['template'][0];
  179. }
  180. else
  181. {
  182. $this->path = Config::$global['base']['template'];
  183. }
  184. $this->path .= DIRECTORY_SEPARATOR;
  185. }
  186. else
  187. {
  188. $temp = explode(DIRECTORY_SEPARATOR, MAZE_PROJECT_PATH);
  189. $this->path = $temp[count($temp)-2] . DIRECTORY_SEPARATOR;
  190. }
  191. return $this;
  192. }
  193. /**
  194. * parse
  195. * @param string $parse
  196. *
  197. * @return \Maze\Template\View
  198. */
  199. public function parse($parse)
  200. {
  201. $parse = 'Maze\\Template\\Parse\\' . ucwords($parse);
  202. if(class_exists($parse))
  203. {
  204. $this->parse = new $parse($this->compile);
  205. }
  206. return $this;
  207. }
  208. /**
  209. * runing
  210. *
  211. * @return \Maze\Template\View
  212. */
  213. public function runing()
  214. {
  215. if($this->content)
  216. {
  217. return $this->content;
  218. }
  219. return $this->template()->compile();
  220. }
  221. /**
  222. * file
  223. *
  224. * @return string
  225. */
  226. public function file()
  227. {
  228. if(empty($this->compile))
  229. {
  230. return '';
  231. }
  232. return $this->compile->file();
  233. }
  234. /**
  235. * compile
  236. *
  237. * @return \Maze\Template\View
  238. */
  239. public function compile()
  240. {
  241. $this->compile = new Compile($this->file, $this->template, $this->service, $this->project, $this->path);
  242. $this->content = $this->compile->get();
  243. if($this->content !== false)
  244. {
  245. return $this->content;
  246. }
  247. return $this->service();
  248. }
  249. /**
  250. * service
  251. *
  252. * @return \Maze\Template\View
  253. */
  254. public function service()
  255. {
  256. $view = $this;
  257. require $this->service;
  258. return $this->content;
  259. }
  260. /**
  261. * template
  262. *
  263. * @return \Maze\Template\View
  264. */
  265. public function template($state = 1)
  266. {
  267. //$path = str_replace(MAZE_PROJECT_PATH, '', Config::$global['base']['assets']);
  268. //$path = $path == Config::$global['base']['assets'] ? str_replace(MAZE_PATH, '', Config::$global['base']['assets']) : $path;
  269. if($this->path && strpos(Config::$global['base']['assets'], DIRECTORY_SEPARATOR . $this->path) !== false)
  270. {
  271. $this->path = '';
  272. }
  273. $html = isset(Config::$global['base']['html']) ? Config::$global['base']['html'] . DIRECTORY_SEPARATOR : self::TEMPLATE;
  274. $this->template = Config::$global['base']['assets'] . $this->path . $html;
  275. if($state == 1 && !is_file($this->template . $this->file . '.html') && isset(Config::$global['base']['template'][0]))
  276. {
  277. # 当前文件不存在,则跳转
  278. $this->path(Config::$global['base']['template'][0] . DIRECTORY_SEPARATOR);
  279. return $this->template(2);
  280. }
  281. if($this->path && empty(Config::$global['host']['replace']))
  282. {
  283. if(isset(Config::$global['base']['replace']) && is_array(Config::$global['base']['replace']))
  284. {
  285. foreach(Config::$global['base']['replace'] as $k => $v)
  286. {
  287. if(isset(Config::$global['host'][$k]))
  288. {
  289. Config::$global['host'][$k] = str_replace('assets/', 'assets/' . $this->path, Config::$global['host'][$k]);
  290. }
  291. }
  292. }
  293. Config::$global['host']['replace'] = true;
  294. }
  295. //echo Config::$global['host']['css'];die;
  296. if($this->path && (MAZE_PROJECT_LIB . DIRECTORY_SEPARATOR == $this->path || MAZE_PROJECT_NAME . DIRECTORY_SEPARATOR == $this->path))
  297. {
  298. $this->path = '';
  299. }
  300. $this->service = (isset(Config::$global['base']['service']) && $this->path) ? Config::$global['base']['service'] . DIRECTORY_SEPARATOR : $this->path;
  301. $this->service = MAZE_PROJECT_PATH . self::SERVICE . $this->service . $this->file . '.php';
  302. return $this;
  303. }
  304. /**
  305. * fetch
  306. *
  307. * @return \Maze\Template\View
  308. */
  309. public function fetch()
  310. {
  311. $this->method[] = func_get_args();
  312. return $this;
  313. }
  314. /**
  315. * set 设置变量
  316. * @param $key 变量名
  317. * @param $value 变量的值
  318. *
  319. * @return \Maze\Template\View
  320. */
  321. public function set($key, $value = false)
  322. {
  323. $this->compile->set($key, $value);
  324. return $this;
  325. }
  326. /**
  327. * hack
  328. * @param $key
  329. *
  330. * @return \Maze\Template\View
  331. */
  332. public function hack($key, $value)
  333. {
  334. $this->compile->set('Maze::$global[\'hack\'][\'' . $key. '\']', $value);
  335. $value = '<{function(){echo 1;}}>';
  336. $this->fetch($key, $value);
  337. return $this;
  338. }
  339. /**
  340. * display
  341. * @param $file 文件名
  342. * @param $local 是否本地化 默认不抓取文件到本地
  343. *
  344. * @return mixed
  345. */
  346. public function display($file = false, $local = false)
  347. {
  348. if($file)
  349. {
  350. $this->compile->read($file, $this->template, $this->service, $local);
  351. }
  352. if($this->method)
  353. {
  354. if(is_string($this->parse))
  355. {
  356. $this->parse($this->parse);
  357. }
  358. if(is_object($this->parse))
  359. {
  360. # 为了更好的兼容大部分php配置,忍痛取消这里的匿名函数...
  361. foreach($this->method as $k => $v)
  362. {
  363. $this->parse->make($v);
  364. }
  365. /*
  366. $callback = function($param, $key)
  367. {
  368. $this->parse->make($param);
  369. };
  370. array_walk($this->method, $callback);
  371. */
  372. $this->content = $this->compile->create($this->parse->get());
  373. }
  374. }
  375. else
  376. {
  377. $this->content = $this->compile->create($this->compile->template());
  378. }
  379. }
  380. /**
  381. * result 获取当前视图模板的基本信息
  382. *
  383. * @return array
  384. */
  385. public function result()
  386. {
  387. $result['service'] = $this->service;
  388. $result['template'] = $this->template . $this->file . '.html';
  389. $result['cmp'] = $this->compile->file();
  390. //$content = file_get_contents($result['template']);
  391. //$rule = '<script class="include"(.*?)path="(.*?)"(.*?)file="(.*?)"(.*?)><\/script>';
  392. //preg_match_all(DIRECTORY_SEPARATOR . $rule . '/i', $content, $result);
  393. return $result;
  394. }
  395. }