Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Template.php #9

Open
wants to merge 1 commit into
base: 2.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ class Template
* @var CacheInterface
*/
protected $cache;

/**
* 扩展指令解析规则
*
* @var array
*/
protected $directive = [];

/**
* 架构函数
Expand Down Expand Up @@ -210,6 +217,18 @@ public function extend(string $rule, callable $callback = null): void
{
$this->extend[$rule] = $callback;
}

/**
* 扩展模板指令解析规则
*
* @access public
* @param string $rule 解析规则
* @param callable|null $callback 解析规则
* @return void
*/
public function directive(string $rule, callable $callback = null):void{
$this->directive[$rule] = $callback;
}

/**
* 渲染模板文件
Expand Down Expand Up @@ -493,6 +512,9 @@ public function parse(string &$content): void

// 解析普通模板标签 {$tagName}
$this->parseTag($content);

// 解析特殊指令


// 还原被替换的Literal标签
$this->parseLiteral($content, true);
Expand Down Expand Up @@ -962,6 +984,24 @@ private function parseTag(string &$content): void

unset($matches);
}

// 解析特殊指令,如:@json()
$sRegex = $this->getRegex('@');
if(preg_match_all($sRegex, $content, $matches, PREG_SET_ORDER)){
foreach($matches as $match){
$directive = stripslashes($match[1]);

// 指令
if(isset($this->directive[$directive])){
$callback = $this->directive[$directive];
$parseStr = $callback($match[2]);

$content = str_replace($match[0], $parseStr, $content);
}
}

unset($matches);
}
}

/**
Expand Down Expand Up @@ -1269,7 +1309,10 @@ private function getRegex(string $tagName): string
} else {
$regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>(?:(?!' . $end . ').)*))' . $end;
}
} else {
} elseif('@' == $tagName){
$regex = '\@([a-wA-w_]*)\(([\$a-wA-w_]*)\)';
// $regex = '\@([a-wA-w_]*\([\$a-wA-w_]*\))';
} else {
$begin = $this->config['taglib_begin'];
$end = $this->config['taglib_end'];
$single = strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1 ? true : false;
Expand Down