diff --git a/src/Fenom.php b/src/Fenom.php index 000b90b..1e307e7 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -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; + } } - }); + ); } } diff --git a/src/Fenom/Compiler.php b/src/Fenom/Compiler.php index ace37b7..2c5486c 100644 --- a/src/Fenom/Compiler.php +++ b/src/Fenom/Compiler.php @@ -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 = 'getStorage()->display(array(' . implode(', ', $stack) . '), $var); ?>'; + $body = '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) + { + } } diff --git a/src/Fenom/Modifier.php b/src/Fenom/Modifier.php index b1a8ef3..4646ba3 100644 --- a/src/Fenom/Modifier.php +++ b/src/Fenom/Modifier.php @@ -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]; } diff --git a/src/Fenom/Provider.php b/src/Fenom/Provider.php index f73f0d5..bfbc359 100644 --- a/src/Fenom/Provider.php +++ b/src/Fenom/Provider.php @@ -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), diff --git a/src/Fenom/Render.php b/src/Fenom/Render.php index f72d48a..e126d75 100644 --- a/src/Fenom/Render.php +++ b/src/Fenom/Render.php @@ -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; diff --git a/src/Fenom/Tag.php b/src/Fenom/Tag.php index 7b42765..5bdde33 100644 --- a/src/Fenom/Tag.php +++ b/src/Fenom/Tag.php @@ -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; } diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index 03ecb29..b76dd57 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -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; } diff --git a/src/Fenom/Tokenizer.php b/src/Fenom/Tokenizer.php index 43bdb52..e186f9a 100644 --- a/src/Fenom/Tokenizer.php +++ b/src/Fenom/Tokenizer.php @@ -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("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; diff --git a/tests/TestCase.php b/tests/TestCase.php index bb2fa11..a7f86f7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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())), ); diff --git a/tests/cases/Fenom/AutoEscapeTest.php b/tests/cases/Fenom/AutoEscapeTest.php index 59f25b5..6751d3e 100644 --- a/tests/cases/Fenom/AutoEscapeTest.php +++ b/tests/cases/Fenom/AutoEscapeTest.php @@ -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 + ), ); } diff --git a/tests/cases/Fenom/ExtendsTest.php b/tests/cases/Fenom/ExtendsTest.php index 006783f..26a6f07 100644 --- a/tests/cases/Fenom/ExtendsTest.php +++ b/tests/cases/Fenom/ExtendsTest.php @@ -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() diff --git a/tests/cases/Fenom/MacrosTest.php b/tests/cases/Fenom/MacrosTest.php index 80b4a08..57cb51e 100644 --- a/tests/cases/Fenom/MacrosTest.php +++ b/tests/cases/Fenom/MacrosTest.php @@ -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) + ); } /** diff --git a/tests/cases/Fenom/ModifiersTest.php b/tests/cases/Fenom/ModifiersTest.php index 3081b31..f44d608 100644 --- a/tests/cases/Fenom/ModifiersTest.php +++ b/tests/cases/Fenom/ModifiersTest.php @@ -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, + ) + ) + ); } } \ No newline at end of file diff --git a/tests/cases/Fenom/ProviderTest.php b/tests/cases/Fenom/ProviderTest.php index 6974eb5..77d8706 100644 --- a/tests/cases/Fenom/ProviderTest.php +++ b/tests/cases/Fenom/ProviderTest.php @@ -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() diff --git a/tests/cases/Fenom/TagOptionTest.php b/tests/cases/Fenom/TagOptionTest.php index 8b3c087..71d0854 100644 --- a/tests/cases/Fenom/TagOptionTest.php +++ b/tests/cases/Fenom/TagOptionTest.php @@ -3,10 +3,12 @@ namespace Fenom; -class TagOptionTest extends TestCase { +class TagOptionTest extends TestCase +{ - public function testTrim() { + public function testTrim() + { } } \ No newline at end of file diff --git a/tests/cases/Fenom/TagsTest.php b/tests/cases/Fenom/TagsTest.php index 3cfea60..8431770 100644 --- a/tests/cases/Fenom/TagsTest.php +++ b/tests/cases/Fenom/TagsTest.php @@ -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, " + ); } /** diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index 97bbf7c..8391480 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -17,34 +17,34 @@ class TemplateTest extends TestCase { parent::setUp(); $this->tpl('welcome.tpl', 'Welcome, {$username} ({$email})'); - $_GET['one'] = 'get1'; - $_POST['one'] = 'post1'; + $_GET['one'] = 'get1'; + $_POST['one'] = 'post1'; $_REQUEST['one'] = 'request1'; - $_FILES['one'] = 'files1'; - $_SERVER['one'] = 'server1'; + $_FILES['one'] = 'files1'; + $_SERVER['one'] = 'server1'; $_SESSION['one'] = 'session1'; - $GLOBALS['one'] = 'globals1'; - $_ENV['one'] = 'env1'; - $_COOKIE['one'] = 'cookie1'; + $GLOBALS['one'] = 'globals1'; + $_ENV['one'] = 'env1'; + $_COOKIE['one'] = 'cookie1'; } public static function providerVars() { - $a = array("a" => "World"); - $obj = new \stdClass; + $a = array("a" => "World"); + $obj = new \stdClass; $obj->name = "Object"; $obj->list = $a; - $obj->c = "c"; + $obj->c = "c"; // $world = new $b = array( - "b" => array( - "c" => "Username", + "b" => array( + "c" => "Username", "c_char" => "c", - "mcp" => "Master", + "mcp" => "Master", 'm{$c}p' => "Unknown", - 'obj' => $obj + 'obj' => $obj ), - "c" => "c", + "c" => "c", "world" => new Helper('world') ); $c = array_replace_recursive($b, array("b" => array(3 => $b["b"], 4 => "Mister"))); @@ -65,8 +65,11 @@ class TemplateTest extends TestCase array('hello, {$b[ "mcp" ]}!', $b, 'hello, Master!'), array('hello, {$b[ "m{$c}p" ]}!', $b, 'hello, Master!'), array('hello, {$b."m{$c}p"}!', $b, 'hello, Master!'), - array('hello, {$b[ "m{$b.c_char}p" ]}!', - $b, 'hello, Master!'), + array( + 'hello, {$b[ "m{$b.c_char}p" ]}!', + $b, + 'hello, Master!' + ), array('hello, {$b[ \'m{$c}p\' ]}!', $b, 'hello, Unknown!'), array('hello, {$b.4}!', $c, 'hello, Mister!'), array('hello, {$b[4]}!', $c, 'hello, Mister!'), @@ -80,22 +83,33 @@ class TemplateTest extends TestCase array('hello, {$b[3].$c}!', $c, 'hello, Username!'), array('hello, {$b[(3)].$c}!', $c, 'hello, Username!'), array('hello, {$b[3][$b.c_char]}!', $c, 'hello, Username!'), - array('hello, {$b[ "m{$b.c_char}p" ]} and {$b.3[$b.c_char]}!', - $c, 'hello, Master and Username!'), + array( + 'hello, {$b[ "m{$b.c_char}p" ]} and {$b.3[$b.c_char]}!', + $c, + 'hello, Master and Username!' + ), array('hello, {$b.obj->name}!', $c, 'hello, Object!'), array('hello, {$b.obj->list.a}!', $c, 'hello, World!'), array('hello, {$b[obj]->name}!', $c, 'hello, Object!'), array('hello, {$b["obj"]->name}!', $c, 'hello, Object!'), - array('hello, {$b."obj"->name}!', $c, 'hello, Object!'), - array('hello, {$b.obj->name|upper}!', - $c, 'hello, OBJECT!'), - array('hello, {$b.obj->list.a|upper}!', - $c, 'hello, WORLD!'), + array( + 'hello, {$b.obj->name|upper}!', + $c, + 'hello, OBJECT!' + ), + array( + 'hello, {$b.obj->list.a|upper}!', + $c, + 'hello, WORLD!' + ), array('hello, {$b[ $b.obj->c ]}!', $b, 'hello, Username!'), array('hello, {$b[ ( $b.obj->c ) ]}!', $b, 'hello, Username!'), - array('hello, {$b[ "{$b.obj->c}" ]}!', - $b, 'hello, Username!'), + array( + 'hello, {$b[ "{$b.obj->c}" ]}!', + $b, + 'hello, Username!' + ), array('hello, {"World"}!', $a, 'hello, World!'), array('hello, {"W{$a}d"}!', $a, 'hello, WWorldd!'), array('hello, {$world->chunk(1)->self->chunk("new")}!', $b, 'hello, world!'), @@ -113,26 +127,31 @@ class TemplateTest extends TestCase array('hello, {$b[9/].c}!', 'Fenom\Error\CompileException', "Unexpected token ']'"), array('hello, {$b[3]$c}!', 'Fenom\Error\CompileException', "Unexpected token '\$c'"), array('hello, {$b[3]c}!', 'Fenom\Error\CompileException', "Unexpected token 'c'"), - array('hello, {$b.obj->valid()}!', 'Fenom\Error\SecurityException', "Forbidden to call methods", Fenom::DENY_METHODS), + array( + 'hello, {$b.obj->valid()}!', + 'Fenom\Error\SecurityException', + "Forbidden to call methods", + Fenom::DENY_METHODS + ), ); } public static function providerModifiers() { $b = array( - "a" => "World", - "b" => array( + "a" => "World", + "b" => array( "c" => "Username", ), - "c" => "c", - "lorem" => "Lorem ipsum dolor sit amet", - "next" => " next -->", - "rescue" => "Chip & Dale", + "c" => "c", + "lorem" => "Lorem ipsum dolor sit amet", + "next" => " next -->", + "rescue" => "Chip & Dale", "rescue_html" => "Chip & Dale", - "rescue_url" => "Chip+%26+Dale", - "date" => "26-07-2012", - "time" => 1343323616, - "tags" => "my name is Legion" + "rescue_url" => "Chip+%26+Dale", + "date" => "26-07-2012", + "time" => 1343323616, + "tags" => "my name is Legion" ); return array( array('hello, {$a|upper}!', $b, 'hello, WORLD!'), @@ -162,7 +181,12 @@ class TemplateTest extends TestCase { return array( array('Mod: {$lorem|}!', 'Fenom\Error\CompileException', "Unexpected end of expression"), - array('Mod: {$lorem|str_rot13}!', 'Fenom\Error\CompileException', "Modifier str_rot13 not found", Fenom::DENY_INLINE_FUNCS), + array( + 'Mod: {$lorem|str_rot13}!', + 'Fenom\Error\CompileException', + "Modifier str_rot13 not found", + Fenom::DENY_INLINE_FUNCS + ), array('Mod: {$lorem|my_encode}!', 'Fenom\Error\CompileException', "Modifier my_encode not found"), array('Mod: {$lorem|truncate:}!', 'Fenom\Error\CompileException', "Unexpected end of expression"), array('Mod: {$lorem|truncate:abs}!', 'Fenom\Error\CompileException', "Unexpected token 'abs'"), @@ -184,7 +208,6 @@ class TemplateTest extends TestCase array('Exp: {$y-' . $x . '} result', $b, 'Exp: 18 result'), array('Exp: {' . $x . '*$y} result', $b, 'Exp: 243 result'), array('Exp: {$y^' . $x . '} result', $b, 'Exp: 18 result'), - array('Exp: {$x+$y} result', $b, 'Exp: 36 result'), array('Exp: {$y/$x} result', $b, 'Exp: 3 result'), array('Exp: {$y-$x} result', $b, 'Exp: 18 result'), @@ -197,14 +220,19 @@ class TemplateTest extends TestCase array('Exp: {-1} result', $b, 'Exp: -1 result'), array('Exp: {$z = 5} {$z} result', $b, 'Exp: 5 5 result'), array('Exp: {$k.i = "str"} {$k.i} result', $b, 'Exp: str str result'), - - array('Exp: {($y*$x - (($x+$y) + $y/$x) ^ $y)/4} result', - $b, 'Exp: 53.75 result'), + array( + 'Exp: {($y*$x - (($x+$y) + $y/$x) ^ $y)/4} result', + $b, + 'Exp: 53.75 result' + ), array('Exp: {$x+max($x, $y)} result', $b, 'Exp: 36 result'), array('Exp: {max(1,2)} result', $b, 'Exp: 2 result'), array('Exp: {round(sin(pi()), 8)} result', $b, 'Exp: 0 result'), - array('Exp: {max($x, $y) + round(sin(pi()), 8) - min($x, $y) +3} result', - $b, 'Exp: 21 result'), + array( + 'Exp: {max($x, $y) + round(sin(pi()), 8) - min($x, $y) +3} result', + $b, + 'Exp: 21 result' + ), ); } @@ -232,16 +260,16 @@ class TemplateTest extends TestCase public static function providerInclude() { - $a = array( - "name" => "welcome", - "tpl" => "welcome.tpl", - "fragment" => "come", + $a = array( + "name" => "welcome", + "tpl" => "welcome.tpl", + "fragment" => "come", "pr_fragment" => "Come", - "pr_name" => "Welcome", - "username" => "Master", - "email" => "dev@null.net" + "pr_name" => "Welcome", + "username" => "Master", + "email" => "dev@null.net" ); - $result = 'Include Welcome, Master (dev@null.net) template'; + $result = 'Include Welcome, Master (dev@null.net) template'; $result2 = 'Include Welcome, Flame (dev@null.net) template'; $result3 = 'Include Welcome, Master (flame@dev.null) template'; $result4 = 'Include Welcome, Flame (flame@dev.null) template'; @@ -259,9 +287,17 @@ class TemplateTest extends TestCase array('Include {include "welcome.tpl" username="Flame"} template', $a, $result2), array('Include {include "welcome.tpl" username="Flame"} template', $a, $result2, Fenom::FORCE_INCLUDE), array('Include {include "welcome.tpl" email="flame@dev.null"} template', $a, $result3), - array('Include {include "welcome.tpl" email="flame@dev.null"} template', $a, $result3, Fenom::FORCE_INCLUDE), - array('Include {include "welcome.tpl" username="Flame" email="flame@dev.null"} template', - $a, $result4), + array( + 'Include {include "welcome.tpl" email="flame@dev.null"} template', + $a, + $result3, + Fenom::FORCE_INCLUDE + ), + array( + 'Include {include "welcome.tpl" username="Flame" email="flame@dev.null"} template', + $a, + $result4 + ), ); } @@ -269,21 +305,29 @@ class TemplateTest extends TestCase { return array( array('Include {include} template', 'Fenom\Error\CompileException', "Unexpected end of expression"), - array('Include {include another="welcome.tpl"} template', 'Fenom\Error\CompileException', "Unexpected token 'another'"), - array('Include {include "../../TestCase.php"} template', 'Fenom\Error\SecurityException', "Template ../../TestCase.php not found"), + array( + 'Include {include another="welcome.tpl"} template', + 'Fenom\Error\CompileException', + "Unexpected token 'another'" + ), + array( + 'Include {include "../../TestCase.php"} template', + 'Fenom\Error\SecurityException', + "Template ../../TestCase.php not found" + ), ); } public static function providerInsert() { - $a = array( - "name" => "welcome", - "tpl" => "welcome.tpl", - "fragment" => "come", + $a = array( + "name" => "welcome", + "tpl" => "welcome.tpl", + "fragment" => "come", "pr_fragment" => "Come", - "pr_name" => "Welcome", - "username" => "Master", - "email" => "dev@null.net" + "pr_name" => "Welcome", + "username" => "Master", + "email" => "dev@null.net" ); $result = 'Include Welcome, Master (dev@null.net) template'; return array( @@ -296,15 +340,51 @@ class TemplateTest extends TestCase { return array( array('Include {insert} template', 'Fenom\Error\CompileException', "Unexpected end of expression"), - array('Include {insert another="welcome.tpl"} template', 'Fenom\Error\CompileException', "Unexpected token 'another'"), - array('Include {insert $tpl} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"), - array('Include {insert "$tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"), - array('Include {insert "{$tpl}"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"), - array('Include {insert "$name.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"), - array('Include {insert "{$name}.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"), - array('Include {insert "{$pr_name|lower}.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"), - array('Include {insert "wel{$fragment}.tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"), - array('Include {insert "welcome.tpl" email="flame@dev.null"} template', 'Fenom\Error\CompileException', "Unexpected token 'email'"), + array( + 'Include {insert another="welcome.tpl"} template', + 'Fenom\Error\CompileException', + "Unexpected token 'another'" + ), + array( + 'Include {insert $tpl} template', + 'Fenom\Error\CompileException', + "Tag {insert} accept only static template name" + ), + array( + 'Include {insert "$tpl"} template', + 'Fenom\Error\CompileException', + "Tag {insert} accept only static template name" + ), + array( + 'Include {insert "{$tpl}"} template', + 'Fenom\Error\CompileException', + "Tag {insert} accept only static template name" + ), + array( + 'Include {insert "$name.tpl"} template', + 'Fenom\Error\CompileException', + "Tag {insert} accept only static template name" + ), + array( + 'Include {insert "{$name}.tpl"} template', + 'Fenom\Error\CompileException', + "Tag {insert} accept only static template name" + ), + array( + 'Include {insert "{$pr_name|lower}.tpl"} template', + 'Fenom\Error\CompileException', + "Tag {insert} accept only static template name" + ), + array( + 'Include {insert "wel{$fragment}.tpl"} template', + 'Fenom\Error\CompileException', + "Tag {insert} accept only static template name" + ), + array( + 'Include {insert "welcome.tpl" email="flame@dev.null"} template', + 'Fenom\Error\CompileException', + "Unexpected token 'email'" + ), ); } @@ -313,8 +393,8 @@ class TemplateTest extends TestCase $a = array( "val1" => 1, "val0" => 0, - "x" => 9, - "y" => 27 + "x" => 9, + "y" => 27 ); return array( array('if: {if 1} block1 {/if} end', $a, 'if: block1 end'), @@ -324,12 +404,21 @@ class TemplateTest extends TestCase array('if: {if $val1} block1 {else} block2 {/if} end', $a, 'if: block1 end'), array('if: {if $val1 || $val0} block1 {else} block2 {/if} end', $a, 'if: block1 end'), array('if: {if $val1 && $val0} block1 {else} block2 {/if} end', $a, 'if: block2 end'), - array('if: {if $x-9} block1 {elseif $x} block2 {else} block3 {/if} end', - $a, 'if: block2 end'), - array('if: {if round(sin(pi()), 8)} block1 {elseif $x} block2 {else} block3 {/if} end', - $a, 'if: block2 end'), - array('if: {if round(sin(pi()), 8)} block1 {elseif $val0} block2 {else} block3 {/if} end', - $a, 'if: block3 end'), + array( + 'if: {if $x-9} block1 {elseif $x} block2 {else} block3 {/if} end', + $a, + 'if: block2 end' + ), + array( + 'if: {if round(sin(pi()), 8)} block1 {elseif $x} block2 {else} block3 {/if} end', + $a, + 'if: block2 end' + ), + array( + 'if: {if round(sin(pi()), 8)} block1 {elseif $val0} block2 {else} block3 {/if} end', + $a, + 'if: block3 end' + ), array('if: {if empty($val0)} block1 {else} block2 {/if} end', $a, 'if: block1 end'), array('if: {if $val0?} block1 {else} block2 {/if} end', $a, 'if: block2 end'), array('if: {if $val1?} block1 {else} block2 {/if} end', $a, 'if: block1 end'), @@ -341,8 +430,11 @@ class TemplateTest extends TestCase array('if: {if false} block1 {else} block2 {/if} end', $a, 'if: block2 end'), array('if: {if null} block1 {else} block2 {/if} end', $a, 'if: block2 end'), array('if: {if max(2, 4) > 1 && max(2, 3) < 1} block1 {else} block2 {/if} end', $a, 'if: block2 end'), - array('if: {if ($val1 || $val0) && $x} block1 {else} block2 {/if} end', - $a, 'if: block1 end'), + array( + 'if: {if ($val1 || $val0) && $x} block1 {else} block2 {/if} end', + $a, + 'if: block1 end' + ), array('if: {if $unexist} block1 {else} block2 {/if} end', $a, 'if: block2 end', Fenom::FORCE_VERIFY), ); } @@ -351,9 +443,21 @@ class TemplateTest extends TestCase { return array( array('If: {if} block1 {/if} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), - array('If: {if 1} block1 {elseif} block2 {/if} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), - array('If: {if 1} block1 {else} block2 {elseif 0} block3 {/if} end', 'Fenom\Error\CompileException', "Incorrect use of the tag {elseif}"), - array('If: {if 1} block1 {else} block2 {/if} block3 {elseif 0} end', 'Fenom\Error\CompileException', "Unexpected tag 'elseif' (this tag can be used with 'if')"), + array( + 'If: {if 1} block1 {elseif} block2 {/if} end', + 'Fenom\Error\CompileException', + "Unexpected end of expression" + ), + array( + 'If: {if 1} block1 {else} block2 {elseif 0} block3 {/if} end', + 'Fenom\Error\CompileException', + "Incorrect use of the tag {elseif}" + ), + array( + 'If: {if 1} block1 {else} block2 {/if} block3 {elseif 0} end', + 'Fenom\Error\CompileException', + "Unexpected tag 'elseif' (this tag can be used with 'if')" + ), ); } @@ -366,10 +470,14 @@ class TemplateTest extends TestCase ); return array( array('Create: {var $v = $x+$y} Result: {$v} end', $a, 'Create: Result: 36 end'), - array('Create: {var $v = - $x - + - $y} Result: {$v} end', $a, 'Create: Result: 36 end'), + array( + 'Create: {var $v = + $x + + + $y} Result: {$v} end', + $a, + 'Create: Result: 36 end' + ), array('Create: {var $v = $z++} Result: {$v}, {$z} end', $a, 'Create: Result: 99, 100 end'), array('Create: {var $v = $z++ + 1} Result: {$v}, {$z} end', $a, 'Create: Result: 100, 100 end'), array('Create: {var $v = --$z} Result: {$v}, {$z} end', $a, 'Create: Result: 98, 98 end'), @@ -377,16 +485,27 @@ class TemplateTest extends TestCase array('Create: {var $v = $y-$x} Result: {$v} end', $a, 'Create: Result: 18 end'), array('Create: {var $v = $y*$x-2} Result: {$v} end', $a, 'Create: Result: 241 end'), array('Create: {var $v = ($y^$x)+7} Result: {$v} end', $a, 'Create: Result: 25 end'), - array('Create: {var $v = [1,2,3]} Result: {$v.1} end', $a, 'Create: Result: 2 end'), - array('Create: {var $v = []} Result: {if $v} have items {else} empty {/if} end', - $a, 'Create: Result: empty end'), - array('Create: {var $v = ["one"|upper => 1, 4 => $x, "three" => 3]} Result: {$v.three}, {$v.4}, {$v.ONE} end', - $a, 'Create: Result: 3, 9, 1 end'), - array('Create: {var $v = ["key1" => $y*$x-2, "key2" => ["z" => $z]]} Result: {$v.key1}, {$v.key2.z} end', - $a, 'Create: Result: 241, 99 end'), - array('Create: {var $v = count([1,2,3])+7} Result: {$v} end', - $a, 'Create: Result: 10 end'), + array( + 'Create: {var $v = []} Result: {if $v} have items {else} empty {/if} end', + $a, + 'Create: Result: empty end' + ), + array( + 'Create: {var $v = ["one"|upper => 1, 4 => $x, "three" => 3]} Result: {$v.three}, {$v.4}, {$v.ONE} end', + $a, + 'Create: Result: 3, 9, 1 end' + ), + array( + 'Create: {var $v = ["key1" => $y*$x-2, "key2" => ["z" => $z]]} Result: {$v.key1}, {$v.key2.z} end', + $a, + 'Create: Result: 241, 99 end' + ), + array( + 'Create: {var $v = count([1,2,3])+7} Result: {$v} end', + $a, + 'Create: Result: 10 end' + ), ); } @@ -394,19 +513,60 @@ class TemplateTest extends TestCase { return array( array('Create: {var $v} Result: {$v} end', 'Fenom\Error\CompileException', "Unclosed tag: {var} opened"), - array('Create: {var $v = } Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), + array( + 'Create: {var $v = } Result: {$v} end', + 'Fenom\Error\CompileException', + "Unexpected end of expression" + ), array('Create: {var $v = 1++} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '++'"), array('Create: {var $v = c} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token 'c'"), - array('Create: {var $v = ($a)++} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '++'"), - array('Create: {var $v = --$a++} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '++'"), - array('Create: {var $v = $a|upper++} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '++'"), - array('Create: {var $v = max($a,2)++} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '++'"), - array('Create: {var $v = max($a,2)} Result: {$v} end', 'Fenom\Error\CompileException', "Function max not found", Fenom::DENY_NATIVE_FUNCS), - array('Create: {var $v = 4*} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), + array( + 'Create: {var $v = ($a)++} Result: {$v} end', + 'Fenom\Error\CompileException', + "Unexpected token '++'" + ), + array( + 'Create: {var $v = --$a++} Result: {$v} end', + 'Fenom\Error\CompileException', + "Unexpected token '++'" + ), + array( + 'Create: {var $v = $a|upper++} Result: {$v} end', + 'Fenom\Error\CompileException', + "Unexpected token '++'" + ), + array( + 'Create: {var $v = max($a,2)++} Result: {$v} end', + 'Fenom\Error\CompileException', + "Unexpected token '++'" + ), + array( + 'Create: {var $v = max($a,2)} Result: {$v} end', + 'Fenom\Error\CompileException', + "Function max not found", + Fenom::DENY_NATIVE_FUNCS + ), + array( + 'Create: {var $v = 4*} Result: {$v} end', + 'Fenom\Error\CompileException', + "Unexpected end of expression" + ), array('Create: {var $v = ""$a} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token '\$a'"), - array('Create: {var $v = [1,2} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), - array('Create: {var $v = empty(2)} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token 2, isset() and empty() accept only variables"), - array('Create: {var $v = isset(2)} Result: {$v} end', 'Fenom\Error\CompileException', "Unexpected token 2, isset() and empty() accept only variables"), + array( + 'Create: {var $v = [1,2} Result: {$v} end', + 'Fenom\Error\CompileException', + "Unexpected end of expression" + ), + array( + 'Create: {var $v = empty(2)} Result: {$v} end', + 'Fenom\Error\CompileException', + "Unexpected token 2, isset() and empty() accept only variables" + ), + array( + 'Create: {var $v = isset(2)} Result: {$v} end', + 'Fenom\Error\CompileException', + "Unexpected token 2, isset() and empty() accept only variables" + ), ); } @@ -414,21 +574,21 @@ class TemplateTest extends TestCase public static function providerTernary() { $a = array( - "a" => 1, - "em" => "empty", - "empty" => array( - "array" => array(), - "int" => 0, + "a" => 1, + "em" => "empty", + "empty" => array( + "array" => array(), + "int" => 0, "string" => "", "double" => 0.0, - "bool" => false, + "bool" => false, ), "nonempty" => array( - "array" => array(1, 2), - "int" => 2, + "array" => array(1, 2), + "int" => 2, "string" => "abc", "double" => 0.2, - "bool" => true, + "bool" => true, ) ); return array( @@ -494,53 +654,185 @@ class TemplateTest extends TestCase public static function providerForeach() { $a = array( - "list" => array(1 => "one", 2 => "two", 3 => "three"), + "list" => array(1 => "one", 2 => "two", 3 => "three"), "empty" => array() ); return array( array('Foreach: {foreach $list as $e} {$e}, {/foreach} end', $a, 'Foreach: one, two, three, end'), array('Foreach: {foreach $list as $e} {$e},{break} break {/foreach} end', $a, 'Foreach: one, end'), - array('Foreach: {foreach $list as $e} {$e},{continue} continue {/foreach} end', $a, 'Foreach: one, two, three, end'), - array('Foreach: {foreach ["one", "two", "three"] as $e} {$e}, {/foreach} end', $a, 'Foreach: one, two, three, end'), - array('Foreach: {foreach $list as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: 1 => one, 2 => two, 3 => three, end'), - array('Foreach: {foreach [1 => "one", 2 => "two", 3 => "three"] as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: 1 => one, 2 => two, 3 => three, end'), + array( + 'Foreach: {foreach $list as $e} {$e},{continue} continue {/foreach} end', + $a, + 'Foreach: one, two, three, end' + ), + array( + 'Foreach: {foreach ["one", "two", "three"] as $e} {$e}, {/foreach} end', + $a, + 'Foreach: one, two, three, end' + ), + array( + 'Foreach: {foreach $list as $k => $e} {$k} => {$e}, {/foreach} end', + $a, + 'Foreach: 1 => one, 2 => two, 3 => three, end' + ), + array( + 'Foreach: {foreach [1 => "one", 2 => "two", 3 => "three"] as $k => $e} {$k} => {$e}, {/foreach} end', + $a, + 'Foreach: 1 => one, 2 => two, 3 => three, end' + ), array('Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'), array('Foreach: {foreach [] as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'), - array('Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {foreachelse} empty {/foreach} end', $a, 'Foreach: empty end'), - array('Foreach: {foreach $list as $e index=$i} {$i}: {$e}, {/foreach} end', $a, 'Foreach: 0: one, 1: two, 2: three, end'), - array('Foreach: {foreach $list as $k => $e index=$i} {$i}: {$k} => {$e}, {/foreach} end', $a, 'Foreach: 0: 1 => one, 1: 2 => two, 2: 3 => three, end'), - array('Foreach: {foreach $empty as $k => $e index=$i} {$i}: {$k} => {$e}, {foreachelse} empty {/foreach} end', $a, 'Foreach: empty end'), - array('Foreach: {foreach $list as $k => $e first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {/foreach} end', $a, 'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, end'), - array('Foreach: {foreach $list as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {/foreach} end', $a, 'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, last end'), - array('Foreach: {foreach $empty as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {foreachelse} empty {/foreach} end', $a, 'Foreach: empty end'), - array('Foreach: {foreach [1 => "one", 2 => "two", 3 => "three"] as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {/foreach} end', $a, 'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, last end'), + array( + 'Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {foreachelse} empty {/foreach} end', + $a, + 'Foreach: empty end' + ), + array( + 'Foreach: {foreach $list as $e index=$i} {$i}: {$e}, {/foreach} end', + $a, + 'Foreach: 0: one, 1: two, 2: three, end' + ), + array( + 'Foreach: {foreach $list as $k => $e index=$i} {$i}: {$k} => {$e}, {/foreach} end', + $a, + 'Foreach: 0: 1 => one, 1: 2 => two, 2: 3 => three, end' + ), + array( + 'Foreach: {foreach $empty as $k => $e index=$i} {$i}: {$k} => {$e}, {foreachelse} empty {/foreach} end', + $a, + 'Foreach: empty end' + ), + array( + 'Foreach: {foreach $list as $k => $e first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {/foreach} end', + $a, + 'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, end' + ), + array( + 'Foreach: {foreach $list as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {/foreach} end', + $a, + 'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, last end' + ), + array( + 'Foreach: {foreach $empty as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {foreachelse} empty {/foreach} end', + $a, + 'Foreach: empty end' + ), + array( + 'Foreach: {foreach [1 => "one", 2 => "two", 3 => "three"] as $k => $e last=$l first=$f index=$i} {if $f}first{/if} {$i}: {$k} => {$e}, {if $l}last{/if} {/foreach} end', + $a, + 'Foreach: first 0: 1 => one, 1: 2 => two, 2: 3 => three, last end' + ), ); } public static function providerForeachInvalid() { return array( - array('Foreach: {foreach} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected end of tag {foreach}"), - array('Foreach: {foreach $list} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), - array('Foreach: {foreach $list+1 as $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"), - array('Foreach: {foreach array_random() as $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'array_random'"), - array('Foreach: {foreach $list as $e+1} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"), - array('Foreach: {foreach $list as $k+1 => $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"), - array('Foreach: {foreach $list as max($i,1) => $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'max'"), - array('Foreach: {foreach $list as max($e,1)} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'max'"), - array('Foreach: {foreach $list => $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '=>'"), - array('Foreach: {foreach $list $k => $e} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '\$k'"), - array('Foreach: {foreach $list as $k =>} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), - array('Foreach: {foreach last=$l $list as $e } {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'last' in tag {foreach}"), - array('Foreach: {foreach $list as $e unknown=1} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unknown parameter 'unknown'"), - array('Foreach: {foreach $list as $e index=$i+1} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"), - array('Foreach: {foreach $list as $e first=$f+1} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"), - array('Foreach: {foreach $list as $e last=$l+1} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token '+'"), - array('Foreach: {foreach $list as $e index=max($i,1)} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'max'"), - array('Foreach: {foreach $list as $e first=max($i,1)} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'max'"), - array('Foreach: {foreach $list as $e last=max($i,1)} {$e}, {/foreach} end', 'Fenom\Error\CompileException', "Unexpected token 'max'"), - array('Foreach: {foreach $list as $e} {$e}, {foreachelse} {break} {/foreach} end', 'Fenom\Error\CompileException', "Improper usage of the tag {break}"), - array('Foreach: {foreach $list as $e} {$e}, {foreachelse} {continue} {/foreach} end', 'Fenom\Error\CompileException', "Improper usage of the tag {continue}"), + array( + 'Foreach: {foreach} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected end of tag {foreach}" + ), + array( + 'Foreach: {foreach $list} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected end of expression" + ), + array( + 'Foreach: {foreach $list+1 as $e} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token '+'" + ), + array( + 'Foreach: {foreach array_random() as $e} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token 'array_random'" + ), + array( + 'Foreach: {foreach $list as $e+1} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token '+'" + ), + array( + 'Foreach: {foreach $list as $k+1 => $e} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token '+'" + ), + array( + 'Foreach: {foreach $list as max($i,1) => $e} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token 'max'" + ), + array( + 'Foreach: {foreach $list as max($e,1)} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token 'max'" + ), + array( + 'Foreach: {foreach $list => $e} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token '=>'" + ), + array( + 'Foreach: {foreach $list $k => $e} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token '\$k'" + ), + array( + 'Foreach: {foreach $list as $k =>} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected end of expression" + ), + array( + 'Foreach: {foreach last=$l $list as $e } {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token 'last' in tag {foreach}" + ), + array( + 'Foreach: {foreach $list as $e unknown=1} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unknown parameter 'unknown'" + ), + array( + 'Foreach: {foreach $list as $e index=$i+1} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token '+'" + ), + array( + 'Foreach: {foreach $list as $e first=$f+1} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token '+'" + ), + array( + 'Foreach: {foreach $list as $e last=$l+1} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token '+'" + ), + array( + 'Foreach: {foreach $list as $e index=max($i,1)} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token 'max'" + ), + array( + 'Foreach: {foreach $list as $e first=max($i,1)} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token 'max'" + ), + array( + 'Foreach: {foreach $list as $e last=max($i,1)} {$e}, {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected token 'max'" + ), + array( + 'Foreach: {foreach $list as $e} {$e}, {foreachelse} {break} {/foreach} end', + 'Fenom\Error\CompileException', + "Improper usage of the tag {break}" + ), + array( + 'Foreach: {foreach $list as $e} {$e}, {foreachelse} {continue} {/foreach} end', + 'Fenom\Error\CompileException', + "Improper usage of the tag {continue}" + ), ); } @@ -552,8 +844,12 @@ class TemplateTest extends TestCase array('{if 0}none{/if} literal:{ignore} {$a} {/ignore} end', $a, 'literal: {$a} end'), array('{if 0}none{/if} literal: { $a} end', $a, 'literal: { $a} end'), array('{if 0}none{/if} literal: { $a}{$a}{ $a} end', $a, 'literal: { $a}lit. A{ $a} end'), - array('{if 0}none{/if} literal: { - $a} end', $a, 'literal: { $a} end'), + array( + '{if 0}none{/if} literal: { + $a} end', + $a, + 'literal: { $a} end' + ), array('{if 0}none{/if}literal: function () { return 1; } end', $a, 'literal: function () { return 1; } end') ); } @@ -592,9 +888,21 @@ class TemplateTest extends TestCase public static function providerSwitchInvalid() { return array( - array('Switch: {switch}{case 1} one {/switch} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), - array('Switch: {switch 1}{case} one{/switch} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), - array('Switch: {switch 1}{case $var} one {/switch} end', 'Fenom\Error\CompileException', "Unexpected token '\$var' in expression"), + array( + 'Switch: {switch}{case 1} one {/switch} end', + 'Fenom\Error\CompileException', + "Unexpected end of expression" + ), + array( + 'Switch: {switch 1}{case} one{/switch} end', + 'Fenom\Error\CompileException', + "Unexpected end of expression" + ), + array( + 'Switch: {switch 1}{case $var} one {/switch} end', + 'Fenom\Error\CompileException', + "Unexpected token '\$var' in expression" + ), ); } @@ -626,16 +934,32 @@ class TemplateTest extends TestCase array('For: {for $a=$c step=$s to=$m} $a: {$a}, {/for} end', $a, 'For: $a: 1, $a: 2, $a: 3, end'), array('For: {for $a=-1 step=-max(1,2) to=-5} $a: {$a}, {/for} end', $a, 'For: $a: -1, $a: -3, $a: -5, end'), array('For: {for $a=4 step=2 to=10} $a: {$a}, {break} break {/for} end', $a, 'For: $a: 4, end'), - array('For: {for $a=4 step=2 to=8} $a: {$a}, {continue} continue {/for} end', - $a, 'For: $a: 4, $a: 6, $a: 8, end'), - array('For: {for $a=4 step=2 to=8 index=$i} $a{$i}: {$a}, {/for} end', $a, 'For: $a0: 4, $a1: 6, $a2: 8, end'), - array('For: {for $a=4 step=2 to=8 index=$i first=$f} {if $f}first{/if} $a{$i}: {$a}, {/for} end', - $a, 'For: first $a0: 4, $a1: 6, $a2: 8, end'), - array('For: {for $a=4 step=2 to=8 index=$i first=$f last=$l} {if $f} first {/if} $a{$i}: {$a}, {if $l} last {/if} {/for} end', - $a, 'For: first $a0: 4, $a1: 6, $a2: 8, last end'), + array( + 'For: {for $a=4 step=2 to=8} $a: {$a}, {continue} continue {/for} end', + $a, + 'For: $a: 4, $a: 6, $a: 8, end' + ), + array( + 'For: {for $a=4 step=2 to=8 index=$i} $a{$i}: {$a}, {/for} end', + $a, + 'For: $a0: 4, $a1: 6, $a2: 8, end' + ), + array( + 'For: {for $a=4 step=2 to=8 index=$i first=$f} {if $f}first{/if} $a{$i}: {$a}, {/for} end', + $a, + 'For: first $a0: 4, $a1: 6, $a2: 8, end' + ), + array( + 'For: {for $a=4 step=2 to=8 index=$i first=$f last=$l} {if $f} first {/if} $a{$i}: {$a}, {if $l} last {/if} {/for} end', + $a, + 'For: first $a0: 4, $a1: 6, $a2: 8, last end' + ), array('For: {for $a=1 to=-1 } $a: {$a}, {forelse} empty {/for} end', $a, 'For: empty end'), - array('For: {for $a=1 to=-1 index=$i first=$f last=$l} {if $f} first {/if} $a{$i}: {$a}, {if $l} last {/if} {forelse} empty {/for} end', - $a, 'For: empty end'), + array( + 'For: {for $a=1 to=-1 index=$i first=$f last=$l} {if $f} first {/if} $a{$i}: {$a}, {if $l} last {/if} {forelse} empty {/for} end', + $a, + 'For: empty end' + ), ); } @@ -645,27 +969,79 @@ class TemplateTest extends TestCase array('For: {for} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), array('For: {for $a=} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected end of expression"), array('For: {for $a+1=3 to=6} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token '+'"), - array('For: {for max($a,$b)=3 to=6} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token '='"), + array( + 'For: {for max($a,$b)=3 to=6} block1 {/for} end', + 'Fenom\Error\CompileException', + "Unexpected token '='" + ), array('For: {for to=6 $a=3} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token 'to'"), - array('For: {for index=$i $a=3 to=6} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token 'index'"), - array('For: {for first=$i $a=3 to=6} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token 'first'"), - array('For: {for last=$i $a=3 to=6} block1 {/for} end', 'Fenom\Error\CompileException', "Unexpected token 'last'"), - array('For: {for $a=4 to=6 unk=4} block1 {/for} end', 'Fenom\Error\CompileException', "Unknown parameter 'unk'"), - array('For: {for $a=4 to=6 step=0} block1 {/for} end', 'Fenom\Error\CompileException', "Invalid step value"), - array('For: {for $a=4 to=6} $a: {$a}, {forelse} {break} {/for} end', 'Fenom\Error\CompileException', "Improper usage of the tag {break}"), - array('For: {for $a=4 to=6} $a: {$a}, {forelse} {continue} {/for} end', 'Fenom\Error\CompileException', "Improper usage of the tag {continue}"), + array( + 'For: {for index=$i $a=3 to=6} block1 {/for} end', + 'Fenom\Error\CompileException', + "Unexpected token 'index'" + ), + array( + 'For: {for first=$i $a=3 to=6} block1 {/for} end', + 'Fenom\Error\CompileException', + "Unexpected token 'first'" + ), + array( + 'For: {for last=$i $a=3 to=6} block1 {/for} end', + 'Fenom\Error\CompileException', + "Unexpected token 'last'" + ), + array( + 'For: {for $a=4 to=6 unk=4} block1 {/for} end', + 'Fenom\Error\CompileException', + "Unknown parameter 'unk'" + ), + array( + 'For: {for $a=4 to=6 step=0} block1 {/for} end', + 'Fenom\Error\CompileException', + "Invalid step value" + ), + array( + 'For: {for $a=4 to=6} $a: {$a}, {forelse} {break} {/for} end', + 'Fenom\Error\CompileException', + "Improper usage of the tag {break}" + ), + array( + 'For: {for $a=4 to=6} $a: {$a}, {forelse} {continue} {/for} end', + 'Fenom\Error\CompileException', + "Improper usage of the tag {continue}" + ), ); } public static function providerLayersInvalid() { return array( - array('Layers: {foreach $list as $e} block1 {if 1} {foreachelse} {/if} {/foreach} end', 'Fenom\Error\CompileException', "Unexpected tag 'foreachelse' (this tag can be used with 'foreach')"), - array('Layers: {foreach $list as $e} block1 {if 1} {/foreach} {/if} end', 'Fenom\Error\CompileException', "Unexpected closing of the tag 'foreach'"), + array( + 'Layers: {foreach $list as $e} block1 {if 1} {foreachelse} {/if} {/foreach} end', + 'Fenom\Error\CompileException', + "Unexpected tag 'foreachelse' (this tag can be used with 'foreach')" + ), + array( + 'Layers: {foreach $list as $e} block1 {if 1} {/foreach} {/if} end', + 'Fenom\Error\CompileException', + "Unexpected closing of the tag 'foreach'" + ), array('Layers: {blah} end', 'Fenom\Error\CompileException', "Unexpected tag 'blah'"), - array('Layers: {for $a=4 to=6} block1 {if 1} {forelse} {/if} {/for} end', 'Fenom\Error\CompileException', "Unexpected tag 'forelse' (this tag can be used with 'for')"), - array('Layers: {for $a=4 to=6} block1 {if 1} {/for} {/if} end', 'Fenom\Error\CompileException', "Unexpected closing of the tag 'for'"), - array('Layers: {switch 1} {if 1} {case 1} {/if} {/switch} end', 'Fenom\Error\CompileException', "Unexpected tag 'case' (this tag can be used with 'switch')"), + array( + 'Layers: {for $a=4 to=6} block1 {if 1} {forelse} {/if} {/for} end', + 'Fenom\Error\CompileException', + "Unexpected tag 'forelse' (this tag can be used with 'for')" + ), + array( + 'Layers: {for $a=4 to=6} block1 {if 1} {/for} {/if} end', + 'Fenom\Error\CompileException', + "Unexpected closing of the tag 'for'" + ), + array( + 'Layers: {switch 1} {if 1} {case 1} {/if} {/switch} end', + 'Fenom\Error\CompileException', + "Unexpected tag 'case' (this tag can be used with 'switch')" + ), array('Layers: {/switch} end', 'Fenom\Error\CompileException', "Unexpected closing of the tag 'switch'"), array('Layers: {if 1} end', 'Fenom\Error\CompileException', "Unclosed tag: {if}"), ); @@ -676,10 +1052,22 @@ class TemplateTest extends TestCase return array( array('{extends file="parent.tpl"}{block name="bk1"} block1 {/block}', "Template extended by block1"), array('{extends "parent.tpl"}{block "bk1"} block1 {/block}', "Template extended by block1"), - array('{extends "parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} garbage', "Template extended by block1"), - array('{extends file="parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} garbage', "Template multi-extended by block1"), - array('{extends "parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} {block "bk3"} block3 {/block} garbage', "Template multi-extended by block1"), - array('{extends "parent.tpl"}{var $bk = "bk3"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} {block "$bk"} block3 {/block} garbage', "Template multi-extended by block1"), + array( + '{extends "parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} garbage', + "Template extended by block1" + ), + array( + '{extends file="parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} garbage', + "Template multi-extended by block1" + ), + array( + '{extends "parent.tpl"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} {block "bk3"} block3 {/block} garbage', + "Template multi-extended by block1" + ), + array( + '{extends "parent.tpl"}{var $bk = "bk3"}{block "bk1"} block1 {/block}{block "bk2"} block2 {/block} {block "$bk"} block3 {/block} garbage', + "Template multi-extended by block1" + ), ); } @@ -752,12 +1140,10 @@ class TemplateTest extends TestCase array('{if $one in "qwertyuiop"} block1 {else} block2 {/if}', 'block2'), array('{if $one not in "qwertyuiop 1"} block1 {else} block2 {/if}', 'block2'), array('{if $one not in "qwertyuiop"} block1 {else}v block2 {/if}', 'block1'), - array('{if $one in [1, 2, 3]} block1 {else} block2 {/if}', 'block1'), array('{if $one in list [1, 2, 3]} block1 {else} block2 {/if}', 'block1'), array('{if $one in ["one", "two", "three"]} block1 {else} block2 {/if}', 'block2'), array('{if $one in keys [1 => "one", 2 => "two", 3 => "three"]} block1 {else} block2 {/if}', 'block1'), - array('{if $one in $two} block1 {else} block2 {/if}', 'block2'), ); } @@ -799,11 +1185,9 @@ class TemplateTest extends TestCase array('{$.const.MY}', ''), array('{$.version}', Fenom::VERSION), array('{"string"|append:"_":$.get.one}', 'string_get1'), - array('{$.get.one?}', '1'), array('{$.get.one is set}', '1'), array('{$.get.two is empty}', '1'), - array('{$.version}', Fenom::VERSION), array('{$.tpl?}', '1'), array('{$.tpl.name}', 'runtime.tpl'), @@ -833,22 +1217,61 @@ class TemplateTest extends TestCase public function providerStaticInvalid() { return array( - array('{Fenom\TemplateTest::multi x=3 y=4}', 'Fenom\Error\SecurityException', "Static methods are disabled", Fenom::DENY_STATICS), - array('{Fenom\TemplateTest::multi(3,4)}', 'Fenom\Error\SecurityException', "Static methods are disabled", Fenom::DENY_STATICS), - array('{12 + Fenom\TemplateTest::multi(3,4)}', 'Fenom\Error\SecurityException', "Static methods are disabled", Fenom::DENY_STATICS), - array('{12 + 3|Fenom\TemplateTest::multi:4}', 'Fenom\Error\SecurityException', "Static methods are disabled", Fenom::DENY_STATICS), - - array('{Fenom\TemplateTest::multi_invalid x=3 y=4}', 'Fenom\Error\CompileException', 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'), - array('{Fenom\TemplateTest::multi_invalid(3,4)}', 'Fenom\Error\CompileException', 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'), - array('{12 + Fenom\TemplateTest::multi_invalid(3,4)}', 'Fenom\Error\CompileException', 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'), - array('{12 + 3|Fenom\TemplateTest::multi_invalid:4}', 'Fenom\Error\CompileException', 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist'), + array( + '{Fenom\TemplateTest::multi x=3 y=4}', + 'Fenom\Error\SecurityException', + "Static methods are disabled", + Fenom::DENY_STATICS + ), + array( + '{Fenom\TemplateTest::multi(3,4)}', + 'Fenom\Error\SecurityException', + "Static methods are disabled", + Fenom::DENY_STATICS + ), + array( + '{12 + Fenom\TemplateTest::multi(3,4)}', + 'Fenom\Error\SecurityException', + "Static methods are disabled", + Fenom::DENY_STATICS + ), + array( + '{12 + 3|Fenom\TemplateTest::multi:4}', + 'Fenom\Error\SecurityException', + "Static methods are disabled", + Fenom::DENY_STATICS + ), + array( + '{Fenom\TemplateTest::multi_invalid x=3 y=4}', + 'Fenom\Error\CompileException', + 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist' + ), + array( + '{Fenom\TemplateTest::multi_invalid(3,4)}', + 'Fenom\Error\CompileException', + 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist' + ), + array( + '{12 + Fenom\TemplateTest::multi_invalid(3,4)}', + 'Fenom\Error\CompileException', + 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist' + ), + array( + '{12 + 3|Fenom\TemplateTest::multi_invalid:4}', + 'Fenom\Error\CompileException', + 'Method Fenom\TemplateTest::multi_invalid doesn\'t exist' + ), ); } public function _testSandbox() { try { - var_dump($this->fenom->setOptions(0)->compileCode("{autoescape true}{test_block_function:raw}{\$html}{/test_block_function}{/autoescape}")->getBody()); + var_dump( + $this->fenom->setOptions(0)->compileCode( + "{autoescape true}{test_block_function:raw}{\$html}{/test_block_function}{/autoescape}" + )->getBody() + ); } catch (\Exception $e) { print_r($e->getMessage() . "\n" . $e->getTraceAsString()); while ($e->getPrevious()) { diff --git a/tests/cases/Fenom/TokenizerTest.php b/tests/cases/Fenom/TokenizerTest.php index 51e4da8..4f47898 100644 --- a/tests/cases/Fenom/TokenizerTest.php +++ b/tests/cases/Fenom/TokenizerTest.php @@ -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(':'); diff --git a/tests/cases/FenomTest.php b/tests/cases/FenomTest.php index 1678ecd..6a28548 100644 --- a/tests/cases/FenomTest.php +++ b/tests/cases/FenomTest.php @@ -1,8 +1,5 @@ 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} {/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} {/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); + } } diff --git a/tests/resources/provider/pipe.tpl b/tests/resources/provider/pipe.tpl new file mode 100644 index 0000000..299d596 --- /dev/null +++ b/tests/resources/provider/pipe.tpl @@ -0,0 +1 @@ +{foreach $items as $key => $value}{$key}:{$value}{/foreach} \ No newline at end of file