Template.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\exception\TemplateNotFoundException;
  13. /**
  14. * ThinkPHP分离出来的模板引擎
  15. * 支持XML标签和普通标签的模板解析
  16. * 编译型模板引擎 支持动态缓存
  17. */
  18. class Template
  19. {
  20. // 模板变量
  21. protected $data = [];
  22. // 引擎配置
  23. protected $config = [
  24. 'view_path' => '', // 模板路径
  25. 'view_base' => '',
  26. 'view_suffix' => 'html', // 默认模板文件后缀
  27. 'view_depr' => DS,
  28. 'cache_suffix' => 'php', // 默认模板缓存后缀
  29. 'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数
  30. 'tpl_deny_php' => false, // 默认模板引擎是否禁用PHP原生代码
  31. 'tpl_begin' => '{', // 模板引擎普通标签开始标记
  32. 'tpl_end' => '}', // 模板引擎普通标签结束标记
  33. 'strip_space' => false, // 是否去除模板文件里面的html空格与换行
  34. 'tpl_cache' => true, // 是否开启模板编译缓存,设为false则每次都会重新编译
  35. 'compile_type' => 'file', // 模板编译类型
  36. 'cache_prefix' => '', // 模板缓存前缀标识,可以动态改变
  37. 'cache_time' => 0, // 模板缓存有效期 0 为永久,(以数字为值,单位:秒)
  38. 'layout_on' => false, // 布局模板开关
  39. 'layout_name' => 'layout', // 布局模板入口文件
  40. 'layout_item' => '{__CONTENT__}', // 布局模板的内容替换标识
  41. 'taglib_begin' => '{', // 标签库标签开始标记
  42. 'taglib_end' => '}', // 标签库标签结束标记
  43. 'taglib_load' => true, // 是否使用内置标签库之外的其它标签库,默认自动检测
  44. 'taglib_build_in' => 'cx', // 内置标签库名称(标签使用不必指定标签库名称),以逗号分隔 注意解析顺序
  45. 'taglib_pre_load' => '', // 需要额外加载的标签库(须指定标签库名称),多个以逗号分隔
  46. 'display_cache' => false, // 模板渲染缓存
  47. 'cache_id' => '', // 模板缓存ID
  48. 'tpl_replace_string' => [],
  49. 'tpl_var_identify' => 'array', // .语法变量识别,array|object|'', 为空时自动识别
  50. ];
  51. private $literal = [];
  52. private $includeFile = []; // 记录所有模板包含的文件路径及更新时间
  53. protected $storage;
  54. /**
  55. * 构造函数
  56. * @access public
  57. * @param array $config
  58. */
  59. public function __construct(array $config = [])
  60. {
  61. $this->config['cache_path'] = TEMP_PATH;
  62. $this->config = array_merge($this->config, $config);
  63. $this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']);
  64. $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']);
  65. $this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']);
  66. $this->config['tpl_end'] = $this->stripPreg($this->config['tpl_end']);
  67. // 初始化模板编译存储器
  68. $type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File';
  69. $class = false !== strpos($type, '\\') ? $type : '\\think\\template\\driver\\' . ucwords($type);
  70. $this->storage = new $class();
  71. }
  72. /**
  73. * 字符串替换 避免正则混淆
  74. * @access private
  75. * @param string $str
  76. * @return string
  77. */
  78. private function stripPreg($str)
  79. {
  80. return str_replace(
  81. ['{', '}', '(', ')', '|', '[', ']', '-', '+', '*', '.', '^', '?'],
  82. ['\{', '\}', '\(', '\)', '\|', '\[', '\]', '\-', '\+', '\*', '\.', '\^', '\?'],
  83. $str);
  84. }
  85. /**
  86. * 模板变量赋值
  87. * @access public
  88. * @param mixed $name
  89. * @param mixed $value
  90. * @return void
  91. */
  92. public function assign($name, $value = '')
  93. {
  94. if (is_array($name)) {
  95. $this->data = array_merge($this->data, $name);
  96. } else {
  97. $this->data[$name] = $value;
  98. }
  99. }
  100. /**
  101. * 模板引擎参数赋值
  102. * @access public
  103. * @param mixed $name
  104. * @param mixed $value
  105. */
  106. public function __set($name, $value)
  107. {
  108. $this->config[$name] = $value;
  109. }
  110. /**
  111. * 模板引擎配置项
  112. * @access public
  113. * @param array|string $config
  114. * @return void|array
  115. */
  116. public function config($config)
  117. {
  118. if (is_array($config)) {
  119. $this->config = array_merge($this->config, $config);
  120. } elseif (isset($this->config[$config])) {
  121. return $this->config[$config];
  122. } else {
  123. return;
  124. }
  125. }
  126. /**
  127. * 模板变量获取
  128. * @access public
  129. * @param string $name 变量名
  130. * @return mixed
  131. */
  132. public function get($name = '')
  133. {
  134. if ('' == $name) {
  135. return $this->data;
  136. } else {
  137. $data = $this->data;
  138. foreach (explode('.', $name) as $key => $val) {
  139. if (isset($data[$val])) {
  140. $data = $data[$val];
  141. } else {
  142. $data = null;
  143. break;
  144. }
  145. }
  146. return $data;
  147. }
  148. }
  149. /**
  150. * 渲染模板文件
  151. * @access public
  152. * @param string $template 模板文件
  153. * @param array $vars 模板变量
  154. * @param array $config 模板参数
  155. * @return void
  156. */
  157. public function fetch($template, $vars = [], $config = [])
  158. {
  159. if ($vars) {
  160. $this->data = $vars;
  161. }
  162. if ($config) {
  163. $this->config($config);
  164. }
  165. if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
  166. // 读取渲染缓存
  167. $cacheContent = Cache::get($this->config['cache_id']);
  168. if (false !== $cacheContent) {
  169. echo $cacheContent;
  170. return;
  171. }
  172. }
  173. $template = $this->parseTemplateFile($template);
  174. if ($template) {
  175. $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($template) . '.' . ltrim($this->config['cache_suffix'], '.');
  176. if (!$this->checkCache($cacheFile)) {
  177. // 缓存无效 重新模板编译
  178. $content = file_get_contents($template);
  179. $this->compiler($content, $cacheFile);
  180. }
  181. // 页面缓存
  182. ob_start();
  183. ob_implicit_flush(0);
  184. // 读取编译存储
  185. $this->storage->read($cacheFile, $this->data);
  186. // 获取并清空缓存
  187. $content = ob_get_clean();
  188. if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
  189. // 缓存页面输出
  190. Cache::set($this->config['cache_id'], $content, $this->config['cache_time']);
  191. }
  192. echo $content;
  193. }
  194. }
  195. /**
  196. * 渲染模板内容
  197. * @access public
  198. * @param string $content 模板内容
  199. * @param array $vars 模板变量
  200. * @param array $config 模板参数
  201. * @return void
  202. */
  203. public function display($content, $vars = [], $config = [])
  204. {
  205. if ($vars) {
  206. $this->data = $vars;
  207. }
  208. if ($config) {
  209. $this->config($config);
  210. }
  211. $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($content) . '.' . ltrim($this->config['cache_suffix'], '.');
  212. if (!$this->checkCache($cacheFile)) {
  213. // 缓存无效 模板编译
  214. $this->compiler($content, $cacheFile);
  215. }
  216. // 读取编译存储
  217. $this->storage->read($cacheFile, $this->data);
  218. }
  219. /**
  220. * 设置布局
  221. * @access public
  222. * @param mixed $name 布局模板名称 false 则关闭布局
  223. * @param string $replace 布局模板内容替换标识
  224. * @return object
  225. */
  226. public function layout($name, $replace = '')
  227. {
  228. if (false === $name) {
  229. // 关闭布局
  230. $this->config['layout_on'] = false;
  231. } else {
  232. // 开启布局
  233. $this->config['layout_on'] = true;
  234. // 名称必须为字符串
  235. if (is_string($name)) {
  236. $this->config['layout_name'] = $name;
  237. }
  238. if (!empty($replace)) {
  239. $this->config['layout_item'] = $replace;
  240. }
  241. }
  242. return $this;
  243. }
  244. /**
  245. * 检查编译缓存是否有效
  246. * 如果无效则需要重新编译
  247. * @access private
  248. * @param string $cacheFile 缓存文件名
  249. * @return boolean
  250. */
  251. private function checkCache($cacheFile)
  252. {
  253. // 未开启缓存功能
  254. if (!$this->config['tpl_cache']) {
  255. return false;
  256. }
  257. // 缓存文件不存在
  258. if (!is_file($cacheFile)) {
  259. return false;
  260. }
  261. // 读取缓存文件失败
  262. if (!$handle = @fopen($cacheFile, "r")) {
  263. return false;
  264. }
  265. // 读取第一行
  266. preg_match('/\/\*(.+?)\*\//', fgets($handle), $matches);
  267. if (!isset($matches[1])) {
  268. return false;
  269. }
  270. $includeFile = unserialize($matches[1]);
  271. if (!is_array($includeFile)) {
  272. return false;
  273. }
  274. // 检查模板文件是否有更新
  275. foreach ($includeFile as $path => $time) {
  276. if (is_file($path) && filemtime($path) > $time) {
  277. // 模板文件如果有更新则缓存需要更新
  278. return false;
  279. }
  280. }
  281. // 检查编译存储是否有效
  282. return $this->storage->check($cacheFile, $this->config['cache_time']);
  283. }
  284. /**
  285. * 检查编译缓存是否存在
  286. * @access public
  287. * @param string $cacheId 缓存的id
  288. * @return boolean
  289. */
  290. public function isCache($cacheId)
  291. {
  292. if ($cacheId && $this->config['display_cache']) {
  293. // 缓存页面输出
  294. return Cache::has($cacheId);
  295. }
  296. return false;
  297. }
  298. /**
  299. * 编译模板文件内容
  300. * @access private
  301. * @param string $content 模板内容
  302. * @param string $cacheFile 缓存文件名
  303. * @return void
  304. */
  305. private function compiler(&$content, $cacheFile)
  306. {
  307. // 判断是否启用布局
  308. if ($this->config['layout_on']) {
  309. if (false !== strpos($content, '{__NOLAYOUT__}')) {
  310. // 可以单独定义不使用布局
  311. $content = str_replace('{__NOLAYOUT__}', '', $content);
  312. } else {
  313. // 读取布局模板
  314. $layoutFile = $this->parseTemplateFile($this->config['layout_name']);
  315. if ($layoutFile) {
  316. // 替换布局的主体内容
  317. $content = str_replace($this->config['layout_item'], $content, file_get_contents($layoutFile));
  318. }
  319. }
  320. } else {
  321. $content = str_replace('{__NOLAYOUT__}', '', $content);
  322. }
  323. // 模板解析
  324. $this->parse($content);
  325. if ($this->config['strip_space']) {
  326. /* 去除html空格与换行 */
  327. $find = ['~>\s+<~', '~>(\s+\n|\r)~'];
  328. $replace = ['><', '>'];
  329. $content = preg_replace($find, $replace, $content);
  330. }
  331. // 优化生成的php代码
  332. $content = preg_replace('/\?>\s*<\?php\s(?!echo\b)/s', '', $content);
  333. // 模板过滤输出
  334. $replace = $this->config['tpl_replace_string'];
  335. $content = str_replace(array_keys($replace), array_values($replace), $content);
  336. // 添加安全代码及模板引用记录
  337. $content = '<?php if (!defined(\'THINK_PATH\')) exit(); /*' . serialize($this->includeFile) . '*/ ?>' . "\n" . $content;
  338. // 编译存储
  339. $this->storage->write($cacheFile, $content);
  340. $this->includeFile = [];
  341. return;
  342. }
  343. /**
  344. * 模板解析入口
  345. * 支持普通标签和TagLib解析 支持自定义标签库
  346. * @access public
  347. * @param string $content 要解析的模板内容
  348. * @return void
  349. */
  350. public function parse(&$content)
  351. {
  352. // 内容为空不解析
  353. if (empty($content)) {
  354. return;
  355. }
  356. // 替换literal标签内容
  357. $this->parseLiteral($content);
  358. // 解析继承
  359. $this->parseExtend($content);
  360. // 解析布局
  361. $this->parseLayout($content);
  362. // 检查include语法
  363. $this->parseInclude($content);
  364. // 替换包含文件中literal标签内容
  365. $this->parseLiteral($content);
  366. // 检查PHP语法
  367. $this->parsePhp($content);
  368. // 获取需要引入的标签库列表
  369. // 标签库只需要定义一次,允许引入多个一次
  370. // 一般放在文件的最前面
  371. // 格式:<taglib name="html,mytag..." />
  372. // 当TAGLIB_LOAD配置为true时才会进行检测
  373. if ($this->config['taglib_load']) {
  374. $tagLibs = $this->getIncludeTagLib($content);
  375. if (!empty($tagLibs)) {
  376. // 对导入的TagLib进行解析
  377. foreach ($tagLibs as $tagLibName) {
  378. $this->parseTagLib($tagLibName, $content);
  379. }
  380. }
  381. }
  382. // 预先加载的标签库 无需在每个模板中使用taglib标签加载 但必须使用标签库XML前缀
  383. if ($this->config['taglib_pre_load']) {
  384. $tagLibs = explode(',', $this->config['taglib_pre_load']);
  385. foreach ($tagLibs as $tag) {
  386. $this->parseTagLib($tag, $content);
  387. }
  388. }
  389. // 内置标签库 无需使用taglib标签导入就可以使用 并且不需使用标签库XML前缀
  390. $tagLibs = explode(',', $this->config['taglib_build_in']);
  391. foreach ($tagLibs as $tag) {
  392. $this->parseTagLib($tag, $content, true);
  393. }
  394. // 解析普通模板标签 {$tagName}
  395. $this->parseTag($content);
  396. // 还原被替换的Literal标签
  397. $this->parseLiteral($content, true);
  398. return;
  399. }
  400. /**
  401. * 检查PHP语法
  402. * @access private
  403. * @param string $content 要解析的模板内容
  404. * @return void
  405. * @throws \think\Exception
  406. */
  407. private function parsePhp(&$content)
  408. {
  409. // 短标签的情况要将<?标签用echo方式输出 否则无法正常输出xml标识
  410. $content = preg_replace('/(<\?(?!php|=|$))/i', '<?php echo \'\\1\'; ?>' . "\n", $content);
  411. // PHP语法检查
  412. if ($this->config['tpl_deny_php'] && false !== strpos($content, '<?php')) {
  413. throw new Exception('not allow php tag', 11600);
  414. }
  415. return;
  416. }
  417. /**
  418. * 解析模板中的布局标签
  419. * @access private
  420. * @param string $content 要解析的模板内容
  421. * @return void
  422. */
  423. private function parseLayout(&$content)
  424. {
  425. // 读取模板中的布局标签
  426. if (preg_match($this->getRegex('layout'), $content, $matches)) {
  427. // 替换Layout标签
  428. $content = str_replace($matches[0], '', $content);
  429. // 解析Layout标签
  430. $array = $this->parseAttr($matches[0]);
  431. if (!$this->config['layout_on'] || $this->config['layout_name'] != $array['name']) {
  432. // 读取布局模板
  433. $layoutFile = $this->parseTemplateFile($array['name']);
  434. if ($layoutFile) {
  435. $replace = isset($array['replace']) ? $array['replace'] : $this->config['layout_item'];
  436. // 替换布局的主体内容
  437. $content = str_replace($replace, $content, file_get_contents($layoutFile));
  438. }
  439. }
  440. } else {
  441. $content = str_replace('{__NOLAYOUT__}', '', $content);
  442. }
  443. return;
  444. }
  445. /**
  446. * 解析模板中的include标签
  447. * @access private
  448. * @param string $content 要解析的模板内容
  449. * @return void
  450. */
  451. private function parseInclude(&$content)
  452. {
  453. $regex = $this->getRegex('include');
  454. $func = function ($template) use (&$func, &$regex, &$content) {
  455. if (preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) {
  456. foreach ($matches as $match) {
  457. $array = $this->parseAttr($match[0]);
  458. $file = $array['file'];
  459. unset($array['file']);
  460. // 分析模板文件名并读取内容
  461. $parseStr = $this->parseTemplateName($file);
  462. foreach ($array as $k => $v) {
  463. // 以$开头字符串转换成模板变量
  464. if (0 === strpos($v, '$')) {
  465. $v = $this->get(substr($v, 1));
  466. }
  467. $parseStr = str_replace('[' . $k . ']', $v, $parseStr);
  468. }
  469. $content = str_replace($match[0], $parseStr, $content);
  470. // 再次对包含文件进行模板分析
  471. $func($parseStr);
  472. }
  473. unset($matches);
  474. }
  475. };
  476. // 替换模板中的include标签
  477. $func($content);
  478. return;
  479. }
  480. /**
  481. * 解析模板中的extend标签
  482. * @access private
  483. * @param string $content 要解析的模板内容
  484. * @return void
  485. */
  486. private function parseExtend(&$content)
  487. {
  488. $regex = $this->getRegex('extend');
  489. $array = $blocks = $baseBlocks = [];
  490. $extend = '';
  491. $func = function ($template) use (&$func, &$regex, &$array, &$extend, &$blocks, &$baseBlocks) {
  492. if (preg_match($regex, $template, $matches)) {
  493. if (!isset($array[$matches['name']])) {
  494. $array[$matches['name']] = 1;
  495. // 读取继承模板
  496. $extend = $this->parseTemplateName($matches['name']);
  497. // 递归检查继承
  498. $func($extend);
  499. // 取得block标签内容
  500. $blocks = array_merge($blocks, $this->parseBlock($template));
  501. return;
  502. }
  503. } else {
  504. // 取得顶层模板block标签内容
  505. $baseBlocks = $this->parseBlock($template, true);
  506. if (empty($extend)) {
  507. // 无extend标签但有block标签的情况
  508. $extend = $template;
  509. }
  510. }
  511. };
  512. $func($content);
  513. if (!empty($extend)) {
  514. if ($baseBlocks) {
  515. $children = [];
  516. foreach ($baseBlocks as $name => $val) {
  517. $replace = $val['content'];
  518. if (!empty($children[$name])) {
  519. // 如果包含有子block标签
  520. foreach ($children[$name] as $key) {
  521. $replace = str_replace($baseBlocks[$key]['begin'] . $baseBlocks[$key]['content'] . $baseBlocks[$key]['end'], $blocks[$key]['content'], $replace);
  522. }
  523. }
  524. if (isset($blocks[$name])) {
  525. // 带有{__block__}表示与所继承模板的相应标签合并,而不是覆盖
  526. $replace = str_replace(['{__BLOCK__}', '{__block__}'], $replace, $blocks[$name]['content']);
  527. if (!empty($val['parent'])) {
  528. // 如果不是最顶层的block标签
  529. $parent = $val['parent'];
  530. if (isset($blocks[$parent])) {
  531. $blocks[$parent]['content'] = str_replace($blocks[$name]['begin'] . $blocks[$name]['content'] . $blocks[$name]['end'], $replace, $blocks[$parent]['content']);
  532. }
  533. $blocks[$name]['content'] = $replace;
  534. $children[$parent][] = $name;
  535. continue;
  536. }
  537. } elseif (!empty($val['parent'])) {
  538. // 如果子标签没有被继承则用原值
  539. $children[$val['parent']][] = $name;
  540. $blocks[$name] = $val;
  541. }
  542. if (!$val['parent']) {
  543. // 替换模板中的顶级block标签
  544. $extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend);
  545. }
  546. }
  547. }
  548. $content = $extend;
  549. unset($blocks, $baseBlocks);
  550. }
  551. return;
  552. }
  553. /**
  554. * 替换页面中的literal标签
  555. * @access private
  556. * @param string $content 模板内容
  557. * @param boolean $restore 是否为还原
  558. * @return void
  559. */
  560. private function parseLiteral(&$content, $restore = false)
  561. {
  562. $regex = $this->getRegex($restore ? 'restoreliteral' : 'literal');
  563. if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
  564. if (!$restore) {
  565. $count = count($this->literal);
  566. // 替换literal标签
  567. foreach ($matches as $match) {
  568. $this->literal[] = substr($match[0], strlen($match[1]), -strlen($match[2]));
  569. $content = str_replace($match[0], "<!--###literal{$count}###-->", $content);
  570. $count++;
  571. }
  572. } else {
  573. // 还原literal标签
  574. foreach ($matches as $match) {
  575. $content = str_replace($match[0], $this->literal[$match[1]], $content);
  576. }
  577. // 清空literal记录
  578. $this->literal = [];
  579. }
  580. unset($matches);
  581. }
  582. return;
  583. }
  584. /**
  585. * 获取模板中的block标签
  586. * @access private
  587. * @param string $content 模板内容
  588. * @param boolean $sort 是否排序
  589. * @return array
  590. */
  591. private function parseBlock(&$content, $sort = false)
  592. {
  593. $regex = $this->getRegex('block');
  594. $result = [];
  595. if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
  596. $right = $keys = [];
  597. foreach ($matches as $match) {
  598. if (empty($match['name'][0])) {
  599. if (count($right) > 0) {
  600. $tag = array_pop($right);
  601. $start = $tag['offset'] + strlen($tag['tag']);
  602. $length = $match[0][1] - $start;
  603. $result[$tag['name']] = [
  604. 'begin' => $tag['tag'],
  605. 'content' => substr($content, $start, $length),
  606. 'end' => $match[0][0],
  607. 'parent' => count($right) ? end($right)['name'] : '',
  608. ];
  609. $keys[$tag['name']] = $match[0][1];
  610. }
  611. } else {
  612. // 标签头压入栈
  613. $right[] = [
  614. 'name' => $match[2][0],
  615. 'offset' => $match[0][1],
  616. 'tag' => $match[0][0],
  617. ];
  618. }
  619. }
  620. unset($right, $matches);
  621. if ($sort) {
  622. // 按block标签结束符在模板中的位置排序
  623. array_multisort($keys, $result);
  624. }
  625. }
  626. return $result;
  627. }
  628. /**
  629. * 搜索模板页面中包含的TagLib库
  630. * 并返回列表
  631. * @access private
  632. * @param string $content 模板内容
  633. * @return array|null
  634. */
  635. private function getIncludeTagLib(&$content)
  636. {
  637. // 搜索是否有TagLib标签
  638. if (preg_match($this->getRegex('taglib'), $content, $matches)) {
  639. // 替换TagLib标签
  640. $content = str_replace($matches[0], '', $content);
  641. return explode(',', $matches['name']);
  642. }
  643. return;
  644. }
  645. /**
  646. * TagLib库解析
  647. * @access public
  648. * @param string $tagLib 要解析的标签库
  649. * @param string $content 要解析的模板内容
  650. * @param boolean $hide 是否隐藏标签库前缀
  651. * @return void
  652. */
  653. public function parseTagLib($tagLib, &$content, $hide = false)
  654. {
  655. if (false !== strpos($tagLib, '\\')) {
  656. // 支持指定标签库的命名空间
  657. $className = $tagLib;
  658. $tagLib = substr($tagLib, strrpos($tagLib, '\\') + 1);
  659. } else {
  660. $className = '\\think\\template\\taglib\\' . ucwords($tagLib);
  661. }
  662. $tLib = new $className($this);
  663. $tLib->parseTag($content, $hide ? '' : $tagLib);
  664. return;
  665. }
  666. /**
  667. * 分析标签属性
  668. * @access public
  669. * @param string $str 属性字符串
  670. * @param string $name 不为空时返回指定的属性名
  671. * @return array
  672. */
  673. public function parseAttr($str, $name = null)
  674. {
  675. $regex = '/\s+(?>(?P<name>[\w-]+)\s*)=(?>\s*)([\"\'])(?P<value>(?:(?!\\2).)*)\\2/is';
  676. $array = [];
  677. if (preg_match_all($regex, $str, $matches, PREG_SET_ORDER)) {
  678. foreach ($matches as $match) {
  679. $array[$match['name']] = $match['value'];
  680. }
  681. unset($matches);
  682. }
  683. if (!empty($name) && isset($array[$name])) {
  684. return $array[$name];
  685. } else {
  686. return $array;
  687. }
  688. }
  689. /**
  690. * 模板标签解析
  691. * 格式: {TagName:args [|content] }
  692. * @access private
  693. * @param string $content 要解析的模板内容
  694. * @return void
  695. */
  696. private function parseTag(&$content)
  697. {
  698. $regex = $this->getRegex('tag');
  699. if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
  700. foreach ($matches as $match) {
  701. $str = stripslashes($match[1]);
  702. $flag = substr($str, 0, 1);
  703. switch ($flag) {
  704. case '$':
  705. // 解析模板变量 格式 {$varName}
  706. // 是否带有?号
  707. if (false !== $pos = strpos($str, '?')) {
  708. $array = preg_split('/([!=]={1,2}|(?<!-)[><]={0,1})/', substr($str, 0, $pos), 2, PREG_SPLIT_DELIM_CAPTURE);
  709. $name = $array[0];
  710. $this->parseVar($name);
  711. $this->parseVarFunction($name);
  712. $str = trim(substr($str, $pos + 1));
  713. $this->parseVar($str);
  714. $first = substr($str, 0, 1);
  715. if (strpos($name, ')')) {
  716. // $name为对象或是自动识别,或者含有函数
  717. if (isset($array[1])) {
  718. $this->parseVar($array[2]);
  719. $name .= $array[1] . $array[2];
  720. }
  721. switch ($first) {
  722. case '?':
  723. $str = '<?php echo (' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
  724. break;
  725. case '=':
  726. $str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
  727. break;
  728. default:
  729. $str = '<?php echo ' . $name . '?' . $str . '; ?>';
  730. }
  731. } else {
  732. if (isset($array[1])) {
  733. $this->parseVar($array[2]);
  734. $express = $name . $array[1] . $array[2];
  735. } else {
  736. $express = false;
  737. }
  738. // $name为数组
  739. switch ($first) {
  740. case '?':
  741. // {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx
  742. $str = '<?php echo ' . ($express ?: 'isset(' . $name . ')') . '?' . $name . ':' . substr($str, 1) . '; ?>';
  743. break;
  744. case '=':
  745. // {$varname?='xxx'} $varname为真时才输出xxx
  746. $str = '<?php if(' . ($express ?: '!empty(' . $name . ')') . ') echo ' . substr($str, 1) . '; ?>';
  747. break;
  748. case ':':
  749. // {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx
  750. $str = '<?php echo ' . ($express ?: '!empty(' . $name . ')') . '?' . $name . $str . '; ?>';
  751. break;
  752. default:
  753. $str = '<?php echo ' . ($express ?: '!empty(' . $name . ')') . '?' . $str . '; ?>';
  754. }
  755. }
  756. } else {
  757. $this->parseVar($str);
  758. $this->parseVarFunction($str);
  759. $str = '<?php echo ' . $str . '; ?>';
  760. }
  761. break;
  762. case ':':
  763. // 输出某个函数的结果
  764. $str = substr($str, 1);
  765. $this->parseVar($str);
  766. $str = '<?php echo ' . $str . '; ?>';
  767. break;
  768. case '~':
  769. // 执行某个函数
  770. $str = substr($str, 1);
  771. $this->parseVar($str);
  772. $str = '<?php ' . $str . '; ?>';
  773. break;
  774. case '-':
  775. case '+':
  776. // 输出计算
  777. $this->parseVar($str);
  778. $str = '<?php echo ' . $str . '; ?>';
  779. break;
  780. case '/':
  781. // 注释标签
  782. $flag2 = substr($str, 1, 1);
  783. if ('/' == $flag2 || ('*' == $flag2 && substr(rtrim($str), -2) == '*/')) {
  784. $str = '';
  785. }
  786. break;
  787. default:
  788. // 未识别的标签直接返回
  789. $str = $this->config['tpl_begin'] . $str . $this->config['tpl_end'];
  790. break;
  791. }
  792. $content = str_replace($match[0], $str, $content);
  793. }
  794. unset($matches);
  795. }
  796. return;
  797. }
  798. /**
  799. * 模板变量解析,支持使用函数
  800. * 格式: {$varname|function1|function2=arg1,arg2}
  801. * @access public
  802. * @param string $varStr 变量数据
  803. * @return void
  804. */
  805. public function parseVar(&$varStr)
  806. {
  807. $varStr = trim($varStr);
  808. if (preg_match_all('/\$[a-zA-Z_](?>\w*)(?:[:\.][0-9a-zA-Z_](?>\w*))+/', $varStr, $matches, PREG_OFFSET_CAPTURE)) {
  809. static $_varParseList = [];
  810. while ($matches[0]) {
  811. $match = array_pop($matches[0]);
  812. //如果已经解析过该变量字串,则直接返回变量值
  813. if (isset($_varParseList[$match[0]])) {
  814. $parseStr = $_varParseList[$match[0]];
  815. } else {
  816. if (strpos($match[0], '.')) {
  817. $vars = explode('.', $match[0]);
  818. $first = array_shift($vars);
  819. if ('$Think' == $first) {
  820. // 所有以Think.打头的以特殊变量对待 无需模板赋值就可以输出
  821. $parseStr = $this->parseThinkVar($vars);
  822. } elseif ('$Request' == $first) {
  823. // 获取Request请求对象参数
  824. $method = array_shift($vars);
  825. if (!empty($vars)) {
  826. $params = implode('.', $vars);
  827. if ('true' != $params) {
  828. $params = '\'' . $params . '\'';
  829. }
  830. } else {
  831. $params = '';
  832. }
  833. $parseStr = '\think\Request::instance()->' . $method . '(' . $params . ')';
  834. } else {
  835. switch ($this->config['tpl_var_identify']) {
  836. case 'array': // 识别为数组
  837. $parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']';
  838. break;
  839. case 'obj': // 识别为对象
  840. $parseStr = $first . '->' . implode('->', $vars);
  841. break;
  842. default: // 自动判断数组或对象
  843. $parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')';
  844. }
  845. }
  846. } else {
  847. $parseStr = str_replace(':', '->', $match[0]);
  848. }
  849. $_varParseList[$match[0]] = $parseStr;
  850. }
  851. $varStr = substr_replace($varStr, $parseStr, $match[1], strlen($match[0]));
  852. }
  853. unset($matches);
  854. }
  855. return;
  856. }
  857. /**
  858. * 对模板中使用了函数的变量进行解析
  859. * 格式 {$varname|function1|function2=arg1,arg2}
  860. * @access public
  861. * @param string $varStr 变量字符串
  862. * @return void
  863. */
  864. public function parseVarFunction(&$varStr)
  865. {
  866. if (false == strpos($varStr, '|')) {
  867. return;
  868. }
  869. static $_varFunctionList = [];
  870. $_key = md5($varStr);
  871. //如果已经解析过该变量字串,则直接返回变量值
  872. if (isset($_varFunctionList[$_key])) {
  873. $varStr = $_varFunctionList[$_key];
  874. } else {
  875. $varArray = explode('|', $varStr);
  876. // 取得变量名称
  877. $name = array_shift($varArray);
  878. // 对变量使用函数
  879. $length = count($varArray);
  880. // 取得模板禁止使用函数列表
  881. $template_deny_funs = explode(',', $this->config['tpl_deny_func_list']);
  882. for ($i = 0; $i < $length; $i++) {
  883. $args = explode('=', $varArray[$i], 2);
  884. // 模板函数过滤
  885. $fun = trim($args[0]);
  886. switch ($fun) {
  887. case 'default': // 特殊模板函数
  888. if (false === strpos($name, '(')) {
  889. $name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')';
  890. } else {
  891. $name = '(' . $name . ' ?: ' . $args[1] . ')';
  892. }
  893. break;
  894. default: // 通用模板函数
  895. if (!in_array($fun, $template_deny_funs)) {
  896. if (isset($args[1])) {
  897. if (strstr($args[1], '###')) {
  898. $args[1] = str_replace('###', $name, $args[1]);
  899. $name = "$fun($args[1])";
  900. } else {
  901. $name = "$fun($name,$args[1])";
  902. }
  903. } else {
  904. if (!empty($args[0])) {
  905. $name = "$fun($name)";
  906. }
  907. }
  908. }
  909. }
  910. }
  911. $_varFunctionList[$_key] = $name;
  912. $varStr = $name;
  913. }
  914. return;
  915. }
  916. /**
  917. * 特殊模板变量解析
  918. * 格式 以 $Think. 打头的变量属于特殊模板变量
  919. * @access public
  920. * @param array $vars 变量数组
  921. * @return string
  922. */
  923. public function parseThinkVar($vars)
  924. {
  925. $type = strtoupper(trim(array_shift($vars)));
  926. $param = implode('.', $vars);
  927. if ($vars) {
  928. switch ($type) {
  929. case 'SERVER':
  930. $parseStr = '\\think\\Request::instance()->server(\'' . $param . '\')';
  931. break;
  932. case 'GET':
  933. $parseStr = '\\think\\Request::instance()->get(\'' . $param . '\')';
  934. break;
  935. case 'POST':
  936. $parseStr = '\\think\\Request::instance()->post(\'' . $param . '\')';
  937. break;
  938. case 'COOKIE':
  939. $parseStr = '\\think\\Cookie::get(\'' . $param . '\')';
  940. break;
  941. case 'SESSION':
  942. $parseStr = '\\think\\Session::get(\'' . $param . '\')';
  943. break;
  944. case 'ENV':
  945. $parseStr = '\\think\\Request::instance()->env(\'' . $param . '\')';
  946. break;
  947. case 'REQUEST':
  948. $parseStr = '\\think\\Request::instance()->request(\'' . $param . '\')';
  949. break;
  950. case 'CONST':
  951. $parseStr = strtoupper($param);
  952. break;
  953. case 'LANG':
  954. $parseStr = '\\think\\Lang::get(\'' . $param . '\')';
  955. break;
  956. case 'CONFIG':
  957. $parseStr = '\\think\\Config::get(\'' . $param . '\')';
  958. break;
  959. default:
  960. $parseStr = '\'\'';
  961. break;
  962. }
  963. } else {
  964. switch ($type) {
  965. case 'NOW':
  966. $parseStr = "date('Y-m-d g:i a',time())";
  967. break;
  968. case 'VERSION':
  969. $parseStr = 'THINK_VERSION';
  970. break;
  971. case 'LDELIM':
  972. $parseStr = '\'' . ltrim($this->config['tpl_begin'], '\\') . '\'';
  973. break;
  974. case 'RDELIM':
  975. $parseStr = '\'' . ltrim($this->config['tpl_end'], '\\') . '\'';
  976. break;
  977. default:
  978. if (defined($type)) {
  979. $parseStr = $type;
  980. } else {
  981. $parseStr = '';
  982. }
  983. }
  984. }
  985. return $parseStr;
  986. }
  987. /**
  988. * 分析加载的模板文件并读取内容 支持多个模板文件读取
  989. * @access private
  990. * @param string $templateName 模板文件名
  991. * @return string
  992. */
  993. private function parseTemplateName($templateName)
  994. {
  995. $array = explode(',', $templateName);
  996. $parseStr = '';
  997. foreach ($array as $templateName) {
  998. if (empty($templateName)) {
  999. continue;
  1000. }
  1001. if (0 === strpos($templateName, '$')) {
  1002. //支持加载变量文件名
  1003. $templateName = $this->get(substr($templateName, 1));
  1004. }
  1005. $template = $this->parseTemplateFile($templateName);
  1006. if ($template) {
  1007. // 获取模板文件内容
  1008. $parseStr .= file_get_contents($template);
  1009. }
  1010. }
  1011. return $parseStr;
  1012. }
  1013. /**
  1014. * 解析模板文件名
  1015. * @access private
  1016. * @param string $template 文件名
  1017. * @return string|false
  1018. */
  1019. private function parseTemplateFile($template)
  1020. {
  1021. if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
  1022. if (strpos($template, '@')) {
  1023. list($module, $template) = explode('@', $template);
  1024. }
  1025. if (0 !== strpos($template, '/')) {
  1026. $template = str_replace(['/', ':'], $this->config['view_depr'], $template);
  1027. } else {
  1028. $template = str_replace(['/', ':'], $this->config['view_depr'], substr($template, 1));
  1029. }
  1030. if ($this->config['view_base']) {
  1031. $module = isset($module) ? $module : Request::instance()->module();
  1032. $path = $this->config['view_base'] . ($module ? $module . DS : '');
  1033. } else {
  1034. $path = isset($module) ? APP_PATH . $module . DS . basename($this->config['view_path']) . DS : $this->config['view_path'];
  1035. }
  1036. $template = $path . $template . '.' . ltrim($this->config['view_suffix'], '.');
  1037. }
  1038. if (is_file($template)) {
  1039. // 记录模板文件的更新时间
  1040. $this->includeFile[$template] = filemtime($template);
  1041. return $template;
  1042. } else {
  1043. throw new TemplateNotFoundException('template not exists:' . $template, $template);
  1044. }
  1045. }
  1046. /**
  1047. * 按标签生成正则
  1048. * @access private
  1049. * @param string $tagName 标签名
  1050. * @return string
  1051. */
  1052. private function getRegex($tagName)
  1053. {
  1054. $regex = '';
  1055. if ('tag' == $tagName) {
  1056. $begin = $this->config['tpl_begin'];
  1057. $end = $this->config['tpl_end'];
  1058. if (strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1) {
  1059. $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>[^' . $end . ']*))' . $end;
  1060. } else {
  1061. $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>(?:(?!' . $end . ').)*))' . $end;
  1062. }
  1063. } else {
  1064. $begin = $this->config['taglib_begin'];
  1065. $end = $this->config['taglib_end'];
  1066. $single = strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1 ? true : false;
  1067. switch ($tagName) {
  1068. case 'block':
  1069. if ($single) {
  1070. $regex = $begin . '(?:' . $tagName . '\b(?>(?:(?!name=).)*)\bname=([\'\"])(?P<name>[\$\w\-\/\.]+)\\1(?>[^' . $end . ']*)|\/' . $tagName . ')' . $end;
  1071. } else {
  1072. $regex = $begin . '(?:' . $tagName . '\b(?>(?:(?!name=).)*)\bname=([\'\"])(?P<name>[\$\w\-\/\.]+)\\1(?>(?:(?!' . $end . ').)*)|\/' . $tagName . ')' . $end;
  1073. }
  1074. break;
  1075. case 'literal':
  1076. if ($single) {
  1077. $regex = '(' . $begin . $tagName . '\b(?>[^' . $end . ']*)' . $end . ')';
  1078. $regex .= '(?:(?>[^' . $begin . ']*)(?>(?!' . $begin . '(?>' . $tagName . '\b[^' . $end . ']*|\/' . $tagName . ')' . $end . ')' . $begin . '[^' . $begin . ']*)*)';
  1079. $regex .= '(' . $begin . '\/' . $tagName . $end . ')';
  1080. } else {
  1081. $regex = '(' . $begin . $tagName . '\b(?>(?:(?!' . $end . ').)*)' . $end . ')';
  1082. $regex .= '(?:(?>(?:(?!' . $begin . ').)*)(?>(?!' . $begin . '(?>' . $tagName . '\b(?>(?:(?!' . $end . ').)*)|\/' . $tagName . ')' . $end . ')' . $begin . '(?>(?:(?!' . $begin . ').)*))*)';
  1083. $regex .= '(' . $begin . '\/' . $tagName . $end . ')';
  1084. }
  1085. break;
  1086. case 'restoreliteral':
  1087. $regex = '<!--###literal(\d+)###-->';
  1088. break;
  1089. case 'include':
  1090. $name = 'file';
  1091. case 'taglib':
  1092. case 'layout':
  1093. case 'extend':
  1094. if (empty($name)) {
  1095. $name = 'name';
  1096. }
  1097. if ($single) {
  1098. $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?P<name>[\$\w\-\/\.\:@,\\\\]+)\\1(?>[^' . $end . ']*)' . $end;
  1099. } else {
  1100. $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?P<name>[\$\w\-\/\.\:@,\\\\]+)\\1(?>(?:(?!' . $end . ').)*)' . $end;
  1101. }
  1102. break;
  1103. }
  1104. }
  1105. return '/' . $regex . '/is';
  1106. }
  1107. }