mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Fix tabulation
Add 'macro' namespace in import support
This commit is contained in:
parent
b3480e820b
commit
7631e3508d
110
src/Aspect.php
110
src/Aspect.php
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
use Aspect\Template,
|
use Aspect\Template,
|
||||||
Aspect\ProviderInterface;
|
Aspect\ProviderInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Aspect Template Engine
|
* Aspect Template Engine
|
||||||
@ -89,14 +89,14 @@ class Aspect {
|
|||||||
protected $_on_cmp = array();
|
protected $_on_cmp = array();
|
||||||
protected $_on_post_cmp = array();
|
protected $_on_post_cmp = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ProviderInterface
|
* @var ProviderInterface
|
||||||
*/
|
*/
|
||||||
private $_provider;
|
private $_provider;
|
||||||
/**
|
/**
|
||||||
* @var array of Aspect\ProviderInterface
|
* @var array of Aspect\ProviderInterface
|
||||||
*/
|
*/
|
||||||
protected $_providers = array();
|
protected $_providers = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array of modifiers [modifier_name => callable]
|
* @var array of modifiers [modifier_name => callable]
|
||||||
@ -122,7 +122,7 @@ class Aspect {
|
|||||||
protected $_allowed_funcs = array(
|
protected $_allowed_funcs = array(
|
||||||
"count" => 1, "is_string" => 1, "is_array" => 1, "is_numeric" => 1, "is_int" => 1,
|
"count" => 1, "is_string" => 1, "is_array" => 1, "is_numeric" => 1, "is_int" => 1,
|
||||||
"is_object" => 1, "strtotime" => 1, "gettype" => 1, "is_double" => 1, "json_encode" => 1, "json_decode" => 1,
|
"is_object" => 1, "strtotime" => 1, "gettype" => 1, "is_double" => 1, "json_encode" => 1, "json_decode" => 1,
|
||||||
"ip2long" => 1, "long2ip" => 1, "strip_tags" => 1, "nl2br" => 1, "explode" => 1, "implode" => 1
|
"ip2long" => 1, "long2ip" => 1, "strip_tags" => 1, "nl2br" => 1, "explode" => 1, "implode" => 1
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -252,12 +252,12 @@ class Aspect {
|
|||||||
return $aspect;
|
return $aspect;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Aspect\ProviderInterface $provider
|
* @param Aspect\ProviderInterface $provider
|
||||||
*/
|
*/
|
||||||
public function __construct(Aspect\ProviderInterface $provider) {
|
public function __construct(Aspect\ProviderInterface $provider) {
|
||||||
$this->_provider = $provider;
|
$this->_provider = $provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set compile directory
|
* Set compile directory
|
||||||
@ -482,22 +482,22 @@ class Aspect {
|
|||||||
return $this->_options;
|
return $this->_options;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool|string $scm
|
* @param bool|string $scm
|
||||||
* @return Aspect\ProviderInterface
|
* @return Aspect\ProviderInterface
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function getProvider($scm = false) {
|
public function getProvider($scm = false) {
|
||||||
if($scm) {
|
if($scm) {
|
||||||
if(isset($this->_provider[$scm])) {
|
if(isset($this->_provider[$scm])) {
|
||||||
return $this->_provider[$scm];
|
return $this->_provider[$scm];
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException("Provider for '$scm' not found");
|
throw new InvalidArgumentException("Provider for '$scm' not found");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return $this->_provider;
|
return $this->_provider;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return empty template
|
* Return empty template
|
||||||
@ -538,8 +538,8 @@ class Aspect {
|
|||||||
*/
|
*/
|
||||||
public function getTemplate($template) {
|
public function getTemplate($template) {
|
||||||
if(isset($this->_storage[ $template ])) {
|
if(isset($this->_storage[ $template ])) {
|
||||||
/** @var Aspect\Template $tpl */
|
/** @var Aspect\Template $tpl */
|
||||||
$tpl = $this->_storage[ $template ];
|
$tpl = $this->_storage[ $template ];
|
||||||
if(($this->_options & self::CHECK_MTIME) && !$tpl->isValid()) {
|
if(($this->_options & self::CHECK_MTIME) && !$tpl->isValid()) {
|
||||||
return $this->_storage[ $template ] = $this->compile($template);
|
return $this->_storage[ $template ] = $this->compile($template);
|
||||||
} else {
|
} else {
|
||||||
@ -589,29 +589,29 @@ class Aspect {
|
|||||||
return sprintf("%s.%u.%d.php", basename($tpl), crc32($hash), strlen($hash));
|
return sprintf("%s.%u.%d.php", basename($tpl), crc32($hash), strlen($hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile and save template
|
* Compile and save template
|
||||||
*
|
*
|
||||||
* @param string $tpl
|
* @param string $tpl
|
||||||
* @param bool $store store template on disk
|
* @param bool $store store template on disk
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
* @return \Aspect\Template
|
* @return \Aspect\Template
|
||||||
*/
|
*/
|
||||||
public function compile($tpl, $store = true) {
|
public function compile($tpl, $store = true) {
|
||||||
$template = Template::factory($this)->load($tpl);
|
$template = Template::factory($this)->load($tpl);
|
||||||
if($store) {
|
if($store) {
|
||||||
$tpl_tmp = tempnam($this->_compile_dir, basename($tpl));
|
$tpl_tmp = tempnam($this->_compile_dir, basename($tpl));
|
||||||
$tpl_fp = fopen($tpl_tmp, "w");
|
$tpl_fp = fopen($tpl_tmp, "w");
|
||||||
if(!$tpl_fp) {
|
if(!$tpl_fp) {
|
||||||
throw new \RuntimeException("Can't to open temporary file $tpl_tmp. Directory ".$this->_compile_dir." is writable?");
|
throw new \RuntimeException("Can't to open temporary file $tpl_tmp. Directory ".$this->_compile_dir." is writable?");
|
||||||
}
|
}
|
||||||
fwrite($tpl_fp, $template->getTemplateCode());
|
fwrite($tpl_fp, $template->getTemplateCode());
|
||||||
fclose($tpl_fp);
|
fclose($tpl_fp);
|
||||||
$file_name = $this->_compile_dir."/".$this->_getHash($tpl);
|
$file_name = $this->_compile_dir."/".$this->_getHash($tpl);
|
||||||
if(!rename($tpl_tmp, $file_name)) {
|
if(!rename($tpl_tmp, $file_name)) {
|
||||||
throw new \RuntimeException("Can't to move $tpl_tmp to $tpl");
|
throw new \RuntimeException("Can't to move $tpl_tmp to $tpl");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,8 +18,8 @@ class Compiler {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function tagInclude(Tokenizer $tokens, Template $tpl) {
|
public static function tagInclude(Tokenizer $tokens, Template $tpl) {
|
||||||
$cname = $tpl->parseFirstArg($tokens, $name);
|
$cname = $tpl->parseFirstArg($tokens, $name);
|
||||||
$p = $tpl->parseParams($tokens);
|
$p = $tpl->parseParams($tokens);
|
||||||
if($p) { // if we have additionally variables
|
if($p) { // if we have additionally variables
|
||||||
if($name && ($tpl->getStorage()->getOptions() & \Aspect::FORCE_INCLUDE)) { // if FORCE_INCLUDE enabled and template name known
|
if($name && ($tpl->getStorage()->getOptions() & \Aspect::FORCE_INCLUDE)) { // if FORCE_INCLUDE enabled and template name known
|
||||||
$inc = $tpl->getStorage()->compile($name, false);
|
$inc = $tpl->getStorage()->compile($name, false);
|
||||||
@ -34,10 +34,10 @@ class Compiler {
|
|||||||
$tpl->addDepend($inc);
|
$tpl->addDepend($inc);
|
||||||
return '$_tpl = (array)$tpl; ?>'.$inc->_body.'<?php $tpl->exchangeArray($_tpl); unset($_tpl);';
|
return '$_tpl = (array)$tpl; ?>'.$inc->_body.'<?php $tpl->exchangeArray($_tpl); unset($_tpl);';
|
||||||
} else {
|
} else {
|
||||||
return '$tpl->getStorage()->getTemplate('.$cname.')->display((array)$tpl);';
|
return '$tpl->getStorage()->getTemplate('.$cname.')->display((array)$tpl);';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,24 +50,24 @@ class Compiler {
|
|||||||
*/
|
*/
|
||||||
public static function ifOpen(Tokenizer $tokens, Scope $scope) {
|
public static function ifOpen(Tokenizer $tokens, Scope $scope) {
|
||||||
$scope["else"] = false;
|
$scope["else"] = false;
|
||||||
return 'if('.$scope->tpl->parseExp($tokens, true).') {';
|
return 'if('.$scope->tpl->parseExp($tokens, true).') {';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tag {elseif ...}
|
* Tag {elseif ...}
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @param Tokenizer $tokens
|
* @param Tokenizer $tokens
|
||||||
* @param Scope $scope
|
* @param Scope $scope
|
||||||
* @throws ImproperUseException
|
* @throws ImproperUseException
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function tagElseIf(Tokenizer $tokens, Scope $scope) {
|
public static function tagElseIf(Tokenizer $tokens, Scope $scope) {
|
||||||
if($scope["else"]) {
|
if($scope["else"]) {
|
||||||
throw new ImproperUseException('Incorrect use of the tag {elseif}');
|
throw new ImproperUseException('Incorrect use of the tag {elseif}');
|
||||||
}
|
}
|
||||||
return '} elseif('.$scope->tpl->parseExp($tokens, true).') {';
|
return '} elseif('.$scope->tpl->parseExp($tokens, true).') {';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tag {else}
|
* Tag {else}
|
||||||
@ -80,8 +80,8 @@ class Compiler {
|
|||||||
*/
|
*/
|
||||||
public static function tagElse(Tokenizer $tokens, Scope $scope) {
|
public static function tagElse(Tokenizer $tokens, Scope $scope) {
|
||||||
$scope["else"] = true;
|
$scope["else"] = true;
|
||||||
return '} else {';
|
return '} else {';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open tag {foreach ...}
|
* Open tag {foreach ...}
|
||||||
@ -322,11 +322,11 @@ class Compiler {
|
|||||||
*/
|
*/
|
||||||
public static function tagContinue($tokens, Scope $scope) {
|
public static function tagContinue($tokens, Scope $scope) {
|
||||||
if(empty($scope["no-continue"])) {
|
if(empty($scope["no-continue"])) {
|
||||||
return 'continue;';
|
return 'continue;';
|
||||||
} else {
|
} else {
|
||||||
throw new ImproperUseException("Improper usage of the tag {continue}");
|
throw new ImproperUseException("Improper usage of the tag {continue}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tag {default}
|
* Tag {default}
|
||||||
@ -343,8 +343,8 @@ class Compiler {
|
|||||||
$code = $scope["switch"]."\n".$code;
|
$code = $scope["switch"]."\n".$code;
|
||||||
$scope["switch"] = "";
|
$scope["switch"] = "";
|
||||||
}
|
}
|
||||||
return $code;
|
return $code;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tag {break}
|
* Tag {break}
|
||||||
@ -361,28 +361,28 @@ class Compiler {
|
|||||||
} else {
|
} else {
|
||||||
throw new ImproperUseException("Improper usage of the tag {break}");
|
throw new ImproperUseException("Improper usage of the tag {break}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatch {extends} tag
|
* Dispatch {extends} tag
|
||||||
* @param Tokenizer $tokens
|
* @param Tokenizer $tokens
|
||||||
* @param Template $tpl
|
* @param Template $tpl
|
||||||
* @throws ImproperUseException
|
* @throws ImproperUseException
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function tagExtends(Tokenizer $tokens, Template $tpl) {
|
public static function tagExtends(Tokenizer $tokens, Template $tpl) {
|
||||||
if(!empty($tpl->_extends)) {
|
if(!empty($tpl->_extends)) {
|
||||||
throw new ImproperUseException("Only one {extends} allowed");
|
throw new ImproperUseException("Only one {extends} allowed");
|
||||||
}
|
}
|
||||||
$tpl_name = $tpl->parseFirstArg($tokens, $name);
|
$tpl_name = $tpl->parseFirstArg($tokens, $name);
|
||||||
$tpl->addPostCompile(__CLASS__."::extendBody");
|
$tpl->addPostCompile(__CLASS__."::extendBody");
|
||||||
if($name) { // static extends
|
if($name) { // static extends
|
||||||
//$tpl->_static = true;
|
//$tpl->_static = true;
|
||||||
$tpl->_extends = $tpl->getStorage()->getRawTemplate()->load($name, false);
|
$tpl->_extends = $tpl->getStorage()->getRawTemplate()->load($name, false);
|
||||||
$tpl->addDepend($tpl->_extends); // for valid compile-time need take template from storage
|
$tpl->addDepend($tpl->_extends); // for valid compile-time need take template from storage
|
||||||
return "";
|
return "";
|
||||||
} else { // dynamic extends
|
} else { // dynamic extends
|
||||||
$tpl->_extends = $tpl_name;
|
$tpl->_extends = $tpl_name;
|
||||||
//$tpl->_static = false;
|
//$tpl->_static = false;
|
||||||
return '$parent = $tpl->getStorage()->getTemplate('.$tpl_name.');';
|
return '$parent = $tpl->getStorage()->getTemplate('.$tpl_name.');';
|
||||||
}
|
}
|
||||||
@ -394,8 +394,8 @@ class Compiler {
|
|||||||
* @param Template $tpl
|
* @param Template $tpl
|
||||||
*/
|
*/
|
||||||
public static function extendBody(&$body, $tpl) {
|
public static function extendBody(&$body, $tpl) {
|
||||||
if(isset($tpl->_extends)) { // is child
|
if(isset($tpl->_extends)) { // is child
|
||||||
if(is_object($tpl->_extends)) { // static extends
|
if(is_object($tpl->_extends)) { // static extends
|
||||||
/* @var Template $t */
|
/* @var Template $t */
|
||||||
$t = $tpl->_extends;
|
$t = $tpl->_extends;
|
||||||
if(empty($tpl->_dynamic)) {
|
if(empty($tpl->_dynamic)) {
|
||||||
@ -418,10 +418,10 @@ class Compiler {
|
|||||||
$tpl->addDepend($t);
|
$tpl->addDepend($t);
|
||||||
$body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); ?>'.$t->_body;
|
$body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); ?>'.$t->_body;
|
||||||
}
|
}
|
||||||
} else { // dynamic extends
|
} else { // dynamic extends
|
||||||
$body .= '<?php $parent->blocks = &$tpl->blocks; $parent->display((array)$tpl); unset($tpl->blocks, $parent->blocks); ?>';
|
$body .= '<?php $parent->blocks = &$tpl->blocks; $parent->display((array)$tpl); unset($tpl->blocks, $parent->blocks); ?>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -433,7 +433,7 @@ class Compiler {
|
|||||||
if(!$scalar) {
|
if(!$scalar) {
|
||||||
$tpl->_static = false;
|
$tpl->_static = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tag {block ...}
|
* Tag {block ...}
|
||||||
@ -454,9 +454,9 @@ class Compiler {
|
|||||||
/*if($scope->level) {
|
/*if($scope->level) {
|
||||||
$scope->tpl->_static = false;
|
$scope->tpl->_static = false;
|
||||||
}*/
|
}*/
|
||||||
if(isset($scope->tpl->_extends)) { // is child
|
if(isset($scope->tpl->_extends)) { // is child
|
||||||
return 'if(empty($tpl->blocks['.$scope["cname"].'])) { $tpl->blocks['.$scope["cname"].'] = function($tpl) {';
|
return 'if(empty($tpl->blocks['.$scope["cname"].'])) { $tpl->blocks['.$scope["cname"].'] = function($tpl) {';
|
||||||
} else { // is parent
|
} else { // is parent
|
||||||
if(isset($scope->tpl->_blocks)) { // has blocks from child
|
if(isset($scope->tpl->_blocks)) { // has blocks from child
|
||||||
if(isset($scope->tpl->_blocks[ $scope["name"] ])) { // skip own block and insert child's block after
|
if(isset($scope->tpl->_blocks[ $scope["name"] ])) { // skip own block and insert child's block after
|
||||||
$scope["body"] = $scope->tpl->_body;
|
$scope["body"] = $scope->tpl->_body;
|
||||||
@ -464,9 +464,9 @@ class Compiler {
|
|||||||
} // else just put block content as is
|
} // else just put block content as is
|
||||||
return '';
|
return '';
|
||||||
} else {
|
} else {
|
||||||
return 'if(isset($tpl->blocks['.$scope["cname"].'])) { echo $tpl->blocks['.$scope["cname"].']->__invoke($tpl); } else {';
|
return 'if(isset($tpl->blocks['.$scope["cname"].'])) { echo $tpl->blocks['.$scope["cname"].']->__invoke($tpl); } else {';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -477,24 +477,24 @@ class Compiler {
|
|||||||
*/
|
*/
|
||||||
public static function tagBlockClose($tokens, Scope $scope) {
|
public static function tagBlockClose($tokens, Scope $scope) {
|
||||||
|
|
||||||
if(isset($scope->tpl->_extends)) { // is child
|
if(isset($scope->tpl->_extends)) { // is child
|
||||||
if(!isset($scope->tpl->_blocks[ $scope["name"] ])) {
|
if(!isset($scope->tpl->_blocks[ $scope["name"] ])) {
|
||||||
$scope->tpl->_blocks[ $scope["name"] ] = $scope->getContent();
|
$scope->tpl->_blocks[ $scope["name"] ] = $scope->getContent();
|
||||||
} // dynamic extends
|
} // dynamic extends
|
||||||
return '}; }';
|
return '}; }';
|
||||||
} else { // is parent
|
} else { // is parent
|
||||||
if(isset($scope->tpl->_blocks)) {
|
if(isset($scope->tpl->_blocks)) {
|
||||||
if(isset($scope["body"])) {
|
if(isset($scope["body"])) {
|
||||||
$scope->tpl->_body = $scope["body"].
|
$scope->tpl->_body = $scope["body"].
|
||||||
'<?php if(isset($tpl->blocks['.$scope["cname"].'])) { echo $tpl->blocks['.$scope["cname"].']->__invoke($tpl); } else {?>'.
|
'<?php if(isset($tpl->blocks['.$scope["cname"].'])) { echo $tpl->blocks['.$scope["cname"].']->__invoke($tpl); } else {?>'.
|
||||||
$scope->tpl->_blocks[ $scope["name"] ].'}';
|
$scope->tpl->_blocks[ $scope["name"] ].'}';
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
} else {
|
} else {
|
||||||
return '}';
|
return '}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function tagParent($tokens, Scope $scope) {
|
public static function tagParent($tokens, Scope $scope) {
|
||||||
@ -510,8 +510,8 @@ class Compiler {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function stdClose() {
|
public static function stdClose() {
|
||||||
return '}';
|
return '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Standard function parser
|
* Standard function parser
|
||||||
@ -625,15 +625,20 @@ class Compiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function filterOpen(Tokenizer $tokens, Scope $scope) {
|
public static function filterOpen(Tokenizer $tokens, Scope $scope) {
|
||||||
$scope["filter"] = $scope->tpl->parseModifier($tokens, "ob_get_clean()");
|
$scope["filter"] = $scope->tpl->parseModifier($tokens, "ob_get_clean()");
|
||||||
return "ob_start();";
|
return "ob_start();";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function filterClose($tokens, Scope $scope) {
|
public static function filterClose($tokens, Scope $scope) {
|
||||||
return "echo ".$scope["filter"].";";
|
return "echo ".$scope["filter"].";";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Tokenizer $tokens
|
||||||
|
* @param Scope $scope
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public static function captureOpen(Tokenizer $tokens, Scope $scope) {
|
public static function captureOpen(Tokenizer $tokens, Scope $scope) {
|
||||||
if($tokens->is("|")) {
|
if($tokens->is("|")) {
|
||||||
$scope["value"] = $scope->tpl->parseModifier($tokens, "ob_get_clean()");
|
$scope["value"] = $scope->tpl->parseModifier($tokens, "ob_get_clean()");
|
||||||
@ -650,6 +655,14 @@ class Compiler {
|
|||||||
return $scope["var"]." = ".$scope["value"].";";
|
return $scope["var"]." = ".$scope["value"].";";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tag {cycle}
|
||||||
|
*
|
||||||
|
* @param Tokenizer $tokens
|
||||||
|
* @param Template $tpl
|
||||||
|
* @return string
|
||||||
|
* @throws ImproperUseException
|
||||||
|
*/
|
||||||
public static function tagCycle(Tokenizer $tokens, Template $tpl) {
|
public static function tagCycle(Tokenizer $tokens, Template $tpl) {
|
||||||
$exp = $tpl->parseExp($tokens, true);
|
$exp = $tpl->parseExp($tokens, true);
|
||||||
if($tokens->valid()) {
|
if($tokens->valid()) {
|
||||||
@ -679,6 +692,9 @@ class Compiler {
|
|||||||
}
|
}
|
||||||
if($tokens->is(T_AS)) {
|
if($tokens->is(T_AS)) {
|
||||||
$alias = $tokens->next()->get(Tokenizer::MACRO_STRING);
|
$alias = $tokens->next()->get(Tokenizer::MACRO_STRING);
|
||||||
|
if($alias === "macro") {
|
||||||
|
$alias = "";
|
||||||
|
}
|
||||||
$tokens->next();
|
$tokens->next();
|
||||||
} else {
|
} else {
|
||||||
$alias = "";
|
$alias = "";
|
||||||
@ -697,10 +713,11 @@ class Compiler {
|
|||||||
$tpl->addDepend($donor);
|
$tpl->addDepend($donor);
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Declare or invoke macros
|
* Define macro
|
||||||
*
|
*
|
||||||
* @param Tokenizer $tokens
|
* @param Tokenizer $tokens
|
||||||
* @param Scope $scope
|
* @param Scope $scope
|
||||||
|
@ -90,13 +90,13 @@ class FSProvider implements ProviderInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $tpl
|
* @param string $tpl
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isTemplateExists($tpl) {
|
public function isTemplateExists($tpl) {
|
||||||
return file_exists($this->_path."/".$tpl);
|
return file_exists($this->_path."/".$tpl);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLastModifiedBatch($tpls) {
|
public function getLastModifiedBatch($tpls) {
|
||||||
$tpls = array_flip($tpls);
|
$tpls = array_flip($tpls);
|
||||||
|
@ -25,12 +25,12 @@ class Modifier {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function dateFormat($date, $format = "%b %e, %Y") {
|
public static function dateFormat($date, $format = "%b %e, %Y") {
|
||||||
if(is_string($date) && !is_numeric($date)) {
|
if(is_string($date) && !is_numeric($date)) {
|
||||||
$date = strtotime($date);
|
$date = strtotime($date);
|
||||||
if(!$date) $date = time();
|
if(!$date) $date = time();
|
||||||
}
|
}
|
||||||
return strftime($format, $date);
|
return strftime($format, $date);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $date
|
* @param string $date
|
||||||
@ -38,12 +38,12 @@ class Modifier {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function date($date, $format = "Y m d") {
|
public static function date($date, $format = "Y m d") {
|
||||||
if(is_string($date) && !is_numeric($date)) {
|
if(is_string($date) && !is_numeric($date)) {
|
||||||
$date = strtotime($date);
|
$date = strtotime($date);
|
||||||
if(!$date) $date = time();
|
if(!$date) $date = time();
|
||||||
}
|
}
|
||||||
return date($format, $date);
|
return date($format, $date);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escape string
|
* Escape string
|
||||||
@ -53,14 +53,14 @@ class Modifier {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function escape($text, $type = 'html') {
|
public static function escape($text, $type = 'html') {
|
||||||
switch($type) {
|
switch($type) {
|
||||||
case "url":
|
case "url":
|
||||||
return urlencode($text);
|
return urlencode($text);
|
||||||
case "html";
|
case "html";
|
||||||
return htmlspecialchars($text, ENT_COMPAT, 'UTF-8');
|
return htmlspecialchars($text, ENT_COMPAT, 'UTF-8');
|
||||||
default:
|
default:
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -71,15 +71,15 @@ class Modifier {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function unescape($text, $type = 'html') {
|
public static function unescape($text, $type = 'html') {
|
||||||
switch($type) {
|
switch($type) {
|
||||||
case "url":
|
case "url":
|
||||||
return urldecode($text);
|
return urldecode($text);
|
||||||
case "html";
|
case "html";
|
||||||
return htmlspecialchars_decode($text);
|
return htmlspecialchars_decode($text);
|
||||||
default:
|
default:
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $string
|
* @param string $string
|
||||||
|
@ -13,10 +13,10 @@ class Render extends \ArrayObject {
|
|||||||
"time" => 0,
|
"time" => 0,
|
||||||
"depends" => array()
|
"depends" => array()
|
||||||
);
|
);
|
||||||
/**
|
/**
|
||||||
* @var \Closure
|
* @var \Closure
|
||||||
*/
|
*/
|
||||||
protected $_code;
|
protected $_code;
|
||||||
/**
|
/**
|
||||||
* Template name
|
* Template name
|
||||||
* @var string
|
* @var string
|
||||||
@ -55,8 +55,8 @@ class Render extends \ArrayObject {
|
|||||||
$this->_scm = $props["scm"];
|
$this->_scm = $props["scm"];
|
||||||
$this->_time = $props["time"];
|
$this->_time = $props["time"];
|
||||||
$this->_depends = $props["depends"];
|
$this->_depends = $props["depends"];
|
||||||
$this->_code = $code;
|
$this->_code = $code;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get template storage
|
* Get template storage
|
||||||
@ -86,8 +86,8 @@ class Render extends \ArrayObject {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function __toString() {
|
public function __toString() {
|
||||||
return $this->_name;
|
return $this->_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get template name
|
* Get template name
|
||||||
@ -97,9 +97,9 @@ class Render extends \ArrayObject {
|
|||||||
return $this->_name;
|
return $this->_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTime() {
|
public function getTime() {
|
||||||
return $this->_time;
|
return $this->_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -107,15 +107,15 @@ class Render extends \ArrayObject {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isValid() {
|
public function isValid() {
|
||||||
$provider = $this->_aspect->getProvider(strstr($this->_name, ":"), true);
|
$provider = $this->_aspect->getProvider(strstr($this->_name, ":"), true);
|
||||||
if($provider->getLastModified($this->_name) >= $this->_time) {
|
if($provider->getLastModified($this->_name) >= $this->_time) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
foreach($this->_depends as $tpl => $time) {
|
foreach($this->_depends as $tpl => $time) {
|
||||||
if($this->_aspect->getTemplate($tpl)->getTime() !== $time) {
|
if($this->_aspect->getTemplate($tpl)->getTime() !== $time) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,10 +125,10 @@ class Render extends \ArrayObject {
|
|||||||
* @return Render
|
* @return Render
|
||||||
*/
|
*/
|
||||||
public function display(array $values) {
|
public function display(array $values) {
|
||||||
$this->exchangeArray($values);
|
$this->exchangeArray($values);
|
||||||
$this->_code->__invoke($this);
|
$this->_code->__invoke($this);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute template and return result as string
|
* Execute template and return result as string
|
||||||
@ -154,6 +154,6 @@ class Render extends \ArrayObject {
|
|||||||
* @throws \BadMethodCallException
|
* @throws \BadMethodCallException
|
||||||
*/
|
*/
|
||||||
public function __call($method, $args) {
|
public function __call($method, $args) {
|
||||||
throw new \BadMethodCallException("Unknown method ".$method);
|
throw new \BadMethodCallException("Unknown method ".$method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,17 +7,17 @@ namespace Aspect;
|
|||||||
class Scope extends \ArrayObject {
|
class Scope extends \ArrayObject {
|
||||||
|
|
||||||
public $id = 0;
|
public $id = 0;
|
||||||
public $line = 0;
|
public $line = 0;
|
||||||
public $name;
|
public $name;
|
||||||
public $level = 0;
|
public $level = 0;
|
||||||
/**
|
/**
|
||||||
* @var Template
|
* @var Template
|
||||||
*/
|
*/
|
||||||
public $tpl;
|
public $tpl;
|
||||||
public $closed = false;
|
public $closed = false;
|
||||||
public $is_next_close = false;
|
public $is_next_close = false;
|
||||||
public $is_compiler = true;
|
public $is_compiler = true;
|
||||||
private $_action;
|
private $_action;
|
||||||
private static $count = 0;
|
private static $count = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,12 +29,12 @@ class Scope extends \ArrayObject {
|
|||||||
*/
|
*/
|
||||||
public function __construct($name, $tpl, $line, $action, $level) {
|
public function __construct($name, $tpl, $line, $action, $level) {
|
||||||
$this->id = ++self::$count;
|
$this->id = ++self::$count;
|
||||||
$this->line = $line;
|
$this->line = $line;
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->tpl = $tpl;
|
$this->tpl = $tpl;
|
||||||
$this->_action = $action;
|
$this->_action = $action;
|
||||||
$this->level = $level;
|
$this->level = $level;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -63,7 +63,7 @@ class Scope extends \ArrayObject {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasTag($tag, $level) {
|
public function hasTag($tag, $level) {
|
||||||
if(isset($this->_action["tags"][$tag])) {
|
if(isset($this->_action["tags"][$tag])) {
|
||||||
if($level) {
|
if($level) {
|
||||||
return isset($this->_action["float_tags"][$tag]);
|
return isset($this->_action["float_tags"][$tag]);
|
||||||
} else {
|
} else {
|
||||||
@ -71,7 +71,7 @@ class Scope extends \ArrayObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call tag callback
|
* Call tag callback
|
||||||
@ -81,8 +81,8 @@ class Scope extends \ArrayObject {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function tag($tag, $tokenizer) {
|
public function tag($tag, $tokenizer) {
|
||||||
return call_user_func($this->_action["tags"][$tag], $tokenizer, $this);
|
return call_user_func($this->_action["tags"][$tag], $tokenizer, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close callback
|
* Close callback
|
||||||
|
@ -220,13 +220,13 @@ class Template extends Render {
|
|||||||
return "<?php \n".
|
return "<?php \n".
|
||||||
"/** Aspect template '".$this->_name."' compiled at ".date('Y-m-d H:i:s')." */\n".
|
"/** Aspect template '".$this->_name."' compiled at ".date('Y-m-d H:i:s')." */\n".
|
||||||
"return new Aspect\\Render(\$aspect, ".$this->_getClosureSource().", ".var_export(array(
|
"return new Aspect\\Render(\$aspect, ".$this->_getClosureSource().", ".var_export(array(
|
||||||
//"options" => $this->_options,
|
//"options" => $this->_options,
|
||||||
"provider" => $this->_scm,
|
"provider" => $this->_scm,
|
||||||
"name" => $this->_name,
|
"name" => $this->_name,
|
||||||
"base_name" => $this->_base_name,
|
"base_name" => $this->_base_name,
|
||||||
"time" => $this->_time,
|
"time" => $this->_time,
|
||||||
"depends" => $this->_depends
|
"depends" => $this->_depends
|
||||||
), true).");\n";
|
), true).");\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -256,13 +256,13 @@ class Template extends Render {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add depends from template
|
* Add depends from template
|
||||||
* @param Render $tpl
|
* @param Render $tpl
|
||||||
*/
|
*/
|
||||||
public function addDepend(Render $tpl) {
|
public function addDepend(Render $tpl) {
|
||||||
$this->_depends[$tpl->getScm()][$tpl->getName()] = $tpl->getTime();
|
$this->_depends[$tpl->getScm()][$tpl->getName()] = $tpl->getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute template and return result as string
|
* Execute template and return result as string
|
||||||
@ -334,13 +334,13 @@ class Template extends Render {
|
|||||||
if($tokens->key()) { // if tokenizer still have tokens
|
if($tokens->key()) { // if tokenizer still have tokens
|
||||||
throw new UnexpectedException($tokens);
|
throw new UnexpectedException($tokens);
|
||||||
}
|
}
|
||||||
if(!$code) {
|
if(!$code) {
|
||||||
return "";
|
return "";
|
||||||
} else {
|
} else {
|
||||||
return "<?php\n/* {$this->_name}:{$this->_line}: {$src} */\n {$code} ?>";
|
return "<?php\n/* {$this->_name}:{$this->_line}: {$src} */\n {$code} ?>";
|
||||||
}
|
}
|
||||||
} catch (ImproperUseException $e) {
|
} catch (ImproperUseException $e) {
|
||||||
throw new CompileException($e->getMessage()." in {$this} line {$this->_line}", 0, E_ERROR, $this->_name, $this->_line, $e);
|
throw new CompileException($e->getMessage()." in {$this} line {$this->_line}", 0, E_ERROR, $this->_name, $this->_line, $e);
|
||||||
} catch (\LogicException $e) {
|
} catch (\LogicException $e) {
|
||||||
throw new SecurityException($e->getMessage()." in {$this} line {$this->_line}, near '{".$tokens->getSnippetAsString(0,0)."' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
|
throw new SecurityException($e->getMessage()." in {$this} line {$this->_line}, near '{".$tokens->getSnippetAsString(0,0)."' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
@ -645,13 +645,13 @@ class Template extends Render {
|
|||||||
return 'isset('.$_var.')';
|
return 'isset('.$_var.')';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$expr1 = $this->parseExp($tokens, true);
|
$expr1 = $this->parseExp($tokens, true);
|
||||||
if(!$tokens->is(":")) {
|
if(!$tokens->is(":")) {
|
||||||
throw new UnexpectedException($tokens, null, "ternary operator");
|
throw new UnexpectedException($tokens, null, "ternary operator");
|
||||||
}
|
}
|
||||||
$expr2 = $this->parseExp($tokens, true);
|
$expr2 = $this->parseExp($tokens, true);
|
||||||
if($empty) {
|
if($empty) {
|
||||||
return '(empty('.$_var.') ? '.$expr2.' : '.$expr1.')';
|
return '(empty('.$_var.') ? '.$expr2.' : '.$expr1.')';
|
||||||
} else {
|
} else {
|
||||||
return '(isset('.$_var.') ? '.$expr1.' : '.$expr2.')';
|
return '(isset('.$_var.') ? '.$expr1.' : '.$expr2.')';
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user