12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139 |
- <?php
- namespace think;
- use think\exception\TemplateNotFoundException;
- use think\template\TagLib;
- class Template
- {
-
- protected $data = [];
-
- protected $config = [
- 'view_path' => '',
- 'view_base' => '',
- 'view_suffix' => 'html',
- 'view_depr' => DS,
- 'cache_suffix' => 'php',
- 'tpl_deny_func_list' => 'echo,exit',
- 'tpl_deny_php' => false,
- 'tpl_begin' => '{',
- 'tpl_end' => '}',
- 'strip_space' => false,
- 'tpl_cache' => true,
- 'compile_type' => 'file',
- 'cache_prefix' => '',
- 'cache_time' => 0,
- 'layout_on' => false,
- 'layout_name' => 'layout',
- 'layout_item' => '{__CONTENT__}',
- 'taglib_begin' => '{',
- 'taglib_end' => '}',
- 'taglib_load' => true,
- 'taglib_build_in' => 'cx',
- 'taglib_pre_load' => '',
- 'display_cache' => false,
- 'cache_id' => '',
- 'tpl_replace_string' => [],
- 'tpl_var_identify' => 'array',
- ];
- private $literal = [];
- private $includeFile = [];
- protected $storage;
-
- public function __construct(array $config = [])
- {
- $this->config['cache_path'] = TEMP_PATH;
- $this->config = array_merge($this->config, $config);
- $this->config['taglib_begin_origin'] = $this->config['taglib_begin'];
- $this->config['taglib_end_origin'] = $this->config['taglib_end'];
- $this->config['taglib_begin'] = preg_quote($this->config['taglib_begin'], '/');
- $this->config['taglib_end'] = preg_quote($this->config['taglib_end'], '/');
- $this->config['tpl_begin'] = preg_quote($this->config['tpl_begin'], '/');
- $this->config['tpl_end'] = preg_quote($this->config['tpl_end'], '/');
-
- $type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File';
- $class = false !== strpos($type, '\\') ? $type : '\\think\\template\\driver\\' . ucwords($type);
- $this->storage = new $class();
- }
-
- public function assign($name, $value = '')
- {
- if (is_array($name)) {
- $this->data = array_merge($this->data, $name);
- } else {
- $this->data[$name] = $value;
- }
- }
-
- public function __set($name, $value)
- {
- $this->config[$name] = $value;
- }
-
- public function config($config)
- {
- if (is_array($config)) {
- $this->config = array_merge($this->config, $config);
- } elseif (isset($this->config[$config])) {
- return $this->config[$config];
- } else {
- return;
- }
- }
-
- public function get($name = '')
- {
- if ('' == $name) {
- return $this->data;
- } else {
- $data = $this->data;
- foreach (explode('.', $name) as $key => $val) {
- if (isset($data[$val])) {
- $data = $data[$val];
- } else {
- $data = null;
- break;
- }
- }
- return $data;
- }
- }
-
- public function fetch($template, $vars = [], $config = [])
- {
- if ($vars) {
- $this->data = $vars;
- }
- if ($config) {
- $this->config($config);
- }
- if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
-
- $cacheContent = Cache::get($this->config['cache_id']);
- if (false !== $cacheContent) {
- echo $cacheContent;
- return;
- }
- }
- $template = $this->parseTemplateFile($template);
- if ($template) {
- $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($this->config['layout_name'] . $template) . '.' . ltrim($this->config['cache_suffix'], '.');
- if (!$this->checkCache($cacheFile)) {
-
- $content = file_get_contents($template);
- $this->compiler($content, $cacheFile);
- }
-
- ob_start();
- ob_implicit_flush(0);
-
- $this->storage->read($cacheFile, $this->data);
-
- $content = ob_get_clean();
- if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
-
- Cache::set($this->config['cache_id'], $content, $this->config['cache_time']);
- }
- echo $content;
- }
- }
-
- public function display($content, $vars = [], $config = [])
- {
- if ($vars) {
- $this->data = $vars;
- }
- if ($config) {
- $this->config($config);
- }
- $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($content) . '.' . ltrim($this->config['cache_suffix'], '.');
- if (!$this->checkCache($cacheFile)) {
-
- $this->compiler($content, $cacheFile);
- }
-
- $this->storage->read($cacheFile, $this->data);
- }
-
- public function layout($name, $replace = '')
- {
- if (false === $name) {
-
- $this->config['layout_on'] = false;
- } else {
-
- $this->config['layout_on'] = true;
-
- if (is_string($name)) {
- $this->config['layout_name'] = $name;
- }
- if (!empty($replace)) {
- $this->config['layout_item'] = $replace;
- }
- }
- return $this;
- }
-
- private function checkCache($cacheFile)
- {
-
- if (!$this->config['tpl_cache']) {
- return false;
- }
-
- if (!is_file($cacheFile)) {
- return false;
- }
-
- if (!$handle = @fopen($cacheFile, "r")) {
- return false;
- }
-
- preg_match('/\/\*(.+?)\*\//', fgets($handle), $matches);
- if (!isset($matches[1])) {
- return false;
- }
- $includeFile = unserialize($matches[1]);
- if (!is_array($includeFile)) {
- return false;
- }
-
- foreach ($includeFile as $path => $time) {
- if (is_file($path) && filemtime($path) > $time) {
-
- return false;
- }
- }
-
- return $this->storage->check($cacheFile, $this->config['cache_time']);
- }
-
- public function isCache($cacheId)
- {
- if ($cacheId && $this->config['display_cache']) {
-
- return Cache::has($cacheId);
- }
- return false;
- }
-
- private function compiler(&$content, $cacheFile)
- {
-
- if ($this->config['layout_on']) {
- if (false !== strpos($content, '{__NOLAYOUT__}')) {
-
- $content = str_replace('{__NOLAYOUT__}', '', $content);
- } else {
-
- $layoutFile = $this->parseTemplateFile($this->config['layout_name']);
- if ($layoutFile) {
-
- $content = str_replace($this->config['layout_item'], $content, file_get_contents($layoutFile));
- }
- }
- } else {
- $content = str_replace('{__NOLAYOUT__}', '', $content);
- }
-
- $this->parse($content);
- if ($this->config['strip_space']) {
-
- $find = ['~>\s+<~', '~>(\s+\n|\r)~'];
- $replace = ['><', '>'];
- $content = preg_replace($find, $replace, $content);
- }
-
- $content = preg_replace('/\?>\s*<\?php\s(?!echo\b)/s', '', $content);
-
- $replace = $this->config['tpl_replace_string'];
- $content = str_replace(array_keys($replace), array_values($replace), $content);
-
- $content = '<?php if (!defined(\'THINK_PATH\')) exit(); /*' . serialize($this->includeFile) . '*/ ?>' . "\n" . $content;
-
- $this->storage->write($cacheFile, $content);
- $this->includeFile = [];
- return;
- }
-
- public function parse(&$content)
- {
-
- if (empty($content)) {
- return;
- }
-
- $this->parseLiteral($content);
-
- $this->parseExtend($content);
-
- $this->parseLayout($content);
-
- $this->parseInclude($content);
-
- $this->parseLiteral($content);
-
- $this->parsePhp($content);
-
-
-
-
-
- if ($this->config['taglib_load']) {
- $tagLibs = $this->getIncludeTagLib($content);
- if (!empty($tagLibs)) {
-
- foreach ($tagLibs as $tagLibName) {
- $this->parseTagLib($tagLibName, $content);
- }
- }
- }
-
- if ($this->config['taglib_pre_load']) {
- $tagLibs = explode(',', $this->config['taglib_pre_load']);
- foreach ($tagLibs as $tag) {
- $this->parseTagLib($tag, $content);
- }
- }
-
- $tagLibs = explode(',', $this->config['taglib_build_in']);
- foreach ($tagLibs as $tag) {
- $this->parseTagLib($tag, $content, true);
- }
-
- $this->parseTag($content);
-
- $this->parseLiteral($content, true);
- return;
- }
-
- private function parsePhp(&$content)
- {
-
- $content = preg_replace('/(<\?(?!php|=|$))/i', '<?php echo \'\\1\'; ?>' . "\n", $content);
-
- if ($this->config['tpl_deny_php'] && false !== strpos($content, '<?php')) {
- throw new Exception('not allow php tag', 11600);
- }
- return;
- }
-
- private function parseLayout(&$content)
- {
-
- if (preg_match($this->getRegex('layout'), $content, $matches)) {
-
- $content = str_replace($matches[0], '', $content);
-
- $array = $this->parseAttr($matches[0]);
- if (!$this->config['layout_on'] || $this->config['layout_name'] != $array['name']) {
-
- $layoutFile = $this->parseTemplateFile($array['name']);
- if ($layoutFile) {
- $replace = isset($array['replace']) ? $array['replace'] : $this->config['layout_item'];
-
- $content = str_replace($replace, $content, file_get_contents($layoutFile));
- }
- }
- } else {
- $content = str_replace('{__NOLAYOUT__}', '', $content);
- }
- return;
- }
-
- private function parseInclude(&$content)
- {
- $regex = $this->getRegex('include');
- $func = function ($template) use (&$func, &$regex, &$content) {
- if (preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) {
- foreach ($matches as $match) {
- $array = $this->parseAttr($match[0]);
- $file = $array['file'];
- unset($array['file']);
-
- $parseStr = $this->parseTemplateName($file);
- foreach ($array as $k => $v) {
-
- if (0 === strpos($v, '$')) {
- $v = $this->get(substr($v, 1));
- }
- $parseStr = str_replace('[' . $k . ']', $v, $parseStr);
- }
- $content = str_replace($match[0], $parseStr, $content);
-
- $func($parseStr);
- }
- unset($matches);
- }
- };
-
- $func($content);
- return;
- }
-
- private function parseExtend(&$content)
- {
- $regex = $this->getRegex('extend');
- $array = $blocks = $baseBlocks = [];
- $extend = '';
- $func = function ($template) use (&$func, &$regex, &$array, &$extend, &$blocks, &$baseBlocks) {
- if (preg_match($regex, $template, $matches)) {
- if (!isset($array[$matches['name']])) {
- $array[$matches['name']] = 1;
-
- $extend = $this->parseTemplateName($matches['name']);
-
- $func($extend);
-
- $blocks = array_merge($blocks, $this->parseBlock($template));
- return;
- }
- } else {
-
- $baseBlocks = $this->parseBlock($template, true);
- if (empty($extend)) {
-
- $extend = $template;
- }
- }
- };
- $func($content);
- if (!empty($extend)) {
- if ($baseBlocks) {
- $children = [];
- foreach ($baseBlocks as $name => $val) {
- $replace = $val['content'];
- if (!empty($children[$name])) {
-
- foreach ($children[$name] as $key) {
- $replace = str_replace($baseBlocks[$key]['begin'] . $baseBlocks[$key]['content'] . $baseBlocks[$key]['end'], $blocks[$key]['content'], $replace);
- }
- }
- if (isset($blocks[$name])) {
-
- $replace = str_replace(['{__BLOCK__}', '{__block__}'], $replace, $blocks[$name]['content']);
- if (!empty($val['parent'])) {
-
- $parent = $val['parent'];
- if (isset($blocks[$parent])) {
- $blocks[$parent]['content'] = str_replace($blocks[$name]['begin'] . $blocks[$name]['content'] . $blocks[$name]['end'], $replace, $blocks[$parent]['content']);
- }
- $blocks[$name]['content'] = $replace;
- $children[$parent][] = $name;
- continue;
- }
- } elseif (!empty($val['parent'])) {
-
- $children[$val['parent']][] = $name;
- $blocks[$name] = $val;
- }
- if (!$val['parent']) {
-
- $extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend);
- }
- }
- }
- $content = $extend;
- unset($blocks, $baseBlocks);
- }
- return;
- }
-
- private function parseLiteral(&$content, $restore = false)
- {
- $regex = $this->getRegex($restore ? 'restoreliteral' : 'literal');
- if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
- if (!$restore) {
- $count = count($this->literal);
-
- foreach ($matches as $match) {
- $this->literal[] = substr($match[0], strlen($match[1]), -strlen($match[2]));
- $content = str_replace($match[0], "<!--###literal{$count}###-->", $content);
- $count++;
- }
- } else {
-
- foreach ($matches as $match) {
- $content = str_replace($match[0], $this->literal[$match[1]], $content);
- }
-
- $this->literal = [];
- }
- unset($matches);
- }
- return;
- }
-
- private function parseBlock(&$content, $sort = false)
- {
- $regex = $this->getRegex('block');
- $result = [];
- if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
- $right = $keys = [];
- foreach ($matches as $match) {
- if (empty($match['name'][0])) {
- if (count($right) > 0) {
- $tag = array_pop($right);
- $start = $tag['offset'] + strlen($tag['tag']);
- $length = $match[0][1] - $start;
- $result[$tag['name']] = [
- 'begin' => $tag['tag'],
- 'content' => substr($content, $start, $length),
- 'end' => $match[0][0],
- 'parent' => count($right) ? end($right)['name'] : '',
- ];
- $keys[$tag['name']] = $match[0][1];
- }
- } else {
-
- $right[] = [
- 'name' => $match[2][0],
- 'offset' => $match[0][1],
- 'tag' => $match[0][0],
- ];
- }
- }
- unset($right, $matches);
- if ($sort) {
-
- array_multisort($keys, $result);
- }
- }
- return $result;
- }
-
- private function getIncludeTagLib(&$content)
- {
-
- if (preg_match($this->getRegex('taglib'), $content, $matches)) {
-
- $content = str_replace($matches[0], '', $content);
- return explode(',', $matches['name']);
- }
- return;
- }
-
- public function parseTagLib($tagLib, &$content, $hide = false)
- {
- if (false !== strpos($tagLib, '\\')) {
-
- $className = $tagLib;
- $tagLib = substr($tagLib, strrpos($tagLib, '\\') + 1);
- } else {
- $className = '\\think\\template\\taglib\\' . ucwords($tagLib);
- }
-
- $tLib = new $className($this);
- $tLib->parseTag($content, $hide ? '' : $tagLib);
- return;
- }
-
- public function parseAttr($str, $name = null)
- {
- $regex = '/\s+(?>(?P<name>[\w-]+)\s*)=(?>\s*)([\"\'])(?P<value>(?:(?!\\2).)*)\\2/is';
- $array = [];
- if (preg_match_all($regex, $str, $matches, PREG_SET_ORDER)) {
- foreach ($matches as $match) {
- $array[$match['name']] = $match['value'];
- }
- unset($matches);
- }
- if (!empty($name) && isset($array[$name])) {
- return $array[$name];
- } else {
- return $array;
- }
- }
-
- private function parseTag(&$content)
- {
- $regex = $this->getRegex('tag');
- if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
- foreach ($matches as $match) {
- $str = stripslashes($match[1]);
- $flag = substr($str, 0, 1);
- switch ($flag) {
- case '$':
-
-
- if (false !== $pos = strpos($str, '?')) {
- $array = preg_split('/([!=]={1,2}|(?<!-)[><]={0,1})/', substr($str, 0, $pos), 2, PREG_SPLIT_DELIM_CAPTURE);
- $name = $array[0];
- $this->parseVar($name);
- $this->parseVarFunction($name);
- $str = trim(substr($str, $pos + 1));
- $this->parseVar($str);
- $first = substr($str, 0, 1);
- if (strpos($name, ')')) {
-
- if (isset($array[1])) {
- $this->parseVar($array[2]);
- $name .= $array[1] . $array[2];
- }
- switch ($first) {
- case '?':
- $str = '<?php echo (' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
- break;
- case '=':
- $str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
- break;
- default:
- $str = '<?php echo ' . $name . '?' . $str . '; ?>';
- }
- } else {
- if (isset($array[1])) {
- $this->parseVar($array[2]);
- $express = $name . $array[1] . $array[2];
- } else {
- $express = false;
- }
-
- switch ($first) {
- case '?':
-
- $str = '<?php echo ' . ($express ?: 'isset(' . $name . ')') . '?' . $name . ':' . substr($str, 1) . '; ?>';
- break;
- case '=':
-
- $str = '<?php if(' . ($express ?: '!empty(' . $name . ')') . ') echo ' . substr($str, 1) . '; ?>';
- break;
- case ':':
-
- $str = '<?php echo ' . ($express ?: '!empty(' . $name . ')') . '?' . $name . $str . '; ?>';
- break;
- default:
- $str = '<?php echo ' . ($express ?: '!empty(' . $name . ')') . '?' . $str . '; ?>';
- }
- }
- } else {
- $this->parseVar($str);
- $this->parseVarFunction($str);
- $str = '<?php echo ' . $str . '; ?>';
- }
- break;
- case ':':
-
- $str = substr($str, 1);
- $this->parseVar($str);
- $str = '<?php echo ' . $str . '; ?>';
- break;
- case '~':
-
- $str = substr($str, 1);
- $this->parseVar($str);
- $str = '<?php ' . $str . '; ?>';
- break;
- case '-':
- case '+':
-
- $this->parseVar($str);
- $str = '<?php echo ' . $str . '; ?>';
- break;
- case '/':
-
- $flag2 = substr($str, 1, 1);
- if ('/' == $flag2 || ('*' == $flag2 && substr(rtrim($str), -2) == '*/')) {
- $str = '';
- }
- break;
- default:
-
- $str = $this->config['tpl_begin'] . $str . $this->config['tpl_end'];
- break;
- }
- $content = str_replace($match[0], $str, $content);
- }
- unset($matches);
- }
- return;
- }
-
- public function parseVar(&$varStr)
- {
- $varStr = trim($varStr);
- if (preg_match_all('/\$[a-zA-Z_](?>\w*)(?:[:\.][0-9a-zA-Z_](?>\w*))+/', $varStr, $matches, PREG_OFFSET_CAPTURE)) {
- static $_varParseList = [];
- while ($matches[0]) {
- $match = array_pop($matches[0]);
-
- if (isset($_varParseList[$match[0]])) {
- $parseStr = $_varParseList[$match[0]];
- } else {
- if (strpos($match[0], '.')) {
- $vars = explode('.', $match[0]);
- $first = array_shift($vars);
- if ('$Think' == $first) {
-
- $parseStr = $this->parseThinkVar($vars);
- } elseif ('$Request' == $first) {
-
- $method = array_shift($vars);
- if (!empty($vars)) {
- $params = implode('.', $vars);
- if ('true' != $params) {
- $params = '\'' . $params . '\'';
- }
- } else {
- $params = '';
- }
- $parseStr = '\think\Request::instance()->' . $method . '(' . $params . ')';
- } else {
- switch ($this->config['tpl_var_identify']) {
- case 'array':
- $parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']';
- break;
- case 'obj':
- $parseStr = $first . '->' . implode('->', $vars);
- break;
- default:
- $parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')';
- }
- }
- } else {
- $parseStr = str_replace(':', '->', $match[0]);
- }
- $_varParseList[$match[0]] = $parseStr;
- }
- $varStr = substr_replace($varStr, $parseStr, $match[1], strlen($match[0]));
- }
- unset($matches);
- }
- return;
- }
-
- public function parseVarFunction(&$varStr)
- {
- if (false == strpos($varStr, '|')) {
- return;
- }
- static $_varFunctionList = [];
- $_key = md5($varStr);
-
- if (isset($_varFunctionList[$_key])) {
- $varStr = $_varFunctionList[$_key];
- } else {
- $varArray = explode('|', $varStr);
-
- $name = array_shift($varArray);
-
- $length = count($varArray);
-
- $template_deny_funs = explode(',', $this->config['tpl_deny_func_list']);
- for ($i = 0; $i < $length; $i++) {
- $args = explode('=', $varArray[$i], 2);
-
- $fun = trim($args[0]);
- switch ($fun) {
- case 'default':
- if (false === strpos($name, '(')) {
- $name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')';
- } else {
- $name = '(' . $name . ' ?: ' . $args[1] . ')';
- }
- break;
- default:
- if (!in_array($fun, $template_deny_funs)) {
- if (isset($args[1])) {
- if (strstr($args[1], '###')) {
- $args[1] = str_replace('###', $name, $args[1]);
- $name = "$fun($args[1])";
- } else {
- $name = "$fun($name,$args[1])";
- }
- } else {
- if (!empty($args[0])) {
- $name = "$fun($name)";
- }
- }
- }
- }
- }
- $_varFunctionList[$_key] = $name;
- $varStr = $name;
- }
- return;
- }
-
- public function parseThinkVar($vars)
- {
- $type = strtoupper(trim(array_shift($vars)));
- $param = implode('.', $vars);
- if ($vars) {
- switch ($type) {
- case 'SERVER':
- $parseStr = '\\think\\Request::instance()->server(\'' . $param . '\')';
- break;
- case 'GET':
- $parseStr = '\\think\\Request::instance()->get(\'' . $param . '\')';
- break;
- case 'POST':
- $parseStr = '\\think\\Request::instance()->post(\'' . $param . '\')';
- break;
- case 'COOKIE':
- $parseStr = '\\think\\Cookie::get(\'' . $param . '\')';
- break;
- case 'SESSION':
- $parseStr = '\\think\\Session::get(\'' . $param . '\')';
- break;
- case 'ENV':
- $parseStr = '\\think\\Request::instance()->env(\'' . $param . '\')';
- break;
- case 'REQUEST':
- $parseStr = '\\think\\Request::instance()->request(\'' . $param . '\')';
- break;
- case 'CONST':
- $parseStr = strtoupper($param);
- break;
- case 'LANG':
- $parseStr = '\\think\\Lang::get(\'' . $param . '\')';
- break;
- case 'CONFIG':
- $parseStr = '\\think\\Config::get(\'' . $param . '\')';
- break;
- default:
- $parseStr = '\'\'';
- break;
- }
- } else {
- switch ($type) {
- case 'NOW':
- $parseStr = "date('Y-m-d g:i a',time())";
- break;
- case 'VERSION':
- $parseStr = 'THINK_VERSION';
- break;
- case 'LDELIM':
- $parseStr = '\'' . ltrim($this->config['tpl_begin'], '\\') . '\'';
- break;
- case 'RDELIM':
- $parseStr = '\'' . ltrim($this->config['tpl_end'], '\\') . '\'';
- break;
- default:
- if (defined($type)) {
- $parseStr = $type;
- } else {
- $parseStr = '';
- }
- }
- }
- return $parseStr;
- }
-
- private function parseTemplateName($templateName)
- {
- $array = explode(',', $templateName);
- $parseStr = '';
- foreach ($array as $templateName) {
- if (empty($templateName)) {
- continue;
- }
- if (0 === strpos($templateName, '$')) {
-
- $templateName = $this->get(substr($templateName, 1));
- }
- $template = $this->parseTemplateFile($templateName);
- if ($template) {
-
- $parseStr .= file_get_contents($template);
- }
- }
- return $parseStr;
- }
-
- private function parseTemplateFile($template)
- {
- if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
- if (strpos($template, '@')) {
- list($module, $template) = explode('@', $template);
- }
- if (0 !== strpos($template, '/')) {
- $template = str_replace(['/', ':'], $this->config['view_depr'], $template);
- } else {
- $template = str_replace(['/', ':'], $this->config['view_depr'], substr($template, 1));
- }
- if ($this->config['view_base']) {
- $module = isset($module) ? $module : Request::instance()->module();
- $path = $this->config['view_base'] . ($module ? $module . DS : '');
- } else {
- $path = isset($module) ? APP_PATH . $module . DS . basename($this->config['view_path']) . DS : $this->config['view_path'];
- }
- $template = realpath($path . $template . '.' . ltrim($this->config['view_suffix'], '.'));
- }
- if (is_file($template)) {
-
- $this->includeFile[$template] = filemtime($template);
- return $template;
- } else {
- throw new TemplateNotFoundException('template not exists:' . $template, $template);
- }
- }
-
- private function getRegex($tagName)
- {
- $regex = '';
- if ('tag' == $tagName) {
- $begin = $this->config['tpl_begin'];
- $end = $this->config['tpl_end'];
- if (strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1) {
- $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>[^' . $end . ']*))' . $end;
- } else {
- $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>(?:(?!' . $end . ').)*))' . $end;
- }
- } else {
- $begin = $this->config['taglib_begin'];
- $end = $this->config['taglib_end'];
- $single = strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1 ? true : false;
- switch ($tagName) {
- case 'block':
- if ($single) {
- $regex = $begin . '(?:' . $tagName . '\b(?>(?:(?!name=).)*)\bname=([\'\"])(?P<name>[\$\w\-\/\.]+)\\1(?>[^' . $end . ']*)|\/' . $tagName . ')' . $end;
- } else {
- $regex = $begin . '(?:' . $tagName . '\b(?>(?:(?!name=).)*)\bname=([\'\"])(?P<name>[\$\w\-\/\.]+)\\1(?>(?:(?!' . $end . ').)*)|\/' . $tagName . ')' . $end;
- }
- break;
- case 'literal':
- if ($single) {
- $regex = '(' . $begin . $tagName . '\b(?>[^' . $end . ']*)' . $end . ')';
- $regex .= '(?:(?>[^' . $begin . ']*)(?>(?!' . $begin . '(?>' . $tagName . '\b[^' . $end . ']*|\/' . $tagName . ')' . $end . ')' . $begin . '[^' . $begin . ']*)*)';
- $regex .= '(' . $begin . '\/' . $tagName . $end . ')';
- } else {
- $regex = '(' . $begin . $tagName . '\b(?>(?:(?!' . $end . ').)*)' . $end . ')';
- $regex .= '(?:(?>(?:(?!' . $begin . ').)*)(?>(?!' . $begin . '(?>' . $tagName . '\b(?>(?:(?!' . $end . ').)*)|\/' . $tagName . ')' . $end . ')' . $begin . '(?>(?:(?!' . $begin . ').)*))*)';
- $regex .= '(' . $begin . '\/' . $tagName . $end . ')';
- }
- break;
- case 'restoreliteral':
- $regex = '<!--###literal(\d+)###-->';
- break;
- case 'include':
- $name = 'file';
- case 'taglib':
- case 'layout':
- case 'extend':
- if (empty($name)) {
- $name = 'name';
- }
- if ($single) {
- $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?P<name>[\$\w\-\/\.\:@,\\\\]+)\\1(?>[^' . $end . ']*)' . $end;
- } else {
- $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?P<name>[\$\w\-\/\.\:@,\\\\]+)\\1(?>(?:(?!' . $end . ').)*)' . $end;
- }
- break;
- }
- }
- return '/' . $regex . '/is';
- }
- }
|