62c977e059fa1f791c08dab373eb0858dd98ff19.svn-base 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. * load service
  143. * @param string $service
  144. *
  145. * @return \Maze\Template\View
  146. */
  147. static public function get($file, $path = '', $project = false)
  148. {
  149. $view = View::getInstance($file, $path, $project);
  150. return $view->runing();
  151. }
  152. /**
  153. * path
  154. * @param string $path
  155. *
  156. * @return \Maze\Template\View
  157. */
  158. public function path($path = false)
  159. {
  160. if(isset(Config::$global['base']['maze_template']) && Config::$global['base']['maze_template'] == true)
  161. {
  162. $save = new Save(false, 'cookie');
  163. $template = Input::get('maze_template');
  164. if($template)
  165. {
  166. if($template == 'no')
  167. {
  168. $save->un('maze_template');
  169. }
  170. else
  171. {
  172. $save->add('maze_template', $template);
  173. }
  174. }
  175. $template = $save->get('maze_template');
  176. }
  177. if(isset($template) && $template)
  178. {
  179. $this->path = $template . DIRECTORY_SEPARATOR;
  180. }
  181. elseif($path)
  182. {
  183. $this->path = $path;
  184. }
  185. elseif(isset(Config::$global['base']['template']))
  186. {
  187. if(is_array(Config::$global['base']['template']))
  188. {
  189. $this->path = Maze::mobile() ? Config::$global['base']['template'][1] : Config::$global['base']['template'][0];
  190. }
  191. else
  192. {
  193. $this->path = Config::$global['base']['template'];
  194. }
  195. $this->path .= DIRECTORY_SEPARATOR;
  196. }
  197. else
  198. {
  199. $temp = explode(DIRECTORY_SEPARATOR, MAZE_PROJECT_PATH);
  200. $this->path = $temp[count($temp)-2] . DIRECTORY_SEPARATOR;
  201. }
  202. return $this;
  203. }
  204. /**
  205. * parse
  206. * @param string $parse
  207. *
  208. * @return \Maze\Template\View
  209. */
  210. public function parse($parse)
  211. {
  212. $parse = 'Maze\\Template\\Parse\\' . ucwords($parse);
  213. if(class_exists($parse))
  214. {
  215. $this->parse = new $parse($this->compile);
  216. }
  217. return $this;
  218. }
  219. /**
  220. * runing
  221. *
  222. * @return \Maze\Template\View
  223. */
  224. public function runing()
  225. {
  226. if($this->content)
  227. {
  228. return $this->content;
  229. }
  230. return $this->template()->compile();
  231. }
  232. /**
  233. * file
  234. *
  235. * @return string
  236. */
  237. public function file()
  238. {
  239. if(empty($this->compile))
  240. {
  241. return '';
  242. }
  243. return $this->compile->file();
  244. }
  245. /**
  246. * compile
  247. *
  248. * @return \Maze\Template\View
  249. */
  250. public function compile()
  251. {
  252. $this->compile = new Compile($this->file, $this->template, $this->service, $this->project, $this->path);
  253. $this->content = $this->compile->get();
  254. if($this->content !== false)
  255. {
  256. return $this->content;
  257. }
  258. return $this->service();
  259. }
  260. /**
  261. * service
  262. *
  263. * @return \Maze\Template\View
  264. */
  265. public function service()
  266. {
  267. $view = $this;
  268. require $this->service;
  269. return $this->content;
  270. }
  271. /**
  272. * template
  273. *
  274. * @return \Maze\Template\View
  275. */
  276. public function template($state = 1)
  277. {
  278. //$path = str_replace(MAZE_PROJECT_PATH, '', Config::$global['base']['assets']);
  279. //$path = $path == Config::$global['base']['assets'] ? str_replace(MAZE_PATH, '', Config::$global['base']['assets']) : $path;
  280. if($this->path && strpos(Config::$global['base']['assets'], DIRECTORY_SEPARATOR . $this->path) !== false)
  281. {
  282. $this->path = '';
  283. }
  284. $html = isset(Config::$global['base']['html']) ? Config::$global['base']['html'] . DIRECTORY_SEPARATOR : self::TEMPLATE;
  285. $this->template = Config::$global['base']['assets'] . $this->path . $html;
  286. if($state == 1 && !is_file($this->template . $this->file . '.html') && isset(Config::$global['base']['template'][0]))
  287. {
  288. # 当前文件不存在,则跳转
  289. $this->path(Config::$global['base']['template'][0] . DIRECTORY_SEPARATOR);
  290. return $this->template(2);
  291. }
  292. if($this->path && empty(Config::$global['host']['replace']))
  293. {
  294. if(isset(Config::$global['base']['replace']) && is_array(Config::$global['base']['replace']))
  295. {
  296. foreach(Config::$global['base']['replace'] as $k => $v)
  297. {
  298. if(isset(Config::$global['host'][$k]))
  299. {
  300. Config::$global['host'][$k] = str_replace('assets/', 'assets/' . $this->path, Config::$global['host'][$k]);
  301. }
  302. }
  303. }
  304. Config::$global['host']['replace'] = true;
  305. }
  306. //echo Config::$global['host']['css'];die;
  307. if($this->path && (MAZE_PROJECT_LIB . DIRECTORY_SEPARATOR == $this->path || MAZE_PROJECT_NAME . DIRECTORY_SEPARATOR == $this->path))
  308. {
  309. $this->path = '';
  310. }
  311. $this->service = (isset(Config::$global['base']['service']) && $this->path) ? Config::$global['base']['service'] . DIRECTORY_SEPARATOR : $this->path;
  312. $this->service = MAZE_PROJECT_PATH . self::SERVICE . $this->service . $this->file . '.php';
  313. return $this;
  314. }
  315. /**
  316. * fetch
  317. *
  318. * @return \Maze\Template\View
  319. */
  320. public function fetch()
  321. {
  322. $this->method[] = func_get_args();
  323. return $this;
  324. }
  325. /**
  326. * set 设置变量
  327. * @param $key 变量名
  328. * @param $value 变量的值
  329. *
  330. * @return \Maze\Template\View
  331. */
  332. public function set($key, $value = false)
  333. {
  334. $this->compile->set($key, $value);
  335. return $this;
  336. }
  337. /**
  338. * hack
  339. * @param $key
  340. *
  341. * @return \Maze\Template\View
  342. */
  343. public function hack($key, $value)
  344. {
  345. $this->compile->set('Maze::$global[\'hack\'][\'' . $key. '\']', $value);
  346. $value = '<{function(){echo 1;}}>';
  347. $this->fetch($key, $value);
  348. return $this;
  349. }
  350. /**
  351. * display
  352. * @param $file 文件名
  353. * @param $local 是否本地化 默认不抓取文件到本地
  354. *
  355. * @return mixed
  356. */
  357. public function display($file = false, $local = false)
  358. {
  359. if($file)
  360. {
  361. $this->compile->read($file, $this->template, $this->service, $local);
  362. }
  363. if($this->method)
  364. {
  365. if(is_string($this->parse))
  366. {
  367. $this->parse($this->parse);
  368. }
  369. if(is_object($this->parse))
  370. {
  371. # 为了更好的兼容大部分php配置,忍痛取消这里的匿名函数...
  372. foreach($this->method as $k => $v)
  373. {
  374. $this->parse->make($v);
  375. }
  376. /*
  377. $callback = function($param, $key)
  378. {
  379. $this->parse->make($param);
  380. };
  381. array_walk($this->method, $callback);
  382. */
  383. $this->content = $this->compile->create($this->parse->get());
  384. }
  385. }
  386. else
  387. {
  388. $this->content = $this->compile->create($this->compile->template());
  389. }
  390. }
  391. /**
  392. * result 获取当前视图模板的基本信息
  393. *
  394. * @return array
  395. */
  396. public function result()
  397. {
  398. $result['service'] = $this->service;
  399. $result['template'] = $this->template . $this->file . '.html';
  400. $result['cmp'] = $this->compile->file();
  401. //$content = file_get_contents($result['template']);
  402. //$rule = '<script class="include"(.*?)path="(.*?)"(.*?)file="(.*?)"(.*?)><\/script>';
  403. //preg_match_all(DIRECTORY_SEPARATOR . $rule . '/i', $content, $result);
  404. return $result;
  405. }
  406. }