Reformat. Fix pipe. Add more tests

This commit is contained in:
bzick 2014-05-06 14:22:58 +04:00
parent 6f969ee124
commit cd490d2bf6
20 changed files with 1625 additions and 739 deletions

View File

@ -20,33 +20,33 @@ class Fenom
const VERSION = '2.0';
/* Actions */
const INLINE_COMPILER = 1;
const BLOCK_COMPILER = 5;
const BLOCK_COMPILER = 5;
const INLINE_FUNCTION = 2;
const BLOCK_FUNCTION = 7;
const BLOCK_FUNCTION = 7;
/* Options */
const DENY_ACCESSOR = 0x8;
const DENY_METHODS = 0x10;
const DENY_ACCESSOR = 0x8;
const DENY_METHODS = 0x10;
const DENY_NATIVE_FUNCS = 0x20;
const FORCE_INCLUDE = 0x40;
const AUTO_RELOAD = 0x80;
const FORCE_COMPILE = 0x100;
const AUTO_ESCAPE = 0x200;
const DISABLE_CACHE = 0x400;
const FORCE_VERIFY = 0x800;
const AUTO_TRIM = 0x1000; // reserved
const FORCE_INCLUDE = 0x40;
const AUTO_RELOAD = 0x80;
const FORCE_COMPILE = 0x100;
const AUTO_ESCAPE = 0x200;
const DISABLE_CACHE = 0x400;
const FORCE_VERIFY = 0x800;
const AUTO_TRIM = 0x1000; // reserved
const DENY_STATICS = 0x2000;
const AUTO_STRIP = 0x4000;
const AUTO_STRIP = 0x4000;
/* @deprecated */
const DENY_INLINE_FUNCS = 0x20;
/* Default parsers */
const DEFAULT_CLOSE_COMPILER = 'Fenom\Compiler::stdClose';
const DEFAULT_FUNC_PARSER = 'Fenom\Compiler::stdFuncParser';
const DEFAULT_FUNC_OPEN = 'Fenom\Compiler::stdFuncOpen';
const DEFAULT_FUNC_CLOSE = 'Fenom\Compiler::stdFuncClose';
const SMART_FUNC_PARSER = 'Fenom\Compiler::smartFuncParser';
const DEFAULT_FUNC_PARSER = 'Fenom\Compiler::stdFuncParser';
const DEFAULT_FUNC_OPEN = 'Fenom\Compiler::stdFuncOpen';
const DEFAULT_FUNC_CLOSE = 'Fenom\Compiler::stdFuncClose';
const SMART_FUNC_PARSER = 'Fenom\Compiler::smartFuncParser';
const MAX_MACRO_RECURSIVE = 32;
@ -55,17 +55,17 @@ class Fenom
* @see setOptions
*/
private static $_options_list = array(
"disable_accessor" => self::DENY_ACCESSOR,
"disable_methods" => self::DENY_METHODS,
"disable_accessor" => self::DENY_ACCESSOR,
"disable_methods" => self::DENY_METHODS,
"disable_native_funcs" => self::DENY_NATIVE_FUNCS,
"disable_cache" => self::DISABLE_CACHE,
"force_compile" => self::FORCE_COMPILE,
"auto_reload" => self::AUTO_RELOAD,
"force_include" => self::FORCE_INCLUDE,
"auto_escape" => self::AUTO_ESCAPE,
"force_verify" => self::FORCE_VERIFY,
"auto_trim" => self::AUTO_TRIM,
"disable_statics" => self::DENY_STATICS,
"disable_cache" => self::DISABLE_CACHE,
"force_compile" => self::FORCE_COMPILE,
"auto_reload" => self::AUTO_RELOAD,
"force_include" => self::FORCE_INCLUDE,
"auto_escape" => self::AUTO_ESCAPE,
"force_verify" => self::FORCE_VERIFY,
"auto_trim" => self::AUTO_TRIM,
"disable_statics" => self::DENY_STATICS,
);
/**
@ -121,138 +121,153 @@ class Fenom
* @var string[] list of modifiers [modifier_name => callable]
*/
protected $_modifiers = array(
"upper" => 'strtoupper',
"up" => 'strtoupper',
"lower" => 'strtolower',
"low" => 'strtolower',
"upper" => 'strtoupper',
"up" => 'strtoupper',
"lower" => 'strtolower',
"low" => 'strtolower',
"date_format" => 'Fenom\Modifier::dateFormat',
"date" => 'Fenom\Modifier::date',
"truncate" => 'Fenom\Modifier::truncate',
"escape" => 'Fenom\Modifier::escape',
"e" => 'Fenom\Modifier::escape', // alias of escape
"unescape" => 'Fenom\Modifier::unescape',
"strip" => 'Fenom\Modifier::strip',
"length" => 'Fenom\Modifier::length',
"iterable" => 'Fenom\Modifier::isIterable'
"date" => 'Fenom\Modifier::date',
"truncate" => 'Fenom\Modifier::truncate',
"escape" => 'Fenom\Modifier::escape',
"e" => 'Fenom\Modifier::escape', // alias of escape
"unescape" => 'Fenom\Modifier::unescape',
"strip" => 'Fenom\Modifier::strip',
"length" => 'Fenom\Modifier::length',
"iterable" => 'Fenom\Modifier::isIterable'
);
/**
* @var array of allowed PHP functions
*/
protected $_allowed_funcs = array(
"count" => 1, "is_string" => 1, "is_array" => 1, "is_numeric" => 1, "is_int" => 1, 'constant' => 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
"count" => 1,
"is_string" => 1,
"is_array" => 1,
"is_numeric" => 1,
"is_int" => 1,
'constant' => 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
);
/**
* @var array[] of compilers and functions
*/
protected $_actions = array(
'foreach' => array( // {foreach ...} {break} {continue} {foreachelse} {/foreach}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::foreachOpen',
'close' => 'Fenom\Compiler::foreachClose',
'tags' => array(
'foreach' => array( // {foreach ...} {break} {continue} {foreachelse} {/foreach}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::foreachOpen',
'close' => 'Fenom\Compiler::foreachClose',
'tags' => array(
'foreachelse' => 'Fenom\Compiler::foreachElse',
'break' => 'Fenom\Compiler::tagBreak',
'continue' => 'Fenom\Compiler::tagContinue',
'break' => 'Fenom\Compiler::tagBreak',
'continue' => 'Fenom\Compiler::tagContinue',
),
'float_tags' => array('break' => 1, 'continue' => 1)
),
'if' => array( // {if ...} {elseif ...} {else} {/if}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::ifOpen',
'if' => array( // {if ...} {elseif ...} {else} {/if}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::ifOpen',
'close' => 'Fenom\Compiler::stdClose',
'tags' => array(
'tags' => array(
'elseif' => 'Fenom\Compiler::tagElseIf',
'else' => 'Fenom\Compiler::tagElse'
'else' => 'Fenom\Compiler::tagElse'
)
),
'switch' => array( // {switch ...} {case ..., ...} {default} {/switch}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::switchOpen',
'close' => 'Fenom\Compiler::switchClose',
'tags' => array(
'case' => 'Fenom\Compiler::tagCase',
'switch' => array( // {switch ...} {case ..., ...} {default} {/switch}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::switchOpen',
'close' => 'Fenom\Compiler::switchClose',
'tags' => array(
'case' => 'Fenom\Compiler::tagCase',
'default' => 'Fenom\Compiler::tagDefault'
),
'float_tags' => array('break' => 1)
),
'for' => array( // {for ...} {break} {continue} {/for}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::forOpen',
'close' => 'Fenom\Compiler::forClose',
'tags' => array(
'forelse' => 'Fenom\Compiler::forElse',
'break' => 'Fenom\Compiler::tagBreak',
'for' => array( // {for ...} {break} {continue} {/for}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::forOpen',
'close' => 'Fenom\Compiler::forClose',
'tags' => array(
'forelse' => 'Fenom\Compiler::forElse',
'break' => 'Fenom\Compiler::tagBreak',
'continue' => 'Fenom\Compiler::tagContinue',
),
'float_tags' => array('break' => 1, 'continue' => 1)
),
'while' => array( // {while ...} {break} {continue} {/while}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::whileOpen',
'close' => 'Fenom\Compiler::stdClose',
'tags' => array(
'break' => 'Fenom\Compiler::tagBreak',
'while' => array( // {while ...} {break} {continue} {/while}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::whileOpen',
'close' => 'Fenom\Compiler::stdClose',
'tags' => array(
'break' => 'Fenom\Compiler::tagBreak',
'continue' => 'Fenom\Compiler::tagContinue',
),
'float_tags' => array('break' => 1, 'continue' => 1)
),
'include' => array( // {include ...}
'type' => self::INLINE_COMPILER,
'include' => array( // {include ...}
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagInclude'
),
'insert' => array( // {include ...}
'type' => self::INLINE_COMPILER,
'insert' => array( // {include ...}
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagInsert'
),
'var' => array( // {var ...}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::varOpen',
'var' => array( // {var ...}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::varOpen',
'close' => 'Fenom\Compiler::varClose'
),
'block' => array( // {block ...} {parent} {/block}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::tagBlockOpen',
'close' => 'Fenom\Compiler::tagBlockClose',
'tags' => array('parent' => 'Fenom\Compiler::tagParent'),
'block' => array( // {block ...} {parent} {/block}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::tagBlockOpen',
'close' => 'Fenom\Compiler::tagBlockClose',
'tags' => array('parent' => 'Fenom\Compiler::tagParent'),
'float_tags' => array('parent' => 1)
),
'extends' => array( // {extends ...}
'type' => self::INLINE_COMPILER,
'extends' => array( // {extends ...}
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagExtends'
),
'use' => array( // {use}
'type' => self::INLINE_COMPILER,
'use' => array( // {use}
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagUse'
),
'filter' => array( // {filter} ... {/filter}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::filterOpen',
'filter' => array( // {filter} ... {/filter}
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::filterOpen',
'close' => 'Fenom\Compiler::filterClose'
),
'macro' => array(
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::macroOpen',
'macro' => array(
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::macroOpen',
'close' => 'Fenom\Compiler::macroClose'
),
'import' => array(
'type' => self::INLINE_COMPILER,
'import' => array(
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagImport'
),
'cycle' => array(
'type' => self::INLINE_COMPILER,
'cycle' => array(
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagCycle'
),
'raw' => array(
'type' => self::INLINE_COMPILER,
'raw' => array(
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagRaw'
),
'autoescape' => array(
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::autoescapeOpen',
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::autoescapeOpen',
'close' => 'Fenom\Compiler::autoescapeClose'
)
);
@ -263,31 +278,31 @@ class Fenom
* @var array
*/
protected $_tests = array(
'integer' => 'is_int(%s)',
'int' => 'is_int(%s)',
'float' => 'is_float(%s)',
'double' => 'is_float(%s)',
'decimal' => 'is_float(%s)',
'string' => 'is_string(%s)',
'bool' => 'is_bool(%s)',
'boolean' => 'is_bool(%s)',
'number' => 'is_numeric(%s)',
'numeric' => 'is_numeric(%s)',
'scalar' => 'is_scalar(%s)',
'object' => 'is_object(%s)',
'integer' => 'is_int(%s)',
'int' => 'is_int(%s)',
'float' => 'is_float(%s)',
'double' => 'is_float(%s)',
'decimal' => 'is_float(%s)',
'string' => 'is_string(%s)',
'bool' => 'is_bool(%s)',
'boolean' => 'is_bool(%s)',
'number' => 'is_numeric(%s)',
'numeric' => 'is_numeric(%s)',
'scalar' => 'is_scalar(%s)',
'object' => 'is_object(%s)',
'callable' => 'is_callable(%s)',
'callback' => 'is_callable(%s)',
'array' => 'is_array(%s)',
'array' => 'is_array(%s)',
'iterable' => '\Fenom\Modifier::isIterable(%s)',
'const' => 'defined(%s)',
'const' => 'defined(%s)',
'template' => '$tpl->getStorage()->templateExists(%s)',
'empty' => 'empty(%s)',
'set' => 'isset(%s)',
'_empty' => '!%s', // for none variable
'_set' => '(%s !== null)', // for none variable
'odd' => '(%s & 1)',
'even' => '!(%s %% 2)',
'third' => '!(%s %% 3)'
'empty' => 'empty(%s)',
'set' => 'isset(%s)',
'_empty' => '!%s', // for none variable
'_set' => '(%s !== null)', // for none variable
'odd' => '(%s & 1)',
'even' => '!(%s %% 2)',
'third' => '!(%s %% 3)'
);
/**
@ -429,7 +444,7 @@ class Fenom
public function addCompiler($compiler, $parser)
{
$this->_actions[$compiler] = array(
'type' => self::INLINE_COMPILER,
'type' => self::INLINE_COMPILER,
'parser' => $parser
);
return $this;
@ -444,7 +459,7 @@ class Fenom
{
if (method_exists($storage, "tag" . $compiler)) {
$this->_actions[$compiler] = array(
'type' => self::INLINE_COMPILER,
'type' => self::INLINE_COMPILER,
'parser' => array($storage, "tag" . $compiler)
);
}
@ -460,13 +475,17 @@ class Fenom
* @param array $tags
* @return Fenom
*/
public function addBlockCompiler($compiler, $open_parser, $close_parser = self::DEFAULT_CLOSE_COMPILER, array $tags = array())
{
public function addBlockCompiler(
$compiler,
$open_parser,
$close_parser = self::DEFAULT_CLOSE_COMPILER,
array $tags = array()
) {
$this->_actions[$compiler] = array(
'type' => self::BLOCK_COMPILER,
'open' => $open_parser,
'type' => self::BLOCK_COMPILER,
'open' => $open_parser,
'close' => $close_parser ? : self::DEFAULT_CLOSE_COMPILER,
'tags' => $tags,
'tags' => $tags,
);
return $this;
}
@ -482,8 +501,8 @@ class Fenom
public function addBlockCompilerSmart($compiler, $storage, array $tags, array $floats = array())
{
$c = array(
'type' => self::BLOCK_COMPILER,
"tags" => array(),
'type' => self::BLOCK_COMPILER,
"tags" => array(),
"float_tags" => array()
);
if (method_exists($storage, $compiler . "Open")) {
@ -519,8 +538,8 @@ class Fenom
public function addFunction($function, $callback, $parser = self::DEFAULT_FUNC_PARSER)
{
$this->_actions[$function] = array(
'type' => self::INLINE_FUNCTION,
'parser' => $parser,
'type' => self::INLINE_FUNCTION,
'parser' => $parser,
'function' => $callback,
);
return $this;
@ -534,8 +553,8 @@ class Fenom
public function addFunctionSmart($function, $callback)
{
$this->_actions[$function] = array(
'type' => self::INLINE_FUNCTION,
'parser' => self::SMART_FUNC_PARSER,
'type' => self::INLINE_FUNCTION,
'parser' => self::SMART_FUNC_PARSER,
'function' => $callback,
);
return $this;
@ -548,12 +567,16 @@ class Fenom
* @param callable|string $parser_close
* @return Fenom
*/
public function addBlockFunction($function, $callback, $parser_open = self::DEFAULT_FUNC_OPEN, $parser_close = self::DEFAULT_FUNC_CLOSE)
{
public function addBlockFunction(
$function,
$callback,
$parser_open = self::DEFAULT_FUNC_OPEN,
$parser_close = self::DEFAULT_FUNC_CLOSE
) {
$this->_actions[$function] = array(
'type' => self::BLOCK_FUNCTION,
'open' => $parser_open,
'close' => $parser_close,
'type' => self::BLOCK_FUNCTION,
'open' => $parser_open,
'close' => $parser_close,
'function' => $callback,
);
return $this;
@ -766,17 +789,16 @@ class Fenom
}
/**
*
*
* @param string $template name of template
* Creates pipe-line of template's data to callback
* @param string $template name of the template
* @param callable $callback template's data handler
* @param array $vars
* @param callable $callback
* @param float $chunk
* @param float $chunk amount of bytes of chunk
* @return array
*/
public function pipe($template, $callback, array $vars = array(), $chunk = 1e6)
{
ob_start($callback, $chunk, true);
ob_start($callback, $chunk, PHP_OUTPUT_HANDLER_STDFLAGS);
$data = $this->getTemplate($template)->display($vars);
ob_end_flush();
return $data;
@ -792,7 +814,6 @@ class Fenom
public function getTemplate($template, $options = 0)
{
$options |= $this->_options;
// var_dump($this->_options & self::FORCE_COMPILE);
if (is_array($template)) {
$key = dechex($options) . "@" . implode(",", $template);
} else {
@ -843,7 +864,7 @@ class Fenom
$file_name = $this->_getCacheName($template, $opts);
if (is_file($this->_compile_dir . "/" . $file_name)) {
$fenom = $this; // used in template
$_tpl = include($this->_compile_dir . "/" . $file_name);
$_tpl = include($this->_compile_dir . "/" . $file_name);
/* @var Fenom\Render $_tpl */
if (!($this->_options & self::AUTO_RELOAD) || ($this->_options & self::AUTO_RELOAD) && $_tpl->isValid()) {
return $_tpl;
@ -895,9 +916,9 @@ class Fenom
}
}
if ($store) {
$cache = $this->_getCacheName($tpl, $options);
$cache = $this->_getCacheName($tpl, $options);
$tpl_tmp = tempnam($this->_compile_dir, $cache);
$tpl_fp = fopen($tpl_tmp, "w");
$tpl_fp = fopen($tpl_tmp, "w");
if (!$tpl_fp) {
throw new \RuntimeException("Can't to open temporary file $tpl_tmp. Directory " . $this->_compile_dir . " is writable?");
}
@ -972,14 +993,16 @@ class Fenom
*/
public static function registerAutoload($dir = null)
{
if(!$dir) {
if (!$dir) {
$dir = __DIR__;
}
return spl_autoload_register(function($classname) use ($dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $classname).'.php';
if(is_file($file)) {
require_once $file;
return spl_autoload_register(
function ($classname) use ($dir) {
$file = $dir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $classname) . '.php';
if (is_file($file)) {
require_once $file;
}
}
});
);
}
}

View File

@ -33,10 +33,10 @@ class Compiler
*/
public static function tagInclude(Tokenizer $tokens, Tag $tag)
{
$tpl = $tag->tpl;
$name = false;
$tpl = $tag->tpl;
$name = false;
$cname = $tpl->parsePlainArg($tokens, $name);
$p = $tpl->parseParams($tokens);
$p = $tpl->parseParams($tokens);
if ($name) {
if ($tpl->getStorage()->getOptions() & \Fenom::FORCE_INCLUDE) {
$inc = $tpl->getStorage()->compile($name, false);
@ -134,17 +134,17 @@ class Compiler
*/
public static function foreachOpen(Tokenizer $tokens, Tag $scope)
{
$p = array("index" => false, "first" => false, "last" => false);
$key = null;
$p = array("index" => false, "first" => false, "last" => false);
$key = null;
$before = $body = array();
if ($tokens->is(T_VARIABLE)) {
$from = $scope->tpl->parseTerm($tokens);
$from = $scope->tpl->parseTerm($tokens);
$prepend = "";
} elseif ($tokens->is('[')) {
$from = $scope->tpl->parseArray($tokens);
$uid = '$v' . $scope->tpl->i++;
$from = $scope->tpl->parseArray($tokens);
$uid = '$v' . $scope->tpl->i++;
$prepend = $uid . ' = ' . $from . ';';
$from = $uid;
$from = $uid;
} else {
throw new UnexpectedTokenException($tokens, null, "tag {foreach}");
}
@ -153,12 +153,12 @@ class Compiler
$value = $scope->tpl->parseVariable($tokens);
if ($tokens->is(T_DOUBLE_ARROW)) {
$tokens->next();
$key = $value;
$key = $value;
$value = $scope->tpl->parseVariable($tokens);
}
$scope["after"] = array();
$scope["else"] = false;
$scope["else"] = false;
while ($token = $tokens->key()) {
$param = $tokens->get(T_STRING);
@ -171,22 +171,22 @@ class Compiler
}
if ($p["index"]) {
$before[] = $p["index"] . ' = 0';
$before[] = $p["index"] . ' = 0';
$scope["after"][] = $p["index"] . '++';
}
if ($p["first"]) {
$before[] = $p["first"] . ' = true';
$before[] = $p["first"] . ' = true';
$scope["after"][] = $p["first"] . ' && (' . $p["first"] . ' = false )';
}
if ($p["last"]) {
$before[] = $p["last"] . ' = false';
$before[] = $p["last"] . ' = false';
$scope["uid"] = "v" . $scope->tpl->i++;
$before[] = '$' . $scope["uid"] . " = count($from)";
$body[] = 'if(!--$' . $scope["uid"] . ') ' . $p["last"] . ' = true';
$before[] = '$' . $scope["uid"] . " = count($from)";
$body[] = 'if(!--$' . $scope["uid"] . ') ' . $p["last"] . ' = true';
}
$before = $before ? implode("; ", $before) . ";" : "";
$body = $body ? implode("; ", $body) . ";" : "";
$before = $before ? implode("; ", $before) . ";" : "";
$body = $body ? implode("; ", $body) . ";" : "";
$scope["after"] = $scope["after"] ? implode("; ", $scope["after"]) . ";" : "";
if ($key) {
return "$prepend if($from) { $before foreach($from as $key => $value) { $body";
@ -235,41 +235,55 @@ class Compiler
*/
public static function forOpen(Tokenizer $tokens, Tag $scope)
{
$p = array("index" => false, "first" => false, "last" => false, "step" => 1, "to" => false, "max" => false, "min" => false);
$p = array(
"index" => false,
"first" => false,
"last" => false,
"step" => 1,
"to" => false,
"max" => false,
"min" => false
);
$scope["after"] = $before = $body = array();
$i = array('', '');
$c = "";
$var = $scope->tpl->parseTerm($tokens, $is_var);
$i = array('', '');
$c = "";
$var = $scope->tpl->parseTerm($tokens, $is_var);
if (!$is_var) {
throw new UnexpectedTokenException($tokens);
}
$tokens->get("=");
$tokens->next();
$val = $scope->tpl->parseExpr($tokens);
$p = $scope->tpl->parseParams($tokens, $p);
$p = $scope->tpl->parseParams($tokens, $p);
if (is_numeric($p["step"])) {
if ($p["step"] > 0) {
$condition = "$var <= {$p['to']}";
if ($p["last"]) $c = "($var + {$p['step']}) > {$p['to']}";
if ($p["last"]) {
$c = "($var + {$p['step']}) > {$p['to']}";
}
} elseif ($p["step"] < 0) {
$condition = "$var >= {$p['to']}";
if ($p["last"]) $c = "($var + {$p['step']}) < {$p['to']}";
if ($p["last"]) {
$c = "($var + {$p['step']}) < {$p['to']}";
}
} else {
throw new InvalidUsageException("Invalid step value");
}
} else {
$condition = "({$p['step']} > 0 && $var <= {$p['to']} || {$p['step']} < 0 && $var >= {$p['to']})";
if ($p["last"]) $c = "({$p['step']} > 0 && ($var + {$p['step']}) <= {$p['to']} || {$p['step']} < 0 && ($var + {$p['step']}) >= {$p['to']})";
if ($p["last"]) {
$c = "({$p['step']} > 0 && ($var + {$p['step']}) <= {$p['to']} || {$p['step']} < 0 && ($var + {$p['step']}) >= {$p['to']})";
}
}
if ($p["first"]) {
$before[] = $p["first"] . ' = true';
$before[] = $p["first"] . ' = true';
$scope["after"][] = $p["first"] . ' && (' . $p["first"] . ' = false )';
}
if ($p["last"]) {
$before[] = $p["last"] . ' = false';
$body[] = "if($c) {$p['last']} = true";
$body[] = "if($c) {$p['last']} = true";
}
if ($p["index"]) {
@ -277,11 +291,11 @@ class Compiler
$i[1] .= $p["index"] . '++,';
}
$scope["else"] = false;
$scope["else"] = false;
$scope["else_cond"] = "$var==$val";
$before = $before ? implode("; ", $before) . ";" : "";
$body = $body ? implode("; ", $body) . ";" : "";
$scope["after"] = $scope["after"] ? implode("; ", $scope["after"]) . ";" : "";
$before = $before ? implode("; ", $before) . ";" : "";
$body = $body ? implode("; ", $body) . ";" : "";
$scope["after"] = $scope["after"] ? implode("; ", $scope["after"]) . ";" : "";
return "$before for({$i[0]} $var=$val; $condition;{$i[1]} $var+={$p['step']}) { $body";
}
@ -295,7 +309,7 @@ class Compiler
public static function forElse(Tokenizer $tokens, Tag $scope)
{
$scope["no-break"] = $scope["no-continue"] = true;
$scope["else"] = true;
$scope["else"] = true;
return " } if({$scope['else_cond']}) {";
}
@ -335,12 +349,12 @@ class Compiler
*/
public static function switchOpen(Tokenizer $tokens, Tag $scope)
{
$expr = $scope->tpl->parseExpr($tokens);
$scope["case"] = array();
$scope["last"] = array();
$expr = $scope->tpl->parseExpr($tokens);
$scope["case"] = array();
$scope["last"] = array();
$scope["default"] = '';
$scope["var"] = $scope->tpl->tmpVar();
$scope["expr"] = $scope["var"] . ' = strval(' . $expr . ')';
$scope["var"] = $scope->tpl->tmpVar();
$scope["expr"] = $scope["var"] . ' = strval(' . $expr . ')';
// lazy init
return '';
}
@ -414,8 +428,8 @@ class Compiler
public static function switchClose(Tokenizer $tokens, Tag $scope)
{
self::_caseResort($scope);
$expr = $scope["var"];
$code = $scope["expr"] . ";\n";
$expr = $scope["var"];
$code = $scope["expr"] . ";\n";
$default = $scope["default"];
foreach ($scope["case"] as $case => $content) {
if (is_numeric($case)) {
@ -505,12 +519,12 @@ class Compiler
$stack[] = "'$t'";
}
$stack[] = $tpl->dynamic_extends;
$body = '<?php $tpl->getStorage()->display(array(' . implode(', ', $stack) . '), $var); ?>';
$body = '<?php $tpl->getStorage()->display(array(' . implode(', ', $stack) . '), $var); ?>';
} else {
$child = $tpl;
while ($child && $child->extends) {
$parent = $tpl->extend($child->extends);
$child = $parent->extends ? $parent : false;
$child = $parent->extends ? $parent : false;
}
$tpl->extends = false;
}
@ -554,7 +568,7 @@ class Compiler
if (!$name) {
throw new \RuntimeException("Invalid block name");
}
$scope["name"] = $name;
$scope["name"] = $name;
$scope["use_parent"] = false;
}
@ -564,7 +578,7 @@ class Compiler
*/
public static function tagBlockClose($tokens, Tag $scope)
{
$tpl = $scope->tpl;
$tpl = $scope->tpl;
$name = $scope["name"];
if (isset($tpl->blocks[$name])) { // block defined
@ -584,10 +598,10 @@ class Compiler
}
$tpl->blocks[$scope["name"]] = array(
"from" => $tpl->getName(),
"import" => false,
"from" => $tpl->getName(),
"import" => false,
"use_parent" => $scope["use_parent"],
"block" => $scope->getContent()
"block" => $scope->getContent()
);
}
@ -647,7 +661,7 @@ class Compiler
} else {
$ref = new \ReflectionFunction($tag->callback);
}
$args = array();
$args = array();
$params = $tag->tpl->parseParams($tokens);
foreach ($ref->getParameters() as $param) {
if (isset($params[$param->getName()])) {
@ -815,7 +829,7 @@ class Compiler
*/
public static function tagImport(Tokenizer $tokens, Tag $tag)
{
$tpl = $tag->tpl;
$tpl = $tag->tpl;
$import = array();
if ($tokens->is('[')) {
$tokens->next();
@ -880,10 +894,10 @@ class Compiler
*/
public static function macroOpen(Tokenizer $tokens, Tag $scope)
{
$scope["name"] = $tokens->get(Tokenizer::MACRO_STRING);
$scope["name"] = $tokens->get(Tokenizer::MACRO_STRING);
$scope["recursive"] = false;
$args = array();
$defaults = array();
$args = array();
$defaults = array();
if (!$tokens->valid()) {
return;
}
@ -910,10 +924,10 @@ class Compiler
}
$tokens->skipIf(')');
$scope["macro"] = array(
"name" => $scope["name"],
"args" => $args,
"defaults" => $defaults,
"body" => "",
"name" => $scope["name"],
"args" => $args,
"defaults" => $defaults,
"body" => "",
"recursive" => false
);
return;
@ -928,7 +942,7 @@ class Compiler
if ($scope["recursive"]) {
$scope["macro"]["recursive"] = true;
}
$scope["macro"]["body"] = $scope->cutContent();
$scope["macro"]["body"] = $scope->cutContent();
$scope->tpl->macros[$scope["name"]] = $scope["macro"];
}
@ -959,6 +973,8 @@ class Compiler
* @param Tokenizer $tokens
* @param Tag $tag
*/
public static function autoescapeClose(Tokenizer $tokens, Tag $tag) { }
public static function autoescapeClose(Tokenizer $tokens, Tag $tag)
{
}
}

View File

@ -27,7 +27,9 @@ class Modifier
{
if (!is_numeric($date)) {
$date = strtotime($date);
if (!$date) $date = time();
if (!$date) {
$date = time();
}
}
return strftime($format, $date);
}
@ -41,7 +43,9 @@ class Modifier
{
if (!is_numeric($date)) {
$date = strtotime($date);
if (!$date) $date = time();
if (!$date) {
$date = time();
}
}
return date($format, $date);
}
@ -103,7 +107,11 @@ class Modifier
if (preg_match('#^(.{' . $length . '}).*?(.{' . $length . '})?$#usS', $string, $match)) {
if (count($match) == 3) {
if ($by_words) {
return preg_replace('#\s.*$#usS', "", $match[1]) . $etc . preg_replace('#.*\s#usS', "", $match[2]);
return preg_replace('#\s.*$#usS', "", $match[1]) . $etc . preg_replace(
'#.*\s#usS',
"",
$match[2]
);
} else {
return $match[1] . $etc . $match[2];
}

View File

@ -108,7 +108,7 @@ class Provider implements ProviderInterface
*/
public function getList($extension = "tpl")
{
$list = array();
$list = array();
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->_path,
\FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS),

View File

@ -18,12 +18,12 @@ use Fenom;
class Render extends \ArrayObject
{
private static $_props = array(
"name" => "runtime",
"name" => "runtime",
"base_name" => "",
"scm" => false,
"time" => 0,
"depends" => array(),
"macros" => array()
"scm" => false,
"time" => 0,
"depends" => array(),
"macros" => array()
);
/**
* @var \Closure
@ -84,12 +84,12 @@ class Render extends \ArrayObject
{
$this->_fenom = $fenom;
$props += self::$_props;
$this->_name = $props["name"];
$this->_name = $props["name"];
$this->_base_name = $props["base_name"];
$this->_scm = $props["scm"];
$this->_time = $props["time"];
$this->_depends = $props["depends"];
$this->_macros = $props["macros"];
$this->_scm = $props["scm"];
$this->_time = $props["time"];
$this->_depends = $props["depends"];
$this->_macros = $props["macros"];
// $this->_blocks = $props["blocks"];
$this->_code = $code;
}
@ -251,9 +251,9 @@ class Render extends \ArrayObject
{
if ($name == 'info') {
return array(
'name' => $this->_name,
'name' => $this->_name,
'schema' => $this->_scm,
'time' => $this->_time
'time' => $this->_time
);
} else {
return null;

View File

@ -13,8 +13,8 @@ namespace Fenom;
class Tag extends \ArrayObject
{
const COMPILER = 1;
const FUNC = 2;
const BLOCK = 4;
const FUNC = 2;
const BLOCK = 4;
const LTRIM = 1;
@ -50,19 +50,19 @@ class Tag extends \ArrayObject
*/
public function __construct($name, Template $tpl, $info, &$body)
{
$this->tpl = $tpl;
$this->name = $name;
$this->line = $tpl->getLine();
$this->level = $tpl->getStackSize();
$this->_body = & $body;
$this->tpl = $tpl;
$this->name = $name;
$this->line = $tpl->getLine();
$this->level = $tpl->getStackSize();
$this->_body = & $body;
$this->_offset = strlen($body);
$this->_type = $info["type"];
$this->escape = $tpl->getOptions() & \Fenom::AUTO_ESCAPE;
$this->_type = $info["type"];
$this->escape = $tpl->getOptions() & \Fenom::AUTO_ESCAPE;
if ($this->_type & self::BLOCK) {
$this->_open = $info["open"];
$this->_close = $info["close"];
$this->_tags = isset($info["tags"]) ? $info["tags"] : array();
$this->_open = $info["open"];
$this->_close = $info["close"];
$this->_tags = isset($info["tags"]) ? $info["tags"] : array();
$this->_floats = isset($info["float_tags"]) ? $info["float_tags"] : array();
$this->_closed = false;
} else {
@ -81,7 +81,7 @@ class Tag extends \ArrayObject
*/
public function tagOption($option)
{
if(method_exists($this, 'opt'.$option)) {
if (method_exists($this, 'opt' . $option)) {
$this->options[] = $option;
} else {
throw new \RuntimeException("Unknown tag option $option");
@ -93,9 +93,10 @@ class Tag extends \ArrayObject
* @param int $option option constant
* @param bool $value true add option, false remove option
*/
public function setOption($option, $value) {
public function setOption($option, $value)
{
$actual = (bool)($this->tpl->getOptions() & $option);
if($actual != $value) {
if ($actual != $value) {
$this->_changed[$option] = $actual;
$this->tpl->setOption(\Fenom::AUTO_ESCAPE, $value);
}
@ -107,7 +108,7 @@ class Tag extends \ArrayObject
*/
public function restore($option)
{
if(isset($this->_changed[$option])) {
if (isset($this->_changed[$option])) {
$this->tpl->setOption($option, $this->_changed[$option]);
unset($this->_changed[$option]);
}
@ -115,7 +116,7 @@ class Tag extends \ArrayObject
public function restoreAll()
{
foreach($this->_changed as $option => $value) {
foreach ($this->_changed as $option => $value) {
$this->tpl->setOption($option, $this->_changed[$option]);
unset($this->_changed[$option]);
}
@ -138,8 +139,8 @@ class Tag extends \ArrayObject
*/
public function start($tokenizer)
{
foreach($this->options as $option) {
$option = 'opt'.$option;
foreach ($this->options as $option) {
$option = 'opt' . $option;
$this->$option();
}
return call_user_func($this->_open, $tokenizer, $this);
@ -195,9 +196,9 @@ class Tag extends \ArrayObject
throw new \LogicException("Tag {$this->name} already closed");
}
if ($this->_close) {
foreach($this->options as $option) {
$option = 'opt'.$option.'end';
if(method_exists($this, $option)) {
foreach ($this->options as $option) {
$option = 'opt' . $option . 'end';
if (method_exists($this, $option)) {
$this->$option();
}
}
@ -236,7 +237,7 @@ class Tag extends \ArrayObject
*/
public function cutContent()
{
$content = substr($this->_body, $this->_offset + 1);
$content = substr($this->_body, $this->_offset + 1);
$this->_body = substr($this->_body, 0, $this->_offset);
return $content;
}

View File

@ -116,9 +116,9 @@ class Template extends Render
*/
public function __construct(Fenom $fenom, $options)
{
$this->_fenom = $fenom;
$this->_options = $options;
$this->_filters = $this->_fenom->getFilters();
$this->_fenom = $fenom;
$this->_options = $options;
$this->_filters = $this->_fenom->getFilters();
$this->_tag_filters = $this->_fenom->getTagFilters();
}
@ -155,15 +155,15 @@ class Template extends Render
public function load($name, $compile = true)
{
$this->_name = $name;
$this->_crc = crc32($this->_name);
$this->_crc = crc32($this->_name);
if ($provider = strstr($name, ':', true)) {
$this->_scm = $provider;
$this->_scm = $provider;
$this->_base_name = substr($name, strlen($provider) + 1);
} else {
$this->_base_name = $name;
}
$this->_provider = $this->_fenom->getProvider($provider);
$this->_src = $this->_provider->getSource($this->_base_name, $this->_time);
$this->_src = $this->_provider->getSource($this->_base_name, $this->_time);
if ($compile) {
$this->compile();
}
@ -180,7 +180,7 @@ class Template extends Render
public function source($name, $src, $compile = true)
{
$this->_name = $name;
$this->_src = $src;
$this->_src = $src;
if ($compile) {
$this->compile();
}
@ -194,7 +194,7 @@ class Template extends Render
*/
public function compile()
{
$end = $pos = 0;
$end = $pos = 0;
$this->escape = $this->_options & Fenom::AUTO_ESCAPE;
foreach ($this->_fenom->getPreFilters() as $filter) {
$this->_src = call_user_func($filter, $this, $this->_src);
@ -226,11 +226,15 @@ class Template extends Render
$end = $start + 1;
do {
$need_more = false;
$end = strpos($this->_src, '}', $end + 1); // search close-symbol of the tag
$end = strpos($this->_src, '}', $end + 1); // 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(
$this->_src,
$start,
$end - $start + 1
); // variable $tag contains fenom tag '{...}'
$_tag = substr($tag, 1, -1); // strip delimiters '{' and '}'
@ -252,7 +256,10 @@ class Template extends Render
} else {
$this->_appendCode($this->parseTag($tokens), $tag); // start the tag lexer
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);
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);
}
}
}
@ -267,14 +274,17 @@ class Template extends Render
$this->_appendText(substr($this->_src, $end ? $end + 1 : 0)); // append tail of the template
if ($this->_stack) {
$_names = array();
$_line = 0;
$_line = 0;
foreach ($this->_stack as $scope) {
if (!$_line) {
$_line = $scope->line;
}
$_names[] = '{' . $scope->name . '} opened on line ' . $scope->line;
}
throw new CompileException("Unclosed tag" . (count($_names) == 1 ? "" : "s") . ": " . implode(", ", $_names), 0, 1, $this->_name, $_line);
throw new CompileException("Unclosed tag" . (count($_names) == 1 ? "" : "s") . ": " . implode(
", ",
$_names
), 0, 1, $this->_name, $_line);
}
$this->_src = ""; // cleanup
if ($this->_post) {
@ -293,8 +303,9 @@ class Template extends Render
* @param int $option
* @param bool $value
*/
public function setOption($option, $value) {
if($value) {
public function setOption($option, $value)
{
if ($value) {
$this->_options |= $option;
} else {
$this->_options &= ~$option;
@ -472,7 +483,7 @@ class Template extends Render
*/
public function out($data, $escape = null)
{
if($escape === null) {
if ($escape === null) {
$escape = $this->_options & Fenom::AUTO_ESCAPE;
}
if ($escape) {
@ -491,7 +502,7 @@ class Template extends Render
$donor = $this->_fenom->compile($tpl, false);
foreach ($donor->blocks as $name => $block) {
if (!isset($this->blocks[$name])) {
$block['import'] = $this->getName();
$block['import'] = $this->getName();
$this->blocks[$name] = $block;
}
}
@ -508,19 +519,19 @@ class Template extends Render
if (!$this->_body) {
$this->compile();
}
$parent = $this->_fenom->getRawTemplate()->load($tpl, false);
$parent->blocks = & $this->blocks;
$parent->macros = & $this->macros;
$parent = $this->_fenom->getRawTemplate()->load($tpl, false);
$parent->blocks = & $this->blocks;
$parent->macros = & $this->macros;
$parent->extended = $this->getName();
if (!$this->ext_stack) {
$this->ext_stack[] = $this->getName();
}
$this->ext_stack[] = $parent->getName();
$parent->_options = $this->_options;
$parent->_options = $this->_options;
$parent->ext_stack = $this->ext_stack;
$parent->compile();
$this->_body = $parent->_body;
$this->_src = $parent->_src;
$this->_src = $parent->_src;
$this->addDepend($parent);
return $parent;
}
@ -552,9 +563,15 @@ class Template extends Render
} catch (InvalidUsageException $e) {
throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}", 0, E_ERROR, $this->_name, $this->_line, $e);
} catch (\LogicException $e) {
throw new SecurityException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
throw new SecurityException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(
0,
0
) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
} catch (\Exception $e) {
throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(
0,
0
) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
}
}
@ -594,7 +611,8 @@ class Template extends Render
{
$action = $tokens->get(Tokenizer::MACRO_STRING);
$tokens->next();
if ($tokens->is("(", T_DOUBLE_COLON, T_NS_SEPARATOR) && !$tokens->isWhiteSpaced()) { // just invoke function or static method
if ($tokens->is("(", T_DOUBLE_COLON, T_NS_SEPARATOR) && !$tokens->isWhiteSpaced()
) { // just invoke function or static method
$tokens->back();
return $this->out($this->parseExpr($tokens));
} elseif ($tokens->is('.')) {
@ -626,7 +644,10 @@ class Template extends Render
}
}
if ($tags = $this->_fenom->getTagOwners($action)) { // unknown template tag
throw new TokenizeException("Unexpected tag '$action' (this tag can be used with '" . implode("', '", $tags) . "')");
throw new TokenizeException("Unexpected tag '$action' (this tag can be used with '" . implode(
"', '",
$tags
) . "')");
} else {
throw new TokenizeException("Unexpected tag '$action'");
}
@ -650,9 +671,9 @@ class Template extends Render
*/
public function parseExpr(Tokenizer $tokens)
{
$exp = array();
$var = false; // last term was: true - variable, false - mixed
$op = false; // last exp was operator
$exp = array();
$var = false; // last term was: true - variable, false - mixed
$op = false; // last exp was operator
$cond = false; // was comparison operator
while ($tokens->valid()) {
// parse term
@ -660,18 +681,18 @@ class Template extends Render
if ($term !== false) {
if ($this->_options & Fenom::FORCE_VERIFY) {
$term = '(isset(' . $term . ') ? ' . $term . ' : null)';
$var = false;
$var = false;
}
if ($tokens->is('|')) {
$term = $this->parseModifier($tokens, $term);
$var = false;
$var = false;
}
if ($tokens->is('?', '!')) {
$term = $this->parseTernary($tokens, $term, $var);
$var = false;
$var = false;
}
$exp[] = $term;
$op = false;
$op = false;
} else {
break;
}
@ -700,10 +721,10 @@ class Template extends Render
}
$operator = $tokens->current();
if ($operator == "is") {
$item = array_pop($exp);
$item = array_pop($exp);
$exp[] = $this->parseIs($tokens, $item, $var);
} elseif ($operator == "in" || ($operator == "not" && $tokens->isNextToken("in"))) {
$item = array_pop($exp);
$item = array_pop($exp);
$exp[] = $this->parseIn($tokens, $item, $var);
} else {
break;
@ -778,7 +799,7 @@ class Template extends Render
$is_var = true;
}
} elseif ($tokens->is('$')) {
$var = $this->parseAccessor($tokens, $is_var);
$var = $this->parseAccessor($tokens, $is_var);
$code = $unary . $var;
} elseif ($tokens->is(Tokenizer::MACRO_INCDEC)) {
$code = $unary . $tokens->getAndNext() . $this->parseVariable($tokens);
@ -797,8 +818,8 @@ class Template extends Render
$code = $unary . $func . $this->parseArgs($tokens->next());
} elseif ($tokens->isNext(T_NS_SEPARATOR, T_DOUBLE_COLON)) {
$method = $this->parseStatic($tokens);
$args = $this->parseArgs($tokens);
$code = $unary . $method . $args;
$args = $this->parseArgs($tokens);
$code = $unary . $method . $args;
} else {
return false;
}
@ -885,17 +906,17 @@ class Template extends Render
public function parseAccessor(Tokenizer $tokens, &$is_var)
{
$is_var = false;
$vars = array(
'get' => '$_GET',
'post' => '$_POST',
$vars = array(
'get' => '$_GET',
'post' => '$_POST',
'session' => '$_SESSION',
'cookie' => '$_COOKIE',
'cookie' => '$_COOKIE',
'request' => '$_REQUEST',
'files' => '$_FILES',
'files' => '$_FILES',
'globals' => '$GLOBALS',
'server' => '$_SERVER',
'env' => '$_ENV',
'tpl' => '$tpl->info'
'server' => '$_SERVER',
'env' => '$_ENV',
'tpl' => '$tpl->info'
);
if ($this->_options & Fenom::DENY_ACCESSOR) {
throw new \LogicException("Accessor are disabled");
@ -952,7 +973,12 @@ class Template extends Render
return '((' . $var . ' !== null) ? ' . $var . ' : (' . $this->parseExpr($tokens) . '))';
}
}
} elseif ($tokens->is(Tokenizer::MACRO_BINARY, Tokenizer::MACRO_BOOLEAN, Tokenizer::MACRO_MATH) || !$tokens->valid()) {
} elseif ($tokens->is(
Tokenizer::MACRO_BINARY,
Tokenizer::MACRO_BOOLEAN,
Tokenizer::MACRO_MATH
) || !$tokens->valid()
) {
if ($empty) {
if ($is_var) {
return '!empty(' . $var . ')';
@ -1000,11 +1026,11 @@ class Template extends Render
$tokens->next();
if ($tokens->current() == 'not') {
$invert = '!';
$equal = '!=';
$equal = '!=';
$tokens->next();
} else {
$invert = '';
$equal = '==';
$equal = '==';
}
if ($tokens->is(Tokenizer::MACRO_STRING)) {
$action = $tokens->current();
@ -1041,12 +1067,12 @@ class Template extends Render
{
$checkers = array(
"string" => 'is_int(strpos(%2$s, %1$s))',
"list" => "in_array(%s, %s)",
"keys" => "array_key_exists(%s, %s)",
"auto" => '\Fenom\Modifier::in(%s, %s)'
"list" => "in_array(%s, %s)",
"keys" => "array_key_exists(%s, %s)",
"auto" => '\Fenom\Modifier::in(%s, %s)'
);
$checker = null;
$invert = '';
$checker = null;
$invert = '';
if ($tokens->current() == 'not') {
$invert = '!';
$tokens->next();
@ -1256,14 +1282,23 @@ class Template extends Render
{
if ($tokens->is("[")) {
$_arr = "array(";
$key = $val = false;
$key = $val = false;
$tokens->next();
while ($tokens->valid()) {
if ($tokens->is(',') && $val) {
$key = true;
$val = false;
$_arr .= $tokens->getAndNext() . ' ';
} elseif ($tokens->is(Tokenizer::MACRO_SCALAR, T_VARIABLE, T_STRING, T_EMPTY, T_ISSET, "(", "#") && !$val) {
} elseif ($tokens->is(
Tokenizer::MACRO_SCALAR,
T_VARIABLE,
T_STRING,
T_EMPTY,
T_ISSET,
"(",
"#"
) && !$val
) {
$_arr .= $this->parseExpr($tokens);
$key = false;
$val = true;
@ -1299,15 +1334,15 @@ class Template extends Render
public function parseMacroCall(Tokenizer $tokens, $name)
{
$recursive = false;
$macro = false;
$macro = false;
if (isset($this->macros[$name])) {
$macro = $this->macros[$name];
$macro = $this->macros[$name];
$recursive = $macro['recursive'];
} else {
foreach ($this->_stack as $scope) {
if ($scope->name == 'macro' && $scope['name'] == $name) { // invoke recursive
$recursive = $scope;
$macro = $scope['macro'];
$macro = $scope['macro'];
break;
}
}
@ -1316,7 +1351,7 @@ class Template extends Render
}
}
$tokens->next();
$p = $this->parseParams($tokens);
$p = $this->parseParams($tokens);
$args = array();
foreach ($macro['args'] as $arg) {
if (isset($p[$arg])) {
@ -1381,17 +1416,26 @@ class Template extends Render
$tokens->next();
$arg = $colon = false;
while ($tokens->valid()) {
if (!$arg && $tokens->is(T_VARIABLE, T_STRING, "(", Tokenizer::MACRO_SCALAR, '"', Tokenizer::MACRO_UNARY, Tokenizer::MACRO_INCDEC)) {
if (!$arg && $tokens->is(
T_VARIABLE,
T_STRING,
"(",
Tokenizer::MACRO_SCALAR,
'"',
Tokenizer::MACRO_UNARY,
Tokenizer::MACRO_INCDEC
)
) {
$_args .= $this->parseExpr($tokens);
$arg = true;
$arg = true;
$colon = false;
} elseif (!$arg && $tokens->is('[')) {
$_args .= $this->parseArray($tokens);
$arg = true;
$arg = true;
$colon = false;
} elseif ($arg && $tokens->is(',')) {
$_args .= $tokens->getAndNext() . ' ';
$arg = false;
$arg = false;
$colon = true;
} elseif (!$colon && $tokens->is(')')) {
$tokens->next();
@ -1417,7 +1461,7 @@ class Template extends Render
if ($tokens->isNext('|')) {
return $this->parseExpr($tokens);
} else {
$str = $tokens->getAndNext();
$str = $tokens->getAndNext();
$static = stripslashes(substr($str, 1, -1));
return $str;
}

View File

@ -39,10 +39,10 @@ defined('T_YIELD') || define('T_YIELD', 267);
*/
class Tokenizer
{
const TOKEN = 0;
const TEXT = 1;
const TOKEN = 0;
const TEXT = 1;
const WHITESPACE = 2;
const LINE = 3;
const LINE = 3;
/**
* Some text value: foo, bar, new, class ...
@ -92,64 +92,174 @@ class Tokenizer
* @var array groups of tokens
*/
public static $macros = array(
self::MACRO_STRING => array(
\T_ABSTRACT => 1, \T_ARRAY => 1, \T_AS => 1, \T_BREAK => 1, \T_BREAK => 1, \T_CASE => 1,
\T_CATCH => 1, \T_CLASS => 1, \T_CLASS_C => 1, \T_CLONE => 1, \T_CONST => 1, \T_CONTINUE => 1,
\T_DECLARE => 1, \T_DEFAULT => 1, \T_DIR => 1, \T_DO => 1, \T_ECHO => 1, \T_ELSE => 1,
\T_ELSEIF => 1, \T_EMPTY => 1, \T_ENDDECLARE => 1, \T_ENDFOR => 1, \T_ENDFOREACH => 1, \T_ENDIF => 1,
\T_ENDSWITCH => 1, \T_ENDWHILE => 1, \T_EVAL => 1, \T_EXIT => 1, \T_EXTENDS => 1, \T_FILE => 1,
\T_FINAL => 1, \T_FOR => 1, \T_FOREACH => 1, \T_FUNCTION => 1, \T_FUNC_C => 1, \T_GLOBAL => 1,
\T_GOTO => 1, \T_HALT_COMPILER => 1, \T_IF => 1, \T_IMPLEMENTS => 1, \T_INCLUDE => 1, \T_INCLUDE_ONCE => 1,
\T_INSTANCEOF => 1, \T_INSTEADOF => 1, \T_INTERFACE => 1, \T_ISSET => 1, \T_LINE => 1, \T_LIST => 1,
\T_LOGICAL_AND => 1, \T_LOGICAL_OR => 1, \T_LOGICAL_XOR => 1, \T_METHOD_C => 1, \T_NAMESPACE => 1, \T_NS_C => 1,
\T_NEW => 1, \T_PRINT => 1, \T_PRIVATE => 1, \T_PUBLIC => 1, \T_PROTECTED => 1, \T_REQUIRE => 1,
\T_REQUIRE_ONCE => 1, \T_RETURN => 1, \T_RETURN => 1, \T_STRING => 1, \T_SWITCH => 1, \T_THROW => 1,
\T_TRAIT => 1, \T_TRAIT_C => 1, \T_TRY => 1, \T_UNSET => 1, \T_USE => 1, \T_VAR => 1,
\T_WHILE => 1, \T_YIELD => 1
self::MACRO_STRING => array(
\T_ABSTRACT => 1,
\T_ARRAY => 1,
\T_AS => 1,
\T_BREAK => 1,
\T_BREAK => 1,
\T_CASE => 1,
\T_CATCH => 1,
\T_CLASS => 1,
\T_CLASS_C => 1,
\T_CLONE => 1,
\T_CONST => 1,
\T_CONTINUE => 1,
\T_DECLARE => 1,
\T_DEFAULT => 1,
\T_DIR => 1,
\T_DO => 1,
\T_ECHO => 1,
\T_ELSE => 1,
\T_ELSEIF => 1,
\T_EMPTY => 1,
\T_ENDDECLARE => 1,
\T_ENDFOR => 1,
\T_ENDFOREACH => 1,
\T_ENDIF => 1,
\T_ENDSWITCH => 1,
\T_ENDWHILE => 1,
\T_EVAL => 1,
\T_EXIT => 1,
\T_EXTENDS => 1,
\T_FILE => 1,
\T_FINAL => 1,
\T_FOR => 1,
\T_FOREACH => 1,
\T_FUNCTION => 1,
\T_FUNC_C => 1,
\T_GLOBAL => 1,
\T_GOTO => 1,
\T_HALT_COMPILER => 1,
\T_IF => 1,
\T_IMPLEMENTS => 1,
\T_INCLUDE => 1,
\T_INCLUDE_ONCE => 1,
\T_INSTANCEOF => 1,
\T_INSTEADOF => 1,
\T_INTERFACE => 1,
\T_ISSET => 1,
\T_LINE => 1,
\T_LIST => 1,
\T_LOGICAL_AND => 1,
\T_LOGICAL_OR => 1,
\T_LOGICAL_XOR => 1,
\T_METHOD_C => 1,
\T_NAMESPACE => 1,
\T_NS_C => 1,
\T_NEW => 1,
\T_PRINT => 1,
\T_PRIVATE => 1,
\T_PUBLIC => 1,
\T_PROTECTED => 1,
\T_REQUIRE => 1,
\T_REQUIRE_ONCE => 1,
\T_RETURN => 1,
\T_RETURN => 1,
\T_STRING => 1,
\T_SWITCH => 1,
\T_THROW => 1,
\T_TRAIT => 1,
\T_TRAIT_C => 1,
\T_TRY => 1,
\T_UNSET => 1,
\T_USE => 1,
\T_VAR => 1,
\T_WHILE => 1,
\T_YIELD => 1
),
self::MACRO_INCDEC => array(
\T_INC => 1, \T_DEC => 1
self::MACRO_INCDEC => array(
\T_INC => 1,
\T_DEC => 1
),
self::MACRO_UNARY => array(
"!" => 1, "~" => 1, "-" => 1
self::MACRO_UNARY => array(
"!" => 1,
"~" => 1,
"-" => 1
),
self::MACRO_BINARY => array(
\T_BOOLEAN_AND => 1, \T_BOOLEAN_OR => 1, \T_IS_GREATER_OR_EQUAL => 1, \T_IS_EQUAL => 1, \T_IS_IDENTICAL => 1,
\T_IS_NOT_EQUAL => 1, \T_IS_NOT_IDENTICAL => 1, \T_IS_SMALLER_OR_EQUAL => 1, \T_LOGICAL_AND => 1,
\T_LOGICAL_OR => 1, \T_LOGICAL_XOR => 1, \T_SL => 1, \T_SR => 1,
"+" => 1, "-" => 1, "*" => 1, "/" => 1, ">" => 1, "<" => 1, "^" => 1, "%" => 1, "&" => 1
self::MACRO_BINARY => array(
\T_BOOLEAN_AND => 1,
\T_BOOLEAN_OR => 1,
\T_IS_GREATER_OR_EQUAL => 1,
\T_IS_EQUAL => 1,
\T_IS_IDENTICAL => 1,
\T_IS_NOT_EQUAL => 1,
\T_IS_NOT_IDENTICAL => 1,
\T_IS_SMALLER_OR_EQUAL => 1,
\T_LOGICAL_AND => 1,
\T_LOGICAL_OR => 1,
\T_LOGICAL_XOR => 1,
\T_SL => 1,
\T_SR => 1,
"+" => 1,
"-" => 1,
"*" => 1,
"/" => 1,
">" => 1,
"<" => 1,
"^" => 1,
"%" => 1,
"&" => 1
),
self::MACRO_BOOLEAN => array(
\T_LOGICAL_OR => 1, \T_LOGICAL_XOR => 1, \T_BOOLEAN_AND => 1, \T_BOOLEAN_OR => 1, \T_LOGICAL_AND => 1
\T_LOGICAL_OR => 1,
\T_LOGICAL_XOR => 1,
\T_BOOLEAN_AND => 1,
\T_BOOLEAN_OR => 1,
\T_LOGICAL_AND => 1
),
self::MACRO_MATH => array(
"+" => 1, "-" => 1, "*" => 1, "/" => 1, "^" => 1, "%" => 1, "&" => 1, "|" => 1
self::MACRO_MATH => array(
"+" => 1,
"-" => 1,
"*" => 1,
"/" => 1,
"^" => 1,
"%" => 1,
"&" => 1,
"|" => 1
),
self::MACRO_COND => array(
\T_IS_EQUAL => 1, \T_IS_IDENTICAL => 1, ">" => 1, "<" => 1, \T_SL => 1, \T_SR => 1,
\T_IS_NOT_EQUAL => 1, \T_IS_NOT_IDENTICAL => 1, \T_IS_SMALLER_OR_EQUAL => 1,
self::MACRO_COND => array(
\T_IS_EQUAL => 1,
\T_IS_IDENTICAL => 1,
">" => 1,
"<" => 1,
\T_SL => 1,
\T_SR => 1,
\T_IS_NOT_EQUAL => 1,
\T_IS_NOT_IDENTICAL => 1,
\T_IS_SMALLER_OR_EQUAL => 1,
),
self::MACRO_EQUALS => array(
\T_AND_EQUAL => 1, \T_DIV_EQUAL => 1, \T_MINUS_EQUAL => 1, \T_MOD_EQUAL => 1,
\T_MUL_EQUAL => 1, \T_OR_EQUAL => 1, \T_PLUS_EQUAL => 1, \T_SL_EQUAL => 1, \T_SR_EQUAL => 1,
\T_XOR_EQUAL => 1, '=' => 1,
self::MACRO_EQUALS => array(
\T_AND_EQUAL => 1,
\T_DIV_EQUAL => 1,
\T_MINUS_EQUAL => 1,
\T_MOD_EQUAL => 1,
\T_MUL_EQUAL => 1,
\T_OR_EQUAL => 1,
\T_PLUS_EQUAL => 1,
\T_SL_EQUAL => 1,
\T_SR_EQUAL => 1,
\T_XOR_EQUAL => 1,
'=' => 1,
// \T_CONCAT_EQUAL => 1,
),
self::MACRO_SCALAR => array(
\T_LNUMBER => 1, \T_DNUMBER => 1, \T_CONSTANT_ENCAPSED_STRING => 1
self::MACRO_SCALAR => array(
\T_LNUMBER => 1,
\T_DNUMBER => 1,
\T_CONSTANT_ENCAPSED_STRING => 1
)
);
public static $description = array(
self::MACRO_STRING => 'string',
self::MACRO_INCDEC => 'increment/decrement operator',
self::MACRO_UNARY => 'unary operator',
self::MACRO_BINARY => 'binary operator',
self::MACRO_STRING => 'string',
self::MACRO_INCDEC => 'increment/decrement operator',
self::MACRO_UNARY => 'unary operator',
self::MACRO_BINARY => 'binary operator',
self::MACRO_BOOLEAN => 'boolean operator',
self::MACRO_MATH => 'math operator',
self::MACRO_COND => 'conditional operator',
self::MACRO_EQUALS => 'equal operator',
self::MACRO_SCALAR => 'scalar value'
self::MACRO_MATH => 'math operator',
self::MACRO_COND => 'conditional operator',
self::MACRO_EQUALS => 'equal operator',
self::MACRO_SCALAR => 'scalar value'
);
/**
@ -157,7 +267,12 @@ class Tokenizer
* @var array
*/
private static $spec = array(
'true' => 1, 'false' => 1, 'null' => 1, 'TRUE' => 1, 'FALSE' => 1, 'NULL' => 1
'true' => 1,
'false' => 1,
'null' => 1,
'TRUE' => 1,
'FALSE' => 1,
'NULL' => 1
);
/**
@ -165,9 +280,9 @@ class Tokenizer
*/
public function __construct($query)
{
$tokens = array(-1 => array(\T_WHITESPACE, '', '', 1));
$tokens = array(-1 => array(\T_WHITESPACE, '', '', 1));
$_tokens = token_get_all("<?php " . $query);
$line = 1;
$line = 1;
array_shift($_tokens);
$i = 0;
foreach ($_tokens as $token) {
@ -197,8 +312,8 @@ class Tokenizer
}
unset($tokens[-1]);
$this->tokens = $tokens;
$this->_max = count($this->tokens) - 1;
$this->tokens = $tokens;
$this->_max = count($this->tokens) - 1;
$this->_last_no = $this->tokens[$this->_max][3];
}
@ -387,7 +502,7 @@ class Tokenizer
public function hasBackList($token1 /*, $token2 ...*/)
{
$tokens = func_get_args();
$c = $this->p;
$c = $this->p;
foreach ($tokens as $token) {
$c--;
if ($c < 0 || $this->tokens[$c][0] !== $token) {
@ -520,7 +635,7 @@ class Tokenizer
public function getSnippet($before = 0, $after = 0)
{
$from = 0;
$to = $this->p;
$to = $this->p;
if ($before > 0) {
if ($before > $this->p) {
$from = $this->p;

View File

@ -12,38 +12,38 @@ class TestCase extends \PHPUnit_Framework_TestCase
public $fenom;
public $values = array(
"zero" => 0,
"one" => 1,
"two" => 2,
"zero" => 0,
"one" => 1,
"two" => 2,
"three" => 3,
"float" => 4.5,
"bool" => true,
0 => "empty value",
1 => "one value",
2 => "two value",
3 => "three value",
"bool" => true,
0 => "empty value",
1 => "one value",
2 => "two value",
3 => "three value",
);
public static function getVars()
{
return array(
"zero" => 0,
"one" => 1,
"two" => 2,
"zero" => 0,
"one" => 1,
"two" => 2,
"three" => 3,
"float" => 4.5,
"bool" => true,
"obj" => new \StdClass,
"list" => array(
"a" => 1,
"bool" => true,
"obj" => new \StdClass,
"list" => array(
"a" => 1,
"one" => 1,
"b" => 2,
"b" => 2,
"two" => 2
),
0 => "empty value",
1 => "one value",
2 => "two value",
3 => "three value",
0 => "empty value",
1 => "one value",
2 => "two value",
3 => "three value",
);
}
@ -56,6 +56,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
}
$this->fenom = Fenom::factory(FENOM_RESOURCES . '/' . $this->template_path, FENOM_RESOURCES . '/compile');
$this->fenom->addProvider('persist', new Provider(FENOM_RESOURCES . '/provider'));
$this->fenom->addModifier('dots', __CLASS__ . '::dots');
$this->fenom->addModifier('concat', __CLASS__ . '::concat');
$this->fenom->addModifier('append', __CLASS__ . '::append');
@ -123,7 +124,8 @@ class TestCase extends \PHPUnit_Framework_TestCase
$this->fenom->setOptions($options);
$tpl = $this->fenom->compileCode($code, "runtime.tpl");
if ($dump) {
echo "\n========= DUMP BEGIN ===========\n" . $code . "\n--- to ---\n" . $tpl->getBody() . "\n========= DUMP END =============\n";
echo "\n========= DUMP BEGIN ===========\n" . $code . "\n--- to ---\n" . $tpl->getBody(
) . "\n========= DUMP END =============\n";
}
$this->assertSame(Modifier::strip($result, true), Modifier::strip($tpl->fetch($vars), true), "Test $code");
return $tpl;
@ -134,7 +136,8 @@ class TestCase extends \PHPUnit_Framework_TestCase
$this->tpl($name, $code);
$tpl = $this->fenom->getTemplate($name);
if ($dump) {
echo "\n========= DUMP BEGIN ===========\n" . $code . "\n--- to ---\n" . $tpl->getBody() . "\n========= DUMP END =============\n";
echo "\n========= DUMP BEGIN ===========\n" . $code . "\n--- to ---\n" . $tpl->getBody(
) . "\n========= DUMP END =============\n";
}
$this->assertSame(Modifier::strip($result, true), Modifier::strip($tpl->fetch($vars), true), "Test tpl $name");
}
@ -234,7 +237,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
public static function providerArrays()
{
$scalars = array();
$data = array(
$data = array(
array('[]', array()),
array('[[],[]]', array(array(), array())),
);

View File

@ -19,7 +19,7 @@ class AutoEscapeTest extends TestCase
array('{$html}, {$html}', "$html, $html", $vars, 0),
array('{$html}, {$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{raw $html}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{raw $html}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{raw $html}, {$html}', "$html, $html", $vars, 0),
array('{raw "{$html|up}"}, {$html}', strtoupper($html) . ", $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{$html}{/autoescape}, {$html}', "$escaped, $html", $vars, 0),
array('{autoescape false}{$html}{/autoescape}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
@ -29,34 +29,122 @@ class AutoEscapeTest extends TestCase
array('{autoescape false}{raw $html}{/autoescape}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{raw $html}{/autoescape}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape false}{raw $html}{/autoescape}, {$html}', "$html, $html", $vars, 0),
// inline function
array('{test_function text=$html}, {$html}', "$html, $html", $vars, 0),
array('{test_function text=$html}, {$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{test_function:raw text=$html}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{test_function:raw text="{$html|up}"}, {$html}', strtoupper($html) . ", $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{test_function text=$html}{/autoescape}, {test_function text=$html}', "$escaped, $html", $vars, 0),
array('{autoescape false}{test_function text=$html}{/autoescape}, {test_function text=$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{test_function text=$html}{/autoescape}, {test_function text=$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape false}{test_function text=$html}{/autoescape}, {test_function text=$html}', "$html, $html", $vars, 0),
array('{autoescape true}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}', "$html, $html", $vars, 0),
array('{autoescape false}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape false}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}', "$html, $html", $vars, 0),
array(
'{test_function:raw text="{$html|up}"}, {$html}',
strtoupper($html) . ", $escaped",
$vars,
\Fenom::AUTO_ESCAPE
),
array(
'{autoescape true}{test_function text=$html}{/autoescape}, {test_function text=$html}',
"$escaped, $html",
$vars,
0
),
array(
'{autoescape false}{test_function text=$html}{/autoescape}, {test_function text=$html}',
"$html, $escaped",
$vars,
\Fenom::AUTO_ESCAPE
),
array(
'{autoescape true}{test_function text=$html}{/autoescape}, {test_function text=$html}',
"$escaped, $escaped",
$vars,
\Fenom::AUTO_ESCAPE
),
array(
'{autoescape false}{test_function text=$html}{/autoescape}, {test_function text=$html}',
"$html, $html",
$vars,
0
),
array(
'{autoescape true}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}',
"$html, $html",
$vars,
0
),
array(
'{autoescape false}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}',
"$html, $escaped",
$vars,
\Fenom::AUTO_ESCAPE
),
array(
'{autoescape true}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}',
"$html, $escaped",
$vars,
\Fenom::AUTO_ESCAPE
),
array(
'{autoescape false}{test_function:raw text=$html}{/autoescape}, {test_function text=$html}',
"$html, $html",
$vars,
0
),
// block function
array('{test_block_function}{$html}{/test_block_function}', $html, $vars, 0),
array('{test_block_function}{$html}{/test_block_function}', $escaped, $vars, \Fenom::AUTO_ESCAPE),
array('{test_block_function:raw}{$html}{/test_block_function}', $html, $vars, \Fenom::AUTO_ESCAPE),
array('{test_block_function:raw}{"{$html|up}"}{/test_block_function}', strtoupper($html), $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$escaped, $html", $vars, 0),
array('{autoescape false}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape false}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$html, $html", $vars, 0),
array('{autoescape true}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$html, $html", $vars, 0),
array('{autoescape false}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function:raw}{$html}{/test_block_function}', "$escaped, $html", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function:raw}{$html}{/test_block_function}', "$html, $html", $vars, 0),
array(
'{test_block_function:raw}{"{$html|up}"}{/test_block_function}',
strtoupper($html),
$vars,
\Fenom::AUTO_ESCAPE
),
array(
'{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
"$escaped, $html",
$vars,
0
),
array(
'{autoescape false}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
"$html, $escaped",
$vars,
\Fenom::AUTO_ESCAPE
),
array(
'{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
"$escaped, $escaped",
$vars,
\Fenom::AUTO_ESCAPE
),
array(
'{autoescape false}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
"$html, $html",
$vars,
0
),
array(
'{autoescape true}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
"$html, $html",
$vars,
0
),
array(
'{autoescape false}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function}{$html}{/test_block_function}',
"$html, $escaped",
$vars,
\Fenom::AUTO_ESCAPE
),
array(
'{autoescape true}{test_block_function}{$html}{/test_block_function}{/autoescape}, {test_block_function:raw}{$html}{/test_block_function}',
"$escaped, $html",
$vars,
\Fenom::AUTO_ESCAPE
),
array(
'{autoescape true}{test_block_function:raw}{$html}{/test_block_function}{/autoescape}, {test_block_function:raw}{$html}{/test_block_function}',
"$html, $html",
$vars,
0
),
);
}

View File

@ -21,9 +21,21 @@ class ExtendsTest extends TestCase
public static function providerExtendsInvalid()
{
return array(
array('{extends "extends/dynamic/child.3.tpl"} {extends "extends/dynamic/child.3.tpl"}', 'Fenom\Error\CompileException', "Only one {extends} allowed"),
array('{if true}{extends "extends/dynamic/child.3.tpl"}{/if}', 'Fenom\Error\CompileException', "Tag {extends} can not be nested"),
array('{if true}{use "extends/dynamic/use.tpl"}{/if}', 'Fenom\Error\CompileException', "Tag {use} can not be nested"),
array(
'{extends "extends/dynamic/child.3.tpl"} {extends "extends/dynamic/child.3.tpl"}',
'Fenom\Error\CompileException',
"Only one {extends} allowed"
),
array(
'{if true}{extends "extends/dynamic/child.3.tpl"}{/if}',
'Fenom\Error\CompileException',
"Tag {extends} can not be nested"
),
array(
'{if true}{use "extends/dynamic/use.tpl"}{/if}',
'Fenom\Error\CompileException',
"Tag {use} can not be nested"
),
array('{use $use_this}', 'Fenom\Error\CompileException', "Invalid template name for tag {use}"),
array('{block $use_this}{/block}', 'Fenom\Error\CompileException', "Invalid block name"),
);
@ -54,12 +66,18 @@ Before body
Child 3 content
Before footer
Footer from use";
$this->assertSame($result, $this->fenom->fetch(array(
'extends/auto/child.3.tpl',
'extends/auto/child.2.tpl',
'extends/auto/child.1.tpl',
'extends/auto/parent.tpl',
), array()));
$this->assertSame(
$result,
$this->fenom->fetch(
array(
'extends/auto/child.3.tpl',
'extends/auto/child.2.tpl',
'extends/auto/child.1.tpl',
'extends/auto/parent.tpl',
),
array()
)
);
}
public function testStaticExtendLevel1()
@ -92,11 +110,17 @@ Before body
Child 3 content
Before footer
Footer from use";
$this->assertSame($result, $this->fenom->fetch(array(
'extends/auto/child.3.tpl',
'extends/auto/child.2.tpl',
'extends/auto/static/child.1.tpl'
), array()));
$this->assertSame(
$result,
$this->fenom->fetch(
array(
'extends/auto/child.3.tpl',
'extends/auto/child.2.tpl',
'extends/auto/static/child.1.tpl'
),
array()
)
);
}
public function testStaticExtendNested()

View File

@ -7,56 +7,74 @@ class MacrosTest extends TestCase
public function setUp()
{
parent::setUp();
$this->tpl("math.tpl", '
{macro plus(x, y)}
x + y = {$x + $y}
{/macro}
$this->tpl(
"math.tpl",
'
{macro plus(x, y)}
x + y = {$x + $y}
{/macro}
{macro minus(x, y, z=0)}
x - y - z = {$x - $y - $z}
{/macro}
{macro minus(x, y, z=0)}
x - y - z = {$x - $y - $z}
{/macro}
{macro multi(x, y)}
x * y = {$x * $y}
{/macro}
{macro multi(x, y)}
x * y = {$x * $y}
{/macro}
Math: {macro.plus x=2 y=3}, {macro.minus x=10 y=4}
');
Math: {macro.plus x=2 y=3}, {macro.minus x=10 y=4}
'
);
$this->tpl("import.tpl", '
{import "math.tpl"}
{import "math.tpl" as math}
$this->tpl(
"import.tpl",
'
{import "math.tpl"}
{import "math.tpl" as math}
Imp: {macro.plus x=1 y=2}, {math.minus x=6 y=2 z=1}
');
Imp: {macro.plus x=1 y=2}, {math.minus x=6 y=2 z=1}
'
);
$this->tpl("import_custom.tpl", '
{macro minus($x, $y)}
new minus macros
{/macro}
{import [plus, minus] from "math.tpl" as math}
$this->tpl(
"import_custom.tpl",
'
{macro minus($x, $y)}
new minus macros
{/macro}
{import [plus, minus] from "math.tpl" as math}
a: {math.plus x=1 y=2}, {math.minus x=6 y=2 z=1}, {macro.minus x=5 y=3}.
');
a: {math.plus x=1 y=2}, {math.minus x=6 y=2 z=1}, {macro.minus x=5 y=3}.
'
);
$this->tpl("import_miss.tpl", '
{import [minus] from "math.tpl" as math}
$this->tpl(
"import_miss.tpl",
'
{import [minus] from "math.tpl" as math}
a: {macro.plus x=5 y=3}.
');
a: {macro.plus x=5 y=3}.
'
);
$this->tpl("macro_recursive.tpl", '{macro factorial(num)}
{if $num}
{$num} {macro.factorial num=$num-1} {$num}
{/if}
{/macro}
$this->tpl(
"macro_recursive.tpl",
'{macro factorial(num)}
{if $num}
{$num} {macro.factorial num=$num-1} {$num}
{/if}
{/macro}
{macro.factorial num=10}');
{macro.factorial num=10}'
);
$this->tpl("macro_recursive_import.tpl", '
{import "macro_recursive.tpl" as math}
$this->tpl(
"macro_recursive_import.tpl",
'
{import "macro_recursive.tpl" as math}
{math.factorial num=10}');
{math.factorial num=10}'
);
}
public function _testSandbox()
@ -65,11 +83,15 @@ class MacrosTest extends TestCase
// $this->fenom->compile("macro_recursive.tpl")->display([]);
// $this->fenom->flush();
// var_dump($this->fenom->fetch("macro_recursive.tpl", []));
var_dump($this->fenom->compileCode('{macro factorial(num)}
{if $num}
{$num} {macro.factorial num=$num-1} {$num}
{/if}
{/macro}')->getBody());
var_dump(
$this->fenom->compileCode(
'{macro factorial(num)}
{if $num}
{$num} {macro.factorial num=$num-1} {$num}
{/if}
{/macro}'
)->getBody()
);
// var_dump($this->fenom->display("macro_recursive_import.tpl", array()));
} catch (\Exception $e) {
var_dump($e->getMessage() . ": " . $e->getTraceAsString());
@ -99,7 +121,10 @@ class MacrosTest extends TestCase
{
$tpl = $this->fenom->compile('import_custom.tpl');
$this->assertSame('a: x + y = 3 , x - y - z = 3 , new minus macros .', Modifier::strip($tpl->fetch(array()), true));
$this->assertSame(
'a: x + y = 3 , x - y - z = 3 , new minus macros .',
Modifier::strip($tpl->fetch(array()), true)
);
}
/**
@ -110,7 +135,10 @@ class MacrosTest extends TestCase
{
$tpl = $this->fenom->compile('import_miss.tpl');
$this->assertSame('a: x + y = 3 , x - y - z = 3 , new minus macros .', Modifier::strip($tpl->fetch(array()), true));
$this->assertSame(
'a: x + y = 3 , x - y - z = 3 , new minus macros .',
Modifier::strip($tpl->fetch(array()), true)
);
}
/**

View File

@ -8,7 +8,7 @@ class ModifiersTest extends TestCase
public static function providerTruncate()
{
$lorem = 'Lorem ipsum dolor sit amet'; // en
$uni = 'Лорем ипсум долор сит амет'; // ru
$uni = 'Лорем ипсум долор сит амет'; // ru
return array(
// ascii chars
array($lorem, 'Lorem ip...', 8),
@ -38,13 +38,18 @@ class ModifiersTest extends TestCase
public function testTruncate($in, $out, $count, $delim = '...', $by_words = false, $middle = false)
{
$tpl = $this->fenom->compileCode('{$text|truncate:$count:$delim:$by_words:$middle}');
$this->assertEquals($out, $tpl->fetch(array(
"text" => $in,
"count" => $count,
"delim" => $delim,
"by_words" => $by_words,
"middle" => $middle
)));
$this->assertEquals(
$out,
$tpl->fetch(
array(
"text" => $in,
"count" => $count,
"delim" => $delim,
"by_words" => $by_words,
"middle" => $middle
)
)
);
}
public static function providerUpLow()
@ -71,9 +76,14 @@ class ModifiersTest extends TestCase
public function testUpLow($modifier, $in, $out)
{
$tpl = $this->fenom->compileCode('{$text|' . $modifier . '}');
$this->assertEquals($out, $tpl->fetch(array(
"text" => $in,
)));
$this->assertEquals(
$out,
$tpl->fetch(
array(
"text" => $in,
)
)
);
}
public static function providerLength()
@ -99,9 +109,14 @@ class ModifiersTest extends TestCase
public function testLength($in, $out)
{
$tpl = $this->fenom->compileCode('{$data|length}');
$this->assertEquals($out, $tpl->fetch(array(
"data" => $in,
)));
$this->assertEquals(
$out,
$tpl->fetch(
array(
"data" => $in,
)
)
);
}
}

View File

@ -88,7 +88,7 @@ class ProviderTest extends TestCase
clearstatcache();
$templates = array(
"template1.tpl" => filemtime(FENOM_RESOURCES . '/template/template1.tpl'),
"unexists.tpl" => 1234567890
"unexists.tpl" => 1234567890
);
$this->assertFalse($this->provider->verify($templates));
}
@ -97,11 +97,14 @@ class ProviderTest extends TestCase
{
$list = $this->provider->getList();
sort($list);
$this->assertSame(array(
"sub/template3.tpl",
"template1.tpl",
"template2.tpl"
), $list);
$this->assertSame(
array(
"sub/template3.tpl",
"template1.tpl",
"template2.tpl"
),
$list
);
}
public function testRm()

View File

@ -3,10 +3,12 @@
namespace Fenom;
class TagOptionTest extends TestCase {
class TagOptionTest extends TestCase
{
public function testTrim() {
public function testTrim()
{
}
}

View File

@ -37,7 +37,10 @@ class TagsTest extends TestCase
*/
public function testVarBlockModified($tpl_val, $val)
{
$this->assertRender("{var \$a|low|dots}before {{$tpl_val}} after{/var}\nVar: {\$a}", "Var: " . strtolower("before " . $val . " after") . "...");
$this->assertRender(
"{var \$a|low|dots}before {{$tpl_val}} after{/var}\nVar: {\$a}",
"Var: " . strtolower("before " . $val . " after") . "..."
);
}
public function testCycle()
@ -50,7 +53,10 @@ class TagsTest extends TestCase
*/
public function testCycleIndex()
{
$this->assertRender('{var $a=["one", "two"]}{for $i=1 to=5}{cycle $a index=$i}, {/for}', "two, one, two, one, two, ");
$this->assertRender(
'{var $a=["one", "two"]}{for $i=1 to=5}{cycle $a index=$i}, {/for}',
"two, one, two, one, two, "
);
}
/**

File diff suppressed because it is too large Load Diff

View File

@ -17,7 +17,7 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase
public function testTokens()
{
$code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}';
$code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}';
$tokens = new Tokenizer($code);
$this->assertSame(27, $tokens->count());
$this->assertSame($tokens, $tokens->back());
@ -38,13 +38,16 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase
$this->assertSame(",", $tokens->getNext());
$this->assertSame(",", $tokens->key());
$this->assertSame("please", $tokens->getNext(T_STRING));
$this->assertSame(array(
T_STRING,
'please',
' ',
1,
'T_STRING'
), $tokens->curr);
$this->assertSame(
array(
T_STRING,
'please',
' ',
1,
'T_STRING'
),
$tokens->curr
);
$this->assertSame("resolve", $tokens->getNext($tokens::MACRO_UNARY, T_STRING));
$tokens->next();
@ -80,7 +83,7 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase
public function testSkip()
{
$text = "1 foo: bar ( 3 + double ) ";
$text = "1 foo: bar ( 3 + double ) ";
$tokens = new Tokenizer($text);
$tokens->skip()->skip(T_STRING)->skip(':');

View File

@ -1,8 +1,5 @@
<?php
use Fenom\Render,
Fenom\Provider as FS;
class FenomTest extends \Fenom\TestCase
{
@ -20,9 +17,10 @@ class FenomTest extends \Fenom\TestCase
);
}
public function testCreating()
{
$time = $this->tpl('temp.tpl', 'Template 1 a');
$time = $this->tpl('temp.tpl', 'Template 1 a');
$fenom = new Fenom($provider = new \Fenom\Provider(FENOM_RESOURCES . '/template'));
$fenom->setCompileDir(FENOM_RESOURCES . '/compile');
$this->assertInstanceOf('Fenom\Template', $tpl = $fenom->getTemplate('temp.tpl'));
@ -35,8 +33,12 @@ class FenomTest extends \Fenom\TestCase
public function testFactory()
{
$time = $this->tpl('temp.tpl', 'Template 1 a');
$fenom = Fenom::factory($provider = new \Fenom\Provider(FENOM_RESOURCES . '/template'), FENOM_RESOURCES . '/compile', Fenom::AUTO_ESCAPE);
$time = $this->tpl('temp.tpl', 'Template 1 a');
$fenom = Fenom::factory(
$provider = new \Fenom\Provider(FENOM_RESOURCES . '/template'),
FENOM_RESOURCES . '/compile',
Fenom::AUTO_ESCAPE
);
$this->assertInstanceOf('Fenom\Template', $tpl = $fenom->getTemplate('temp.tpl'));
$this->assertSame($provider, $tpl->getProvider());
$this->assertSame('temp.tpl', $tpl->getBaseName());
@ -45,6 +47,25 @@ class FenomTest extends \Fenom\TestCase
$fenom->clearAllCompiles();
}
/**
* @expectedException LogicException
* @expectedExceptionMessage Cache directory /invalid/path is not writable
*/
public function testFactoryInvalid()
{
Fenom::factory(FENOM_RESOURCES . '/template', '/invalid/path');
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Source must be a valid path or provider object
*/
public function testFactoryInvalid2()
{
Fenom::factory(new StdClass);
}
public function testCompileFile()
{
$a = array(
@ -97,7 +118,10 @@ class FenomTest extends \Fenom\TestCase
{
$this->fenom->addModifier("mymod", "myMod");
$this->tpl('custom.tpl', 'Custom modifier {$a|mymod}');
$this->assertSame("Custom modifier (myMod)Custom(/myMod)", $this->fenom->fetch('custom.tpl', array("a" => "Custom")));
$this->assertSame(
"Custom modifier (myMod)Custom(/myMod)",
$this->fenom->fetch('custom.tpl', array("a" => "Custom"))
);
}
/**
@ -118,13 +142,27 @@ class FenomTest extends \Fenom\TestCase
{
$this->fenom->setOptions(Fenom::FORCE_COMPILE);
$this->fenom->addCompiler("mycompiler", 'myCompiler');
$this->fenom->addBlockCompiler("myblockcompiler", 'myBlockCompilerOpen', 'myBlockCompilerClose', array(
'tag' => 'myBlockCompilerTag'
));
$this->fenom->addBlockCompiler(
"myblockcompiler",
'myBlockCompilerOpen',
'myBlockCompilerClose',
array(
'tag' => 'myBlockCompilerTag'
)
);
$this->tpl('custom.tpl', 'Custom compiler {mycompiler name="bar"}');
$this->assertSame("Custom compiler PHP_VERSION: " . PHP_VERSION . " (for bar)", $this->fenom->fetch('custom.tpl', array()));
$this->tpl('custom.tpl', 'Custom compiler {myblockcompiler name="bar"} block1 {tag name="baz"} block2 {/myblockcompiler}');
$this->assertSame("Custom compiler PHP_VERSION: " . PHP_VERSION . " (for bar) block1 Tag baz of compiler block2 End of compiler", $this->fenom->fetch('custom.tpl', array()));
$this->assertSame(
"Custom compiler PHP_VERSION: " . PHP_VERSION . " (for bar)",
$this->fenom->fetch('custom.tpl', array())
);
$this->tpl(
'custom.tpl',
'Custom compiler {myblockcompiler name="bar"} block1 {tag name="baz"} block2 {/myblockcompiler}'
);
$this->assertSame(
"Custom compiler PHP_VERSION: " . PHP_VERSION . " (for bar) block1 Tag baz of compiler block2 End of compiler",
$this->fenom->fetch('custom.tpl', array())
);
}
/**
@ -146,23 +184,35 @@ class FenomTest extends \Fenom\TestCase
public function testFilter()
{
$punit = $this;
$this->fenom->addPreFilter(function ($tpl, $src) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "== $src ==";
});
$this->fenom->addPreFilter(
function ($tpl, $src) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "== $src ==";
}
);
$this->fenom->addPostFilter(function ($tpl, $code) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "+++ $code +++";
});
$this->fenom->addPostFilter(
function ($tpl, $code) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "+++ $code +++";
}
);
$this->fenom->addFilter(function ($tpl, $text) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "|--- $text ---|";
});
$this->fenom->addFilter(
function ($tpl, $text) use ($punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
return "|--- $text ---|";
}
);
$this->assertSame('+++ |--- == hello ---||--- world == ---| +++', $this->fenom->compileCode('hello {var $user} misterio {/var} world')->fetch(array()));
$this->assertSame('+++ |--- == hello ---||--- world == ---| +++', $this->fenom->compileCode('hello {var $user} <?php misterio ?> {/var} world')->fetch(array()));
$this->assertSame(
'+++ |--- == hello ---||--- world == ---| +++',
$this->fenom->compileCode('hello {var $user} misterio {/var} world')->fetch(array())
);
$this->assertSame(
'+++ |--- == hello ---||--- world == ---| +++',
$this->fenom->compileCode('hello {var $user} <?php misterio ?> {/var} world')->fetch(array())
);
}
/**
@ -170,13 +220,15 @@ class FenomTest extends \Fenom\TestCase
*/
public function testTagFilter()
{
$tags = array();
$tags = array();
$punit = $this;
$this->fenom->addTagFilter(function ($text, $tpl) use (&$tags, $punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
$tags[] = $text;
return $text;
});
$this->fenom->addTagFilter(
function ($text, $tpl) use (&$tags, $punit) {
$punit->assertInstanceOf('Fenom\Template', $tpl);
$tags[] = $text;
return $text;
}
);
$this->fenom->compileCode('hello {var $a} misterio {/var} world, {$b}!');
@ -194,8 +246,10 @@ class FenomTest extends \Fenom\TestCase
{
$this->fenom->addBlockCompilerSmart('SayBlock', 'TestTags', array('SaySomething'), array('SaySomething'));
$this->tpl('block_compiler.tpl', '{SayBlock} and {SaySomething}. It is all, {/SayBlock}');
$this->assertSame('Start saying and say blah-blah-blah. It is all, Stop saying',
$this->fenom->fetch('block_compiler.tpl', array()));
$this->assertSame(
'Start saying and say blah-blah-blah. It is all, Stop saying',
$this->fenom->fetch('block_compiler.tpl', array())
);
}
public function testAddFunctions()
@ -205,6 +259,35 @@ class FenomTest extends \Fenom\TestCase
$this->fenom->addAllowedFunctions(array('substr'));
$this->assertTrue($this->fenom->isAllowedFunction('substr'));
}
/**
* @group pipe
*/
public function testPipe()
{
$iteration = 0;
$test = $this; // for PHP 5.3
$this->fenom->pipe(
"persist:pipe.tpl",
function ($chunk) use (&$iteration, $test) {
if (!$chunk) {
return;
}
$iteration++;
// error_log(var_export($chunk, 1));
$test->assertSame("key$iteration:value$iteration", $chunk);
},
array(
"items" => array(
"key1" => "value1",
"key2" => "value2",
"key3" => "value3",
)
),
11 // strlen(key1) + strlen(:) + strlen(value1)
);
$this->assertSame(3, $iteration);
}
}

View File

@ -0,0 +1 @@
{foreach $items as $key => $value}{$key}:{$value}{/foreach}