mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Fix indents
This commit is contained in:
parent
c76f5c8be0
commit
0457d5c379
@ -16,14 +16,14 @@ use Fenom\Template,
|
||||
class Fenom {
|
||||
const VERSION = '1.0';
|
||||
|
||||
/* Compiler types */
|
||||
/* Compiler types */
|
||||
const INLINE_COMPILER = 1;
|
||||
const BLOCK_COMPILER = 2;
|
||||
const INLINE_FUNCTION = 3;
|
||||
const BLOCK_FUNCTION = 4;
|
||||
const MODIFIER = 5;
|
||||
|
||||
/* Options */
|
||||
/* Options */
|
||||
const DENY_METHODS = 0x10;
|
||||
const DENY_INLINE_FUNCS = 0x20;
|
||||
const FORCE_INCLUDE = 0x40;
|
||||
@ -31,7 +31,7 @@ class Fenom {
|
||||
const FORCE_COMPILE = 0xF0;
|
||||
const DISABLE_CACHE = 0x1F0;
|
||||
|
||||
/* Default parsers */
|
||||
/* Default parsers */
|
||||
const DEFAULT_CLOSE_COMPILER = 'Fenom\Compiler::stdClose';
|
||||
const DEFAULT_FUNC_PARSER = 'Fenom\Compiler::stdFuncParser';
|
||||
const DEFAULT_FUNC_OPEN = 'Fenom\Compiler::stdFuncOpen';
|
||||
@ -207,10 +207,10 @@ class Fenom {
|
||||
'type' => self::INLINE_COMPILER,
|
||||
'parser' => 'Fenom\Compiler::tagImport'
|
||||
),
|
||||
'cycle' => array(
|
||||
'type' => self::INLINE_COMPILER,
|
||||
'parser' => 'Fenom\Compiler::tagCycle'
|
||||
)
|
||||
'cycle' => array(
|
||||
'type' => self::INLINE_COMPILER,
|
||||
'parser' => 'Fenom\Compiler::tagCycle'
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
@ -231,7 +231,7 @@ class Fenom {
|
||||
throw new InvalidArgumentException("Source must be a valid path or provider object");
|
||||
}
|
||||
$fenom = new static($provider);
|
||||
/* @var Fenom $fytro */
|
||||
/* @var Fenom $fytro */
|
||||
$fenom->setCompileDir($compile_dir);
|
||||
if($options) {
|
||||
$fenom->setOptions($options);
|
||||
@ -573,22 +573,22 @@ class Fenom {
|
||||
return $this->getTemplate($template)->fetch($vars);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param string $template name of template
|
||||
* @param array $vars
|
||||
* @param $callback
|
||||
* @param float $chunk
|
||||
* @return \Fenom\Render
|
||||
* @example $fenom->pipe("products.yml.tpl", $iterators, [new SplFileObject("/tmp/products.yml"), "fwrite"], 512*1024)
|
||||
*/
|
||||
public function pipe($template, array $vars, $callback, $chunk = 1e6) {
|
||||
ob_start($callback, $chunk, true);
|
||||
$this->getTemplate($template)->display($vars);
|
||||
ob_end_flush();
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param string $template name of template
|
||||
* @param array $vars
|
||||
* @param $callback
|
||||
* @param float $chunk
|
||||
* @return \Fenom\Render
|
||||
* @example $fenom->pipe("products.yml.tpl", $iterators, [new SplFileObject("/tmp/products.yml"), "fwrite"], 512*1024)
|
||||
*/
|
||||
public function pipe($template, array $vars, $callback, $chunk = 1e6) {
|
||||
ob_start($callback, $chunk, true);
|
||||
$this->getTemplate($template)->display($vars);
|
||||
ob_end_flush();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get template by name
|
||||
@ -685,7 +685,7 @@ class Fenom {
|
||||
* Flush internal memory template cache
|
||||
*/
|
||||
public function flush() {
|
||||
$this->_storage = array();
|
||||
$this->_storage = array();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -397,10 +397,10 @@ class Compiler {
|
||||
$tpl->addDepend($tpl->_extends);
|
||||
return "";
|
||||
} else { // dynamic extends
|
||||
if(!isset($tpl->_compatible)) {
|
||||
$tpl->_compatible = true;
|
||||
if(!isset($tpl->_compatible)) {
|
||||
$tpl->_compatible = true;
|
||||
}
|
||||
$tpl->_extends = $tpl_name;
|
||||
$tpl->_extends = $tpl_name;
|
||||
return '$parent = $tpl->getStorage()->getTemplate('.$tpl_name.', \Fenom\Template::EXTENDED);';
|
||||
}
|
||||
}
|
||||
@ -411,37 +411,37 @@ class Compiler {
|
||||
* @param Template $tpl
|
||||
*/
|
||||
public static function extendBody(&$body, $tpl) {
|
||||
$t = $tpl;
|
||||
if($tpl->uses) {
|
||||
$tpl->blocks += $tpl->uses;
|
||||
}
|
||||
while(isset($t->_extends)) {
|
||||
$t = $t->_extends;
|
||||
if(is_object($t)) {
|
||||
/* @var \Fenom\Template $t */
|
||||
$t->_extended = true;
|
||||
$tpl->addDepend($t);
|
||||
$t->_compatible = &$tpl->_compatible;
|
||||
$t->blocks = &$tpl->blocks;
|
||||
$t->compile();
|
||||
if($t->uses) {
|
||||
$tpl->blocks += $t->uses;
|
||||
}
|
||||
if(!isset($t->_extends)) { // last item => parent
|
||||
if(empty($tpl->_compatible)) {
|
||||
$body = $t->getBody();
|
||||
} else {
|
||||
$body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); ?>'.$t->getBody();
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
$body .= $t->getBody();
|
||||
}
|
||||
} else {
|
||||
$body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); $parent->b = &$tpl->b; $parent->display((array)$tpl); unset($tpl->b, $parent->b); ?>';
|
||||
return;
|
||||
}
|
||||
}
|
||||
$t = $tpl;
|
||||
if($tpl->uses) {
|
||||
$tpl->blocks += $tpl->uses;
|
||||
}
|
||||
while(isset($t->_extends)) {
|
||||
$t = $t->_extends;
|
||||
if(is_object($t)) {
|
||||
/* @var \Fenom\Template $t */
|
||||
$t->_extended = true;
|
||||
$tpl->addDepend($t);
|
||||
$t->_compatible = &$tpl->_compatible;
|
||||
$t->blocks = &$tpl->blocks;
|
||||
$t->compile();
|
||||
if($t->uses) {
|
||||
$tpl->blocks += $t->uses;
|
||||
}
|
||||
if(!isset($t->_extends)) { // last item => parent
|
||||
if(empty($tpl->_compatible)) {
|
||||
$body = $t->getBody();
|
||||
} else {
|
||||
$body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); ?>'.$t->getBody();
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
$body .= $t->getBody();
|
||||
}
|
||||
} else {
|
||||
$body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); $parent->b = &$tpl->b; $parent->display((array)$tpl); unset($tpl->b, $parent->b); ?>';
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -459,27 +459,27 @@ class Compiler {
|
||||
if($name) {
|
||||
$donor = $tpl->getStorage()->getRawTemplate()->load($name, false);
|
||||
$donor->_extended = true;
|
||||
$donor->_extends = $tpl;
|
||||
$donor->_compatible = &$tpl->_compatible;
|
||||
//$donor->blocks = &$tpl->blocks;
|
||||
$donor->compile();
|
||||
$blocks = $donor->blocks;
|
||||
foreach($blocks as $name => $code) {
|
||||
if(isset($tpl->blocks[$name])) {
|
||||
$tpl->blocks[$name] = $code;
|
||||
unset($blocks[$name]);
|
||||
}
|
||||
}
|
||||
$tpl->uses = $blocks + $tpl->uses;
|
||||
$tpl->addDepend($donor);
|
||||
return '?>'.$donor->getBody().'<?php ';
|
||||
$donor->_extends = $tpl;
|
||||
$donor->_compatible = &$tpl->_compatible;
|
||||
//$donor->blocks = &$tpl->blocks;
|
||||
$donor->compile();
|
||||
$blocks = $donor->blocks;
|
||||
foreach($blocks as $name => $code) {
|
||||
if(isset($tpl->blocks[$name])) {
|
||||
$tpl->blocks[$name] = $code;
|
||||
unset($blocks[$name]);
|
||||
}
|
||||
}
|
||||
$tpl->uses = $blocks + $tpl->uses;
|
||||
$tpl->addDepend($donor);
|
||||
return '?>'.$donor->getBody().'<?php ';
|
||||
} else {
|
||||
// throw new InvalidUsageException('template name must be given explicitly yet');
|
||||
// under construction
|
||||
$tpl->_compatible = true;
|
||||
return '$donor = $tpl->getStorage()->getTemplate('.$cname.', \Fenom\Template::EXTENDED);'.PHP_EOL.
|
||||
'$donor->fetch((array)$tpl);'.PHP_EOL.
|
||||
'$tpl->b += (array)$donor->b';
|
||||
'$donor->fetch((array)$tpl);'.PHP_EOL.
|
||||
'$tpl->b += (array)$donor->b';
|
||||
}
|
||||
}
|
||||
|
||||
@ -495,8 +495,8 @@ class Compiler {
|
||||
var_dump("".$scope->tpl);
|
||||
$scope->tpl->_compatible = true;
|
||||
}
|
||||
$scope["cname"] = $scope->tpl->parsePlainArg($tokens, $name);
|
||||
$scope["name"] = $name;
|
||||
$scope["cname"] = $scope->tpl->parsePlainArg($tokens, $name);
|
||||
$scope["name"] = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -507,57 +507,57 @@ class Compiler {
|
||||
*/
|
||||
public static function tagBlockClose($tokens, Scope $scope) {
|
||||
|
||||
$tpl = $scope->tpl;
|
||||
if(isset($tpl->_extends)) { // is child
|
||||
if($scope["name"]) { // is scalar name
|
||||
if($tpl->_compatible) { // is compatible mode
|
||||
$scope->replaceContent(
|
||||
'<?php /* 1) Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(empty($tpl->b['.$scope["cname"].'])) { '.
|
||||
'$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
|
||||
$scope->getContent().
|
||||
"<?php };".
|
||||
"} ?>".PHP_EOL
|
||||
);
|
||||
} elseif(!isset($tpl->blocks[ $scope["name"] ])) { // is block not registered
|
||||
$tpl->blocks[ $scope["name"] ] = $scope->getContent();
|
||||
$scope->replaceContent(
|
||||
'<?php /* 2) Block '.$tpl.': '.$scope["cname"].' '.$tpl->_compatible.' */'.PHP_EOL.' $tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
|
||||
$scope->getContent().
|
||||
"<?php }; ?>".PHP_EOL
|
||||
);
|
||||
}
|
||||
} else { // dynamic name
|
||||
$tpl->_compatible = true; // enable compatible mode
|
||||
$scope->replaceContent(
|
||||
'<?php /* 3) Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(empty($tpl->b['.$scope["cname"].'])) { '.
|
||||
'$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
|
||||
$scope->getContent().
|
||||
"<?php };".
|
||||
"} ?>".PHP_EOL
|
||||
);
|
||||
}
|
||||
} else { // is parent
|
||||
if(isset($tpl->blocks[ $scope["name"] ])) { // has block
|
||||
if($tpl->_compatible) { // compatible mode enabled
|
||||
$scope->replaceContent(
|
||||
'<?php /* 4) Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(isset($tpl->b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL.
|
||||
$tpl->blocks[ $scope["name"] ].
|
||||
'<?php } ?>'.PHP_EOL
|
||||
);
|
||||
$tpl = $scope->tpl;
|
||||
if(isset($tpl->_extends)) { // is child
|
||||
if($scope["name"]) { // is scalar name
|
||||
if($tpl->_compatible) { // is compatible mode
|
||||
$scope->replaceContent(
|
||||
'<?php /* 1) Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(empty($tpl->b['.$scope["cname"].'])) { '.
|
||||
'$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
|
||||
$scope->getContent().
|
||||
"<?php };".
|
||||
"} ?>".PHP_EOL
|
||||
);
|
||||
} elseif(!isset($tpl->blocks[ $scope["name"] ])) { // is block not registered
|
||||
$tpl->blocks[ $scope["name"] ] = $scope->getContent();
|
||||
$scope->replaceContent(
|
||||
'<?php /* 2) Block '.$tpl.': '.$scope["cname"].' '.$tpl->_compatible.' */'.PHP_EOL.' $tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
|
||||
$scope->getContent().
|
||||
"<?php }; ?>".PHP_EOL
|
||||
);
|
||||
}
|
||||
} else { // dynamic name
|
||||
$tpl->_compatible = true; // enable compatible mode
|
||||
$scope->replaceContent(
|
||||
'<?php /* 3) Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(empty($tpl->b['.$scope["cname"].'])) { '.
|
||||
'$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
|
||||
$scope->getContent().
|
||||
"<?php };".
|
||||
"} ?>".PHP_EOL
|
||||
);
|
||||
}
|
||||
} else { // is parent
|
||||
if(isset($tpl->blocks[ $scope["name"] ])) { // has block
|
||||
if($tpl->_compatible) { // compatible mode enabled
|
||||
$scope->replaceContent(
|
||||
'<?php /* 4) Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(isset($tpl->b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL.
|
||||
$tpl->blocks[ $scope["name"] ].
|
||||
'<?php } ?>'.PHP_EOL
|
||||
);
|
||||
|
||||
} else {
|
||||
$scope->replaceContent($tpl->blocks[ $scope["name"] ]);
|
||||
}
|
||||
} else {
|
||||
$scope->replaceContent($tpl->blocks[ $scope["name"] ]);
|
||||
}
|
||||
// } elseif(isset($tpl->_extended) || !empty($tpl->_compatible)) {
|
||||
} elseif(isset($tpl->_extended) && $tpl->_compatible || empty($tpl->_extended)) {
|
||||
$scope->replaceContent(
|
||||
'<?php /* 5) Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(isset($tpl->b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL.
|
||||
$scope->getContent().
|
||||
'<?php } ?>'.PHP_EOL
|
||||
);
|
||||
}
|
||||
}
|
||||
return '';
|
||||
} elseif(isset($tpl->_extended) && $tpl->_compatible || empty($tpl->_extended)) {
|
||||
$scope->replaceContent(
|
||||
'<?php /* 5) Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(isset($tpl->b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL.
|
||||
$scope->getContent().
|
||||
'<?php } ?>'.PHP_EOL
|
||||
);
|
||||
}
|
||||
}
|
||||
return '';
|
||||
|
||||
}
|
||||
|
||||
@ -659,38 +659,38 @@ class Compiler {
|
||||
return 'array('.implode(",", $_code).')';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @return string
|
||||
*/
|
||||
public static function varOpen(Tokenizer $tokens, Scope $scope) {
|
||||
$var = $scope->tpl->parseVariable($tokens, Template::DENY_MODS);
|
||||
if($tokens->is('=')) { // inline tag {var ...}
|
||||
$scope->is_closed = true;
|
||||
$tokens->next();
|
||||
if($tokens->is("[")) {
|
||||
return $var.'='.$scope->tpl->parseArray($tokens);
|
||||
} else {
|
||||
return $var.'='.$scope->tpl->parseExp($tokens, true);
|
||||
}
|
||||
} else {
|
||||
$scope["name"] = $var;
|
||||
if($tokens->is('|')) {
|
||||
$scope["value"] = $scope->tpl->parseModifier($tokens, "ob_get_clean()");
|
||||
} else {
|
||||
$scope["value"] = "ob_get_clean()";
|
||||
}
|
||||
return 'ob_start();';
|
||||
}
|
||||
/**
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @return string
|
||||
*/
|
||||
public static function varOpen(Tokenizer $tokens, Scope $scope) {
|
||||
$var = $scope->tpl->parseVariable($tokens, Template::DENY_MODS);
|
||||
if($tokens->is('=')) { // inline tag {var ...}
|
||||
$scope->is_closed = true;
|
||||
$tokens->next();
|
||||
if($tokens->is("[")) {
|
||||
return $var.'='.$scope->tpl->parseArray($tokens);
|
||||
} else {
|
||||
return $var.'='.$scope->tpl->parseExp($tokens, true);
|
||||
}
|
||||
} else {
|
||||
$scope["name"] = $var;
|
||||
if($tokens->is('|')) {
|
||||
$scope["value"] = $scope->tpl->parseModifier($tokens, "ob_get_clean()");
|
||||
} else {
|
||||
$scope["value"] = "ob_get_clean()";
|
||||
}
|
||||
return 'ob_start();';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @return string
|
||||
*/
|
||||
public static function varClose(Tokenizer $tokens, Scope $scope) {
|
||||
/**
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @return string
|
||||
*/
|
||||
public static function varClose(Tokenizer $tokens, Scope $scope) {
|
||||
return $scope["name"].'='.$scope["value"].';';
|
||||
}
|
||||
|
||||
@ -723,11 +723,11 @@ class Compiler {
|
||||
* @throws InvalidUsageException
|
||||
*/
|
||||
public static function tagCycle(Tokenizer $tokens, Template $tpl) {
|
||||
if($tokens->is("[")) {
|
||||
$exp = $tpl->parseArray($tokens);
|
||||
} else {
|
||||
$exp = $tpl->parseExp($tokens, true);
|
||||
}
|
||||
if($tokens->is("[")) {
|
||||
$exp = $tpl->parseArray($tokens);
|
||||
} else {
|
||||
$exp = $tpl->parseExp($tokens, true);
|
||||
}
|
||||
if($tokens->valid()) {
|
||||
$p = $tpl->parseParams($tokens);
|
||||
if(empty($p["index"])) {
|
||||
@ -741,13 +741,13 @@ class Compiler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runtime cycle callback
|
||||
* @param mixed $vals
|
||||
* @param $index
|
||||
* @return mixed
|
||||
*/
|
||||
public static function cycle($vals, $index) {
|
||||
/**
|
||||
* Runtime cycle callback
|
||||
* @param mixed $vals
|
||||
* @param $index
|
||||
* @return mixed
|
||||
*/
|
||||
public static function cycle($vals, $index) {
|
||||
return $vals[$index % count($vals)];
|
||||
}
|
||||
|
||||
|
@ -26,9 +26,13 @@ class Provider implements ProviderInterface {
|
||||
if(is_file($path)) {
|
||||
unlink($path);
|
||||
} elseif(is_dir($path)) {
|
||||
$iterator = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path,
|
||||
\FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS),
|
||||
\RecursiveIteratorIterator::CHILD_FIRST));
|
||||
$iterator = iterator_to_array(
|
||||
new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($path,
|
||||
\FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS),
|
||||
\RecursiveIteratorIterator::CHILD_FIRST
|
||||
)
|
||||
);
|
||||
foreach($iterator as $file) {
|
||||
/* @var \splFileInfo $file*/
|
||||
if($file->isFile()) {
|
||||
|
@ -18,21 +18,21 @@ use Fenom;
|
||||
*/
|
||||
class Template extends Render {
|
||||
|
||||
/**
|
||||
* Disable array parser.
|
||||
*/
|
||||
const DENY_ARRAY = 1;
|
||||
/**
|
||||
* Disable modifier parser.
|
||||
*/
|
||||
const DENY_MODS = 2;
|
||||
/**
|
||||
* Disable array parser.
|
||||
*/
|
||||
const DENY_ARRAY = 1;
|
||||
/**
|
||||
* Disable modifier parser.
|
||||
*/
|
||||
const DENY_MODS = 2;
|
||||
|
||||
/**
|
||||
* Template was extended
|
||||
*/
|
||||
const DYNAMIC_EXTEND = 0x1000;
|
||||
const EXTENDED = 0x2000;
|
||||
const DYNAMIC_BLOCK = 0x4000;
|
||||
/**
|
||||
* Template was extended
|
||||
*/
|
||||
const DYNAMIC_EXTEND = 0x1000;
|
||||
const EXTENDED = 0x2000;
|
||||
const DYNAMIC_BLOCK = 0x4000;
|
||||
|
||||
/**
|
||||
* @var int shared counter
|
||||
@ -54,13 +54,13 @@ class Template extends Render {
|
||||
*/
|
||||
public $blocks = array();
|
||||
|
||||
public $uses = array();
|
||||
public $uses = array();
|
||||
|
||||
public $parents = array();
|
||||
public $parents = array();
|
||||
|
||||
public $_extends;
|
||||
public $_extended = false;
|
||||
public $_compatible;
|
||||
public $_extends;
|
||||
public $_extended = false;
|
||||
public $_compatible;
|
||||
|
||||
/**
|
||||
* Call stack
|
||||
@ -126,9 +126,9 @@ class Template extends Render {
|
||||
$this->_scm = $provider;
|
||||
$this->_base_name = substr($name, strlen($provider) + 1);
|
||||
} else {
|
||||
$this->_base_name = $name;
|
||||
$this->_base_name = $name;
|
||||
}
|
||||
$this->_provider = $this->_fenom->getProvider($provider);
|
||||
$this->_provider = $this->_fenom->getProvider($provider);
|
||||
$this->_src = $this->_provider->getSource($this->_base_name, $this->_time);
|
||||
if($compile) {
|
||||
$this->compile();
|
||||
@ -158,59 +158,59 @@ class Template extends Render {
|
||||
* @throws CompileException
|
||||
*/
|
||||
public function compile() {
|
||||
$end = $pos = 0;
|
||||
$end = $pos = 0;
|
||||
while(($start = strpos($this->_src, '{', $pos)) !== false) { // search open-symbol of tags
|
||||
switch($this->_src[$start + 1]) { // check next character
|
||||
case "\n": case "\r": case "\t": case " ": case "}": // ignore the tag
|
||||
$pos = $start + 1;
|
||||
continue 2;
|
||||
$pos = $start + 1;
|
||||
continue 2;
|
||||
case "*": // if comments
|
||||
$end = strpos($this->_src, '*}', $start) + 1; // find end of the comment block
|
||||
if($end === false) {
|
||||
throw new CompileException("Unclosed comment block in line {$this->_line}", 0, 1, $this->_name, $this->_line);
|
||||
}
|
||||
$this->_appendText(substr($this->_src, $pos, $start - $pos));
|
||||
if($end === false) {
|
||||
throw new CompileException("Unclosed comment block in line {$this->_line}", 0, 1, $this->_name, $this->_line);
|
||||
}
|
||||
$this->_appendText(substr($this->_src, $pos, $start - $pos));
|
||||
$comment = substr($this->_src, $start, $end - $start); // read the comment block for processing
|
||||
$this->_line += substr_count($comment, "\n"); // count lines in comments
|
||||
unset($comment); // cleanup
|
||||
unset($comment); // cleanup
|
||||
$pos = $end + 1;
|
||||
continue 2;
|
||||
}
|
||||
$frag = substr($this->_src, $pos, $start - $pos); // variable $frag contains chars after previous '}' and current '{'
|
||||
$this->_appendText($frag);
|
||||
$frag = substr($this->_src, $pos, $start - $pos); // variable $frag contains chars after previous '}' and current '{'
|
||||
$this->_appendText($frag);
|
||||
|
||||
$from = $start;
|
||||
reparse: { // yep, i use goto operator. For this algorithm it is good choice
|
||||
$end = strpos($this->_src, '}', $from); // search close-symbol of the tag
|
||||
if($end === false) { // if unexpected end of template
|
||||
throw new CompileException("Unclosed tag in line {$this->_line}", 0, 1, $this->_name, $this->_line);
|
||||
$from = $start;
|
||||
reparse: { // yep, i use goto operator. For this algorithm it is good choice
|
||||
$end = strpos($this->_src, '}', $from); // search close-symbol of the tag
|
||||
if($end === false) { // if unexpected end of template
|
||||
throw new CompileException("Unclosed tag in line {$this->_line}", 0, 1, $this->_name, $this->_line);
|
||||
}
|
||||
$tag = substr($this->_src, $start, $end - $start + 1); // variable $tag contains fenom tag '{...}'
|
||||
|
||||
$_tag = substr($tag, 1, -1); // strip delimiters '{' and '}'
|
||||
$_tag = substr($tag, 1, -1); // strip delimiters '{' and '}'
|
||||
|
||||
if($this->_ignore) { // check ignore
|
||||
if($_tag === '/ignore') { // turn off ignore
|
||||
$this->_ignore = false;
|
||||
} else { // still ignore
|
||||
$this->_appendText($tag);
|
||||
}
|
||||
$pos = $start + strlen($tag);
|
||||
continue;
|
||||
} else {
|
||||
$tokens = new Tokenizer($_tag); // tokenize the tag
|
||||
if($tokens->isIncomplete()) { // all strings finished?
|
||||
$from = $end + 1;
|
||||
goto reparse; // need next close-symbol
|
||||
}
|
||||
$this->_appendCode( $this->_tag($tokens) , $tag); // start the tag lexer
|
||||
$pos = $end + 1; // move search-pointer to end of the tag
|
||||
if($tokens->key()) { // if tokenizer have tokens - throws exceptions
|
||||
throw new CompileException("Unexpected token '".$tokens->current()."' in {$this} line {$this->_line}, near '{".$tokens->getSnippetAsString(0,0)."' <- there", 0, E_ERROR, $this->_name, $this->_line);
|
||||
}
|
||||
if($this->_ignore) { // check ignore
|
||||
if($_tag === '/ignore') { // turn off ignore
|
||||
$this->_ignore = false;
|
||||
} else { // still ignore
|
||||
$this->_appendText($tag);
|
||||
}
|
||||
$pos = $start + strlen($tag);
|
||||
continue;
|
||||
} else {
|
||||
$tokens = new Tokenizer($_tag); // tokenize the tag
|
||||
if($tokens->isIncomplete()) { // all strings finished?
|
||||
$from = $end + 1;
|
||||
goto reparse; // need next close-symbol
|
||||
}
|
||||
$this->_appendCode( $this->_tag($tokens) , $tag); // start the tag lexer
|
||||
$pos = $end + 1; // move search-pointer to end of the tag
|
||||
if($tokens->key()) { // if tokenizer have tokens - throws exceptions
|
||||
throw new CompileException("Unexpected token '".$tokens->current()."' in {$this} line {$this->_line}, near '{".$tokens->getSnippetAsString(0,0)."' <- there", 0, E_ERROR, $this->_name, $this->_line);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($frag, $_tag, $tag); // cleanup
|
||||
}
|
||||
gc_collect_cycles();
|
||||
@ -248,7 +248,7 @@ class Template extends Render {
|
||||
* @param string $text
|
||||
*/
|
||||
private function _appendText($text) {
|
||||
$this->_line += substr_count($text, "\n");
|
||||
$this->_line += substr_count($text, "\n");
|
||||
if($this->_filter) {
|
||||
if(strpos($text, "<?") === false) {
|
||||
$this->_body .= $text;
|
||||
@ -298,7 +298,7 @@ class Template extends Render {
|
||||
if(!$code) {
|
||||
return;
|
||||
} else {
|
||||
$this->_line += substr_count($source, "\n");
|
||||
$this->_line += substr_count($source, "\n");
|
||||
if(strpos($code, '?>') !== false) {
|
||||
$code = $this->_escapeCode($code); // paste PHP_EOL
|
||||
}
|
||||
@ -329,15 +329,15 @@ class Template extends Render {
|
||||
*/
|
||||
public function getTemplateCode() {
|
||||
return "<?php \n".
|
||||
"/** Fenom template '".$this->_name."' compiled at ".date('Y-m-d H:i:s')." */\n".
|
||||
"return new Fenom\\Render(\$fenom, ".$this->_getClosureSource().", ".var_export(array(
|
||||
"options" => $this->_options,
|
||||
"provider" => $this->_scm,
|
||||
"name" => $this->_name,
|
||||
"base_name" => $this->_base_name,
|
||||
"time" => $this->_time,
|
||||
"depends" => $this->_depends
|
||||
), true).");\n";
|
||||
"/** Fenom template '".$this->_name."' compiled at ".date('Y-m-d H:i:s')." */\n".
|
||||
"return new Fenom\\Render(\$fenom, ".$this->_getClosureSource().", ".var_export(array(
|
||||
"options" => $this->_options,
|
||||
"provider" => $this->_scm,
|
||||
"name" => $this->_name,
|
||||
"base_name" => $this->_base_name,
|
||||
"time" => $this->_time,
|
||||
"depends" => $this->_depends
|
||||
), true).");\n";
|
||||
}
|
||||
|
||||
/**
|
||||
@ -391,14 +391,14 @@ class Template extends Render {
|
||||
return parent::fetch($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal tags router
|
||||
* @param Tokenizer $tokens
|
||||
*
|
||||
* @throws SecurityException
|
||||
* @throws CompileException
|
||||
* @return string executable PHP code
|
||||
*/
|
||||
/**
|
||||
* Internal tags router
|
||||
* @param Tokenizer $tokens
|
||||
*
|
||||
* @throws SecurityException
|
||||
* @throws CompileException
|
||||
* @return string executable PHP code
|
||||
*/
|
||||
private function _tag(Tokenizer $tokens) {
|
||||
try {
|
||||
if($tokens->is(Tokenizer::MACRO_STRING)) {
|
||||
@ -776,13 +776,13 @@ class Template extends Render {
|
||||
return $_scalar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse string with or without variable
|
||||
*
|
||||
* @param Tokenizer $tokens
|
||||
* @throws UnexpectedTokenException
|
||||
* @return string
|
||||
*/
|
||||
/**
|
||||
* Parse string with or without variable
|
||||
*
|
||||
* @param Tokenizer $tokens
|
||||
* @throws UnexpectedTokenException
|
||||
* @return string
|
||||
*/
|
||||
public function parseSubstr(Tokenizer $tokens) {
|
||||
if($tokens->is('"',"`")) {
|
||||
$stop = $tokens->current();
|
||||
|
@ -79,7 +79,7 @@ class Tokenizer {
|
||||
|
||||
public $tokens;
|
||||
public $p = 0;
|
||||
public $quotes = 0;
|
||||
public $quotes = 0;
|
||||
private $_max = 0;
|
||||
private $_last_no = 0;
|
||||
|
||||
@ -143,55 +143,55 @@ class Tokenizer {
|
||||
'true' => 1, 'false' => 1, 'null' => 1, 'TRUE' => 1, 'FALSE' => 1, 'NULL' => 1
|
||||
);
|
||||
|
||||
/**
|
||||
* @param $query
|
||||
*/
|
||||
public function __construct($query) {
|
||||
$tokens = array(-1 => array(\T_WHITESPACE, '', '', 1));
|
||||
$_tokens = token_get_all("<?php ".$query);
|
||||
$line = 1;
|
||||
array_shift($_tokens);
|
||||
$i = 0;
|
||||
foreach($_tokens as $token) {
|
||||
if(is_string($token)) {
|
||||
if($token === '"' || $token === "'" || $token === "`") {
|
||||
$this->quotes++;
|
||||
}
|
||||
$tokens[] = array(
|
||||
$token,
|
||||
$token,
|
||||
"",
|
||||
$line,
|
||||
);
|
||||
$i++;
|
||||
} elseif ($token[0] === \T_WHITESPACE) {
|
||||
$tokens[$i-1][2] = $token[1];
|
||||
} else {
|
||||
$tokens[] = array(
|
||||
$token[0],
|
||||
$token[1],
|
||||
"",
|
||||
$line = $token[2],
|
||||
token_name($token[0]) // debug
|
||||
);
|
||||
$i++;
|
||||
}
|
||||
/**
|
||||
* @param $query
|
||||
*/
|
||||
public function __construct($query) {
|
||||
$tokens = array(-1 => array(\T_WHITESPACE, '', '', 1));
|
||||
$_tokens = token_get_all("<?php ".$query);
|
||||
$line = 1;
|
||||
array_shift($_tokens);
|
||||
$i = 0;
|
||||
foreach($_tokens as $token) {
|
||||
if(is_string($token)) {
|
||||
if($token === '"' || $token === "'" || $token === "`") {
|
||||
$this->quotes++;
|
||||
}
|
||||
$tokens[] = array(
|
||||
$token,
|
||||
$token,
|
||||
"",
|
||||
$line,
|
||||
);
|
||||
$i++;
|
||||
} elseif ($token[0] === \T_WHITESPACE) {
|
||||
$tokens[$i-1][2] = $token[1];
|
||||
} else {
|
||||
$tokens[] = array(
|
||||
$token[0],
|
||||
$token[1],
|
||||
"",
|
||||
$line = $token[2],
|
||||
token_name($token[0]) // debug
|
||||
);
|
||||
$i++;
|
||||
}
|
||||
|
||||
}
|
||||
unset($tokens[-1]);
|
||||
$this->tokens = $tokens;
|
||||
}
|
||||
unset($tokens[-1]);
|
||||
$this->tokens = $tokens;
|
||||
$this->_max = count($this->tokens) - 1;
|
||||
$this->_last_no = $this->tokens[$this->_max][3];
|
||||
}
|
||||
|
||||
/**
|
||||
* Is incomplete mean some string not closed
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function isIncomplete() {
|
||||
return ($this->quotes % 2) || ($this->tokens[$this->_max][0] === T_ENCAPSED_AND_WHITESPACE);
|
||||
}
|
||||
/**
|
||||
* Is incomplete mean some string not closed
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function isIncomplete() {
|
||||
return ($this->quotes % 2) || ($this->tokens[$this->_max][0] === T_ENCAPSED_AND_WHITESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current element
|
||||
|
Loading…
Reference in New Issue
Block a user