mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
To PSR-x format
This commit is contained in:
310
src/Fenom.php
310
src/Fenom.php
@ -15,55 +15,56 @@ use Fenom\Template,
|
||||
*
|
||||
* @author Ivan Shalganov <a.cobest@gmail.com>
|
||||
*/
|
||||
class Fenom {
|
||||
class Fenom
|
||||
{
|
||||
const VERSION = '1.2';
|
||||
|
||||
/* Actions */
|
||||
const INLINE_COMPILER = 1;
|
||||
const BLOCK_COMPILER = 2;
|
||||
const INLINE_FUNCTION = 3;
|
||||
const BLOCK_FUNCTION = 4;
|
||||
const MODIFIER = 5;
|
||||
const INLINE_COMPILER = 1;
|
||||
const BLOCK_COMPILER = 2;
|
||||
const INLINE_FUNCTION = 3;
|
||||
const BLOCK_FUNCTION = 4;
|
||||
const MODIFIER = 5;
|
||||
|
||||
/* Options */
|
||||
const DENY_METHODS = 0x10;
|
||||
const DENY_NATIVE_FUNCS = 0x20;
|
||||
const FORCE_INCLUDE = 0x40;
|
||||
const AUTO_RELOAD = 0x80;
|
||||
const FORCE_COMPILE = 0x100;
|
||||
const AUTO_ESCAPE = 0x200;
|
||||
const DISABLE_CACHE = 0x400;
|
||||
const FORCE_VERIFY = 0x800; // reserved
|
||||
const AUTO_TRIM = 0x1000; // reserved
|
||||
const DENY_STATICS = 0x2000; // reserved
|
||||
const DENY_METHODS = 0x10;
|
||||
const DENY_NATIVE_FUNCS = 0x20;
|
||||
const FORCE_INCLUDE = 0x40;
|
||||
const AUTO_RELOAD = 0x80;
|
||||
const FORCE_COMPILE = 0x100;
|
||||
const AUTO_ESCAPE = 0x200;
|
||||
const DISABLE_CACHE = 0x400;
|
||||
const FORCE_VERIFY = 0x800; // reserved
|
||||
const AUTO_TRIM = 0x1000; // reserved
|
||||
const DENY_STATICS = 0x2000; // reserved
|
||||
|
||||
/* @deprecated */
|
||||
const DENY_INLINE_FUNCS = 0x20;
|
||||
const DENY_INLINE_FUNCS = 0x20;
|
||||
|
||||
/* Default parsers */
|
||||
const DEFAULT_CLOSE_COMPILER = 'Fenom\Compiler::stdClose';
|
||||
const DEFAULT_FUNC_PARSER = 'Fenom\Compiler::stdFuncParser';
|
||||
const DEFAULT_FUNC_OPEN = 'Fenom\Compiler::stdFuncOpen';
|
||||
const DEFAULT_FUNC_CLOSE = 'Fenom\Compiler::stdFuncClose';
|
||||
const SMART_FUNC_PARSER = 'Fenom\Compiler::smartFuncParser';
|
||||
const DEFAULT_FUNC_PARSER = 'Fenom\Compiler::stdFuncParser';
|
||||
const DEFAULT_FUNC_OPEN = 'Fenom\Compiler::stdFuncOpen';
|
||||
const DEFAULT_FUNC_CLOSE = 'Fenom\Compiler::stdFuncClose';
|
||||
const SMART_FUNC_PARSER = 'Fenom\Compiler::smartFuncParser';
|
||||
|
||||
const MAX_MACRO_RECURSIVE = 32;
|
||||
const MAX_MACRO_RECURSIVE = 32;
|
||||
|
||||
/**
|
||||
* @var int[] of possible options, as associative array
|
||||
* @see setOptions
|
||||
*/
|
||||
private static $_options_list = array(
|
||||
"disable_methods" => self::DENY_METHODS,
|
||||
"disable_methods" => self::DENY_METHODS,
|
||||
"disable_native_funcs" => self::DENY_NATIVE_FUNCS,
|
||||
"disable_cache" => self::DISABLE_CACHE,
|
||||
"force_compile" => self::FORCE_COMPILE,
|
||||
"auto_reload" => self::AUTO_RELOAD,
|
||||
"force_include" => self::FORCE_INCLUDE,
|
||||
"auto_escape" => self::AUTO_ESCAPE,
|
||||
"force_verify" => self::FORCE_VERIFY,
|
||||
"auto_trim" => self::AUTO_TRIM,
|
||||
"disable_statics" => self::DENY_STATICS,
|
||||
"disable_cache" => self::DISABLE_CACHE,
|
||||
"force_compile" => self::FORCE_COMPILE,
|
||||
"auto_reload" => self::AUTO_RELOAD,
|
||||
"force_include" => self::FORCE_INCLUDE,
|
||||
"auto_escape" => self::AUTO_ESCAPE,
|
||||
"force_verify" => self::FORCE_VERIFY,
|
||||
"auto_trim" => self::AUTO_TRIM,
|
||||
"disable_statics" => self::DENY_STATICS,
|
||||
);
|
||||
|
||||
/**
|
||||
@ -97,19 +98,19 @@ class Fenom {
|
||||
* @var string[] list of modifiers [modifier_name => callable]
|
||||
*/
|
||||
protected $_modifiers = array(
|
||||
"upper" => 'strtoupper',
|
||||
"up" => 'strtoupper',
|
||||
"lower" => 'strtolower',
|
||||
"low" => 'strtolower',
|
||||
"upper" => 'strtoupper',
|
||||
"up" => 'strtoupper',
|
||||
"lower" => 'strtolower',
|
||||
"low" => 'strtolower',
|
||||
"date_format" => 'Fenom\Modifier::dateFormat',
|
||||
"date" => 'Fenom\Modifier::date',
|
||||
"truncate" => 'Fenom\Modifier::truncate',
|
||||
"escape" => 'Fenom\Modifier::escape',
|
||||
"e" => 'Fenom\Modifier::escape', // alias of escape
|
||||
"unescape" => 'Fenom\Modifier::unescape',
|
||||
"strip" => 'Fenom\Modifier::strip',
|
||||
"length" => 'Fenom\Modifier::length',
|
||||
"iterable" => 'Fenom\Modifier::isIterable'
|
||||
"date" => 'Fenom\Modifier::date',
|
||||
"truncate" => 'Fenom\Modifier::truncate',
|
||||
"escape" => 'Fenom\Modifier::escape',
|
||||
"e" => 'Fenom\Modifier::escape', // alias of escape
|
||||
"unescape" => 'Fenom\Modifier::unescape',
|
||||
"strip" => 'Fenom\Modifier::strip',
|
||||
"length" => 'Fenom\Modifier::length',
|
||||
"iterable" => 'Fenom\Modifier::isIterable'
|
||||
);
|
||||
|
||||
/**
|
||||
@ -136,7 +137,7 @@ class Fenom {
|
||||
),
|
||||
'float_tags' => array('break' => 1, 'continue' => 1)
|
||||
),
|
||||
'if' => array( // {if ...} {elseif ...} {else} {/if}
|
||||
'if' => array( // {if ...} {elseif ...} {else} {/if}
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
'open' => 'Fenom\Compiler::ifOpen',
|
||||
'close' => 'Fenom\Compiler::stdClose',
|
||||
@ -145,7 +146,7 @@ class Fenom {
|
||||
'else' => 'Fenom\Compiler::tagElse',
|
||||
)
|
||||
),
|
||||
'switch' => array( // {switch ...} {case ...} {break} {default} {/switch}
|
||||
'switch' => array( // {switch ...} {case ...} {break} {default} {/switch}
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
'open' => 'Fenom\Compiler::switchOpen',
|
||||
'close' => 'Fenom\Compiler::stdClose',
|
||||
@ -156,7 +157,7 @@ class Fenom {
|
||||
),
|
||||
'float_tags' => array('break' => 1)
|
||||
),
|
||||
'for' => array( // {for ...} {break} {continue} {/for}
|
||||
'for' => array( // {for ...} {break} {continue} {/for}
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
'open' => 'Fenom\Compiler::forOpen',
|
||||
'close' => 'Fenom\Compiler::forClose',
|
||||
@ -167,7 +168,7 @@ class Fenom {
|
||||
),
|
||||
'float_tags' => array('break' => 1, 'continue' => 1)
|
||||
),
|
||||
'while' => array( // {while ...} {break} {continue} {/while}
|
||||
'while' => array( // {while ...} {break} {continue} {/while}
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
'open' => 'Fenom\Compiler::whileOpen',
|
||||
'close' => 'Fenom\Compiler::stdClose',
|
||||
@ -181,12 +182,12 @@ class Fenom {
|
||||
'type' => self::INLINE_COMPILER,
|
||||
'parser' => 'Fenom\Compiler::tagInclude'
|
||||
),
|
||||
'var' => array( // {var ...}
|
||||
'var' => array( // {var ...}
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
'open' => 'Fenom\Compiler::varOpen',
|
||||
'close' => 'Fenom\Compiler::varClose'
|
||||
),
|
||||
'block' => array( // {block ...} {parent} {/block}
|
||||
'block' => array( // {block ...} {parent} {/block}
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
'open' => 'Fenom\Compiler::tagBlockOpen',
|
||||
'close' => 'Fenom\Compiler::tagBlockClose',
|
||||
@ -226,7 +227,7 @@ class Fenom {
|
||||
'type' => self::INLINE_COMPILER,
|
||||
'parser' => 'Fenom\Compiler::tagCycle'
|
||||
),
|
||||
'raw' => array(
|
||||
'raw' => array(
|
||||
'type' => self::INLINE_COMPILER,
|
||||
'parser' => 'Fenom\Compiler::tagRaw'
|
||||
),
|
||||
@ -250,10 +251,11 @@ class Fenom {
|
||||
* @throws InvalidArgumentException
|
||||
* @return Fenom
|
||||
*/
|
||||
public static function factory($source, $compile_dir = '/tmp', $options = 0) {
|
||||
if(is_string($source)) {
|
||||
public static function factory($source, $compile_dir = '/tmp', $options = 0)
|
||||
{
|
||||
if (is_string($source)) {
|
||||
$provider = new Fenom\Provider($source);
|
||||
} elseif($source instanceof ProviderInterface) {
|
||||
} elseif ($source instanceof ProviderInterface) {
|
||||
$provider = $source;
|
||||
} else {
|
||||
throw new InvalidArgumentException("Source must be a valid path or provider object");
|
||||
@ -261,7 +263,7 @@ class Fenom {
|
||||
$fenom = new static($provider);
|
||||
/* @var Fenom $fenom */
|
||||
$fenom->setCompileDir($compile_dir);
|
||||
if($options) {
|
||||
if ($options) {
|
||||
$fenom->setOptions($options);
|
||||
}
|
||||
return $fenom;
|
||||
@ -270,7 +272,8 @@ class Fenom {
|
||||
/**
|
||||
* @param Fenom\ProviderInterface $provider
|
||||
*/
|
||||
public function __construct(Fenom\ProviderInterface $provider) {
|
||||
public function __construct(Fenom\ProviderInterface $provider)
|
||||
{
|
||||
$this->_provider = $provider;
|
||||
}
|
||||
|
||||
@ -280,7 +283,8 @@ class Fenom {
|
||||
* @param string $dir directory to store compiled templates in
|
||||
* @return Fenom
|
||||
*/
|
||||
public function setCompileDir($dir) {
|
||||
public function setCompileDir($dir)
|
||||
{
|
||||
$this->_compile_dir = $dir;
|
||||
return $this;
|
||||
}
|
||||
@ -289,7 +293,8 @@ class Fenom {
|
||||
*
|
||||
* @param callable $cb
|
||||
*/
|
||||
public function addPreCompileFilter($cb) {
|
||||
public function addPreCompileFilter($cb)
|
||||
{
|
||||
$this->_on_pre_cmp[] = $cb;
|
||||
}
|
||||
|
||||
@ -297,14 +302,16 @@ class Fenom {
|
||||
*
|
||||
* @param callable $cb
|
||||
*/
|
||||
public function addPostCompileFilter($cb) {
|
||||
public function addPostCompileFilter($cb)
|
||||
{
|
||||
$this->_on_post_cmp[] = $cb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable $cb
|
||||
*/
|
||||
public function addCompileFilter($cb) {
|
||||
public function addCompileFilter($cb)
|
||||
{
|
||||
$this->_on_cmp[] = $cb;
|
||||
}
|
||||
|
||||
@ -315,7 +322,8 @@ class Fenom {
|
||||
* @param string $callback the modifier callback
|
||||
* @return Fenom
|
||||
*/
|
||||
public function addModifier($modifier, $callback) {
|
||||
public function addModifier($modifier, $callback)
|
||||
{
|
||||
$this->_modifiers[$modifier] = $callback;
|
||||
return $this;
|
||||
}
|
||||
@ -327,7 +335,8 @@ class Fenom {
|
||||
* @param callable $parser
|
||||
* @return Fenom
|
||||
*/
|
||||
public function addCompiler($compiler, $parser) {
|
||||
public function addCompiler($compiler, $parser)
|
||||
{
|
||||
$this->_actions[$compiler] = array(
|
||||
'type' => self::INLINE_COMPILER,
|
||||
'parser' => $parser
|
||||
@ -340,11 +349,12 @@ class Fenom {
|
||||
* @param string|object $storage
|
||||
* @return $this
|
||||
*/
|
||||
public function addCompilerSmart($compiler, $storage) {
|
||||
if(method_exists($storage, "tag".$compiler)) {
|
||||
public function addCompilerSmart($compiler, $storage)
|
||||
{
|
||||
if (method_exists($storage, "tag" . $compiler)) {
|
||||
$this->_actions[$compiler] = array(
|
||||
'type' => self::INLINE_COMPILER,
|
||||
'parser' => array($storage, "tag".$compiler)
|
||||
'parser' => array($storage, "tag" . $compiler)
|
||||
);
|
||||
}
|
||||
return $this;
|
||||
@ -359,11 +369,12 @@ class Fenom {
|
||||
* @param array $tags
|
||||
* @return Fenom
|
||||
*/
|
||||
public function addBlockCompiler($compiler, $open_parser, $close_parser = self::DEFAULT_CLOSE_COMPILER, array $tags = array()) {
|
||||
public function addBlockCompiler($compiler, $open_parser, $close_parser = self::DEFAULT_CLOSE_COMPILER, array $tags = array())
|
||||
{
|
||||
$this->_actions[$compiler] = array(
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
'open' => $open_parser,
|
||||
'close' => $close_parser ?: self::DEFAULT_CLOSE_COMPILER,
|
||||
'close' => $close_parser ? : self::DEFAULT_CLOSE_COMPILER,
|
||||
'tags' => $tags,
|
||||
);
|
||||
return $this;
|
||||
@ -377,27 +388,28 @@ class Fenom {
|
||||
* @throws LogicException
|
||||
* @return Fenom
|
||||
*/
|
||||
public function addBlockCompilerSmart($compiler, $storage, array $tags, array $floats = array()) {
|
||||
public function addBlockCompilerSmart($compiler, $storage, array $tags, array $floats = array())
|
||||
{
|
||||
$c = array(
|
||||
'type' => self::BLOCK_COMPILER,
|
||||
"tags" => array(),
|
||||
"float_tags" => array()
|
||||
);
|
||||
if(method_exists($storage, $compiler."Open")) {
|
||||
$c["open"] = $compiler."Open";
|
||||
if (method_exists($storage, $compiler . "Open")) {
|
||||
$c["open"] = $compiler . "Open";
|
||||
} else {
|
||||
throw new \LogicException("Open compiler {$compiler}Open not found");
|
||||
}
|
||||
if(method_exists($storage, $compiler."Close")) {
|
||||
$c["close"] = $compiler."Close";
|
||||
if (method_exists($storage, $compiler . "Close")) {
|
||||
$c["close"] = $compiler . "Close";
|
||||
} else {
|
||||
throw new \LogicException("Close compiler {$compiler}Close not found");
|
||||
}
|
||||
foreach($tags as $tag) {
|
||||
if(method_exists($storage, "tag".$tag)) {
|
||||
$c["tags"][ $tag ] = "tag".$tag;
|
||||
if($floats && in_array($tag, $floats)) {
|
||||
$c['float_tags'][ $tag ] = 1;
|
||||
foreach ($tags as $tag) {
|
||||
if (method_exists($storage, "tag" . $tag)) {
|
||||
$c["tags"][$tag] = "tag" . $tag;
|
||||
if ($floats && in_array($tag, $floats)) {
|
||||
$c['float_tags'][$tag] = 1;
|
||||
}
|
||||
} else {
|
||||
throw new \LogicException("Tag compiler $tag (tag{$compiler}) not found");
|
||||
@ -413,7 +425,8 @@ class Fenom {
|
||||
* @param callable|string $parser
|
||||
* @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(
|
||||
'type' => self::INLINE_FUNCTION,
|
||||
'parser' => $parser,
|
||||
@ -427,7 +440,8 @@ class Fenom {
|
||||
* @param callable $callback
|
||||
* @return Fenom
|
||||
*/
|
||||
public function addFunctionSmart($function, $callback) {
|
||||
public function addFunctionSmart($function, $callback)
|
||||
{
|
||||
$this->_actions[$function] = array(
|
||||
'type' => self::INLINE_FUNCTION,
|
||||
'parser' => self::SMART_FUNC_PARSER,
|
||||
@ -443,12 +457,13 @@ class Fenom {
|
||||
* @param callable|string $parser_close
|
||||
* @return Fenom
|
||||
*/
|
||||
public function addBlockFunction($function, $callback, $parser_open = self::DEFAULT_FUNC_OPEN, $parser_close = self::DEFAULT_FUNC_CLOSE) {
|
||||
public function addBlockFunction($function, $callback, $parser_open = self::DEFAULT_FUNC_OPEN, $parser_close = self::DEFAULT_FUNC_CLOSE)
|
||||
{
|
||||
$this->_actions[$function] = array(
|
||||
'type' => self::BLOCK_FUNCTION,
|
||||
'open' => $parser_open,
|
||||
'close' => $parser_close,
|
||||
'function' => $callback,
|
||||
'type' => self::BLOCK_FUNCTION,
|
||||
'open' => $parser_open,
|
||||
'close' => $parser_close,
|
||||
'function' => $callback,
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
@ -457,7 +472,8 @@ class Fenom {
|
||||
* @param array $funcs
|
||||
* @return Fenom
|
||||
*/
|
||||
public function addAllowedFunctions(array $funcs) {
|
||||
public function addAllowedFunctions(array $funcs)
|
||||
{
|
||||
$this->_allowed_funcs = $this->_allowed_funcs + array_flip($funcs);
|
||||
return $this;
|
||||
}
|
||||
@ -469,10 +485,11 @@ class Fenom {
|
||||
* @param Fenom\Template $template
|
||||
* @return mixed
|
||||
*/
|
||||
public function getModifier($modifier, Template $template = null) {
|
||||
if(isset($this->_modifiers[$modifier])) {
|
||||
public function getModifier($modifier, Template $template = null)
|
||||
{
|
||||
if (isset($this->_modifiers[$modifier])) {
|
||||
return $this->_modifiers[$modifier];
|
||||
} elseif($this->isAllowedFunction($modifier)) {
|
||||
} elseif ($this->isAllowedFunction($modifier)) {
|
||||
return $modifier;
|
||||
} else {
|
||||
return $this->_loadModifier($modifier, $template);
|
||||
@ -484,7 +501,8 @@ class Fenom {
|
||||
* @param Fenom\Template $template
|
||||
* @return bool
|
||||
*/
|
||||
protected function _loadModifier($modifier, $template) {
|
||||
protected function _loadModifier($modifier, $template)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -494,7 +512,8 @@ class Fenom {
|
||||
* @return bool|string
|
||||
* @deprecated
|
||||
*/
|
||||
public function getFunction($function, Template $template = null) {
|
||||
public function getFunction($function, Template $template = null)
|
||||
{
|
||||
return $this->getTag($function, $template);
|
||||
}
|
||||
|
||||
@ -505,8 +524,9 @@ class Fenom {
|
||||
* @param Fenom\Template $template
|
||||
* @return string|bool
|
||||
*/
|
||||
public function getTag($tag, Template $template = null) {
|
||||
if(isset($this->_actions[$tag])) {
|
||||
public function getTag($tag, Template $template = null)
|
||||
{
|
||||
if (isset($this->_actions[$tag])) {
|
||||
return $this->_actions[$tag];
|
||||
} else {
|
||||
return $this->_loadTag($tag, $template);
|
||||
@ -518,7 +538,8 @@ class Fenom {
|
||||
* @param Fenom\Template $template
|
||||
* @return bool
|
||||
*/
|
||||
protected function _loadTag($tag, $template) {
|
||||
protected function _loadTag($tag, $template)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -526,8 +547,9 @@ class Fenom {
|
||||
* @param string $function
|
||||
* @return bool
|
||||
*/
|
||||
public function isAllowedFunction($function) {
|
||||
if($this->_options & self::DENY_NATIVE_FUNCS) {
|
||||
public function isAllowedFunction($function)
|
||||
{
|
||||
if ($this->_options & self::DENY_NATIVE_FUNCS) {
|
||||
return isset($this->_allowed_funcs[$function]);
|
||||
} else {
|
||||
return is_callable($function);
|
||||
@ -538,10 +560,11 @@ class Fenom {
|
||||
* @param string $tag
|
||||
* @return array
|
||||
*/
|
||||
public function getTagOwners($tag) {
|
||||
public function getTagOwners($tag)
|
||||
{
|
||||
$tags = array();
|
||||
foreach($this->_actions as $owner => $params) {
|
||||
if(isset($params["tags"][$tag])) {
|
||||
foreach ($this->_actions as $owner => $params) {
|
||||
if (isset($params["tags"][$tag])) {
|
||||
$tags[] = $owner;
|
||||
}
|
||||
}
|
||||
@ -554,7 +577,8 @@ class Fenom {
|
||||
* @param string $scm scheme name
|
||||
* @param Fenom\ProviderInterface $provider provider object
|
||||
*/
|
||||
public function addProvider($scm, \Fenom\ProviderInterface $provider) {
|
||||
public function addProvider($scm, \Fenom\ProviderInterface $provider)
|
||||
{
|
||||
$this->_providers[$scm] = $provider;
|
||||
}
|
||||
|
||||
@ -562,8 +586,9 @@ class Fenom {
|
||||
* Set options
|
||||
* @param int|array $options
|
||||
*/
|
||||
public function setOptions($options) {
|
||||
if(is_array($options)) {
|
||||
public function setOptions($options)
|
||||
{
|
||||
if (is_array($options)) {
|
||||
$options = self::_makeMask($options, self::$_options_list, $this->_options);
|
||||
}
|
||||
$this->_storage = array();
|
||||
@ -574,7 +599,8 @@ class Fenom {
|
||||
* Get options as bits
|
||||
* @return int
|
||||
*/
|
||||
public function getOptions() {
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->_options;
|
||||
}
|
||||
|
||||
@ -583,9 +609,10 @@ class Fenom {
|
||||
* @return Fenom\ProviderInterface
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getProvider($scm = false) {
|
||||
if($scm) {
|
||||
if(isset($this->_providers[$scm])) {
|
||||
public function getProvider($scm = false)
|
||||
{
|
||||
if ($scm) {
|
||||
if (isset($this->_providers[$scm])) {
|
||||
return $this->_providers[$scm];
|
||||
} else {
|
||||
throw new InvalidArgumentException("Provider for '$scm' not found");
|
||||
@ -600,7 +627,8 @@ class Fenom {
|
||||
*
|
||||
* @return Fenom\Template
|
||||
*/
|
||||
public function getRawTemplate() {
|
||||
public function getRawTemplate()
|
||||
{
|
||||
return new Template($this, $this->_options);
|
||||
}
|
||||
|
||||
@ -611,7 +639,8 @@ class Fenom {
|
||||
* @param array $vars array of data for template
|
||||
* @return Fenom\Render
|
||||
*/
|
||||
public function display($template, array $vars = array()) {
|
||||
public function display($template, array $vars = array())
|
||||
{
|
||||
return $this->getTemplate($template)->display($vars);
|
||||
}
|
||||
|
||||
@ -621,7 +650,8 @@ class Fenom {
|
||||
* @param array $vars array of data for template
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetch($template, array $vars = array()) {
|
||||
public function fetch($template, array $vars = array())
|
||||
{
|
||||
return $this->getTemplate($template)->fetch($vars);
|
||||
}
|
||||
|
||||
@ -634,7 +664,8 @@ class Fenom {
|
||||
* @param float $chunk
|
||||
* @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);
|
||||
$data = $this->getTemplate($template)->display($vars);
|
||||
ob_end_flush();
|
||||
@ -648,21 +679,22 @@ class Fenom {
|
||||
* @param int $options additional options and flags
|
||||
* @return Fenom\Template
|
||||
*/
|
||||
public function getTemplate($template, $options = 0) {
|
||||
public function getTemplate($template, $options = 0)
|
||||
{
|
||||
$options |= $this->_options;
|
||||
$key = dechex($options)."@".$template;
|
||||
if(isset($this->_storage[ $key ])) {
|
||||
/** @var Fenom\Template $tpl */
|
||||
$tpl = $this->_storage[ $key ];
|
||||
if(($this->_options & self::AUTO_RELOAD) && !$tpl->isValid()) {
|
||||
return $this->_storage[ $key ] = $this->compile($template, true, $options);
|
||||
$key = dechex($options) . "@" . $template;
|
||||
if (isset($this->_storage[$key])) {
|
||||
/** @var Fenom\Template $tpl */
|
||||
$tpl = $this->_storage[$key];
|
||||
if (($this->_options & self::AUTO_RELOAD) && !$tpl->isValid()) {
|
||||
return $this->_storage[$key] = $this->compile($template, true, $options);
|
||||
} else {
|
||||
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);
|
||||
} else {
|
||||
return $this->_storage[ $key ] = $this->_load($template, $options);
|
||||
return $this->_storage[$key] = $this->_load($template, $options);
|
||||
}
|
||||
}
|
||||
|
||||
@ -671,9 +703,10 @@ class Fenom {
|
||||
* @param string $template
|
||||
* @return bool
|
||||
*/
|
||||
public function templateExists($template) {
|
||||
if($provider = strstr($template, ":", true)) {
|
||||
if(isset($this->_providers[$provider])) {
|
||||
public function templateExists($template)
|
||||
{
|
||||
if ($provider = strstr($template, ":", true)) {
|
||||
if (isset($this->_providers[$provider])) {
|
||||
return $this->_providers[$provider]->templateExists(substr($template, strlen($provider) + 1));
|
||||
}
|
||||
} else {
|
||||
@ -689,13 +722,14 @@ class Fenom {
|
||||
* @param int $opts
|
||||
* @return Fenom\Render
|
||||
*/
|
||||
protected function _load($tpl, $opts) {
|
||||
protected function _load($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);
|
||||
} else {
|
||||
$fenom = $this;
|
||||
return include($this->_compile_dir."/".$file_name);
|
||||
return include($this->_compile_dir . "/" . $file_name);
|
||||
}
|
||||
}
|
||||
|
||||
@ -706,8 +740,9 @@ class Fenom {
|
||||
* @param int $options
|
||||
* @return string
|
||||
*/
|
||||
private function _getCacheName($tpl, $options) {
|
||||
$hash = $tpl.":".$options;
|
||||
private function _getCacheName($tpl, $options)
|
||||
{
|
||||
$hash = $tpl . ":" . $options;
|
||||
return sprintf("%s.%x.%x.php", str_replace(":", "_", basename($tpl)), crc32($hash), strlen($hash));
|
||||
}
|
||||
|
||||
@ -720,20 +755,21 @@ class Fenom {
|
||||
* @throws RuntimeException
|
||||
* @return \Fenom\Template
|
||||
*/
|
||||
public function compile($tpl, $store = true, $options = 0) {
|
||||
public function compile($tpl, $store = true, $options = 0)
|
||||
{
|
||||
$options = $this->_options | $options;
|
||||
$template = $this->getRawTemplate()->load($tpl);
|
||||
if($store) {
|
||||
if ($store) {
|
||||
$cache = $this->_getCacheName($tpl, $options);
|
||||
$tpl_tmp = tempnam($this->_compile_dir, $cache);
|
||||
$tpl_fp = fopen($tpl_tmp, "w");
|
||||
if(!$tpl_fp) {
|
||||
throw new \RuntimeException("Can't to open temporary file $tpl_tmp. Directory ".$this->_compile_dir." is writable?");
|
||||
if (!$tpl_fp) {
|
||||
throw new \RuntimeException("Can't to open temporary file $tpl_tmp. Directory " . $this->_compile_dir . " is writable?");
|
||||
}
|
||||
fwrite($tpl_fp, $template->getTemplateCode());
|
||||
fclose($tpl_fp);
|
||||
$file_name = $this->_compile_dir."/".$cache;
|
||||
if(!rename($tpl_tmp, $file_name)) {
|
||||
$file_name = $this->_compile_dir . "/" . $cache;
|
||||
if (!rename($tpl_tmp, $file_name)) {
|
||||
throw new \RuntimeException("Can't to move $tpl_tmp to $tpl");
|
||||
}
|
||||
}
|
||||
@ -743,14 +779,16 @@ class Fenom {
|
||||
/**
|
||||
* Flush internal memory template cache
|
||||
*/
|
||||
public function flush() {
|
||||
public function flush()
|
||||
{
|
||||
$this->_storage = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all compiled templates
|
||||
*/
|
||||
public function clearAllCompiles() {
|
||||
public function clearAllCompiles()
|
||||
{
|
||||
\Fenom\Provider::clean($this->_compile_dir);
|
||||
}
|
||||
|
||||
@ -761,7 +799,8 @@ class Fenom {
|
||||
* @param string $name
|
||||
* @return Fenom\Template
|
||||
*/
|
||||
public function compileCode($code, $name = 'Runtime compile') {
|
||||
public function compileCode($code, $name = 'Runtime compile')
|
||||
{
|
||||
return $this->getRawTemplate()->source($name, $code);
|
||||
}
|
||||
|
||||
@ -775,7 +814,8 @@ class Fenom {
|
||||
* @return int result, ( $mask | a ) & ~b
|
||||
* @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) {
|
||||
if (isset($options[$key])) {
|
||||
if ($options[$key]) {
|
||||
|
Reference in New Issue
Block a user