Merge remote-tracking branch 'refs/remotes/origin/develop'

This commit is contained in:
bzick 2013-08-05 19:26:40 +04:00
commit c1d0268a94
36 changed files with 2620 additions and 1822 deletions

View File

@ -1,6 +1,19 @@
CHANGELOG CHANGELOG
========= =========
## 1.2.0
- Feature #28: macros may be called recursively
- Feature #29: add {unset} tag
- Add hook for loading modifiers and tags
- Add hook for loading modifiers and tags
- Feature #3: Add string operator '~'
- Improve parsers: parserExp, parserVar, parserVariable, parserMacro
- Fix ternary bug
- Bugs--
- Tests++
- Docs++
## 1.1.0 ## 1.1.0
- Bug #19: Bug with "if" expressions starting with "(" - Bug #19: Bug with "if" expressions starting with "("

View File

@ -3,14 +3,14 @@ Fenom - Template Engine for PHP
> Composer package: `{"fenom/fenom": "dev-master"}`. See on [Packagist.org](https://packagist.org/packages/bzick/fenom) > Composer package: `{"fenom/fenom": "dev-master"}`. See on [Packagist.org](https://packagist.org/packages/bzick/fenom)
[![Build Status](https://travis-ci.org/bzick/fenom.png?branch=master)](https://travis-ci.org/bzick/fenom) [![Build Status](https://travis-ci.org/bzick/fenom.png?branch=master)](https://travis-ci.org/fenom/fenom)
## [Usage](./docs/usage.md) :: [Documentation](./docs/readme.md) :: [Benchmark](./docs/benchmark.md) :: [Articles](./docs/articles.md) ## [Usage](./docs/usage.md) :: [Documentation](./docs/readme.md) :: [Benchmark](./docs/benchmark.md) :: [Articles](./docs/articles.md)
* Simple [syntax](./docs/syntax.md) * Simple [syntax](./docs/syntax.md)
* [Fast](./docs/benchmark.md) * [Fast](./docs/benchmark.md)
* [Secure](./docs/settings.md) * [Secure](./docs/settings.md)
* [Simple](./ideology.md) * Simple
* [Flexible](./docs/readme.md#extends) * [Flexible](./docs/ext/extensions.md)
* [Lightweight](./docs/benchmark.md#stats) * [Lightweight](./docs/benchmark.md#stats)
* [Powerful](./docs/readme.md) * [Powerful](./docs/readme.md)
* Easy to use: * Easy to use:

View File

@ -1,9 +1,8 @@
{ {
"name": "fenom/fenom", "name": "fenom/fenom",
"type": "library", "type": "library",
"description": "Fenom - fast template engine for PHP", "description": "Fenom - excellent template engine for PHP",
"homepage": "http://bzick.github.io/fenom/", "keywords": ["fenom", "template", "templating", "templater"],
"keywords": ["fenom", "template", "templating", "cytro"],
"license": "BSD-3", "license": "BSD-3",
"authors": [ "authors": [
{ {

View File

@ -1,5 +1,9 @@
Extensions Extensions
========== ==========
* [Extra pack](https://github.com/bzick/fenom-extra) basic add-ons for web-base project. * [Extra pack](https://github.com/bzick/fenom-extra) of add-ons for Fenom template engine.
* *Smarty pack* (planned) Smarty3 adapter * Tools for static files (css, js).
* Global variables
* Allow more hooks for extending
* Add variable container
* You can only use the necessary add-ons

View File

@ -81,6 +81,10 @@ Operators
* `--$a` - decrement the variable and use it * `--$a` - decrement the variable and use it
* `$a--` - use the variable and decrement it * `$a--` - use the variable and decrement it
### String operator
* `$a ~ $b` - return concatenation of variables `$a` and `$b`
### Ternary operator ### Ternary operator
* `$a ? $b : $c` - returns `$b` if `$a` is not empty, and `$c` otherwise * `$a ? $b : $c` - returns `$b` if `$a` is not empty, and `$c` otherwise

View File

@ -7,64 +7,86 @@
* For the full copyright and license information, please view the license.md * For the full copyright and license information, please view the license.md
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Fenom\Template, use Fenom\ProviderInterface;
Fenom\ProviderInterface; use Fenom\Template;
/** /**
* Fenom Template Engine * Fenom Template Engine
* *
* @author Ivan Shalganov <a.cobest@gmail.com> * @author Ivan Shalganov <a.cobest@gmail.com>
*/ */
class Fenom { class Fenom
const VERSION = '1.1'; {
const VERSION = '1.2';
/* Actions */ /* Actions */
const INLINE_COMPILER = 1; const INLINE_COMPILER = 1;
const BLOCK_COMPILER = 2; const BLOCK_COMPILER = 2;
const INLINE_FUNCTION = 3; const INLINE_FUNCTION = 3;
const BLOCK_FUNCTION = 4; const BLOCK_FUNCTION = 4;
const MODIFIER = 5; const MODIFIER = 5;
/* Options */ /* Options */
const DENY_METHODS = 0x10; const DENY_METHODS = 0x10;
const DENY_INLINE_FUNCS = 0x20; const DENY_NATIVE_FUNCS = 0x20;
const FORCE_INCLUDE = 0x40; const FORCE_INCLUDE = 0x40;
const AUTO_RELOAD = 0x80; const AUTO_RELOAD = 0x80;
const FORCE_COMPILE = 0x100; const FORCE_COMPILE = 0x100;
const AUTO_ESCAPE = 0x200; const AUTO_ESCAPE = 0x200;
const DISABLE_CACHE = 0x400; const DISABLE_CACHE = 0x400;
const FORCE_VERIFY = 0x800; // reserved const FORCE_VERIFY = 0x800; // reserved
const AUTO_TRIM = 0x1000; // reserved const AUTO_TRIM = 0x1000; // reserved
const DENY_STATICS = 0x2000; // reserved const DENY_STATICS = 0x2000; // reserved
/* @deprecated */
const DENY_INLINE_FUNCS = 0x20;
/* Default parsers */ /* Default parsers */
const DEFAULT_CLOSE_COMPILER = 'Fenom\Compiler::stdClose'; const DEFAULT_CLOSE_COMPILER = 'Fenom\Compiler::stdClose';
const DEFAULT_FUNC_PARSER = 'Fenom\Compiler::stdFuncParser'; const DEFAULT_FUNC_PARSER = 'Fenom\Compiler::stdFuncParser';
const DEFAULT_FUNC_OPEN = 'Fenom\Compiler::stdFuncOpen'; const DEFAULT_FUNC_OPEN = 'Fenom\Compiler::stdFuncOpen';
const DEFAULT_FUNC_CLOSE = 'Fenom\Compiler::stdFuncClose'; const DEFAULT_FUNC_CLOSE = 'Fenom\Compiler::stdFuncClose';
const SMART_FUNC_PARSER = 'Fenom\Compiler::smartFuncParser'; const SMART_FUNC_PARSER = 'Fenom\Compiler::smartFuncParser';
const MAX_MACRO_RECURSIVE = 32;
/** /**
* @var int[] of possible options, as associative array * @var int[] of possible options, as associative array
* @see setOptions * @see setOptions
*/ */
private static $_options_list = array( private static $_options_list = array(
"disable_methods" => self::DENY_METHODS, "disable_methods" => self::DENY_METHODS,
"disable_native_funcs" => self::DENY_INLINE_FUNCS, "disable_native_funcs" => self::DENY_NATIVE_FUNCS,
"disable_cache" => self::DISABLE_CACHE, "disable_cache" => self::DISABLE_CACHE,
"force_compile" => self::FORCE_COMPILE, "force_compile" => self::FORCE_COMPILE,
"auto_reload" => self::AUTO_RELOAD, "auto_reload" => self::AUTO_RELOAD,
"force_include" => self::FORCE_INCLUDE, "force_include" => self::FORCE_INCLUDE,
"auto_escape" => self::AUTO_ESCAPE, "auto_escape" => self::AUTO_ESCAPE,
"force_verify" => self::FORCE_VERIFY, "force_verify" => self::FORCE_VERIFY,
"auto_trim" => self::AUTO_TRIM, "auto_trim" => self::AUTO_TRIM,
"disable_statics" => self::DENY_STATICS, "disable_statics" => self::DENY_STATICS,
); );
/**
* @var callable[]
*/
public $pre_filters = array();
/**
* @var callable[]
*/
public $filters = array();
/**
* @var callable[]
*/
public $post_filters = array();
/** /**
* @var Fenom\Render[] Templates storage * @var Fenom\Render[] Templates storage
*/ */
protected $_storage = array(); protected $_storage = array();
/** /**
* @var string compile directory * @var string compile directory
*/ */
@ -75,10 +97,6 @@ class Fenom {
*/ */
protected $_options = 0; protected $_options = 0;
protected $_on_pre_cmp = array();
protected $_on_cmp = array();
protected $_on_post_cmp = array();
/** /**
* @var ProviderInterface * @var ProviderInterface
*/ */
@ -92,20 +110,19 @@ class Fenom {
* @var string[] list of modifiers [modifier_name => callable] * @var string[] list of modifiers [modifier_name => callable]
*/ */
protected $_modifiers = array( protected $_modifiers = array(
"upper" => 'strtoupper', "upper" => 'strtoupper',
"up" => 'strtoupper', "up" => 'strtoupper',
"lower" => 'strtolower', "lower" => 'strtolower',
"low" => 'strtolower', "low" => 'strtolower',
"date_format" => 'Fenom\Modifier::dateFormat', "date_format" => 'Fenom\Modifier::dateFormat',
"date" => 'Fenom\Modifier::date', "date" => 'Fenom\Modifier::date',
"truncate" => 'Fenom\Modifier::truncate', "truncate" => 'Fenom\Modifier::truncate',
"escape" => 'Fenom\Modifier::escape', "escape" => 'Fenom\Modifier::escape',
"e" => 'Fenom\Modifier::escape', // alias of escape "e" => 'Fenom\Modifier::escape', // alias of escape
"unescape" => 'Fenom\Modifier::unescape', "unescape" => 'Fenom\Modifier::unescape',
"strip" => 'Fenom\Modifier::strip', "strip" => 'Fenom\Modifier::strip',
"length" => 'Fenom\Modifier::length', "length" => 'Fenom\Modifier::length',
"default" => 'Fenom\Modifier::defaultValue', "iterable" => 'Fenom\Modifier::isIterable'
"iterable" => 'Fenom\Modifier::isIterable'
); );
/** /**
@ -132,7 +149,7 @@ class Fenom {
), ),
'float_tags' => array('break' => 1, 'continue' => 1) 'float_tags' => array('break' => 1, 'continue' => 1)
), ),
'if' => array( // {if ...} {elseif ...} {else} {/if} 'if' => array( // {if ...} {elseif ...} {else} {/if}
'type' => self::BLOCK_COMPILER, 'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::ifOpen', 'open' => 'Fenom\Compiler::ifOpen',
'close' => 'Fenom\Compiler::stdClose', 'close' => 'Fenom\Compiler::stdClose',
@ -141,7 +158,7 @@ class Fenom {
'else' => 'Fenom\Compiler::tagElse', 'else' => 'Fenom\Compiler::tagElse',
) )
), ),
'switch' => array( // {switch ...} {case ...} {break} {default} {/switch} 'switch' => array( // {switch ...} {case ...} {break} {default} {/switch}
'type' => self::BLOCK_COMPILER, 'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::switchOpen', 'open' => 'Fenom\Compiler::switchOpen',
'close' => 'Fenom\Compiler::stdClose', 'close' => 'Fenom\Compiler::stdClose',
@ -152,7 +169,7 @@ class Fenom {
), ),
'float_tags' => array('break' => 1) 'float_tags' => array('break' => 1)
), ),
'for' => array( // {for ...} {break} {continue} {/for} 'for' => array( // {for ...} {break} {continue} {/for}
'type' => self::BLOCK_COMPILER, 'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::forOpen', 'open' => 'Fenom\Compiler::forOpen',
'close' => 'Fenom\Compiler::forClose', 'close' => 'Fenom\Compiler::forClose',
@ -163,7 +180,7 @@ class Fenom {
), ),
'float_tags' => array('break' => 1, 'continue' => 1) 'float_tags' => array('break' => 1, 'continue' => 1)
), ),
'while' => array( // {while ...} {break} {continue} {/while} 'while' => array( // {while ...} {break} {continue} {/while}
'type' => self::BLOCK_COMPILER, 'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::whileOpen', 'open' => 'Fenom\Compiler::whileOpen',
'close' => 'Fenom\Compiler::stdClose', 'close' => 'Fenom\Compiler::stdClose',
@ -177,12 +194,12 @@ class Fenom {
'type' => self::INLINE_COMPILER, 'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagInclude' 'parser' => 'Fenom\Compiler::tagInclude'
), ),
'var' => array( // {var ...} 'var' => array( // {var ...}
'type' => self::BLOCK_COMPILER, 'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::varOpen', 'open' => 'Fenom\Compiler::varOpen',
'close' => 'Fenom\Compiler::varClose' 'close' => 'Fenom\Compiler::varClose'
), ),
'block' => array( // {block ...} {parent} {/block} 'block' => array( // {block ...} {parent} {/block}
'type' => self::BLOCK_COMPILER, 'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::tagBlockOpen', 'open' => 'Fenom\Compiler::tagBlockOpen',
'close' => 'Fenom\Compiler::tagBlockClose', 'close' => 'Fenom\Compiler::tagBlockClose',
@ -222,7 +239,7 @@ class Fenom {
'type' => self::INLINE_COMPILER, 'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagCycle' 'parser' => 'Fenom\Compiler::tagCycle'
), ),
'raw' => array( 'raw' => array(
'type' => self::INLINE_COMPILER, 'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagRaw' 'parser' => 'Fenom\Compiler::tagRaw'
), ),
@ -230,6 +247,10 @@ class Fenom {
'type' => self::BLOCK_COMPILER, 'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::autoescapeOpen', 'open' => 'Fenom\Compiler::autoescapeOpen',
'close' => 'Fenom\Compiler::autoescapeClose' 'close' => 'Fenom\Compiler::autoescapeClose'
),
'unset' => array(
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagUnset'
) )
); );
@ -242,10 +263,11 @@ class Fenom {
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @return Fenom * @return Fenom
*/ */
public static function factory($source, $compile_dir = '/tmp', $options = 0) { public static function factory($source, $compile_dir = '/tmp', $options = 0)
if(is_string($source)) { {
if (is_string($source)) {
$provider = new Fenom\Provider($source); $provider = new Fenom\Provider($source);
} elseif($source instanceof ProviderInterface) { } elseif ($source instanceof ProviderInterface) {
$provider = $source; $provider = $source;
} else { } else {
throw new InvalidArgumentException("Source must be a valid path or provider object"); throw new InvalidArgumentException("Source must be a valid path or provider object");
@ -253,7 +275,7 @@ class Fenom {
$fenom = new static($provider); $fenom = new static($provider);
/* @var Fenom $fenom */ /* @var Fenom $fenom */
$fenom->setCompileDir($compile_dir); $fenom->setCompileDir($compile_dir);
if($options) { if ($options) {
$fenom->setOptions($options); $fenom->setOptions($options);
} }
return $fenom; return $fenom;
@ -262,7 +284,8 @@ class Fenom {
/** /**
* @param Fenom\ProviderInterface $provider * @param Fenom\ProviderInterface $provider
*/ */
public function __construct(Fenom\ProviderInterface $provider) { public function __construct(Fenom\ProviderInterface $provider)
{
$this->_provider = $provider; $this->_provider = $provider;
} }
@ -272,7 +295,8 @@ class Fenom {
* @param string $dir directory to store compiled templates in * @param string $dir directory to store compiled templates in
* @return Fenom * @return Fenom
*/ */
public function setCompileDir($dir) { public function setCompileDir($dir)
{
$this->_compile_dir = $dir; $this->_compile_dir = $dir;
return $this; return $this;
} }
@ -280,24 +304,50 @@ class Fenom {
/** /**
* *
* @param callable $cb * @param callable $cb
* @return self
*/ */
public function addPreCompileFilter($cb) { public function addPreFilter($cb)
$this->_on_pre_cmp[] = $cb; {
$this->pre_filters[] = $cb;
return $this;
}
public function getPreFilters()
{
return $this->pre_filters;
} }
/** /**
* *
* @param callable $cb * @param callable $cb
* @return self
*/ */
public function addPostCompileFilter($cb) { public function addPostFilter($cb)
$this->_on_post_cmp[] = $cb; {
$this->post_filters[] = $cb;
return $this;
}
public function getPostFilters()
{
return $this->post_filters;
} }
/** /**
* @param callable $cb * @param callable $cb
* @return self
*/ */
public function addCompileFilter($cb) { public function addFilter($cb)
$this->_on_cmp[] = $cb; {
$this->filters[] = $cb;
return $this;
}
public function getFilters()
{
return $this->filters;
} }
/** /**
@ -307,7 +357,8 @@ class Fenom {
* @param string $callback the modifier callback * @param string $callback the modifier callback
* @return Fenom * @return Fenom
*/ */
public function addModifier($modifier, $callback) { public function addModifier($modifier, $callback)
{
$this->_modifiers[$modifier] = $callback; $this->_modifiers[$modifier] = $callback;
return $this; return $this;
} }
@ -319,7 +370,8 @@ class Fenom {
* @param callable $parser * @param callable $parser
* @return Fenom * @return Fenom
*/ */
public function addCompiler($compiler, $parser) { public function addCompiler($compiler, $parser)
{
$this->_actions[$compiler] = array( $this->_actions[$compiler] = array(
'type' => self::INLINE_COMPILER, 'type' => self::INLINE_COMPILER,
'parser' => $parser 'parser' => $parser
@ -332,11 +384,12 @@ class Fenom {
* @param string|object $storage * @param string|object $storage
* @return $this * @return $this
*/ */
public function addCompilerSmart($compiler, $storage) { public function addCompilerSmart($compiler, $storage)
if(method_exists($storage, "tag".$compiler)) { {
if (method_exists($storage, "tag" . $compiler)) {
$this->_actions[$compiler] = array( $this->_actions[$compiler] = array(
'type' => self::INLINE_COMPILER, 'type' => self::INLINE_COMPILER,
'parser' => array($storage, "tag".$compiler) 'parser' => array($storage, "tag" . $compiler)
); );
} }
return $this; return $this;
@ -351,11 +404,12 @@ class Fenom {
* @param array $tags * @param array $tags
* @return Fenom * @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( $this->_actions[$compiler] = array(
'type' => self::BLOCK_COMPILER, 'type' => self::BLOCK_COMPILER,
'open' => $open_parser, 'open' => $open_parser,
'close' => $close_parser ?: self::DEFAULT_CLOSE_COMPILER, 'close' => $close_parser ? : self::DEFAULT_CLOSE_COMPILER,
'tags' => $tags, 'tags' => $tags,
); );
return $this; return $this;
@ -369,27 +423,28 @@ class Fenom {
* @throws LogicException * @throws LogicException
* @return Fenom * @return Fenom
*/ */
public function addBlockCompilerSmart($compiler, $storage, array $tags, array $floats = array()) { public function addBlockCompilerSmart($compiler, $storage, array $tags, array $floats = array())
{
$c = array( $c = array(
'type' => self::BLOCK_COMPILER, 'type' => self::BLOCK_COMPILER,
"tags" => array(), "tags" => array(),
"float_tags" => array() "float_tags" => array()
); );
if(method_exists($storage, $compiler."Open")) { if (method_exists($storage, $compiler . "Open")) {
$c["open"] = $compiler."Open"; $c["open"] = $compiler . "Open";
} else { } else {
throw new \LogicException("Open compiler {$compiler}Open not found"); throw new \LogicException("Open compiler {$compiler}Open not found");
} }
if(method_exists($storage, $compiler."Close")) { if (method_exists($storage, $compiler . "Close")) {
$c["close"] = $compiler."Close"; $c["close"] = $compiler . "Close";
} else { } else {
throw new \LogicException("Close compiler {$compiler}Close not found"); throw new \LogicException("Close compiler {$compiler}Close not found");
} }
foreach($tags as $tag) { foreach ($tags as $tag) {
if(method_exists($storage, "tag".$tag)) { if (method_exists($storage, "tag" . $tag)) {
$c["tags"][ $tag ] = "tag".$tag; $c["tags"][$tag] = "tag" . $tag;
if($floats && in_array($tag, $floats)) { if ($floats && in_array($tag, $floats)) {
$c['float_tags'][ $tag ] = 1; $c['float_tags'][$tag] = 1;
} }
} else { } else {
throw new \LogicException("Tag compiler $tag (tag{$compiler}) not found"); throw new \LogicException("Tag compiler $tag (tag{$compiler}) not found");
@ -405,7 +460,8 @@ class Fenom {
* @param callable|string $parser * @param callable|string $parser
* @return Fenom * @return Fenom
*/ */
public function addFunction($function, $callback, $parser = self::DEFAULT_FUNC_PARSER) { public function addFunction($function, $callback, $parser = self::DEFAULT_FUNC_PARSER)
{
$this->_actions[$function] = array( $this->_actions[$function] = array(
'type' => self::INLINE_FUNCTION, 'type' => self::INLINE_FUNCTION,
'parser' => $parser, 'parser' => $parser,
@ -419,7 +475,8 @@ class Fenom {
* @param callable $callback * @param callable $callback
* @return Fenom * @return Fenom
*/ */
public function addFunctionSmart($function, $callback) { public function addFunctionSmart($function, $callback)
{
$this->_actions[$function] = array( $this->_actions[$function] = array(
'type' => self::INLINE_FUNCTION, 'type' => self::INLINE_FUNCTION,
'parser' => self::SMART_FUNC_PARSER, 'parser' => self::SMART_FUNC_PARSER,
@ -435,12 +492,13 @@ class Fenom {
* @param callable|string $parser_close * @param callable|string $parser_close
* @return Fenom * @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( $this->_actions[$function] = array(
'type' => self::BLOCK_FUNCTION, 'type' => self::BLOCK_FUNCTION,
'open' => $parser_open, 'open' => $parser_open,
'close' => $parser_close, 'close' => $parser_close,
'function' => $callback, 'function' => $callback,
); );
return $this; return $this;
} }
@ -449,7 +507,8 @@ class Fenom {
* @param array $funcs * @param array $funcs
* @return Fenom * @return Fenom
*/ */
public function addAllowedFunctions(array $funcs) { public function addAllowedFunctions(array $funcs)
{
$this->_allowed_funcs = $this->_allowed_funcs + array_flip($funcs); $this->_allowed_funcs = $this->_allowed_funcs + array_flip($funcs);
return $this; return $this;
} }
@ -457,40 +516,75 @@ class Fenom {
/** /**
* Return modifier function * Return modifier function
* *
* @param $modifier * @param string $modifier
* @param Fenom\Template $template
* @return mixed * @return mixed
* @throws \Exception
*/ */
public function getModifier($modifier) { public function getModifier($modifier, Template $template = null)
if(isset($this->_modifiers[$modifier])) { {
if (isset($this->_modifiers[$modifier])) {
return $this->_modifiers[$modifier]; return $this->_modifiers[$modifier];
} elseif($this->isAllowedFunction($modifier)) { } elseif ($this->isAllowedFunction($modifier)) {
return $modifier; return $modifier;
} else { } else {
throw new \Exception("Modifier $modifier not found"); return $this->_loadModifier($modifier, $template);
} }
} }
/** /**
* Return function * @param string $modifier
* * @param Fenom\Template $template
* @return bool
*/
protected function _loadModifier($modifier, $template)
{
return false;
}
/**
* @param string $function * @param string $function
* @param Fenom\Template $template
* @return bool|string
* @deprecated
*/
public function getFunction($function, Template $template = null)
{
return $this->getTag($function, $template);
}
/**
* Returns tag info
*
* @param string $tag
* @param Fenom\Template $template
* @return string|bool * @return string|bool
*/ */
public function getFunction($function) { public function getTag($tag, Template $template = null)
if(isset($this->_actions[$function])) { {
return $this->_actions[$function]; if (isset($this->_actions[$tag])) {
return $this->_actions[$tag];
} else { } else {
return false; return $this->_loadTag($tag, $template);
} }
} }
/**
* @param $tag
* @param Fenom\Template $template
* @return bool
*/
protected function _loadTag($tag, $template)
{
return false;
}
/** /**
* @param string $function * @param string $function
* @return bool * @return bool
*/ */
public function isAllowedFunction($function) { public function isAllowedFunction($function)
if($this->_options & self::DENY_INLINE_FUNCS) { {
if ($this->_options & self::DENY_NATIVE_FUNCS) {
return isset($this->_allowed_funcs[$function]); return isset($this->_allowed_funcs[$function]);
} else { } else {
return is_callable($function); return is_callable($function);
@ -501,10 +595,11 @@ class Fenom {
* @param string $tag * @param string $tag
* @return array * @return array
*/ */
public function getTagOwners($tag) { public function getTagOwners($tag)
{
$tags = array(); $tags = array();
foreach($this->_actions as $owner => $params) { foreach ($this->_actions as $owner => $params) {
if(isset($params["tags"][$tag])) { if (isset($params["tags"][$tag])) {
$tags[] = $owner; $tags[] = $owner;
} }
} }
@ -517,21 +612,18 @@ class Fenom {
* @param string $scm scheme name * @param string $scm scheme name
* @param Fenom\ProviderInterface $provider provider object * @param Fenom\ProviderInterface $provider provider object
*/ */
public function addProvider($scm, \Fenom\ProviderInterface $provider) { public function addProvider($scm, \Fenom\ProviderInterface $provider)
{
$this->_providers[$scm] = $provider; $this->_providers[$scm] = $provider;
} }
/** /**
* Set options. May be bitwise mask of constants DENY_METHODS, DENY_INLINE_FUNCS, DENY_SET_VARS, INCLUDE_SOURCES, * Set options
* FORCE_COMPILE, CHECK_MTIME, or associative array with boolean values:
* disable_methods - disable all calls method in template
* disable_native_funcs - disable all native PHP functions in template
* force_compile - recompile template every time (very slow!)
* compile_check - check template modifications (slow!)
* @param int|array $options * @param int|array $options
*/ */
public function setOptions($options) { public function setOptions($options)
if(is_array($options)) { {
if (is_array($options)) {
$options = self::_makeMask($options, self::$_options_list, $this->_options); $options = self::_makeMask($options, self::$_options_list, $this->_options);
} }
$this->_storage = array(); $this->_storage = array();
@ -542,7 +634,8 @@ class Fenom {
* Get options as bits * Get options as bits
* @return int * @return int
*/ */
public function getOptions() { public function getOptions()
{
return $this->_options; return $this->_options;
} }
@ -551,9 +644,10 @@ class Fenom {
* @return Fenom\ProviderInterface * @return Fenom\ProviderInterface
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function getProvider($scm = false) { public function getProvider($scm = false)
if($scm) { {
if(isset($this->_providers[$scm])) { if ($scm) {
if (isset($this->_providers[$scm])) {
return $this->_providers[$scm]; return $this->_providers[$scm];
} else { } else {
throw new InvalidArgumentException("Provider for '$scm' not found"); throw new InvalidArgumentException("Provider for '$scm' not found");
@ -568,8 +662,9 @@ class Fenom {
* *
* @return Fenom\Template * @return Fenom\Template
*/ */
public function getRawTemplate() { public function getRawTemplate()
return new \Fenom\Template($this, $this->_options); {
return new Template($this, $this->_options);
} }
/** /**
@ -579,7 +674,8 @@ class Fenom {
* @param array $vars array of data for template * @param array $vars array of data for template
* @return Fenom\Render * @return Fenom\Render
*/ */
public function display($template, array $vars = array()) { public function display($template, array $vars = array())
{
return $this->getTemplate($template)->display($vars); return $this->getTemplate($template)->display($vars);
} }
@ -589,7 +685,8 @@ class Fenom {
* @param array $vars array of data for template * @param array $vars array of data for template
* @return mixed * @return mixed
*/ */
public function fetch($template, array $vars = array()) { public function fetch($template, array $vars = array())
{
return $this->getTemplate($template)->fetch($vars); return $this->getTemplate($template)->fetch($vars);
} }
@ -602,7 +699,8 @@ class Fenom {
* @param float $chunk * @param float $chunk
* @return array * @return array
*/ */
public function pipe($template, $callback, array $vars = array(), $chunk = 1e6) { public function pipe($template, $callback, array $vars = array(), $chunk = 1e6)
{
ob_start($callback, $chunk, true); ob_start($callback, $chunk, true);
$data = $this->getTemplate($template)->display($vars); $data = $this->getTemplate($template)->display($vars);
ob_end_flush(); ob_end_flush();
@ -616,21 +714,22 @@ class Fenom {
* @param int $options additional options and flags * @param int $options additional options and flags
* @return Fenom\Template * @return Fenom\Template
*/ */
public function getTemplate($template, $options = 0) { public function getTemplate($template, $options = 0)
{
$options |= $this->_options; $options |= $this->_options;
$key = dechex($options)."@".$template; $key = dechex($options) . "@" . $template;
if(isset($this->_storage[ $key ])) { if (isset($this->_storage[$key])) {
/** @var Fenom\Template $tpl */ /** @var Fenom\Template $tpl */
$tpl = $this->_storage[ $key ]; $tpl = $this->_storage[$key];
if(($this->_options & self::AUTO_RELOAD) && !$tpl->isValid()) { if (($this->_options & self::AUTO_RELOAD) && !$tpl->isValid()) {
return $this->_storage[ $key ] = $this->compile($template, true, $options); return $this->_storage[$key] = $this->compile($template, true, $options);
} else { } else {
return $tpl; return $tpl;
} }
} elseif($this->_options & self::FORCE_COMPILE) { } elseif ($this->_options & self::FORCE_COMPILE) {
return $this->compile($template, $this->_options & self::DISABLE_CACHE & ~self::FORCE_COMPILE, $options); return $this->compile($template, $this->_options & self::DISABLE_CACHE & ~self::FORCE_COMPILE, $options);
} else { } else {
return $this->_storage[ $key ] = $this->_load($template, $options); return $this->_storage[$key] = $this->_load($template, $options);
} }
} }
@ -639,9 +738,10 @@ class Fenom {
* @param string $template * @param string $template
* @return bool * @return bool
*/ */
public function templateExists($template) { public function templateExists($template)
if($provider = strstr($template, ":", true)) { {
if(isset($this->_providers[$provider])) { if ($provider = strstr($template, ":", true)) {
if (isset($this->_providers[$provider])) {
return $this->_providers[$provider]->templateExists(substr($template, strlen($provider) + 1)); return $this->_providers[$provider]->templateExists(substr($template, strlen($provider) + 1));
} }
} else { } else {
@ -657,13 +757,14 @@ class Fenom {
* @param int $opts * @param int $opts
* @return Fenom\Render * @return Fenom\Render
*/ */
protected function _load($tpl, $opts) { protected function _load($tpl, $opts)
{
$file_name = $this->_getCacheName($tpl, $opts); $file_name = $this->_getCacheName($tpl, $opts);
if(!is_file($this->_compile_dir."/".$file_name)) { if (!is_file($this->_compile_dir . "/" . $file_name)) {
return $this->compile($tpl, true, $opts); return $this->compile($tpl, true, $opts);
} else { } else {
$fenom = $this; $fenom = $this;
return include($this->_compile_dir."/".$file_name); return include($this->_compile_dir . "/" . $file_name);
} }
} }
@ -674,8 +775,9 @@ class Fenom {
* @param int $options * @param int $options
* @return string * @return string
*/ */
private function _getCacheName($tpl, $options) { private function _getCacheName($tpl, $options)
$hash = $tpl.":".$options; {
$hash = $tpl . ":" . $options;
return sprintf("%s.%x.%x.php", str_replace(":", "_", basename($tpl)), crc32($hash), strlen($hash)); return sprintf("%s.%x.%x.php", str_replace(":", "_", basename($tpl)), crc32($hash), strlen($hash));
} }
@ -688,20 +790,21 @@ class Fenom {
* @throws RuntimeException * @throws RuntimeException
* @return \Fenom\Template * @return \Fenom\Template
*/ */
public function compile($tpl, $store = true, $options = 0) { public function compile($tpl, $store = true, $options = 0)
{
$options = $this->_options | $options; $options = $this->_options | $options;
$template = Template::factory($this, $options)->load($tpl); $template = $this->getRawTemplate()->load($tpl);
if($store) { if ($store) {
$cache = $this->_getCacheName($tpl, $options); $cache = $this->_getCacheName($tpl, $options);
$tpl_tmp = tempnam($this->_compile_dir, $cache); $tpl_tmp = tempnam($this->_compile_dir, $cache);
$tpl_fp = fopen($tpl_tmp, "w"); $tpl_fp = fopen($tpl_tmp, "w");
if(!$tpl_fp) { if (!$tpl_fp) {
throw new \RuntimeException("Can't to open temporary file $tpl_tmp. Directory ".$this->_compile_dir." is writable?"); throw new \RuntimeException("Can't to open temporary file $tpl_tmp. Directory " . $this->_compile_dir . " is writable?");
} }
fwrite($tpl_fp, $template->getTemplateCode()); fwrite($tpl_fp, $template->getTemplateCode());
fclose($tpl_fp); fclose($tpl_fp);
$file_name = $this->_compile_dir."/".$cache; $file_name = $this->_compile_dir . "/" . $cache;
if(!rename($tpl_tmp, $file_name)) { if (!rename($tpl_tmp, $file_name)) {
throw new \RuntimeException("Can't to move $tpl_tmp to $tpl"); throw new \RuntimeException("Can't to move $tpl_tmp to $tpl");
} }
} }
@ -711,14 +814,16 @@ class Fenom {
/** /**
* Flush internal memory template cache * Flush internal memory template cache
*/ */
public function flush() { public function flush()
{
$this->_storage = array(); $this->_storage = array();
} }
/** /**
* Remove all compiled templates * Remove all compiled templates
*/ */
public function clearAllCompiles() { public function clearAllCompiles()
{
\Fenom\Provider::clean($this->_compile_dir); \Fenom\Provider::clean($this->_compile_dir);
} }
@ -729,8 +834,9 @@ class Fenom {
* @param string $name * @param string $name
* @return Fenom\Template * @return Fenom\Template
*/ */
public function compileCode($code, $name = 'Runtime compile') { public function compileCode($code, $name = 'Runtime compile')
return Template::factory($this, $this->_options)->source($name, $code); {
return $this->getRawTemplate()->source($name, $code);
} }
@ -743,7 +849,8 @@ class Fenom {
* @return int result, ( $mask | a ) & ~b * @return int result, ( $mask | a ) & ~b
* @throws \RuntimeException if key from custom assoc doesn't exists into possible values * @throws \RuntimeException if key from custom assoc doesn't exists into possible values
*/ */
private static function _makeMask(array $values, array $options, $mask = 0) { private static function _makeMask(array $values, array $options, $mask = 0)
{
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
if (isset($options[$key])) { if (isset($options[$key])) {
if ($options[$key]) { if ($options[$key]) {

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,18 @@
<?php
/*
* This file is part of Fenom.
*
* (c) 2013 Ivan Shalganov
*
* For the full copyright and license information, please view the license.md
* file that was distributed with this source code.
*/
namespace Fenom\Error;
/**
* @package Fenom\Error
*/
class CompileException extends \ErrorException
{
}

View File

@ -0,0 +1,18 @@
<?php
/*
* This file is part of Fenom.
*
* (c) 2013 Ivan Shalganov
*
* For the full copyright and license information, please view the license.md
* file that was distributed with this source code.
*/
namespace Fenom\Error;
/**
* @package Fenom\Error
*/
class InvalidUsageException extends \LogicException
{
}

View File

@ -0,0 +1,18 @@
<?php
/*
* This file is part of Fenom.
*
* (c) 2013 Ivan Shalganov
*
* For the full copyright and license information, please view the license.md
* file that was distributed with this source code.
*/
namespace Fenom\Error;
/**
* @package Fenom\Error
*/
class SecurityException extends CompileException
{
}

View File

@ -0,0 +1,18 @@
<?php
/*
* This file is part of Fenom.
*
* (c) 2013 Ivan Shalganov
*
* For the full copyright and license information, please view the license.md
* file that was distributed with this source code.
*/
namespace Fenom\Error;
/**
* @package Fenom\Error
*/
class TokenizeException extends \RuntimeException
{
}

View File

@ -0,0 +1,38 @@
<?php
/*
* This file is part of Fenom.
*
* (c) 2013 Ivan Shalganov
*
* For the full copyright and license information, please view the license.md
* file that was distributed with this source code.
*/
namespace Fenom\Error;
use Fenom\Tokenizer;
/**
* Unexpected token
*/
class UnexpectedTokenException extends \RuntimeException
{
public function __construct(Tokenizer $tokens, $expect = null, $where = null)
{
if ($expect && count($expect) == 1 && is_string($expect[0])) {
$expect = ", expect '" . $expect[0] . "'";
} else {
$expect = "";
}
if (!$tokens->curr) {
$this->message = "Unexpected end of " . ($where ? : "expression") . "$expect";
} elseif ($tokens->curr[0] === T_WHITESPACE) {
$this->message = "Unexpected whitespace$expect";
} elseif ($tokens->curr[0] === T_BAD_CHARACTER) {
$this->message = "Unexpected bad characters (below ASCII 32 except \\t, \\n and \\r) in " . ($where ? : "expression") . "$expect";
} else {
$this->message = "Unexpected token '" . $tokens->current() . "' in " . ($where ? : "expression") . "$expect";
}
}
}
;

View File

@ -13,7 +13,8 @@ namespace Fenom;
* Collection of modifiers * Collection of modifiers
* @author Ivan Shalganov <a.cobest@gmail.com> * @author Ivan Shalganov <a.cobest@gmail.com>
*/ */
class Modifier { class Modifier
{
/** /**
* Date format * Date format
@ -22,10 +23,11 @@ class Modifier {
* @param string $format * @param string $format
* @return string * @return string
*/ */
public static function dateFormat($date, $format = "%b %e, %Y") { public static function dateFormat($date, $format = "%b %e, %Y")
if(is_string($date) && !is_numeric($date)) { {
if (is_string($date) && !is_numeric($date)) {
$date = strtotime($date); $date = strtotime($date);
if(!$date) $date = time(); if (!$date) $date = time();
} }
return strftime($format, $date); return strftime($format, $date);
} }
@ -35,10 +37,11 @@ class Modifier {
* @param string $format * @param string $format
* @return string * @return string
*/ */
public static function date($date, $format = "Y m d") { public static function date($date, $format = "Y m d")
if(is_string($date) && !is_numeric($date)) { {
if (is_string($date) && !is_numeric($date)) {
$date = strtotime($date); $date = strtotime($date);
if(!$date) $date = time(); if (!$date) $date = time();
} }
return date($format, $date); return date($format, $date);
} }
@ -50,8 +53,9 @@ class Modifier {
* @param string $type * @param string $type
* @return string * @return string
*/ */
public static function escape($text, $type = 'html') { public static function escape($text, $type = 'html')
switch(strtolower($type)) { {
switch (strtolower($type)) {
case "url": case "url":
return urlencode($text); return urlencode($text);
case "html"; case "html";
@ -68,8 +72,9 @@ class Modifier {
* @param string $type * @param string $type
* @return string * @return string
*/ */
public static function unescape($text, $type = 'html') { public static function unescape($text, $type = 'html')
switch(strtolower($type)) { {
switch (strtolower($type)) {
case "url": case "url":
return urldecode($text); return urldecode($text);
case "html"; case "html";
@ -89,23 +94,24 @@ class Modifier {
* @param bool $middle * @param bool $middle
* @return string * @return string
*/ */
public static function truncate($string, $length = 80, $etc = '...', $by_words = false, $middle = false) { public static function truncate($string, $length = 80, $etc = '...', $by_words = false, $middle = false)
if($middle) { {
if(preg_match('#^(.{'.$length.'}).*?(.{'.$length.'})?$#usS', $string, $match)) { if ($middle) {
if(count($match) == 3) { if (preg_match('#^(.{' . $length . '}).*?(.{' . $length . '})?$#usS', $string, $match)) {
if($by_words) { if (count($match) == 3) {
return preg_replace('#\s.*$#usS', "", $match[1]).$etc.preg_replace('#^.*\s#usS', "", $match[2]); if ($by_words) {
return preg_replace('#\s.*$#usS', "", $match[1]) . $etc . preg_replace('#^.*\s#usS', "", $match[2]);
} else { } else {
return $match[1].$etc.$match[2]; return $match[1] . $etc . $match[2];
} }
} }
} }
} else { } else {
if(preg_match('#^(.{'.$length.'})#usS', $string, $match)) { if (preg_match('#^(.{' . $length . '})#usS', $string, $match)) {
if($by_words) { if ($by_words) {
return preg_replace('#\s.*$#usS', "", $match[1]).$etc; return preg_replace('#\s.*$#usS', "", $match[1]) . $etc;
} else { } else {
return $match[1].$etc; return $match[1] . $etc;
} }
} }
} }
@ -119,9 +125,10 @@ class Modifier {
* @param bool $to_line strip line ends * @param bool $to_line strip line ends
* @return string * @return string
*/ */
public static function strip($str, $to_line = false) { public static function strip($str, $to_line = false)
{
$str = trim($str); $str = trim($str);
if($to_line) { if ($to_line) {
return preg_replace('#[\s]+#ms', ' ', $str); return preg_replace('#[\s]+#ms', ' ', $str);
} else { } else {
return preg_replace('#[ \t]{2,}#', ' ', $str); return preg_replace('#[ \t]{2,}#', ' ', $str);
@ -133,12 +140,13 @@ class Modifier {
* @param mixed $item * @param mixed $item
* @return int * @return int
*/ */
public static function length($item) { public static function length($item)
if(is_string($item)) { {
if (is_string($item)) {
return strlen(preg_replace('#[\x00-\x7F]|[\x80-\xDF][\x00-\xBF]|[\xE0-\xEF][\x00-\xBF]{2}#s', ' ', $item)); return strlen(preg_replace('#[\x00-\x7F]|[\x80-\xDF][\x00-\xBF]|[\xE0-\xEF][\x00-\xBF]{2}#s', ' ', $item));
} elseif (is_array($item)) { } elseif (is_array($item)) {
return count($item); return count($item);
} elseif($item instanceof \Countable) { } elseif ($item instanceof \Countable) {
return count($item); return count($item);
} else { } else {
return 0; return 0;
@ -151,10 +159,11 @@ class Modifier {
* @param mixed $haystack * @param mixed $haystack
* @return bool * @return bool
*/ */
public static function in($value, $haystack) { public static function in($value, $haystack)
if(is_array($haystack)) { {
if (is_array($haystack)) {
return in_array($value, $haystack) || array_key_exists($value, $haystack); return in_array($value, $haystack) || array_key_exists($value, $haystack);
} elseif(is_string($haystack)) { } elseif (is_string($haystack)) {
return strpos($haystack, $value) !== false; return strpos($haystack, $value) !== false;
} }
return false; return false;
@ -164,7 +173,8 @@ class Modifier {
* @param $value * @param $value
* @return bool * @return bool
*/ */
public static function isIterable($value) { public static function isIterable($value)
{
return is_array($value) || ($value instanceof \Iterator); return is_array($value) || ($value instanceof \Iterator);
} }
} }

View File

@ -10,11 +10,13 @@
namespace Fenom; namespace Fenom;
use Fenom\ProviderInterface; use Fenom\ProviderInterface;
/** /**
* Base template provider * Base template provider
* @author Ivan Shalganov * @author Ivan Shalganov
*/ */
class Provider implements ProviderInterface { class Provider implements ProviderInterface
{
private $_path; private $_path;
/** /**
@ -22,10 +24,11 @@ class Provider implements ProviderInterface {
* *
* @param string $path * @param string $path
*/ */
public static function clean($path) { public static function clean($path)
if(is_file($path)) { {
if (is_file($path)) {
unlink($path); unlink($path);
} elseif(is_dir($path)) { } elseif (is_dir($path)) {
$iterator = iterator_to_array( $iterator = iterator_to_array(
new \RecursiveIteratorIterator( new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, new \RecursiveDirectoryIterator($path,
@ -33,13 +36,13 @@ class Provider implements ProviderInterface {
\RecursiveIteratorIterator::CHILD_FIRST \RecursiveIteratorIterator::CHILD_FIRST
) )
); );
foreach($iterator as $file) { foreach ($iterator as $file) {
/* @var \splFileInfo $file*/ /* @var \splFileInfo $file */
if($file->isFile()) { if ($file->isFile()) {
if(strpos($file->getBasename(), ".") !== 0) { if (strpos($file->getBasename(), ".") !== 0) {
unlink($file->getRealPath()); unlink($file->getRealPath());
} }
} elseif($file->isDir()) { } elseif ($file->isDir()) {
rmdir($file->getRealPath()); rmdir($file->getRealPath());
} }
} }
@ -51,9 +54,10 @@ class Provider implements ProviderInterface {
* *
* @param string $path * @param string $path
*/ */
public static function rm($path) { public static function rm($path)
{
self::clean($path); self::clean($path);
if(is_dir($path)) { if (is_dir($path)) {
rmdir($path); rmdir($path);
} }
} }
@ -62,8 +66,9 @@ class Provider implements ProviderInterface {
* @param string $template_dir directory of templates * @param string $template_dir directory of templates
* @throws \LogicException if directory doesn't exists * @throws \LogicException if directory doesn't exists
*/ */
public function __construct($template_dir) { public function __construct($template_dir)
if($_dir = realpath($template_dir)) { {
if ($_dir = realpath($template_dir)) {
$this->_path = $_dir; $this->_path = $_dir;
} else { } else {
throw new \LogicException("Template directory {$template_dir} doesn't exists"); throw new \LogicException("Template directory {$template_dir} doesn't exists");
@ -76,7 +81,8 @@ class Provider implements ProviderInterface {
* @param int $time load last modified time * @param int $time load last modified time
* @return string * @return string
*/ */
public function getSource($tpl, &$time) { public function getSource($tpl, &$time)
{
$tpl = $this->_getTemplatePath($tpl); $tpl = $this->_getTemplatePath($tpl);
clearstatcache(null, $tpl); clearstatcache(null, $tpl);
$time = filemtime($tpl); $time = filemtime($tpl);
@ -88,7 +94,8 @@ class Provider implements ProviderInterface {
* @param string $tpl * @param string $tpl
* @return int * @return int
*/ */
public function getLastModified($tpl) { public function getLastModified($tpl)
{
clearstatcache(null, $tpl = $this->_getTemplatePath($tpl)); clearstatcache(null, $tpl = $this->_getTemplatePath($tpl));
return filemtime($tpl); return filemtime($tpl);
} }
@ -99,7 +106,8 @@ class Provider implements ProviderInterface {
* @param string $extension all templates must have this extension, default .tpl * @param string $extension all templates must have this extension, default .tpl
* @return array|\Iterator * @return array|\Iterator
*/ */
public function getList($extension = "tpl") { public function getList($extension = "tpl")
{
$list = array(); $list = array();
$iterator = new \RecursiveIteratorIterator( $iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->_path, new \RecursiveDirectoryIterator($this->_path,
@ -107,9 +115,9 @@ class Provider implements ProviderInterface {
\RecursiveIteratorIterator::CHILD_FIRST \RecursiveIteratorIterator::CHILD_FIRST
); );
$path_len = strlen($this->_path); $path_len = strlen($this->_path);
foreach($iterator as $file) { foreach ($iterator as $file) {
/* @var \SplFileInfo $file */ /* @var \SplFileInfo $file */
if($file->isFile() && $file->getExtension() == $extension) { if ($file->isFile() && $file->getExtension() == $extension) {
$list[] = substr($file->getPathname(), $path_len + 1); $list[] = substr($file->getPathname(), $path_len + 1);
} }
} }
@ -122,8 +130,9 @@ class Provider implements ProviderInterface {
* @return string * @return string
* @throws \RuntimeException * @throws \RuntimeException
*/ */
protected function _getTemplatePath($tpl) { protected function _getTemplatePath($tpl)
if(($path = realpath($this->_path."/".$tpl)) && strpos($path, $this->_path) === 0) { {
if (($path = realpath($this->_path . "/" . $tpl)) && strpos($path, $this->_path) === 0) {
return $path; return $path;
} else { } else {
throw new \RuntimeException("Template $tpl not found"); throw new \RuntimeException("Template $tpl not found");
@ -134,8 +143,9 @@ class Provider implements ProviderInterface {
* @param string $tpl * @param string $tpl
* @return bool * @return bool
*/ */
public function templateExists($tpl) { public function templateExists($tpl)
return file_exists($this->_path."/".$tpl); {
return file_exists($this->_path . "/" . $tpl);
} }
/** /**
@ -144,10 +154,11 @@ class Provider implements ProviderInterface {
* @param array $templates [template_name => modified, ...] By conversation, you may trust the template's name * @param array $templates [template_name => modified, ...] By conversation, you may trust the template's name
* @return bool * @return bool
*/ */
public function verify(array $templates) { public function verify(array $templates)
foreach($templates as $template => $mtime) { {
clearstatcache(null, $template = $this->_path.'/'.$template); foreach ($templates as $template => $mtime) {
if(@filemtime($template) !== $mtime) { clearstatcache(null, $template = $this->_path . '/' . $template);
if (@filemtime($template) !== $mtime) {
return false; return false;
} }

View File

@ -14,7 +14,8 @@ namespace Fenom;
* @package Fenom * @package Fenom
* @author Ivan Shalganov <a.cobest@gmail.com> * @author Ivan Shalganov <a.cobest@gmail.com>
*/ */
interface ProviderInterface { interface ProviderInterface
{
/** /**
* @param string $tpl * @param string $tpl
* @return bool * @return bool

View File

@ -14,13 +14,15 @@ use Fenom;
* Primitive template * Primitive template
* @author Ivan Shalganov <a.cobest@gmail.com> * @author Ivan Shalganov <a.cobest@gmail.com>
*/ */
class Render extends \ArrayObject { class Render extends \ArrayObject
{
private static $_props = array( private static $_props = array(
"name" => "runtime", "name" => "runtime",
"base_name" => "", "base_name" => "",
"scm" => false, "scm" => false,
"time" => 0, "time" => 0,
"depends" => array() "depends" => array(),
"macros" => array()
); );
/** /**
* @var \Closure * @var \Closure
@ -72,49 +74,77 @@ class Render extends \ArrayObject {
* @param callable $code template body * @param callable $code template body
* @param array $props * @param array $props
*/ */
public function __construct(Fenom $fenom, \Closure $code, $props = array()) { public function __construct(Fenom $fenom, \Closure $code, array $props = array())
{
$this->_fenom = $fenom; $this->_fenom = $fenom;
$props += self::$_props; $props += self::$_props;
$this->_name = $props["name"]; $this->_name = $props["name"];
// $this->_provider = $this->_fenom->getProvider($props["scm"]);
$this->_scm = $props["scm"]; $this->_scm = $props["scm"];
$this->_time = $props["time"]; $this->_time = $props["time"];
$this->_depends = $props["depends"]; $this->_depends = $props["depends"];
$this->_macros = $props["macros"];
$this->_code = $code; $this->_code = $code;
} }
/** /**
* Get template storage * Get template storage
* @return Fenom * @return \Fenom
*/ */
public function getStorage() { public function getStorage()
{
return $this->_fenom; return $this->_fenom;
} }
public function getDepends() { /**
* Get depends list
* @return array
*/
public function getDepends()
{
return $this->_depends; return $this->_depends;
} }
public function getScm() { /**
* Get schema name
* @return string
*/
public function getScm()
{
return $this->_scm; return $this->_scm;
} }
public function getProvider() { /**
* Get provider of template source
* @return ProviderInterface
*/
public function getProvider()
{
return $this->_fenom->getProvider($this->_scm); return $this->_fenom->getProvider($this->_scm);
} }
public function getBaseName() { /**
* Get name without schema
* @return string
*/
public function getBaseName()
{
return $this->_base_name; return $this->_base_name;
} }
public function getOptions() { /**
* Get parse options
* @return int
*/
public function getOptions()
{
return $this->_options; return $this->_options;
} }
/** /**
* @return string * @return string
*/ */
public function __toString() { public function __toString()
{
return $this->_name; return $this->_name;
} }
@ -122,11 +152,13 @@ class Render extends \ArrayObject {
* Get template name * Get template name
* @return string * @return string
*/ */
public function getName() { public function getName()
{
return $this->_name; return $this->_name;
} }
public function getTime() { public function getTime()
{
return $this->_time; return $this->_time;
} }
@ -135,16 +167,17 @@ class Render extends \ArrayObject {
* Validate template * Validate template
* @return bool * @return bool
*/ */
public function isValid() { public function isValid()
if(count($this->_depends) === 1) { // if no external dependencies, only self {
if (count($this->_depends) === 1) { // if no external dependencies, only self
$provider = $this->_fenom->getProvider($this->_scm); $provider = $this->_fenom->getProvider($this->_scm);
if($provider->getLastModified($this->_name) !== $this->_time) { if ($provider->getLastModified($this->_name) !== $this->_time) {
return false; return false;
} }
} else { } else {
foreach($this->_depends as $scm => $templates) { foreach ($this->_depends as $scm => $templates) {
$provider = $this->_fenom->getProvider($scm); $provider = $this->_fenom->getProvider($scm);
if(!$provider->verify($templates)) { if (!$provider->verify($templates)) {
return false; return false;
} }
} }
@ -152,12 +185,22 @@ class Render extends \ArrayObject {
return true; return true;
} }
/**
* Get internal macro
* @param $name
* @return mixed
*/
public function getMacro($name) {
return $this->_macros[$name];
}
/** /**
* Execute template and write into output * Execute template and write into output
* @param array $values for template * @param array $values for template
* @return Render * @return Render
*/ */
public function display(array $values) { public function display(array $values)
{
$this->exchangeArray($values); $this->exchangeArray($values);
$this->_code->__invoke($this); $this->_code->__invoke($this);
return $this->exchangeArray(array()); return $this->exchangeArray(array());
@ -169,7 +212,8 @@ class Render extends \ArrayObject {
* @return string * @return string
* @throws \Exception * @throws \Exception
*/ */
public function fetch(array $values) { public function fetch(array $values)
{
ob_start(); ob_start();
try { try {
$this->display($values); $this->display($values);
@ -186,7 +230,8 @@ class Render extends \ArrayObject {
* @param $args * @param $args
* @throws \BadMethodCallException * @throws \BadMethodCallException
*/ */
public function __call($method, $args) { public function __call($method, $args)
throw new \BadMethodCallException("Unknown method ".$method); {
throw new \BadMethodCallException("Unknown method " . $method);
} }
} }

View File

@ -14,7 +14,8 @@ namespace Fenom;
* *
* @author Ivan Shalganov <a.cobest@gmail.com> * @author Ivan Shalganov <a.cobest@gmail.com>
*/ */
class Scope extends \ArrayObject { class Scope extends \ArrayObject
{
public $line = 0; public $line = 0;
public $name; public $name;
@ -33,20 +34,21 @@ class Scope extends \ArrayObject {
/** /**
* Creating cope * Creating cope
* *
* @param string $name * @param string $name
* @param Template $tpl * @param Template $tpl
* @param int $line * @param int $line
* @param array $action * @param array $action
* @param int $level * @param int $level
* @param $body * @param $body
*/ */
public function __construct($name, $tpl, $line, $action, $level, &$body) { public function __construct($name, $tpl, $line, $action, $level, &$body)
{
$this->line = $line; $this->line = $line;
$this->name = $name; $this->name = $name;
$this->tpl = $tpl; $this->tpl = $tpl;
$this->_action = $action; $this->_action = $action;
$this->level = $level; $this->level = $level;
$this->_body = &$body; $this->_body = & $body;
$this->_offset = strlen($body); $this->_offset = strlen($body);
} }
@ -54,7 +56,8 @@ class Scope extends \ArrayObject {
* *
* @param string $function * @param string $function
*/ */
public function setFuncName($function) { public function setFuncName($function)
{
$this["function"] = $function; $this["function"] = $function;
$this->is_compiler = false; $this->is_compiler = false;
$this->escape = $this->tpl->escape; $this->escape = $this->tpl->escape;
@ -66,7 +69,8 @@ class Scope extends \ArrayObject {
* @param Tokenizer $tokenizer * @param Tokenizer $tokenizer
* @return mixed * @return mixed
*/ */
public function open($tokenizer) { public function open($tokenizer)
{
return call_user_func($this->_action["open"], $tokenizer, $this); return call_user_func($this->_action["open"], $tokenizer, $this);
} }
@ -77,9 +81,10 @@ class Scope extends \ArrayObject {
* @param int $level * @param int $level
* @return bool * @return bool
*/ */
public function hasTag($tag, $level) { public function hasTag($tag, $level)
if(isset($this->_action["tags"][$tag])) { {
if($level) { if (isset($this->_action["tags"][$tag])) {
if ($level) {
return isset($this->_action["float_tags"][$tag]); return isset($this->_action["float_tags"][$tag]);
} else { } else {
return true; return true;
@ -95,7 +100,8 @@ class Scope extends \ArrayObject {
* @param Tokenizer $tokenizer * @param Tokenizer $tokenizer
* @return string * @return string
*/ */
public function tag($tag, $tokenizer) { public function tag($tag, $tokenizer)
{
return call_user_func($this->_action["tags"][$tag], $tokenizer, $this); return call_user_func($this->_action["tags"][$tag], $tokenizer, $this);
} }
@ -105,7 +111,8 @@ class Scope extends \ArrayObject {
* @param Tokenizer $tokenizer * @param Tokenizer $tokenizer
* @return string * @return string
*/ */
public function close($tokenizer) { public function close($tokenizer)
{
return call_user_func($this->_action["close"], $tokenizer, $this); return call_user_func($this->_action["close"], $tokenizer, $this);
} }
@ -115,7 +122,8 @@ class Scope extends \ArrayObject {
* @throws \LogicException * @throws \LogicException
* @return string * @return string
*/ */
public function getContent() { public function getContent()
{
return substr($this->_body, $this->_offset); return substr($this->_body, $this->_offset);
} }
@ -125,7 +133,8 @@ class Scope extends \ArrayObject {
* @return string * @return string
* @throws \LogicException * @throws \LogicException
*/ */
public function cutContent() { 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); $this->_body = substr($this->_body, 0, $this->_offset);
return $content; return $content;
@ -136,12 +145,14 @@ class Scope extends \ArrayObject {
* *
* @param $new_content * @param $new_content
*/ */
public function replaceContent($new_content) { public function replaceContent($new_content)
{
$this->cutContent(); $this->cutContent();
$this->_body .= $new_content; $this->_body .= $new_content;
} }
public function unEscapeContent() { public function unEscapeContent()
{
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -9,16 +9,18 @@
*/ */
namespace Fenom; namespace Fenom;
use Fenom\Error\UnexpectedTokenException;
/** /**
* for <PHP 5.4 compatible * for PHP <5.4 compatible
*/ */
defined('T_INSTEADOF') || define('T_INSTEADOF', 341); defined('T_INSTEADOF') || define('T_INSTEADOF', 341);
defined('T_TRAIT') || define('T_TRAIT', 355); defined('T_TRAIT') || define('T_TRAIT', 355);
defined('T_TRAIT_C') || define('T_TRAIT_C', 365); defined('T_TRAIT_C') || define('T_TRAIT_C', 365);
/** /**
* for PHP <5.5 * for PHP <5.5 compatible
*/ */
defined('T_YIELD') || define('T_YIELD', 370); defined('T_YIELD') || define('T_YIELD', 390);
/** /**
* Each token have structure * Each token have structure
@ -35,7 +37,8 @@ defined('T_YIELD') || define('T_YIELD', 370);
* @package Fenom * @package Fenom
* @author Ivan Shalganov <a.cobest@gmail.com> * @author Ivan Shalganov <a.cobest@gmail.com>
*/ */
class Tokenizer { class Tokenizer
{
const TOKEN = 0; const TOKEN = 0;
const TEXT = 1; const TEXT = 1;
const WHITESPACE = 2; const WHITESPACE = 2;
@ -90,19 +93,19 @@ class Tokenizer {
*/ */
private static $_macros = array( private static $_macros = array(
self::MACRO_STRING => array( self::MACRO_STRING => array(
\T_ABSTRACT => 1, \T_ARRAY => 1, \T_AS => 1, \T_BREAK => 1, \T_BREAK => 1, \T_CASE => 1, \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_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_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_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_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_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_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_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_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_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_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_VAR => 1, \T_TRAIT => 1, \T_TRAIT_C => 1, \T_TRY => 1, \T_UNSET => 1, \T_VAR => 1,
\T_WHILE => 1, \T_YIELD => 1, \T_USE => 1 \T_WHILE => 1, \T_YIELD => 1, \T_USE => 1
), ),
self::MACRO_INCDEC => array( self::MACRO_INCDEC => array(
\T_INC => 1, \T_DEC => 1 \T_INC => 1, \T_DEC => 1
@ -111,41 +114,41 @@ class Tokenizer {
"!" => 1, "~" => 1, "-" => 1 "!" => 1, "~" => 1, "-" => 1
), ),
self::MACRO_BINARY => array( 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_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_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, \T_LOGICAL_OR => 1, \T_LOGICAL_XOR => 1, \T_SL => 1, \T_SR => 1,
"+" => 1, "-" => 1, "*" => 1, "/" => 1, ">" => 1, "<" => 1, "^" => 1, "%" => 1, "&" => 1 "+" => 1, "-" => 1, "*" => 1, "/" => 1, ">" => 1, "<" => 1, "^" => 1, "%" => 1, "&" => 1
), ),
self::MACRO_BOOLEAN => array( 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( self::MACRO_MATH => array(
"+" => 1, "-" => 1, "*" => 1, "/" => 1, "^" => 1, "%" => 1, "&" => 1, "|" => 1 "+" => 1, "-" => 1, "*" => 1, "/" => 1, "^" => 1, "%" => 1, "&" => 1, "|" => 1
), ),
self::MACRO_COND => array( self::MACRO_COND => array(
\T_IS_EQUAL => 1, \T_IS_IDENTICAL => 1, ">" => 1, "<" => 1, \T_SL => 1, \T_SR => 1, \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, \T_IS_NOT_EQUAL => 1, \T_IS_NOT_IDENTICAL => 1, \T_IS_SMALLER_OR_EQUAL => 1,
), ),
self::MACRO_EQUALS => array( self::MACRO_EQUALS => array(
\T_AND_EQUAL => 1, \T_CONCAT_EQUAL => 1,\T_DIV_EQUAL => 1, \T_MINUS_EQUAL => 1, \T_MOD_EQUAL => 1, \T_AND_EQUAL => 1, \T_CONCAT_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_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_XOR_EQUAL => 1, '=' => 1
), ),
self::MACRO_SCALAR => array( self::MACRO_SCALAR => array(
\T_LNUMBER => 1, \T_DNUMBER => 1, \T_CONSTANT_ENCAPSED_STRING => 1 \T_LNUMBER => 1, \T_DNUMBER => 1, \T_CONSTANT_ENCAPSED_STRING => 1
) )
); );
public static $description = array( public static $description = array(
self::MACRO_STRING => 'string', self::MACRO_STRING => 'string',
self::MACRO_INCDEC => 'increment/decrement operator', self::MACRO_INCDEC => 'increment/decrement operator',
self::MACRO_UNARY => 'unary operator', self::MACRO_UNARY => 'unary operator',
self::MACRO_BINARY => 'binary operator', self::MACRO_BINARY => 'binary operator',
self::MACRO_BOOLEAN => 'boolean operator', self::MACRO_BOOLEAN => 'boolean operator',
self::MACRO_MATH => 'math operator', self::MACRO_MATH => 'math operator',
self::MACRO_COND => 'conditional operator', self::MACRO_COND => 'conditional operator',
self::MACRO_EQUALS => 'equal operator', self::MACRO_EQUALS => 'equal operator',
self::MACRO_SCALAR => 'scalar value' self::MACRO_SCALAR => 'scalar value'
); );
/** /**
@ -159,15 +162,16 @@ class Tokenizer {
/** /**
* @param $query * @param $query
*/ */
public function __construct($query) { public function __construct($query)
{
$tokens = array(-1 => array(\T_WHITESPACE, '', '', 1)); $tokens = array(-1 => array(\T_WHITESPACE, '', '', 1));
$_tokens = token_get_all("<?php ".$query); $_tokens = token_get_all("<?php " . $query);
$line = 1; $line = 1;
array_shift($_tokens); array_shift($_tokens);
$i = 0; $i = 0;
foreach($_tokens as $token) { foreach ($_tokens as $token) {
if(is_string($token)) { if (is_string($token)) {
if($token === '"' || $token === "'" || $token === "`") { if ($token === '"' || $token === "'" || $token === "`") {
$this->quotes++; $this->quotes++;
} }
$tokens[] = array( $tokens[] = array(
@ -178,13 +182,13 @@ class Tokenizer {
); );
$i++; $i++;
} elseif ($token[0] === \T_WHITESPACE) { } elseif ($token[0] === \T_WHITESPACE) {
$tokens[$i-1][2] = $token[1]; $tokens[$i - 1][2] = $token[1];
} else { } else {
$tokens[] = array( $tokens[] = array(
$token[0], $token[0],
$token[1], $token[1],
"", "",
$line = $token[2], $line = $token[2],
token_name($token[0]) // debug token_name($token[0]) // debug
); );
$i++; $i++;
@ -202,7 +206,8 @@ class Tokenizer {
* *
* @return int * @return int
*/ */
public function isIncomplete() { public function isIncomplete()
{
return ($this->quotes % 2) || ($this->tokens[$this->_max][0] === T_ENCAPSED_AND_WHITESPACE); return ($this->quotes % 2) || ($this->tokens[$this->_max][0] === T_ENCAPSED_AND_WHITESPACE);
} }
@ -212,7 +217,8 @@ class Tokenizer {
* @link http://php.net/manual/en/iterator.current.php * @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type. * @return mixed Can return any type.
*/ */
public function current() { public function current()
{
return $this->curr[1]; return $this->curr[1];
} }
@ -222,8 +228,9 @@ class Tokenizer {
* @link http://php.net/manual/en/iterator.next.php * @link http://php.net/manual/en/iterator.next.php
* @return Tokenizer * @return Tokenizer
*/ */
public function next() { public function next()
if($this->p > $this->_max) { {
if ($this->p > $this->_max) {
return $this; return $this;
} }
$this->p++; $this->p++;
@ -238,15 +245,16 @@ class Tokenizer {
* @param string|int $token * @param string|int $token
* @return bool * @return bool
*/ */
private function _valid($expects, $token) { private function _valid($expects, $token)
foreach($expects as $expect) { {
if(is_string($expect) || $expect < 1000) { foreach ($expects as $expect) {
if($expect === $token) { if (is_string($expect) || $expect < 1000) {
if ($expect === $token) {
return true; return true;
} }
} else { } else {
if(isset(self::$_macros[ $expect ][ $token ])) { if (isset(self::$_macros[$expect][$token])) {
return true; return true;
} }
} }
@ -260,13 +268,14 @@ class Tokenizer {
* @throws UnexpectedTokenException * @throws UnexpectedTokenException
* @return mixed * @return mixed
*/ */
public function _next($tokens) { public function _next($tokens)
{
$this->next(); $this->next();
if(!$this->curr) { if (!$this->curr) {
throw new UnexpectedTokenException($this, $tokens); throw new UnexpectedTokenException($this, $tokens);
} }
if($tokens) { if ($tokens) {
if($this->_valid($tokens, $this->key())) { if ($this->_valid($tokens, $this->key())) {
return; return;
} }
} else { } else {
@ -279,7 +288,8 @@ class Tokenizer {
* Fetch next specified token or throw an exception * Fetch next specified token or throw an exception
* @return mixed * @return mixed
*/ */
public function getNext(/*int|string $token1, int|string $token2, ... */) { public function getNext( /*int|string $token1, int|string $token2, ... */)
{
$this->_next(func_get_args()); $this->_next(func_get_args());
return $this->current(); return $this->current();
} }
@ -288,7 +298,8 @@ class Tokenizer {
* @param $token * @param $token
* @return bool * @return bool
*/ */
public function isNextToken($token) { public function isNextToken($token)
{
return $this->next ? $this->next[1] == $token : false; return $this->next ? $this->next[1] == $token : false;
} }
@ -298,15 +309,16 @@ class Tokenizer {
* @param int $limit * @param int $limit
* @return string * @return string
*/ */
public function getSubstr($offset, $limit = 0) { public function getSubstr($offset, $limit = 0)
{
$str = ''; $str = '';
if(!$limit) { if (!$limit) {
$limit = $this->_max; $limit = $this->_max;
} else { } else {
$limit += $offset; $limit += $offset;
} }
for($i = $offset; $i <= $limit; $i++){ for ($i = $offset; $i <= $limit; $i++) {
$str .= $this->tokens[$i][1].$this->tokens[$i][2]; $str .= $this->tokens[$i][1] . $this->tokens[$i][2];
} }
return $str; return $str;
} }
@ -316,8 +328,9 @@ class Tokenizer {
* @return mixed * @return mixed
* @throws UnexpectedTokenException * @throws UnexpectedTokenException
*/ */
public function getAndNext() { public function getAndNext( /* $token1, ... */)
if($this->curr) { {
if ($this->curr) {
$cur = $this->curr[1]; $cur = $this->curr[1];
$this->next(); $this->next();
return $cur; return $cur;
@ -331,7 +344,8 @@ class Tokenizer {
* @param $token1 * @param $token1
* @return bool * @return bool
*/ */
public function isNext($token1/*, ...*/) { public function isNext($token1 /*, ...*/)
{
return $this->next && $this->_valid(func_get_args(), $this->next[0]); return $this->next && $this->_valid(func_get_args(), $this->next[0]);
} }
@ -340,7 +354,8 @@ class Tokenizer {
* @param $token1 * @param $token1
* @return bool * @return bool
*/ */
public function is($token1/*, ...*/) { public function is($token1 /*, ...*/)
{
return $this->curr && $this->_valid(func_get_args(), $this->curr[0]); return $this->curr && $this->_valid(func_get_args(), $this->curr[0]);
} }
@ -349,7 +364,8 @@ class Tokenizer {
* @param $token1 * @param $token1
* @return bool * @return bool
*/ */
public function isPrev($token1/*, ...*/) { public function isPrev($token1 /*, ...*/)
{
return $this->prev && $this->_valid(func_get_args(), $this->prev[0]); return $this->prev && $this->_valid(func_get_args(), $this->prev[0]);
} }
@ -360,8 +376,9 @@ class Tokenizer {
* @throws UnexpectedTokenException * @throws UnexpectedTokenException
* @return mixed * @return mixed
*/ */
public function get($token1 /*, $token2 ...*/) { public function get($token1 /*, $token2 ...*/)
if($this->curr && $this->_valid(func_get_args(), $this->curr[0])) { {
if ($this->curr && $this->_valid(func_get_args(), $this->curr[0])) {
return $this->curr[1]; return $this->curr[1];
} else { } else {
throw new UnexpectedTokenException($this, func_get_args()); throw new UnexpectedTokenException($this, func_get_args());
@ -372,8 +389,9 @@ class Tokenizer {
* Step back * Step back
* @return Tokenizer * @return Tokenizer
*/ */
public function back() { public function back()
if($this->p === 0) { {
if ($this->p === 0) {
return $this; return $this;
} }
$this->p--; $this->p--;
@ -385,12 +403,13 @@ class Tokenizer {
* @param $token1 * @param $token1
* @return bool * @return bool
*/ */
public function hasBackList($token1 /*, $token2 ...*/) { public function hasBackList($token1 /*, $token2 ...*/)
{
$tokens = func_get_args(); $tokens = func_get_args();
$c = $this->p; $c = $this->p;
foreach($tokens as $token) { foreach ($tokens as $token) {
$c--; $c--;
if($c < 0 || $this->tokens[$c][0] !== $token) { if ($c < 0 || $this->tokens[$c][0] !== $token) {
return false; return false;
} }
} }
@ -403,8 +422,9 @@ class Tokenizer {
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function __get($key) { public function __get($key)
switch($key) { {
switch ($key) {
case 'curr': case 'curr':
return $this->curr = ($this->p <= $this->_max) ? $this->tokens[$this->p] : null; return $this->curr = ($this->p <= $this->_max) ? $this->tokens[$this->p] : null;
case 'next': case 'next':
@ -416,7 +436,8 @@ class Tokenizer {
} }
} }
public function count() { public function count()
{
return $this->_max; return $this->_max;
} }
@ -424,7 +445,8 @@ class Tokenizer {
* Return the key of the current element * Return the key of the current element
* @return mixed scalar on success, or null on failure. * @return mixed scalar on success, or null on failure.
*/ */
public function key() { public function key()
{
return $this->curr ? $this->curr[0] : null; return $this->curr ? $this->curr[0] : null;
} }
@ -433,7 +455,8 @@ class Tokenizer {
* @return boolean The return value will be casted to boolean and then evaluated. * @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure. * Returns true on success or false on failure.
*/ */
public function valid() { public function valid()
{
return (bool)$this->curr; return (bool)$this->curr;
} }
@ -443,12 +466,13 @@ class Tokenizer {
* @param int|string $token * @param int|string $token
* @return string * @return string
*/ */
public static function getName($token) { public static function getName($token)
if(is_string($token)) { {
if (is_string($token)) {
return $token; return $token;
} elseif(is_integer($token)) { } elseif (is_integer($token)) {
return token_name($token); return token_name($token);
} elseif(is_array($token)) { } elseif (is_array($token)) {
return token_name($token[0]); return token_name($token[0]);
} else { } else {
return null; return null;
@ -461,9 +485,10 @@ class Tokenizer {
* @throws UnexpectedTokenException * @throws UnexpectedTokenException
* @return Tokenizer * @return Tokenizer
*/ */
public function skip(/*$token1, $token2, ...*/) { public function skip( /*$token1, $token2, ...*/)
if(func_num_args()) { {
if($this->_valid(func_get_args(), $this->curr[0])) { if (func_num_args()) {
if ($this->_valid(func_get_args(), $this->curr[0])) {
$this->next(); $this->next();
return $this; return $this;
} else { } else {
@ -481,8 +506,9 @@ class Tokenizer {
* @param int|string $token1 * @param int|string $token1
* @return Tokenizer * @return Tokenizer
*/ */
public function skipIf($token1/*, $token2, ...*/) { public function skipIf($token1 /*, $token2, ...*/)
if($this->_valid(func_get_args(), $this->curr[0])) { {
if ($this->_valid(func_get_args(), $this->curr[0])) {
$this->next(); $this->next();
} }
return $this; return $this;
@ -495,8 +521,9 @@ class Tokenizer {
* @return Tokenizer * @return Tokenizer
* @throws UnexpectedTokenException * @throws UnexpectedTokenException
*/ */
public function need($token1/*, $token2, ...*/) { public function need($token1 /*, $token2, ...*/)
if($this->_valid(func_get_args(), $this->curr[0])) { {
if ($this->_valid(func_get_args(), $this->curr[0])) {
return $this; return $this;
} else { } else {
throw new UnexpectedTokenException($this, func_get_args()); throw new UnexpectedTokenException($this, func_get_args());
@ -509,37 +536,38 @@ class Tokenizer {
* @param int $after count tokens after current token * @param int $after count tokens after current token
* @return array * @return array
*/ */
public function getSnippet($before = 0, $after = 0) { public function getSnippet($before = 0, $after = 0)
{
$from = 0; $from = 0;
$to = $this->p; $to = $this->p;
if($before > 0) { if ($before > 0) {
if($before > $this->p) { if ($before > $this->p) {
$from = $this->p; $from = $this->p;
} else { } else {
$from = $before; $from = $before;
} }
} elseif($before < 0) { } elseif ($before < 0) {
$from = $this->p + $before; $from = $this->p + $before;
if($from < 0) { if ($from < 0) {
$from = 0; $from = 0;
} }
} }
if($after > 0) { if ($after > 0) {
$to = $this->p + $after; $to = $this->p + $after;
if($to > $this->_max) { if ($to > $this->_max) {
$to = $this->_max; $to = $this->_max;
} }
} elseif($after < 0) { } elseif ($after < 0) {
$to = $this->_max + $after; $to = $this->_max + $after;
if($to < $this->p) { if ($to < $this->p) {
$to = $this->p; $to = $this->p;
} }
} elseif($this->p > $this->_max) { } elseif ($this->p > $this->_max) {
$to = $this->_max; $to = $this->_max;
} }
$code = array(); $code = array();
for($i=$from; $i<=$to; $i++) { for ($i = $from; $i <= $to; $i++) {
$code[] = $this->tokens[ $i ]; $code[] = $this->tokens[$i];
} }
return $code; return $code;
@ -551,10 +579,11 @@ class Tokenizer {
* @param int $after * @param int $after
* @return string * @return string
*/ */
public function getSnippetAsString($before = 0, $after = 0) { public function getSnippetAsString($before = 0, $after = 0)
{
$str = ""; $str = "";
foreach($this->getSnippet($before, $after) as $token) { foreach ($this->getSnippet($before, $after) as $token) {
$str .= $token[1].$token[2]; $str .= $token[1] . $token[2];
} }
return trim(str_replace("\n", '↵', $str)); return trim(str_replace("\n", '↵', $str));
} }
@ -563,7 +592,8 @@ class Tokenizer {
* Check if current is special value: true, TRUE, false, FALSE, null, NULL * Check if current is special value: true, TRUE, false, FALSE, null, NULL
* @return bool * @return bool
*/ */
public function isSpecialVal() { public function isSpecialVal()
{
return isset(self::$spec[$this->current()]); return isset(self::$spec[$this->current()]);
} }
@ -571,14 +601,16 @@ class Tokenizer {
* Check if the token is last * Check if the token is last
* @return bool * @return bool
*/ */
public function isLast() { public function isLast()
{
return $this->p === $this->_max; return $this->p === $this->_max;
} }
/** /**
* Move pointer to the end * Move pointer to the end
*/ */
public function end() { public function end()
{
$this->p = $this->_max; $this->p = $this->_max;
} }
@ -586,7 +618,8 @@ class Tokenizer {
* Return line number of the current token * Return line number of the current token
* @return mixed * @return mixed
*/ */
public function getLine() { public function getLine()
{
return $this->curr ? $this->curr[3] : $this->_last_no; return $this->curr ? $this->curr[3] : $this->_last_no;
} }
@ -594,33 +627,13 @@ class Tokenizer {
* Is current token whitespaced, means previous token has whitespace characters * Is current token whitespaced, means previous token has whitespace characters
* @return bool * @return bool
*/ */
public function isWhiteSpaced() { public function isWhiteSpaced()
{
return $this->prev ? (bool)$this->prev[2] : false; return $this->prev ? (bool)$this->prev[2] : false;
} }
public function getWhitespace() { public function getWhitespace()
{
return $this->curr ? $this->curr[2] : false; return $this->curr ? $this->curr[2] : false;
} }
} }
/**
* Unexpected token
*/
class UnexpectedTokenException extends \RuntimeException {
public function __construct(Tokenizer $tokens, $expect = null, $where = null) {
if($expect && count($expect) == 1 && is_string($expect[0])) {
$expect = ", expect '".$expect[0]."'";
} else {
$expect = "";
}
if(!$tokens->curr) {
$this->message = "Unexpected end of ".($where?:"expression")."$expect";
} elseif($tokens->curr[0] === T_WHITESPACE) {
$this->message = "Unexpected whitespace$expect";
} elseif($tokens->curr[0] === T_BAD_CHARACTER) {
$this->message = "Unexpected bad characters (below ASCII 32 except \\t, \\n and \\r) in ".($where?:"expression")."$expect";
} else {
$this->message = "Unexpected token '".$tokens->current()."' in ".($where?:"expression")."$expect";
}
}
};

View File

@ -2,7 +2,8 @@
namespace Fenom; namespace Fenom;
use Fenom, Fenom\Provider as FS; use Fenom, Fenom\Provider as FS;
class TestCase extends \PHPUnit_Framework_TestCase { class TestCase extends \PHPUnit_Framework_TestCase
{
/** /**
* @var Fenom * @var Fenom
*/ */
@ -10,29 +11,32 @@ class TestCase extends \PHPUnit_Framework_TestCase {
public $values = array( public $values = array(
"zero" => 0, "zero" => 0,
"one" => 1, "one" => 1,
"two" => 2, "two" => 2,
"three" => 3, "three" => 3,
"float" => 4.5, "float" => 4.5,
"bool" => true, "bool" => true,
0 => "empty value", 0 => "empty value",
1 => "one value", 1 => "one value",
2 => "two value", 2 => "two value",
3 => "three value", 3 => "three value",
); );
public static function getVars() { public static function getVars()
{
return array( return array(
"zero" => 0, "zero" => 0,
"one" => 1, "one" => 1,
"two" => 2, "two" => 2,
"three" => 3, "three" => 3,
"float" => 4.5, "float" => 4.5,
"bool" => true, "bool" => true,
"obj" => new \StdClass, "obj" => new \StdClass,
"list" => array( "list" => array(
"a" => 1, "a" => 1,
"b" => 2 "one" => 1,
"b" => 2,
"two" => 2
), ),
0 => "empty value", 0 => "empty value",
1 => "one value", 1 => "one value",
@ -41,50 +45,57 @@ class TestCase extends \PHPUnit_Framework_TestCase {
); );
} }
public function setUp() { public function setUp()
if(!file_exists(FENOM_RESOURCES.'/compile')) { {
mkdir(FENOM_RESOURCES.'/compile', 0777, true); if (!file_exists(FENOM_RESOURCES . '/compile')) {
mkdir(FENOM_RESOURCES . '/compile', 0777, true);
} else { } else {
FS::clean(FENOM_RESOURCES.'/compile/'); FS::clean(FENOM_RESOURCES . '/compile/');
} }
$this->fenom = Fenom::factory(FENOM_RESOURCES.'/template', FENOM_RESOURCES.'/compile'); $this->fenom = Fenom::factory(FENOM_RESOURCES . '/template', FENOM_RESOURCES . '/compile');
$this->fenom->addModifier('dots', __CLASS__.'::dots'); $this->fenom->addModifier('dots', __CLASS__ . '::dots');
$this->fenom->addModifier('concat', __CLASS__.'::concat'); $this->fenom->addModifier('concat', __CLASS__ . '::concat');
$this->fenom->addFunction('test_function', __CLASS__.'::inlineFunction'); $this->fenom->addFunction('test_function', __CLASS__ . '::inlineFunction');
$this->fenom->addBlockFunction('test_block_function', __CLASS__.'::blockFunction'); $this->fenom->addBlockFunction('test_block_function', __CLASS__ . '::blockFunction');
} }
public static function dots($value) { public static function dots($value)
return $value."..."; {
return $value . "...";
} }
public static function concat() { public static function concat()
return call_user_func_array('var_export', func_get_args()); {
} return call_user_func_array('var_export', func_get_args());
}
public static function inlineFunction($params) { public static function inlineFunction($params)
{
return isset($params["text"]) ? $params["text"] : ""; return isset($params["text"]) ? $params["text"] : "";
} }
public static function blockFunction($params, $text) { public static function blockFunction($params, $text)
{
return $text; return $text;
} }
public static function setUpBeforeClass() { public static function setUpBeforeClass()
if(!file_exists(FENOM_RESOURCES.'/template')) { {
mkdir(FENOM_RESOURCES.'/template', 0777, true); if (!file_exists(FENOM_RESOURCES . '/template')) {
mkdir(FENOM_RESOURCES . '/template', 0777, true);
} else { } else {
FS::clean(FENOM_RESOURCES.'/template/'); FS::clean(FENOM_RESOURCES . '/template/');
} }
} }
public function tpl($name, $code) { public function tpl($name, $code)
{
$dir = dirname($name); $dir = dirname($name);
if($dir != "." && !is_dir(FENOM_RESOURCES.'/template/'.$dir)) { if ($dir != "." && !is_dir(FENOM_RESOURCES . '/template/' . $dir)) {
mkdir(FENOM_RESOURCES.'/template/'.$dir, 0777, true); mkdir(FENOM_RESOURCES . '/template/' . $dir, 0777, true);
} }
file_put_contents(FENOM_RESOURCES.'/template/'.$name, $code); file_put_contents(FENOM_RESOURCES . '/template/' . $name, $code);
} }
/** /**
@ -96,20 +107,22 @@ class TestCase extends \PHPUnit_Framework_TestCase {
* @param int $options * @param int $options
* @param bool $dump dump source and result code (for debug) * @param bool $dump dump source and result code (for debug)
*/ */
public function exec($code, $vars, $result, $options = 0, $dump = false) { public function exec($code, $vars, $result, $options = 0, $dump = false)
{
$this->fenom->setOptions($options); $this->fenom->setOptions($options);
$tpl = $this->fenom->compileCode($code, "runtime.tpl"); $tpl = $this->fenom->compileCode($code, "runtime.tpl");
if($dump) { 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), Modifier::strip($tpl->fetch($vars), true), "Test $code"); $this->assertSame(Modifier::strip($result), Modifier::strip($tpl->fetch($vars), true), "Test $code");
} }
public function execTpl($name, $code, $vars, $result, $dump = false) { public function execTpl($name, $code, $vars, $result, $dump = false)
{
$this->tpl($name, $code); $this->tpl($name, $code);
$tpl = $this->fenom->getTemplate($name); $tpl = $this->fenom->getTemplate($name);
if($dump) { 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"); $this->assertSame(Modifier::strip($result, true), Modifier::strip($tpl->fetch($vars), true), "Test tpl $name");
} }
@ -121,12 +134,13 @@ class TestCase extends \PHPUnit_Framework_TestCase {
* @param string $message exception message * @param string $message exception message
* @param int $options Fenom's options * @param int $options Fenom's options
*/ */
public function execError($code, $exception, $message, $options = 0) { public function execError($code, $exception, $message, $options = 0)
{
$opt = $this->fenom->getOptions(); $opt = $this->fenom->getOptions();
$this->fenom->setOptions($options); $this->fenom->setOptions($options);
try { try {
$this->fenom->compileCode($code, "inline.tpl"); $this->fenom->compileCode($code, "inline.tpl");
} catch(\Exception $e) { } catch (\Exception $e) {
$this->assertSame($exception, get_class($e), "Exception $code"); $this->assertSame($exception, get_class($e), "Exception $code");
$this->assertStringStartsWith($message, $e->getMessage()); $this->assertStringStartsWith($message, $e->getMessage());
$this->fenom->setOptions($opt); $this->fenom->setOptions($opt);
@ -136,28 +150,31 @@ class TestCase extends \PHPUnit_Framework_TestCase {
$this->fail("Code $code must be invalid"); $this->fail("Code $code must be invalid");
} }
public function assertRender($tpl, $result, $debug = false) { public function assertRender($tpl, $result, $debug = false)
{
$template = $this->fenom->compileCode($tpl); $template = $this->fenom->compileCode($tpl);
if($debug) { if ($debug) {
print_r("$tpl:\n".$template->getBody()); print_r("$tpl:\n" . $template->getBody());
} }
$this->assertSame($result, $template->fetch($this->values)); $this->assertSame($result, $template->fetch($this->values));
} }
public static function providerNumbers() { public static function providerNumbers()
return array( {
array('0', 0), return array(
array('77', 77), array('0', 0),
array('-33', -33), array('77', 77),
array('0.2', 0.2), array('-33', -33),
array('-0.3', -0.3), array('0.2', 0.2),
array('1e6', 1e6), array('-0.3', -0.3),
array('-2e6', -2e6), array('1e6', 1e6),
); array('-2e6', -2e6),
} );
}
public static function providerStrings() { public static function providerStrings()
{
return array( return array(
array('"str"', 'str'), array('"str"', 'str'),
array('"str\nand\nmany\nlines"', "str\nand\nmany\nlines"), array('"str\nand\nmany\nlines"', "str\nand\nmany\nlines"),
@ -187,81 +204,96 @@ class TestCase extends \PHPUnit_Framework_TestCase {
); );
} }
public function providerVariables() { public function providerVariables()
return array(); {
} return array(
array('$one', 1),
array('$list.one', 1),
array('$list[$$.DEVELOP]', 1),
);
}
public static function providerObjects() { public static function providerObjects()
return array(); {
} return array();
}
public static function providerArrays() { public static function providerArrays()
$scalars = array(); {
$data = array( $scalars = array();
array('[]', array()), $data = array(
array('[[],[]]', array(array(), array())), array('[]', array()),
); array('[[],[]]', array(array(), array())),
foreach(self::providerScalars() as $scalar) { );
$scalars[0][] = $scalar[0]; foreach (self::providerScalars() as $scalar) {
$scalars[1][] = $scalar[1]; $scalars[0][] = $scalar[0];
$scalars[1][] = $scalar[1];
$data[] = array( $data[] = array(
"[".$scalar[0]."]", "[" . $scalar[0] . "]",
array($scalar[1]) array($scalar[1])
); );
$data[] = array( $data[] = array(
"['some_key' =>".$scalar[0]."]", "['some_key' =>" . $scalar[0] . "]",
array('some_key' => $scalar[1]) array('some_key' => $scalar[1])
); );
} }
$data[] = array( $data[] = array(
"[".implode(", ", $scalars[0])."]", "[" . implode(", ", $scalars[0]) . "]",
$scalars[1] $scalars[1]
); );
return $data; return $data;
} }
public static function providerScalars() { public static function providerScalars()
return array_merge( {
self::providerNumbers(), return array_merge(
self::providerStrings() self::providerNumbers(),
); self::providerStrings()
} );
}
public static function providerValues() { public static function providerValues()
return array_merge( {
self::providerScalars(), return array_merge(
self::providerArrays(), self::providerScalars(),
self::providerVariables(), self::providerArrays(),
self::providerObjects() self::providerVariables(),
); self::providerObjects()
} );
}
} }
class Fake implements \ArrayAccess { class Fake implements \ArrayAccess
{
public $vars; public $vars;
public function offsetExists($offset) { public function offsetExists($offset)
{
return true; return true;
} }
public function offsetGet($offset) { public function offsetGet($offset)
if($offset == "object") { {
if ($offset == "object") {
return new self(); return new self();
} else { } else {
return new self($offset); return new self($offset);
} }
} }
public function offsetSet($offset, $value) { public function offsetSet($offset, $value)
{
$this->vars[$offset] = $value; $this->vars[$offset] = $value;
} }
public function offsetUnset($offset) { public function offsetUnset($offset)
{
unset($this->vars[$offset]); unset($this->vars[$offset]);
} }
public function proxy() { public function proxy()
return implode(", ", func_get_args()); {
} return implode(", ", func_get_args());
}
} }

View File

@ -1,24 +1,27 @@
<?php <?php
require_once __DIR__."/../vendor/autoload.php"; require_once __DIR__ . "/../vendor/autoload.php";
define('FENOM_RESOURCES', __DIR__ . "/resources");
define('FENOM_RESOURCES', __DIR__."/resources"); require_once FENOM_RESOURCES . "/actions.php";
require_once __DIR__ . "/TestCase.php";
require_once FENOM_RESOURCES."/actions.php"; ini_set('date.timezone', 'Europe/Moscow');
require_once __DIR__."/TestCase.php";
function drop() { function drop()
{
call_user_func_array("var_dump", func_get_args()); call_user_func_array("var_dump", func_get_args());
$e = new Exception(); $e = new Exception();
echo "-------\nDump trace: \n".$e->getTraceAsString()."\n"; echo "-------\nDump trace: \n" . $e->getTraceAsString() . "\n";
exit(); exit();
} }
function dump() { function dump()
foreach(func_get_args() as $arg) { {
fwrite(STDERR, "DUMP: ".call_user_func("print_r", $arg, true)."\n"); foreach (func_get_args() as $arg) {
fwrite(STDERR, "DUMP: " . call_user_func("print_r", $arg, true) . "\n");
} }
} }

View File

@ -3,9 +3,11 @@
namespace Fenom; namespace Fenom;
class AutoEscapeTest extends TestCase { class AutoEscapeTest extends TestCase
{
public static function providerHTML() { public static function providerHTML()
{
$html = "<script>alert('injection');</script>"; $html = "<script>alert('injection');</script>";
$escaped = htmlspecialchars($html, ENT_COMPAT, 'UTF-8'); $escaped = htmlspecialchars($html, ENT_COMPAT, 'UTF-8');
$vars = array( $vars = array(
@ -17,7 +19,7 @@ class AutoEscapeTest extends TestCase {
array('{$html}, {$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE), 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, $escaped", $vars, \Fenom::AUTO_ESCAPE), array('{raw $html}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{raw "{$html|up}"}, {$html}', strtoupper($html).", $escaped", $vars, \Fenom::AUTO_ESCAPE), array('{raw "{$html|up}"}, {$html}', strtoupper($html) . ", $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{$html}{/autoescape}, {$html}', "$escaped, $html", $vars, 0), array('{autoescape true}{$html}{/autoescape}, {$html}', "$escaped, $html", $vars, 0),
array('{autoescape false}{$html}{/autoescape}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE), array('{autoescape false}{$html}{/autoescape}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{autoescape true}{$html}{/autoescape}, {$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE), array('{autoescape true}{$html}{/autoescape}, {$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
@ -31,7 +33,7 @@ class AutoEscapeTest extends TestCase {
array('{test_function text=$html}, {$html}', "$html, $html", $vars, 0), 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 text=$html}, {$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{raw:test_function text=$html}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE), array('{raw:test_function text=$html}, {$html}', "$html, $escaped", $vars, \Fenom::AUTO_ESCAPE),
array('{raw:test_function text="{$html|up}"}, {$html}', strtoupper($html).", $escaped", $vars, \Fenom::AUTO_ESCAPE), array('{raw:test_function 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 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 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 true}{test_function text=$html}{/autoescape}, {test_function text=$html}', "$escaped, $escaped", $vars, \Fenom::AUTO_ESCAPE),
@ -56,7 +58,8 @@ class AutoEscapeTest extends TestCase {
/** /**
* @dataProvider providerHTML * @dataProvider providerHTML
*/ */
public function testEscaping($tpl, $result, $vars, $options) { public function testEscaping($tpl, $result, $vars, $options)
{
$this->values = $vars; $this->values = $vars;
$this->fenom->setOptions($options); $this->fenom->setOptions($options);
$this->assertRender($tpl, $result); $this->assertRender($tpl, $result);

View File

@ -3,29 +3,33 @@
namespace Fenom; namespace Fenom;
class CommentTest extends TestCase { class CommentTest extends TestCase
{
/** /**
* @dataProvider providerScalars * @dataProvider providerScalars
*/ */
public function testInline($tpl_val) { public function testInline($tpl_val)
{
$this->assertRender("before {* $tpl_val *} after", "before after"); $this->assertRender("before {* $tpl_val *} after", "before after");
$this->assertRender("before {* {{$tpl_val}} {{$tpl_val}} *} after", "before after"); $this->assertRender("before {* {{$tpl_val}} {{$tpl_val}} *} after", "before after");
$this->assertRender("before {*{{$tpl_val}}*} after", "before after"); $this->assertRender("before {*{{$tpl_val}}*} after", "before after");
} }
public function testError() { public function testError()
$this->execError('{* ', 'Fenom\CompileException', "Unclosed comment block in line"); {
$this->execError('{* ', 'Fenom\Error\CompileException', "Unclosed comment block in line");
} }
/** /**
* @dataProvider providerScalars * @dataProvider providerScalars
*/ */
public function testMultiLine($tpl_val) { public function testMultiLine($tpl_val)
$this->assertRender( {
"before-1\nbefore-2 {* before-3\nbefore-4 $tpl_val after-1\nafter-2 *} after-3\nafter-4{* dummy *}\nafter-5", $this->assertRender(
"before-1\nbefore-2 after-3\nafter-4\nafter-5" "before-1\nbefore-2 {* before-3\nbefore-4 $tpl_val after-1\nafter-2 *} after-3\nafter-4{* dummy *}\nafter-5",
); "before-1\nbefore-2 after-3\nafter-4\nafter-5"
);
} }
} }

View File

@ -3,14 +3,17 @@
namespace Fenom; namespace Fenom;
class CustomProviderTest extends TestCase { class CustomProviderTest extends TestCase
{
public function setUp() { public function setUp()
{
parent::setUp(); parent::setUp();
$this->fenom->addProvider("my", new Provider(FENOM_RESOURCES.'/provider')); $this->fenom->addProvider("my", new Provider(FENOM_RESOURCES . '/provider'));
} }
public function testCustom() { public function testCustom()
{
$this->assertRender("start: {include 'my:include.tpl'}", 'start: include template'); $this->assertRender("start: {include 'my:include.tpl'}", 'start: include template');
//$this->assertRender("start: {import 'my:macros.tpl' as ops} {ops.add a=3 b=6}"); //$this->assertRender("start: {import 'my:macros.tpl' as ops} {ops.add a=3 b=6}");
} }

View File

@ -2,41 +2,44 @@
namespace Fenom; namespace Fenom;
use Fenom, Fenom\TestCase; use Fenom, Fenom\TestCase;
class ExtendsTemplateTest extends TestCase { class ExtendsTemplateTest extends TestCase
{
public function _testSandbox() { public function _testSandbox()
$this->fenom = Fenom::factory(FENOM_RESOURCES.'/provider', FENOM_RESOURCES.'/compile'); {
try { $this->fenom = Fenom::factory(FENOM_RESOURCES . '/provider', FENOM_RESOURCES . '/compile');
print_r($this->fenom->getTemplate('use/child.tpl')->getBody()); try {
} catch (\Exception $e) { print_r($this->fenom->getTemplate('use/child.tpl')->getBody());
echo "$e"; } catch (\Exception $e) {
} echo "$e";
exit; }
} exit;
}
/** /**
* Templates skeletons * Templates skeletons
* @param array $vars * @param array $vars
* @return array * @return array
*/ */
public static function templates(array $vars) { public static function templates(array $vars)
{
return array( return array(
array( array(
"name" => "level.0.tpl", "name" => "level.0.tpl",
"level" => 0, "level" => 0,
"blocks" => array( "blocks" => array(
"b1" => "default {\$default}", "b1" => "default {\$default}",
"b2" => "empty 0" "b2" => "empty 0"
), ),
"result" => array( "result" => array(
"b1" => "default ".$vars["default"], "b1" => "default " . $vars["default"],
"b2" => "empty 0" "b2" => "empty 0"
), ),
), ),
array( array(
"name" => "level.1.tpl", "name" => "level.1.tpl",
"level" => 1, "level" => 1,
"use" => false, "use" => false,
"blocks" => array( "blocks" => array(
"b1" => "from level 1" "b1" => "from level 1"
), ),
@ -46,9 +49,9 @@ class ExtendsTemplateTest extends TestCase {
), ),
), ),
array( array(
"name" => "level.2.tpl", "name" => "level.2.tpl",
"level" => 2, "level" => 2,
"use" => false, "use" => false,
"blocks" => array( "blocks" => array(
"b2" => "from level 2", "b2" => "from level 2",
"b4" => "unused block" "b4" => "unused block"
@ -59,9 +62,9 @@ class ExtendsTemplateTest extends TestCase {
), ),
), ),
array( array(
"name" => "level.3.tpl", "name" => "level.3.tpl",
"level" => 3, "level" => 3,
"use" => false, "use" => false,
"blocks" => array( "blocks" => array(
"b1" => "from level 3", "b1" => "from level 3",
"b2" => "also from level 3" "b2" => "also from level 3"
@ -74,35 +77,37 @@ class ExtendsTemplateTest extends TestCase {
); );
} }
/** /**
* Generate templates by skeletons * Generate templates by skeletons
* *
* @param $block_mask * @param $block_mask
* @param $extend_mask * @param $extend_mask
* @param array $skels * @param array $skels
* @return array * @return array
*/ */
public static function generate($block_mask, $extend_mask, $skels) { public static function generate($block_mask, $extend_mask, $skels)
{
$t = array(); $t = array();
foreach($skels as $level => $tpl) { foreach ($skels as $level => $tpl) {
$src = 'level#'.$level.' '; $src = 'level#' . $level . ' ';
foreach($tpl["blocks"] as $bname => &$bcode) { foreach ($tpl["blocks"] as $bname => &$bcode) {
$src .= sprintf($block_mask, $bname, $bname.': '.$bcode)." level#$level "; $src .= sprintf($block_mask, $bname, $bname . ': ' . $bcode) . " level#$level ";
} }
$dst = "level#0 "; $dst = "level#0 ";
foreach($tpl["result"] as $bname => &$bcode) { foreach ($tpl["result"] as $bname => &$bcode) {
$dst .= $bname.': '.$bcode.' level#0 '; $dst .= $bname . ': ' . $bcode . ' level#0 ';
} }
if($level) { if ($level) {
$src = sprintf($extend_mask, $level-1).' '.$src; $src = sprintf($extend_mask, $level - 1) . ' ' . $src;
} }
$t[ $tpl["name"] ] = array("src" => $src, "dst" => $dst); $t[$tpl["name"]] = array("src" => $src, "dst" => $dst);
} }
return $t; return $t;
} }
public function _testTemplateExtends() { public function _testTemplateExtends()
{
$vars = array( $vars = array(
"b1" => "b1", "b1" => "b1",
"b2" => "b2", "b2" => "b2",
@ -112,39 +117,41 @@ class ExtendsTemplateTest extends TestCase {
"default" => 5 "default" => 5
); );
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars)); $tpls = self::generate('{block "%s"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars));
foreach($tpls as $name => $tpl) { foreach ($tpls as $name => $tpl) {
$this->tpl($name, $tpl["src"]); $this->tpl($name, $tpl["src"]);
$this->assertSame($this->fenom->fetch($name, $vars), $tpl["dst"]); $this->assertSame($this->fenom->fetch($name, $vars), $tpl["dst"]);
} }
return; return;
$vars["default"]++; $vars["default"]++;
$this->fenom->flush(); $this->fenom->flush();
$tpls = self::generate('{block "{$%s}"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars)); $tpls = self::generate('{block "{$%s}"}%s{/block}', '{extends "level.%d.tpl"}', self::templates($vars));
arsort($tpls); arsort($tpls);
foreach($tpls as $name => $tpl) { foreach ($tpls as $name => $tpl) {
$this->tpl("d.".$name, $tpl["src"]); $this->tpl("d." . $name, $tpl["src"]);
$this->assertSame($this->fenom->fetch("d.".$name, $vars), $tpl["dst"]); $this->assertSame($this->fenom->fetch("d." . $name, $vars), $tpl["dst"]);
} }
$vars["default"]++; $vars["default"]++;
$this->fenom->flush(); $this->fenom->flush();
$tpls = self::generate('{block "%s"}%s{/block}', '{extends "$level.%d.tpl"}', self::templates($vars)); $tpls = self::generate('{block "%s"}%s{/block}', '{extends "$level.%d.tpl"}', self::templates($vars));
arsort($tpls); arsort($tpls);
foreach($tpls as $name => $tpl) { foreach ($tpls as $name => $tpl) {
$this->tpl("x.".$name, $tpl["src"]); $this->tpl("x." . $name, $tpl["src"]);
$this->assertSame($this->fenom->fetch("x.".$name, $vars), $tpl["dst"]); $this->assertSame($this->fenom->fetch("x." . $name, $vars), $tpl["dst"]);
} }
} }
/** /**
* @group use * @group use
*/ */
public function testUse() { public function testUse()
$this->fenom = Fenom::factory(FENOM_RESOURCES.'/provider', FENOM_RESOURCES.'/compile'); {
$this->assertSame("<html>\n block 1 blocks \n block 2 child \n</html>", $this->fenom->fetch('use/child.tpl')); $this->fenom = Fenom::factory(FENOM_RESOURCES . '/provider', FENOM_RESOURCES . '/compile');
} $this->assertSame("<html>\n block 1 blocks \n block 2 child \n</html>", $this->fenom->fetch('use/child.tpl'));
}
public function _testParent() { public function _testParent()
{
} }
} }

View File

@ -1,70 +1,82 @@
<?php <?php
namespace Fenom; namespace Fenom;
class FunctionsTest extends TestCase { class FunctionsTest extends TestCase
{
const FUNCTION_ARGUMENT_CONSTANT = 1; const FUNCTION_ARGUMENT_CONSTANT = 1;
public static function functionSum($of = array()) { public static function functionSum($of = array())
return array_sum($of); {
} return array_sum($of);
}
public static function functionPow($a, $n = 2) { public static function functionPow($a, $n = 2)
return pow($a, $n); {
} return pow($a, $n);
}
public static function functionInc($a, $i = self::FUNCTION_ARGUMENT_CONSTANT) { public static function functionInc($a, $i = self::FUNCTION_ARGUMENT_CONSTANT)
return $a + $i; {
} return $a + $i;
}
public function setUp() { public function setUp()
{
parent::setUp(); parent::setUp();
$this->fenom->addFunctionSmart('sum', __CLASS__ . '::functionSum'); $this->fenom->addFunctionSmart('sum', __CLASS__ . '::functionSum');
$this->fenom->addFunctionSmart('pow', __CLASS__ . '::functionPow'); $this->fenom->addFunctionSmart('pow', __CLASS__ . '::functionPow');
$this->fenom->addFunctionSmart('inc', __CLASS__ . '::functionInc'); $this->fenom->addFunctionSmart('inc', __CLASS__ . '::functionInc');
$this->tpl('function_params_scalar.tpl', '{pow a=2 n=3}'); $this->tpl('function_params_scalar.tpl', '{pow a=2 n=3}');
$this->tpl('function_params_dynamic.tpl', '{pow a=$a n=$n}'); $this->tpl('function_params_dynamic.tpl', '{pow a=$a n=$n}');
$this->tpl('function_default_param_scalar.tpl', '{pow a=2}'); $this->tpl('function_default_param_scalar.tpl', '{pow a=2}');
$this->tpl('function_default_param_empty_array.tpl', '{sum}'); $this->tpl('function_default_param_empty_array.tpl', '{sum}');
$this->tpl('function_default_param_const.tpl', '{inc a=1}'); $this->tpl('function_default_param_const.tpl', '{inc a=1}');
$this->tpl('function_array_param.tpl', '{sum of=[1, 2, 3, 4, 5]}'); $this->tpl('function_array_param.tpl', '{sum of=[1, 2, 3, 4, 5]}');
$this->tpl('function_array_param_pos.tpl', '{sum [1, 2, 3, 4, 5]}'); $this->tpl('function_array_param_pos.tpl', '{sum [1, 2, 3, 4, 5]}');
} }
public function testFunctionWithParams() { public function testFunctionWithParams()
{
$output = $this->fenom->fetch('function_params_scalar.tpl'); $output = $this->fenom->fetch('function_params_scalar.tpl');
$this->assertEquals('8', $output); $this->assertEquals('8', $output);
} }
public function testFunctionWithDynamicParams() { public function testFunctionWithDynamicParams()
{
$output = $this->fenom->fetch('function_params_dynamic.tpl', array('a' => 3, 'n' => 4)); $output = $this->fenom->fetch('function_params_dynamic.tpl', array('a' => 3, 'n' => 4));
$this->assertEquals('81', $output); $this->assertEquals('81', $output);
} }
public function testFunctionWithDefaultParamScalar() { public function testFunctionWithDefaultParamScalar()
$output = $this->fenom->fetch('function_default_param_scalar.tpl'); {
$this->assertEquals('4', $output); $output = $this->fenom->fetch('function_default_param_scalar.tpl');
} $this->assertEquals('4', $output);
}
public function testFunctionWithDefaultParamArray() { public function testFunctionWithDefaultParamArray()
$output = $this->fenom->fetch('function_default_param_empty_array.tpl'); {
$this->assertEquals('0', $output); $output = $this->fenom->fetch('function_default_param_empty_array.tpl');
} $this->assertEquals('0', $output);
}
public function testFunctionWithDefaultParamConst() { public function testFunctionWithDefaultParamConst()
$output = $this->fenom->fetch('function_default_param_const.tpl'); {
$this->assertEquals('2', $output); $output = $this->fenom->fetch('function_default_param_const.tpl');
} $this->assertEquals('2', $output);
}
public function testFunctionWithArrayNamedParam() { public function testFunctionWithArrayNamedParam()
$output = $this->fenom->fetch('function_array_param.tpl'); {
$this->assertEquals('15', $output); $output = $this->fenom->fetch('function_array_param.tpl');
} $this->assertEquals('15', $output);
}
public function testFunctionWithArrayPositionalParam() { public function testFunctionWithArrayPositionalParam()
$output = $this->fenom->fetch('function_array_param_pos.tpl'); {
$this->assertEquals('15', $output); $output = $this->fenom->fetch('function_array_param_pos.tpl');
} $this->assertEquals('15', $output);
}
} }

View File

@ -1,9 +1,11 @@
<?php <?php
namespace Fenom; namespace Fenom;
class MacrosTest extends TestCase { class MacrosTest extends TestCase
{
public function setUp() { public function setUp()
{
parent::setUp(); parent::setUp();
$this->tpl("math.tpl", ' $this->tpl("math.tpl", '
{macro plus(x, y)} {macro plus(x, y)}
@ -42,22 +44,46 @@ class MacrosTest extends TestCase {
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}
{macro.factorial num=10}');
} }
public function testMacros() { public function _testSandbox()
{
try {
$this->fenom->compile("macro_recursive.tpl");
// $this->fenom->flush();
// var_dump($this->fenom->fetch("macro_recursive.tpl", []));
var_dump( $this->fenom->compile("macro_recursive.tpl")->getTemplateCode());
} catch (\Exception $e) {
var_dump($e->getMessage() . ": " . $e->getTraceAsString());
}
exit;
}
public function testMacros()
{
$tpl = $this->fenom->compile('math.tpl'); $tpl = $this->fenom->compile('math.tpl');
$this->assertStringStartsWith('x + y = ', trim($tpl->macros["plus"]["body"])); $this->assertStringStartsWith('x + y = ', trim($tpl->macros["plus"]["body"]));
$this->assertSame('Math: x + y = 5 , x - y - z = 6', Modifier::strip($tpl->fetch(array()), true)); $this->assertSame('Math: x + y = 5 , x - y - z = 6', Modifier::strip($tpl->fetch(array()), true));
} }
public function testImport() { public function testImport()
{
$tpl = $this->fenom->compile('import.tpl'); $tpl = $this->fenom->compile('import.tpl');
$this->assertSame('Imp: x + y = 3 , x - y - z = 3', Modifier::strip($tpl->fetch(array()), true)); $this->assertSame('Imp: x + y = 3 , x - y - z = 3', Modifier::strip($tpl->fetch(array()), true));
} }
public function testImportCustom() { public function testImportCustom()
{
$tpl = $this->fenom->compile('import_custom.tpl'); $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));
@ -65,11 +91,20 @@ class MacrosTest extends TestCase {
/** /**
* @expectedExceptionMessage Undefined macro 'plus' * @expectedExceptionMessage Undefined macro 'plus'
* @expectedException \Fenom\CompileException * @expectedException \Fenom\Error\CompileException
*/ */
public function testImportMiss() { public function testImportMiss()
{
$tpl = $this->fenom->compile('import_miss.tpl'); $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));
} }
public function testRecursive()
{
$this->fenom->compile('macro_recursive.tpl');
$this->fenom->flush();
$tpl = $this->fenom->getTemplate('macro_recursive.tpl');
$this->assertSame("10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10", Modifier::strip($tpl->fetch(array()), true));
}
} }

View File

@ -2,11 +2,13 @@
namespace Fenom; namespace Fenom;
class ModifiersTest extends TestCase { class ModifiersTest extends TestCase
{
public static function providerTruncate() { public static function providerTruncate()
{
$lorem = 'Lorem ipsum dolor sit amet'; // en $lorem = 'Lorem ipsum dolor sit amet'; // en
$uni = 'Лорем ипсум долор сит амет'; // ru $uni = 'Лорем ипсум долор сит амет'; // ru
return array( return array(
// ascii chars // ascii chars
array($lorem, 'Lorem ip...', 8), array($lorem, 'Lorem ip...', 8),
@ -33,7 +35,8 @@ class ModifiersTest extends TestCase {
* @param bool $by_words * @param bool $by_words
* @param bool $middle * @param bool $middle
*/ */
public function testTruncate($in, $out, $count, $delim = '...', $by_words = false, $middle = false) { public function testTruncate($in, $out, $count, $delim = '...', $by_words = false, $middle = false)
{
$tpl = $this->fenom->compileCode('{$text|truncate:$count:$delim:$by_words:$middle}'); $tpl = $this->fenom->compileCode('{$text|truncate:$count:$delim:$by_words:$middle}');
$this->assertEquals($out, $tpl->fetch(array( $this->assertEquals($out, $tpl->fetch(array(
"text" => $in, "text" => $in,
@ -44,7 +47,8 @@ class ModifiersTest extends TestCase {
))); )));
} }
public static function providerUpLow() { public static function providerUpLow()
{
return array( return array(
array("up", "lorem", "LOREM"), array("up", "lorem", "LOREM"),
array("up", "Lorem", "LOREM"), array("up", "Lorem", "LOREM"),
@ -64,14 +68,16 @@ class ModifiersTest extends TestCase {
* @param $in * @param $in
* @param $out * @param $out
*/ */
public function testUpLow($modifier, $in, $out) { public function testUpLow($modifier, $in, $out)
$tpl = $this->fenom->compileCode('{$text|'.$modifier.'}'); {
$tpl = $this->fenom->compileCode('{$text|' . $modifier . '}');
$this->assertEquals($out, $tpl->fetch(array( $this->assertEquals($out, $tpl->fetch(array(
"text" => $in, "text" => $in,
))); )));
} }
public static function providerLength() { public static function providerLength()
{
return array( return array(
array("length", 6), array("length", 6),
array("длина", 5), array("длина", 5),
@ -90,7 +96,8 @@ class ModifiersTest extends TestCase {
* @param $in * @param $in
* @param $out * @param $out
*/ */
public function testLength($in, $out) { public function testLength($in, $out)
{
$tpl = $this->fenom->compileCode('{$data|length}'); $tpl = $this->fenom->compileCode('{$data|length}');
$this->assertEquals($out, $tpl->fetch(array( $this->assertEquals($out, $tpl->fetch(array(
"data" => $in, "data" => $in,

View File

@ -3,87 +3,97 @@ namespace Fenom;
use Fenom; use Fenom;
use Fenom\TestCase; use Fenom\TestCase;
class ProviderTest extends TestCase { class ProviderTest extends TestCase
{
/** /**
* @var Provider * @var Provider
*/ */
public $provider; public $provider;
public function setUp() { public function setUp()
{
parent::setUp(); parent::setUp();
$this->tpl("template1.tpl", 'Template 1 {$a}'); $this->tpl("template1.tpl", 'Template 1 {$a}');
$this->tpl("template2.tpl", 'Template 2 {$a}'); $this->tpl("template2.tpl", 'Template 2 {$a}');
$this->tpl("sub/template3.tpl", 'Template 3 {$a}'); $this->tpl("sub/template3.tpl", 'Template 3 {$a}');
$this->provider = new Provider(FENOM_RESOURCES.'/template'); $this->provider = new Provider(FENOM_RESOURCES . '/template');
clearstatcache(); clearstatcache();
} }
public function testIsTemplateExists() { public function testIsTemplateExists()
{
clearstatcache(); clearstatcache();
$this->assertTrue($this->provider->templateExists("template1.tpl")); $this->assertTrue($this->provider->templateExists("template1.tpl"));
$this->assertFalse($this->provider->templateExists("unexists.tpl")); $this->assertFalse($this->provider->templateExists("unexists.tpl"));
} }
public function testGetSource() { public function testGetSource()
{
clearstatcache(); clearstatcache();
$src = $this->provider->getSource("template1.tpl", $time); $src = $this->provider->getSource("template1.tpl", $time);
clearstatcache(); clearstatcache();
$this->assertEquals(file_get_contents(FENOM_RESOURCES.'/template/template1.tpl'), $src); $this->assertEquals(file_get_contents(FENOM_RESOURCES . '/template/template1.tpl'), $src);
$this->assertEquals(filemtime(FENOM_RESOURCES.'/template/template1.tpl'), $time); $this->assertEquals(filemtime(FENOM_RESOURCES . '/template/template1.tpl'), $time);
} }
/** /**
* @expectedException \RuntimeException * @expectedException \RuntimeException
*/ */
public function testGetSourceInvalid() { public function testGetSourceInvalid()
{
$this->provider->getSource("unexists.tpl", $time); $this->provider->getSource("unexists.tpl", $time);
} }
public function testGetLastModified() { public function testGetLastModified()
{
$time = $this->provider->getLastModified("template1.tpl"); $time = $this->provider->getLastModified("template1.tpl");
clearstatcache(); clearstatcache();
$this->assertEquals(filemtime(FENOM_RESOURCES.'/template/template1.tpl'), $time); $this->assertEquals(filemtime(FENOM_RESOURCES . '/template/template1.tpl'), $time);
} }
/** /**
* @expectedException \RuntimeException * @expectedException \RuntimeException
*/ */
public function testGetLastModifiedInvalid() { public function testGetLastModifiedInvalid()
{
$this->provider->getLastModified("unexists.tpl"); $this->provider->getLastModified("unexists.tpl");
} }
public function testVerify() { public function testVerify()
{
$templates = array( $templates = array(
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'), "template1.tpl" => filemtime(FENOM_RESOURCES . '/template/template1.tpl'),
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl') "template2.tpl" => filemtime(FENOM_RESOURCES . '/template/template2.tpl')
); );
clearstatcache(); clearstatcache();
$this->assertTrue($this->provider->verify($templates)); $this->assertTrue($this->provider->verify($templates));
clearstatcache(); clearstatcache();
$templates = array( $templates = array(
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl'), "template2.tpl" => filemtime(FENOM_RESOURCES . '/template/template2.tpl'),
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl') "template1.tpl" => filemtime(FENOM_RESOURCES . '/template/template1.tpl')
); );
clearstatcache(); clearstatcache();
$this->assertTrue($this->provider->verify($templates)); $this->assertTrue($this->provider->verify($templates));
} }
public function testVerifyInvalid() { public function testVerifyInvalid()
{
$templates = array( $templates = array(
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'), "template1.tpl" => filemtime(FENOM_RESOURCES . '/template/template1.tpl'),
"template2.tpl" => filemtime(FENOM_RESOURCES.'/template/template2.tpl') + 1 "template2.tpl" => filemtime(FENOM_RESOURCES . '/template/template2.tpl') + 1
); );
clearstatcache(); clearstatcache();
$this->assertFalse($this->provider->verify($templates)); $this->assertFalse($this->provider->verify($templates));
clearstatcache(); clearstatcache();
$templates = array( $templates = array(
"template1.tpl" => filemtime(FENOM_RESOURCES.'/template/template1.tpl'), "template1.tpl" => filemtime(FENOM_RESOURCES . '/template/template1.tpl'),
"unexists.tpl" => 1234567890 "unexists.tpl" => 1234567890
); );
$this->assertFalse($this->provider->verify($templates)); $this->assertFalse($this->provider->verify($templates));
} }
public function testGetAll() { public function testGetAll()
{
$list = $this->provider->getList(); $list = $this->provider->getList();
sort($list); sort($list);
$this->assertSame(array( $this->assertSame(array(

View File

@ -3,22 +3,25 @@ namespace Fenom;
use Fenom, use Fenom,
Fenom\Render; Fenom\Render;
class RenderTest extends \PHPUnit_Framework_TestCase { class RenderTest extends \PHPUnit_Framework_TestCase
{
/** /**
* @var Render * @var Render
*/ */
public static $render; public static $render;
public static function setUpBeforeClass() { public static function setUpBeforeClass()
{
self::$render = new Render(Fenom::factory("."), function ($tpl) { self::$render = new Render(Fenom::factory("."), function ($tpl) {
echo "It is render's function ".$tpl["render"]; echo "It is render's function " . $tpl["render"];
}, array( }, array(
"name" => "render.tpl" "name" => "render.tpl"
)); ));
} }
public function testCreate() { public function testCreate()
{
$r = new Render(Fenom::factory("."), function () { $r = new Render(Fenom::factory("."), function () {
echo "Test render"; echo "Test render";
}, array( }, array(
@ -27,14 +30,16 @@ class RenderTest extends \PHPUnit_Framework_TestCase {
$this->assertSame("Test render", $r->fetch(array())); $this->assertSame("Test render", $r->fetch(array()));
} }
public function testDisplay() { public function testDisplay()
{
ob_start(); ob_start();
self::$render->display(array("render" => "display")); self::$render->display(array("render" => "display"));
$out = ob_get_clean(); $out = ob_get_clean();
$this->assertSame("It is render's function display", $out); $this->assertSame("It is render's function display", $out);
} }
public function testFetch() { public function testFetch()
{
$this->assertSame("It is render's function fetch", self::$render->fetch(array("render" => "fetch"))); $this->assertSame("It is render's function fetch", self::$render->fetch(array("render" => "fetch")));
} }
@ -42,7 +47,8 @@ class RenderTest extends \PHPUnit_Framework_TestCase {
* @expectedException \RuntimeException * @expectedException \RuntimeException
* @expectedExceptionMessage template error * @expectedExceptionMessage template error
*/ */
public function testFetchException() { public function testFetchException()
{
$render = new Render(Fenom::factory("."), function () { $render = new Render(Fenom::factory("."), function () {
echo "error"; echo "error";
throw new \RuntimeException("template error"); throw new \RuntimeException("template error");

View File

@ -1,22 +1,26 @@
<?php <?php
namespace Fenom; namespace Fenom;
class ScopeTest extends TestCase { class ScopeTest extends TestCase
public function openTag($tokenizer, $scope) { {
public function openTag($tokenizer, $scope)
{
$this->assertInstanceOf('Fenom\Tokenizer', $tokenizer); $this->assertInstanceOf('Fenom\Tokenizer', $tokenizer);
$this->assertInstanceOf('Fenom\Scope', $scope); $this->assertInstanceOf('Fenom\Scope', $scope);
$scope["value"] = true; $scope["value"] = true;
return "open-tag"; return "open-tag";
} }
public function closeTag($tokenizer, $scope) { public function closeTag($tokenizer, $scope)
{
$this->assertInstanceOf('Fenom\Tokenizer', $tokenizer); $this->assertInstanceOf('Fenom\Tokenizer', $tokenizer);
$this->assertInstanceOf('Fenom\Scope', $scope); $this->assertInstanceOf('Fenom\Scope', $scope);
$this->assertTrue($scope["value"]); $this->assertTrue($scope["value"]);
return "close-tag"; return "close-tag";
} }
public function testBlock() { public function testBlock()
{
/*$scope = new Scope($this->fenom, new Template($this->fenom), 1, array( /*$scope = new Scope($this->fenom, new Template($this->fenom), 1, array(
"open" => array($this, "openTag"), "open" => array($this, "openTag"),
"close" => array($this, "closeTag") "close" => array($this, "closeTag")
@ -28,5 +32,5 @@ class ScopeTest extends TestCase {
$content = " some ?> content\n\nwith /*#9999999#* / many\n\tlines"; $content = " some ?> content\n\nwith /*#9999999#* / many\n\tlines";
$scope->tpl->_body = "start <?php ".$scope->open($tokenizer)." ?>".$content; $scope->tpl->_body = "start <?php ".$scope->open($tokenizer)." ?>".$content;
$this->assertSame($content, $scope->getContent());*/ $this->assertSame($content, $scope->getContent());*/
} }
} }

View File

@ -3,54 +3,62 @@
namespace Fenom; namespace Fenom;
class TagsTest extends TestCase { class TagsTest extends TestCase
{
public function _testSandbox() { public function _testSandbox()
try { {
var_dump($this->fenom->compileCode('{var $a=Fenom\TestCase::dots("asd")}')->getBody()); try {
} catch(\Exception $e) { var_dump($this->fenom->compileCode('{var $a=Fenom\TestCase::dots("asd")}')->getBody());
echo "$e"; } catch (\Exception $e) {
} echo "$e";
exit; }
} exit;
/**
* @dataProvider providerScalars
*/
public function testVar($tpl_val, $val) {
$this->assertRender("{var \$a=$tpl_val}\nVar: {\$a}", "\nVar: ".$val);
} }
/** /**
* @dataProvider providerScalars * @dataProvider providerScalars
*/ */
public function testVarBlock($tpl_val, $val) { public function testVar($tpl_val, $val)
$this->assertRender("{var \$a}before {{$tpl_val}} after{/var}\nVar: {\$a}", "\nVar: before ".$val." after"); {
$this->assertRender("{var \$a=$tpl_val}\nVar: {\$a}", "\nVar: " . $val);
} }
/** /**
* @dataProvider providerScalars * @dataProvider providerScalars
*/ */
public function testVarBlockModified($tpl_val, $val) { public function testVarBlock($tpl_val, $val)
$this->assertRender("{var \$a|low|dots}before {{$tpl_val}} after{/var}\nVar: {\$a}", "\nVar: ".strtolower("before ".$val." after")."..."); {
$this->assertRender("{var \$a}before {{$tpl_val}} after{/var}\nVar: {\$a}", "\nVar: before " . $val . " after");
} }
public function testCycle() { /**
$this->assertRender('{for $i=0 to=4}{cycle ["one", "two"]}, {/for}', "one, two, one, two, one, "); * @dataProvider providerScalars
*/
public function testVarBlockModified($tpl_val, $val)
{
$this->assertRender("{var \$a|low|dots}before {{$tpl_val}} after{/var}\nVar: {\$a}", "\nVar: " . strtolower("before " . $val . " after") . "...");
} }
/** public function testCycle()
* {
*/ $this->assertRender('{for $i=0 to=4}{cycle ["one", "two"]}, {/for}', "one, two, one, two, one, ");
public function testCycleIndex() { }
$this->assertRender('{var $a=["one", "two"]}{for $i=1 to=5}{cycle $a index=$i}, {/for}', "two, one, two, one, two, ");
}
/** /**
* @dataProvider providerScalars *
*/ */
public function testFilter($tpl_val, $val) { public function testCycleIndex()
$this->assertRender("{filter|up} before {{$tpl_val}} after {/filter}", strtoupper(" before {$val} after ")); {
$this->assertRender('{var $a=["one", "two"]}{for $i=1 to=5}{cycle $a index=$i}, {/for}', "two, one, two, one, two, ");
}
/**
* @dataProvider providerScalars
*/
public function testFilter($tpl_val, $val)
{
$this->assertRender("{filter|up} before {{$tpl_val}} after {/filter}", strtoupper(" before {$val} after "));
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -2,59 +2,62 @@
namespace Fenom; namespace Fenom;
use Fenom\Tokenizer; use Fenom\Tokenizer;
class TokenizerTest extends \PHPUnit_Framework_TestCase { class TokenizerTest extends \PHPUnit_Framework_TestCase
{
public function testTokens() { public function testTokens()
$code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}'; {
$tokens = new Tokenizer($code); $code = 'hello, please resolve this example: sin($x)+tan($x*$t) = {U|[0,1]}';
$this->assertSame(T_STRING, $tokens->key()); $tokens = new Tokenizer($code);
$this->assertSame("hello", $tokens->current()); $this->assertSame(T_STRING, $tokens->key());
$this->assertSame("hello", $tokens->current());
$this->assertTrue($tokens->isNext(",")); $this->assertTrue($tokens->isNext(","));
$this->assertFalse($tokens->isNext("=")); $this->assertFalse($tokens->isNext("="));
$this->assertFalse($tokens->isNext(T_STRING)); $this->assertFalse($tokens->isNext(T_STRING));
$this->assertFalse($tokens->isNext($tokens::MACRO_UNARY)); $this->assertFalse($tokens->isNext($tokens::MACRO_UNARY));
$this->assertFalse($tokens->isNext("=", T_STRING, $tokens::MACRO_UNARY)); $this->assertFalse($tokens->isNext("=", T_STRING, $tokens::MACRO_UNARY));
$this->assertTrue($tokens->isNext("=", T_STRING, $tokens::MACRO_UNARY, ",")); $this->assertTrue($tokens->isNext("=", T_STRING, $tokens::MACRO_UNARY, ","));
$this->assertSame(",", $tokens->getNext()); $this->assertSame(",", $tokens->getNext());
$this->assertSame(",", $tokens->key()); $this->assertSame(",", $tokens->key());
$this->assertSame("please", $tokens->getNext(T_STRING)); $this->assertSame("please", $tokens->getNext(T_STRING));
$this->assertSame("resolve", $tokens->getNext($tokens::MACRO_UNARY, T_STRING)); $this->assertSame("resolve", $tokens->getNext($tokens::MACRO_UNARY, T_STRING));
$tokens->next(); $tokens->next();
$tokens->next(); $tokens->next();
$tokens->next(); $tokens->next();
$this->assertSame(":", $tokens->current()); $this->assertSame(":", $tokens->current());
$this->assertSame(":", $tokens->key()); $this->assertSame(":", $tokens->key());
$this->assertSame("sin", $tokens->getNext($tokens::MACRO_STRING)); $this->assertSame("sin", $tokens->getNext($tokens::MACRO_STRING));
$this->assertSame("sin", $tokens->current()); $this->assertSame("sin", $tokens->current());
$this->assertSame(T_STRING, $tokens->key()); $this->assertSame(T_STRING, $tokens->key());
$this->assertTrue($tokens->is(T_STRING)); $this->assertTrue($tokens->is(T_STRING));
$this->assertTrue($tokens->is($tokens::MACRO_STRING)); $this->assertTrue($tokens->is($tokens::MACRO_STRING));
$this->assertFalse($tokens->is($tokens::MACRO_EQUALS)); $this->assertFalse($tokens->is($tokens::MACRO_EQUALS));
$this->assertFalse($tokens->is(T_DNUMBER)); $this->assertFalse($tokens->is(T_DNUMBER));
$this->assertFalse($tokens->is(":")); $this->assertFalse($tokens->is(":"));
$this->assertSame("(", $tokens->getNext("(",")")); $this->assertSame("(", $tokens->getNext("(", ")"));
$tokens->next(); $tokens->next();
$tokens->next(); $tokens->next();
$this->assertSame("+", $tokens->getNext($tokens::MACRO_BINARY)); $this->assertSame("+", $tokens->getNext($tokens::MACRO_BINARY));
} }
public function testSkip() { public function testSkip()
{
$text = "1 foo: bar ( 3 + double ) "; $text = "1 foo: bar ( 3 + double ) ";
$tokens = new Tokenizer($text); $tokens = new Tokenizer($text);
$tokens->skip()->skip(T_STRING)->skip(':'); $tokens->skip()->skip(T_STRING)->skip(':');
try { try {
$tokens->skip(T_STRING)->skip('(')->skip(':'); $tokens->skip(T_STRING)->skip('(')->skip(':');
} catch(\Exception $e) { } catch (\Exception $e) {
$this->assertInstanceOf('Fenom\UnexpectedTokenException', $e); $this->assertInstanceOf('Fenom\Error\UnexpectedTokenException', $e);
$this->assertStringStartsWith("Unexpected token '3' in expression, expect ':'", $e->getMessage()); $this->assertStringStartsWith("Unexpected token '3' in expression, expect ':'", $e->getMessage());
} }
$this->assertTrue($tokens->valid()); $this->assertTrue($tokens->valid());

View File

@ -3,22 +3,25 @@
use Fenom\Render, use Fenom\Render,
Fenom\Provider as FS; Fenom\Provider as FS;
class FenomTest extends \Fenom\TestCase { class FenomTest extends \Fenom\TestCase
{
public static function providerOptions() { public static function providerOptions()
{
return array( return array(
array("disable_methods", Fenom::DENY_METHODS), array("disable_methods", Fenom::DENY_METHODS),
array("disable_native_funcs", Fenom::DENY_INLINE_FUNCS), array("disable_native_funcs", Fenom::DENY_INLINE_FUNCS),
array("disable_cache", Fenom::DISABLE_CACHE), array("disable_cache", Fenom::DISABLE_CACHE),
array("force_compile", Fenom::FORCE_COMPILE), array("force_compile", Fenom::FORCE_COMPILE),
array("auto_reload", Fenom::AUTO_RELOAD), array("auto_reload", Fenom::AUTO_RELOAD),
array("force_include", Fenom::FORCE_INCLUDE), array("force_include", Fenom::FORCE_INCLUDE),
array("auto_escape", Fenom::AUTO_ESCAPE), array("auto_escape", Fenom::AUTO_ESCAPE),
array("force_verify", Fenom::FORCE_VERIFY) array("force_verify", Fenom::FORCE_VERIFY)
); );
} }
public function testCompileFile() { public function testCompileFile()
{
$a = array( $a = array(
"a" => "a", "a" => "a",
"b" => "b" "b" => "b"
@ -29,17 +32,19 @@ class FenomTest extends \Fenom\TestCase {
$this->assertSame("Template 2 b", $this->fenom->fetch('template2.tpl', $a)); $this->assertSame("Template 2 b", $this->fenom->fetch('template2.tpl', $a));
$this->assertInstanceOf('Fenom\Render', $this->fenom->getTemplate('template1.tpl')); $this->assertInstanceOf('Fenom\Render', $this->fenom->getTemplate('template1.tpl'));
$this->assertInstanceOf('Fenom\Render', $this->fenom->getTemplate('template2.tpl')); $this->assertInstanceOf('Fenom\Render', $this->fenom->getTemplate('template2.tpl'));
$this->assertSame(3, iterator_count(new FilesystemIterator(FENOM_RESOURCES.'/compile'))); $this->assertSame(3, iterator_count(new FilesystemIterator(FENOM_RESOURCES . '/compile')));
} }
public function testStorage() { public function testStorage()
{
$this->tpl('custom.tpl', 'Custom template'); $this->tpl('custom.tpl', 'Custom template');
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array())); $this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
$this->tpl('custom.tpl', 'Custom template 2'); $this->tpl('custom.tpl', 'Custom template 2');
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array())); $this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
} }
public function testCheckMTime() { public function testCheckMTime()
{
$this->fenom->setOptions(Fenom::FORCE_COMPILE); $this->fenom->setOptions(Fenom::FORCE_COMPILE);
$this->tpl('custom.tpl', 'Custom template'); $this->tpl('custom.tpl', 'Custom template');
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array())); $this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
@ -49,7 +54,8 @@ class FenomTest extends \Fenom\TestCase {
$this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array())); $this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array()));
} }
public function testForceCompile() { public function testForceCompile()
{
$this->fenom->setOptions(Fenom::FORCE_COMPILE); $this->fenom->setOptions(Fenom::FORCE_COMPILE);
$this->tpl('custom.tpl', 'Custom template'); $this->tpl('custom.tpl', 'Custom template');
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array())); $this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
@ -57,7 +63,8 @@ class FenomTest extends \Fenom\TestCase {
$this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array())); $this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array()));
} }
public function testSetModifier() { public function testSetModifier()
{
$this->fenom->addModifier("mymod", "myMod"); $this->fenom->addModifier("mymod", "myMod");
$this->tpl('custom.tpl', 'Custom modifier {$a|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")));
@ -66,7 +73,8 @@ class FenomTest extends \Fenom\TestCase {
/** /**
* @group add_functions * @group add_functions
*/ */
public function testSetFunctions() { public function testSetFunctions()
{
$this->fenom->setOptions(Fenom::FORCE_COMPILE); $this->fenom->setOptions(Fenom::FORCE_COMPILE);
$this->fenom->addFunction("myfunc", "myFunc"); $this->fenom->addFunction("myfunc", "myFunc");
$this->fenom->addBlockFunction("myblockfunc", "myBlockFunc"); $this->fenom->addBlockFunction("myblockfunc", "myBlockFunc");
@ -76,22 +84,24 @@ class FenomTest extends \Fenom\TestCase {
$this->assertSame("Custom function Block:foo:this block1:Block", $this->fenom->fetch('custom.tpl', array())); $this->assertSame("Custom function Block:foo:this block1:Block", $this->fenom->fetch('custom.tpl', array()));
} }
public function testSetCompilers() { public function testSetCompilers()
{
$this->fenom->setOptions(Fenom::FORCE_COMPILE); $this->fenom->setOptions(Fenom::FORCE_COMPILE);
$this->fenom->addCompiler("mycompiler", 'myCompiler'); $this->fenom->addCompiler("mycompiler", 'myCompiler');
$this->fenom->addBlockCompiler("myblockcompiler", 'myBlockCompilerOpen', 'myBlockCompilerClose', array( $this->fenom->addBlockCompiler("myblockcompiler", 'myBlockCompilerOpen', 'myBlockCompilerClose', array(
'tag' => 'myBlockCompilerTag' 'tag' => 'myBlockCompilerTag'
)); ));
$this->tpl('custom.tpl', 'Custom compiler {mycompiler name="bar"}'); $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->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->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) block1 Tag baz of compiler block2 End of compiler", $this->fenom->fetch('custom.tpl', array()));
} }
/** /**
* @dataProvider providerOptions * @dataProvider providerOptions
*/ */
public function testOptions($code, $option) { public function testOptions($code, $option)
{
static $options = array(); static $options = array();
static $flags = 0; static $flags = 0;
$options[$code] = true; $options[$code] = true;
@ -104,4 +114,25 @@ class FenomTest extends \Fenom\TestCase {
// printf("remove %010b from option %010b, flags %010b\n", $option, $this->fenom->getOptions(), $flags & ~$option); // printf("remove %010b from option %010b, flags %010b\n", $option, $this->fenom->getOptions(), $flags & ~$option);
// $this->assertSame($this->fenom->getOptions(), $flags & ~$option); // $this->assertSame($this->fenom->getOptions(), $flags & ~$option);
} }
public function testFilter()
{
$punit = $this;
$this->fenom->addPreFilter(function ($src, $tpl) use ($punit) {
$this->assertInstanceOf('Fenom\Template', $tpl);
return "== $src ==";
});
$this->fenom->addPostFilter(function ($code, $tpl) use ($punit) {
$this->assertInstanceOf('Fenom\Template', $tpl);
return "+++ $code +++";
});
$this->fenom->addFilter(function ($text, $tpl) use ($punit) {
$this->assertInstanceOf('Fenom\Template', $tpl);
return "|--- $text ---|";
});
$this->assertSame('+++ |--- == hello ---||--- world == ---| +++', $this->fenom->compileCode('hello {var $user} god {/var} world')->fetch(array()));
}
} }

View File

@ -1,31 +1,38 @@
<?php <?php
function myMod($str) { function myMod($str)
return "(myMod)".$str."(/myMod)"; {
return "(myMod)" . $str . "(/myMod)";
} }
function myFunc($params) { function myFunc($params)
return "MyFunc:".$params["name"]; {
return "MyFunc:" . $params["name"];
} }
function myBlockFunc($params, $content) { function myBlockFunc($params, $content)
return "Block:".$params["name"].':'.trim($content).':Block'; {
return "Block:" . $params["name"] . ':' . trim($content) . ':Block';
} }
function myCompiler(Fenom\Tokenizer $tokenizer, Fenom\Template $tpl) { function myCompiler(Fenom\Tokenizer $tokenizer, Fenom\Template $tpl)
{
$p = $tpl->parseParams($tokenizer); $p = $tpl->parseParams($tokenizer);
return 'echo "PHP_VERSION: ".PHP_VERSION." (for ".'.$p["name"].'.")";'; return 'echo "PHP_VERSION: ".PHP_VERSION." (for ".' . $p["name"] . '.")";';
} }
function myBlockCompilerOpen(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope) { function myBlockCompilerOpen(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope)
{
return myCompiler($tokenizer, $scope->tpl); return myCompiler($tokenizer, $scope->tpl);
} }
function myBlockCompilerClose(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope) { function myBlockCompilerClose(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope)
{
return 'echo "End of compiler";'; return 'echo "End of compiler";';
} }
function myBlockCompilerTag(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope) { function myBlockCompilerTag(Fenom\Tokenizer $tokenizer, Fenom\Scope $scope)
{
$p = $scope->tpl->parseParams($tokenizer); $p = $scope->tpl->parseParams($tokenizer);
return 'echo "Tag ".'.$p["name"].'." of compiler";'; return 'echo "Tag ".' . $p["name"] . '." of compiler";';
} }