<?php
class
Parser
{
private
$_tpl
;
public
function
__construct(
$_tplFile
)
{
if
(!
$this
->_tpl =
file_get_contents
(
$_tplFile
)) {
exit
(
'ERROR:模版文件读取错误'
);
}
}
private
function
parvar()
{
$_patten
=
'/<!--\s+\{\$([\w]+)\}\s+-->/'
;
if
(preg_match(
$_patten
,
$this
->_tpl)) {
$this
->_tpl = preg_replace(
$_patten
,
"<?php echo \$this->_vars['$1'];?>"
,
$this
->_tpl);
}
}
private
function
parif(){
$_pattenif
=
'/<!--\s+\{if\s+\$([\w]+)\}\s+-->/'
;
$_pattenElse
=
'/<!--\s+\{else\}\s+-->/'
;
$_pattenEndif
=
'/<!--\s+\{\/if\}\s+-->/'
;
if
(preg_match(
$_pattenif
,
$this
->_tpl)) {
if
(preg_match(
$_pattenEndif
,
$this
->_tpl)) {
$this
->_tpl = preg_replace(
$_pattenif
,
"<?php if (\$this->_vars['$1']){?>"
,
$this
->_tpl);
$this
->_tpl = preg_replace(
$_pattenEndif
,
"<?php } ?>"
,
$this
->_tpl);
if
(preg_match(
$_pattenElse
,
$this
->_tpl)) {
$this
->_tpl = preg_replace(
$_pattenElse
,
"<?php }else{?>"
,
$this
->_tpl);
}
}
else
{
echo
'ERROR:IF语句没有关闭!'
;
}
}
}
private
function
parCommon(){
$_pattenCommon
=
'/<!--\s+\{#\}(.*)\{#\}\s+-->/'
;
if
(preg_match(
$_pattenCommon
,
$this
->_tpl)) {
$this
->_tpl = preg_replace(
$_pattenCommon
,
"<?php /* $1 */ ?>"
,
$this
->_tpl);
}
}
private
function
parForeach(){
$_pattenForeach
=
'/<!--\s+\{foreach\s+\$([\w]+)\(([\w]+),([\w]+)\)\}\s+-->/'
;
$_pattenForeachEnd
=
'/<!--\s+\{\/foreach\}\s+-->/'
;
$_pattenForeachValue
=
'/<!--\s+\{@([\w]+)\}\s+-->/'
;
if
(preg_match(
$_pattenForeach
,
$this
->_tpl)) {
if
(preg_match(
$_pattenForeachEnd
,
$this
->_tpl)) {
$this
->_tpl = preg_replace(
$_pattenForeach
,
"<?php foreach (\$this->_vars['$1'] as \$$2=>\$$3) {?>"
,
$this
->_tpl);
$this
->_tpl = preg_replace(
$_pattenForeachEnd
,
"<?php }?>"
,
$this
->_tpl);
if
(preg_match(
$_pattenForeachValue
,
$this
->_tpl)) {
$this
->_tpl = preg_replace(
$_pattenForeachValue
,
"<?php echo \$$1;?>"
,
$this
->_tpl);
}
}
else
{
echo
'ERROR:Foreach语句没有关闭!'
;
}
}
}
private
function
parInclude(){
$_pattenInclude
=
'/<!--\s+\{include\s+file=\"([\w\.\-]+)\"\}\s+-->/'
;
if
(preg_match(
$_pattenInclude
,
$this
->_tpl,
$_file
,
$_file
)) {
if
(!
file_exists
(
$_file
[1])||
empty
(
$_file
)) {
echo
'ERROR:包含文件出错!'
;
}
$this
->_tpl = preg_replace(
$_pattenInclude
,
"<?php include '$1';?>"
,
$this
->_tpl);
}
}
private
function
parConfig(){
$_pattenConfig
=
'/<!--\s+\{([\w]+)\}\s+-->/'
;
if
(preg_match(
$_pattenConfig
,
$this
->_tpl)) {
$this
->_tpl = preg_replace(
$_pattenConfig
,
"<?php echo \$this->_config['$1'];?>"
,
$this
->_tpl);
}
}
public
function
compile(
$_path
)
{
$this
->parvar();
$this
->parif();
$this
->parForeach();
$this
->parInclude();
$this
->parCommon();
$this
->parConfig();
if
(!
file_put_contents
(
$_path
,
$this
->_tpl)) {
exit
(
'ERROR:编译文件生成错误!'
);
}
}
}
?>