Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Template.php 48KB

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