mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
commit
a3a84ea606
@ -10,7 +10,7 @@ php:
|
||||
- 7.0
|
||||
|
||||
before_script:
|
||||
- composer install --dev
|
||||
- composer update --dev
|
||||
|
||||
script:
|
||||
- phpunit
|
||||
|
@ -16,7 +16,7 @@
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "*",
|
||||
"satooshi/php-coveralls": "dev-master"
|
||||
"satooshi/php-coveralls": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": { "Fenom\\": "src/" },
|
||||
|
1036
composer.lock
generated
1036
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -7,6 +7,7 @@
|
||||
* For the full copyright and license information, please view the license.md
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
use Fenom\Error\CompileException;
|
||||
use Fenom\ProviderInterface;
|
||||
use Fenom\Template;
|
||||
|
||||
@ -844,16 +845,31 @@ class Fenom
|
||||
}
|
||||
|
||||
/**
|
||||
* Add global accessor ($.)
|
||||
* Add global accessor as PHP code ($.)
|
||||
* @param string $name
|
||||
* @param mixed $accessor
|
||||
* @param string $parser
|
||||
* @return Fenom
|
||||
*/
|
||||
public function addAccessorSmart($name, $accessor, $parser) {
|
||||
public function addAccessorSmart($name, $accessor, $parser = self::ACCESSOR_VAR)
|
||||
{
|
||||
$this->_accessors[$name] = array(
|
||||
"accessor" => $accessor,
|
||||
"parser" => $parser
|
||||
"parser" => $parser,
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add global accessor handler as callback ($.X)
|
||||
* @param string $name
|
||||
* @param callable $callback
|
||||
* @return Fenom
|
||||
*/
|
||||
public function addAccessorCallback($name, $callback)
|
||||
{
|
||||
$this->_accessors[$name] = array(
|
||||
"callback" => $callback
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
@ -872,11 +888,17 @@ class Fenom
|
||||
/**
|
||||
* Get an accessor
|
||||
* @param string $name
|
||||
* @param string $key
|
||||
* @return callable
|
||||
*/
|
||||
public function getAccessor($name) {
|
||||
public function getAccessor($name, $key = null)
|
||||
{
|
||||
if(isset($this->_accessors[$name])) {
|
||||
return $this->_accessors[$name];
|
||||
if($key) {
|
||||
return $this->_accessors[$name][$key];
|
||||
} else {
|
||||
return $this->_accessors[$name];
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
@ -888,7 +910,8 @@ class Fenom
|
||||
* @param string $pattern
|
||||
* @return $this
|
||||
*/
|
||||
public function addCallFilter($pattern) {
|
||||
public function addCallFilter($pattern)
|
||||
{
|
||||
$this->call_filters[] = $pattern;
|
||||
return $this;
|
||||
}
|
||||
@ -975,12 +998,7 @@ class Fenom
|
||||
{
|
||||
$options |= $this->_options;
|
||||
if (is_array($template)) {
|
||||
if(count($template) === 1) {
|
||||
$template = current($template);
|
||||
$key = $options . "@" . $template;
|
||||
} else {
|
||||
$key = $options . "@" . implode(",", $template);
|
||||
}
|
||||
$key = $options . "@" . implode(",", $template);
|
||||
} else {
|
||||
$key = $options . "@" . $template;
|
||||
}
|
||||
@ -1029,13 +1047,15 @@ class Fenom
|
||||
*/
|
||||
protected function _load($template, $opts)
|
||||
{
|
||||
$file_name = $this->_getCacheName($template, $opts);
|
||||
$file_name = $this->getCompileName($template, $opts);
|
||||
if (is_file($this->_compile_dir . "/" . $file_name)) {
|
||||
$fenom = $this; // used in template
|
||||
$_tpl = include($this->_compile_dir . "/" . $file_name);
|
||||
/* @var Fenom\Render $_tpl */
|
||||
|
||||
if (!($this->_options & self::AUTO_RELOAD) || ($this->_options & self::AUTO_RELOAD) && $_tpl->isValid()) {
|
||||
if (!($this->_options & self::AUTO_RELOAD) || ($this->_options & self::AUTO_RELOAD)
|
||||
&& $_tpl instanceof Fenom\Render
|
||||
&& $_tpl->isValid()) {
|
||||
return $_tpl;
|
||||
}
|
||||
}
|
||||
@ -1045,12 +1065,13 @@ class Fenom
|
||||
/**
|
||||
* Generate unique name of compiled template
|
||||
*
|
||||
* @param string $tpl
|
||||
* @param int $options
|
||||
* @param string|string[] $tpl
|
||||
* @param int $options additional options
|
||||
* @return string
|
||||
*/
|
||||
private function _getCacheName($tpl, $options)
|
||||
public function getCompileName($tpl, $options = 0)
|
||||
{
|
||||
$options = $this->_options | $options;
|
||||
if (is_array($tpl)) {
|
||||
$hash = implode(".", $tpl) . ":" . $options;
|
||||
foreach ($tpl as &$t) {
|
||||
@ -1069,12 +1090,11 @@ class Fenom
|
||||
* @param string|array $tpl
|
||||
* @param bool $store store template on disk
|
||||
* @param int $options
|
||||
* @throws RuntimeException
|
||||
* @throws CompileException
|
||||
* @return \Fenom\Template
|
||||
*/
|
||||
public function compile($tpl, $store = true, $options = 0)
|
||||
{
|
||||
$options = $this->_options | $options;
|
||||
if (is_string($tpl)) {
|
||||
$template = $this->getRawTemplate()->load($tpl);
|
||||
} else {
|
||||
@ -1084,17 +1104,15 @@ class Fenom
|
||||
}
|
||||
}
|
||||
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?");
|
||||
$cache_name = $this->getCompileName($tpl, $options);
|
||||
$compile_path = $this->_compile_dir . "/" . $cache_name . "." . mt_rand(0, 100000) . ".tmp";
|
||||
if(!file_put_contents($compile_path, $template->getTemplateCode())) {
|
||||
throw new CompileException("Can't to write to the file $compile_path. 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)) {
|
||||
throw new \RuntimeException("Can't to move $tpl_tmp to $file_name");
|
||||
$cache_path = $this->_compile_dir . "/" . $cache_name;
|
||||
if (!rename($compile_path, $cache_path)) {
|
||||
unlink($compile_path);
|
||||
throw new CompileException("Can't to move the file $compile_path -> $cache_path");
|
||||
}
|
||||
}
|
||||
return $template;
|
||||
|
@ -964,7 +964,7 @@ class Compiler
|
||||
return;
|
||||
}
|
||||
$tokens->next();
|
||||
if($tokens->is('(') || !$tokens->isNext(')')){
|
||||
if ($tokens->is('(') || !$tokens->isNext(')')) {
|
||||
$tokens->next();
|
||||
while ($tokens->is(Tokenizer::MACRO_STRING, T_VARIABLE)) {
|
||||
$param = $tokens->current();
|
||||
|
@ -102,7 +102,7 @@ class Template extends Render
|
||||
*/
|
||||
private $_ignore = false;
|
||||
|
||||
private $_before;
|
||||
private $_before = array();
|
||||
|
||||
private $_filters = array();
|
||||
|
||||
@ -310,7 +310,7 @@ class Template extends Render
|
||||
*/
|
||||
public function before($code)
|
||||
{
|
||||
$this->_before .= $code;
|
||||
$this->_before[] = $code;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -405,7 +405,7 @@ class Template extends Render
|
||||
*/
|
||||
public function getTemplateCode()
|
||||
{
|
||||
$before = $this->_before ? $this->_before . "\n" : "";
|
||||
$before = $this->_before ? implode("\n", $this->_before) . "\n" : "";
|
||||
return "<?php \n" .
|
||||
"/** Fenom template '" . $this->_name . "' compiled at " . date('Y-m-d H:i:s') . " */\n" .
|
||||
$before . // some code 'before' template
|
||||
@ -525,7 +525,7 @@ class Template extends Render
|
||||
$parent = $this->_fenom->getRawTemplate()->load($tpl, false);
|
||||
$parent->blocks = & $this->blocks;
|
||||
$parent->macros = & $this->macros;
|
||||
$parent->_before = & $this->_before;
|
||||
$parent->_before = & $this->_before;
|
||||
$parent->extended = $this->getName();
|
||||
if (!$this->ext_stack) {
|
||||
$this->ext_stack[] = $this->getName();
|
||||
@ -798,12 +798,14 @@ class Template extends Render
|
||||
}
|
||||
$code = $this->parseScalar($tokens);
|
||||
break;
|
||||
/** @noinspection PhpMissingBreakStatementInspection */
|
||||
case '$':
|
||||
$code = $this->parseAccessor($tokens, $is_var);
|
||||
if(!$is_var) {
|
||||
$code = $unary . $code;
|
||||
break;
|
||||
}
|
||||
/* no break */
|
||||
case T_VARIABLE:
|
||||
if(!isset($code)) {
|
||||
$code = $this->parseVariable($tokens);
|
||||
@ -1003,12 +1005,18 @@ class Template extends Render
|
||||
$is_var = false;
|
||||
if($parser) {
|
||||
if(is_array($parser)) {
|
||||
return call_user_func_array($parser['parser'], array($parser['accessor'], $tokens->next(), $this, &$is_var));
|
||||
if(isset($parser['callback'])) {
|
||||
$tokens->next();
|
||||
return 'call_user_func($tpl->getStorage()->getAccessor('.var_export($accessor, true).
|
||||
', "callback"), '.var_export($accessor, true).', $tpl, $var)';
|
||||
} else {
|
||||
return call_user_func_array($parser['parser'], array($parser['accessor'], $tokens->next(), $this, &$is_var));
|
||||
}
|
||||
} else {
|
||||
return call_user_func_array($parser, array($tokens->next(), $this, &$is_var));
|
||||
}
|
||||
} else {
|
||||
throw new \RuntimeException("Unknown accessor '$accessor'");
|
||||
throw new \RuntimeException("Unknown accessor '\$.$accessor'");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1025,7 +1033,7 @@ class Template extends Render
|
||||
{
|
||||
$empty = $tokens->is('?');
|
||||
$tokens->next();
|
||||
if ($tokens->is(":")) {
|
||||
if ($tokens->is(":", "?")) {
|
||||
$tokens->next();
|
||||
if ($empty) {
|
||||
if ($is_var) {
|
||||
|
@ -48,15 +48,20 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function getCompilePath()
|
||||
{
|
||||
return FENOM_RESOURCES . '/compile';
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
if (!file_exists(FENOM_RESOURCES . '/compile')) {
|
||||
mkdir(FENOM_RESOURCES . '/compile', 0777, true);
|
||||
if (!file_exists($this->getCompilePath())) {
|
||||
mkdir($this->getCompilePath(), 0777, true);
|
||||
} else {
|
||||
FS::clean(FENOM_RESOURCES . '/compile/');
|
||||
FS::clean($this->getCompilePath());
|
||||
}
|
||||
|
||||
$this->fenom = Fenom::factory(FENOM_RESOURCES . '/' . $this->template_path, FENOM_RESOURCES . '/compile');
|
||||
$this->fenom = Fenom::factory(FENOM_RESOURCES . '/' . $this->template_path, $this->getCompilePath());
|
||||
$this->fenom->addProvider('persist', new Provider(FENOM_RESOURCES . '/provider'));
|
||||
$this->fenom->addModifier('dots', __CLASS__ . '::dots');
|
||||
$this->fenom->addModifier('concat', __CLASS__ . '::concat');
|
||||
|
@ -256,4 +256,21 @@ class AccessorTest extends TestCase
|
||||
$this->fenom->addAccessorSmart($name, $accessor, $type);
|
||||
$this->assertRender($code, $result, $this->getVars());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group dev
|
||||
*/
|
||||
public function testCallbackAccessor() {
|
||||
$index = 1;
|
||||
$test = $this;
|
||||
$this->fenom->addAccessorCallback('index', function($name, $template, $vars) use (&$index, $test) {
|
||||
$test->assertInstanceOf('Fenom\Render', $template);
|
||||
$test->assertSame(1, $vars['one']);
|
||||
$test->assertSame('index', $name);
|
||||
|
||||
return $index++;
|
||||
});
|
||||
|
||||
$this->assertRender('{$.index}, {$.index}, {$.index}', '1, 2, 3', $this->getVars());
|
||||
}
|
||||
}
|
@ -116,6 +116,19 @@ class FenomTest extends \Fenom\TestCase
|
||||
$this->assertSame("Custom template (new)", $this->fenom->fetch('custom.tpl', array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group dev
|
||||
*/
|
||||
public function testCompileIdAndName()
|
||||
{
|
||||
$this->fenom->setCompileId("iddqd.");
|
||||
$this->tpl('custom.tpl', 'Custom template');
|
||||
$this->assertSame("Custom template", $this->fenom->fetch('custom.tpl', array()));
|
||||
$compile_name = $this->fenom->getCompileName('custom.tpl');
|
||||
$this->assertFileExists($this->getCompilePath().'/'.$compile_name);
|
||||
$this->assertStringStartsWith('iddqd.', $compile_name);
|
||||
}
|
||||
|
||||
public function testSetModifier()
|
||||
{
|
||||
$this->fenom->addModifier("mymod", "myMod");
|
||||
|
Loading…
Reference in New Issue
Block a user