This commit is contained in:
bzick 2013-08-29 11:29:34 +04:00
parent 270f90792a
commit eef42150b2
5 changed files with 23 additions and 33 deletions

View File

@ -1,6 +1,12 @@
CHANGELOG CHANGELOG
========= =========
## 1.3.1 (2013-08-29)
- Bug: accessor don't work in modifier
- Removed too many EOL in template code
- Tests++
## 1.3.0 (2013-08-23) ## 1.3.0 (2013-08-23)
- Feature #41: Add system variable `$`. - Feature #41: Add system variable `$`.

View File

@ -264,9 +264,6 @@ class Template extends Render
} }
gc_collect_cycles(); gc_collect_cycles();
// if($end < strlen($this->_src) && $this->_src[$end + 1] === "\n") {
// $end++;
// }
$this->_appendText(substr($this->_src, $end ? $end + 1 : 0)); // append tail of the template $this->_appendText(substr($this->_src, $end ? $end + 1 : 0)); // append tail of the template
if ($this->_stack) { if ($this->_stack) {
$_names = array(); $_names = array();
@ -340,26 +337,6 @@ class Template extends Render
} }
} }
/**
* Append PHP_EOL after each '?>'
* @param int $code
* @return string
*/
private function _escapeCode($code)
{
$c = "";
foreach (token_get_all($code) as $token) {
if (is_string($token)) {
$c .= $token;
} elseif ($token[0] == T_CLOSE_TAG) {
$c .= $token[1] . PHP_EOL;
} else {
$c .= $token[1];
}
}
return $c;
}
/** /**
* Append PHP code to template body * Append PHP code to template body
* *
@ -372,10 +349,7 @@ class Template extends Render
return; return;
} else { } else {
$this->_line += substr_count($source, "\n"); $this->_line += substr_count($source, "\n");
if (strpos($code, '?>') !== false) { $this->_body .= "<?php\n/* {$this->_name}:{$this->_line}: {$source} */\n $code ?>";
$code = $this->_escapeCode($code); // paste PHP_EOL
}
$this->_body .= "<?php\n/* {$this->_name}:{$this->_line}: {$source} */\n $code ?>" . PHP_EOL;
} }
} }
@ -1266,13 +1240,15 @@ class Template extends Render
$args = array(); $args = array();
while ($tokens->is(":")) { while ($tokens->is(":")) {
$token = $tokens->getNext(Tokenizer::MACRO_SCALAR, T_VARIABLE, '"', Tokenizer::MACRO_STRING, "(", "["); $token = $tokens->getNext(Tokenizer::MACRO_SCALAR, T_VARIABLE, '"', Tokenizer::MACRO_STRING, "(", "[", '$');
if ($tokens->is(Tokenizer::MACRO_SCALAR) || $tokens->isSpecialVal()) { if ($tokens->is(Tokenizer::MACRO_SCALAR) || $tokens->isSpecialVal()) {
$args[] = $token; $args[] = $token;
$tokens->next(); $tokens->next();
} elseif ($tokens->is(T_VARIABLE)) { } elseif ($tokens->is(T_VARIABLE)) {
$args[] = $this->parseVariable($tokens, self::DENY_MODS); $args[] = $this->parseVariable($tokens, self::DENY_MODS);
} elseif ($tokens->is('$')) {
$args[] = $this->parseAccessor($tokens, $is_var);
} elseif ($tokens->is('"', '`', T_ENCAPSED_AND_WHITESPACE)) { } elseif ($tokens->is('"', '`', T_ENCAPSED_AND_WHITESPACE)) {
$args[] = $this->parseQuote($tokens); $args[] = $this->parseQuote($tokens);
} elseif ($tokens->is('(')) { } elseif ($tokens->is('(')) {

View File

@ -56,6 +56,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
$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->addModifier('append', __CLASS__ . '::append');
$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');
} }
@ -70,6 +71,12 @@ class TestCase extends \PHPUnit_Framework_TestCase
return call_user_func_array('var_export', func_get_args()); return call_user_func_array('var_export', func_get_args());
} }
public static function append()
{
return implode("", 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"] : "";

View File

@ -9,7 +9,7 @@ class TagsTest extends TestCase
public function _testSandbox() public function _testSandbox()
{ {
try { try {
var_dump($this->fenom->compileCode('{var $a=Fenom\TestCase::dots("asd")}')->getBody()); var_dump($this->fenom->compileCode("{var \$a=12313}\nVar: {\$a}")->getBody());
} catch (\Exception $e) { } catch (\Exception $e) {
echo "$e"; echo "$e";
} }
@ -21,7 +21,7 @@ class TagsTest extends TestCase
*/ */
public function testVar($tpl_val, $val) public function testVar($tpl_val, $val)
{ {
$this->assertRender("{var \$a=$tpl_val}\nVar: {\$a}", "\nVar: " . $val); $this->assertRender("{var \$a=$tpl_val}\nVar: {\$a}", "Var: " . $val);
} }
/** /**
@ -29,7 +29,7 @@ class TagsTest extends TestCase
*/ */
public function testVarBlock($tpl_val, $val) public function testVarBlock($tpl_val, $val)
{ {
$this->assertRender("{var \$a}before {{$tpl_val}} after{/var}\nVar: {\$a}", "\nVar: before " . $val . " after"); $this->assertRender("{var \$a}before {{$tpl_val}} after{/var}\nVar: {\$a}", "Var: before " . $val . " after");
} }
/** /**
@ -37,7 +37,7 @@ class TagsTest extends TestCase
*/ */
public function testVarBlockModified($tpl_val, $val) public function testVarBlockModified($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|low|dots}before {{$tpl_val}} after{/var}\nVar: {\$a}", "Var: " . strtolower("before " . $val . " after") . "...");
} }
public function testCycle() public function testCycle()

View File

@ -708,6 +708,7 @@ class TemplateTest extends TestCase
array('{$.server.one}', 'server1'), array('{$.server.one}', 'server1'),
array('{$.const.PHP_EOL}', PHP_EOL), array('{$.const.PHP_EOL}', PHP_EOL),
array('{$.version}', Fenom::VERSION), array('{$.version}', Fenom::VERSION),
array('{"string"|append:"_":$.get.one}', 'string_get1'),
array('{$.get.one?}', '1'), array('{$.get.one?}', '1'),
array('{$.get.one is set}', '1'), array('{$.get.one is set}', '1'),
@ -718,7 +719,7 @@ class TemplateTest extends TestCase
public function _testSandbox() public function _testSandbox()
{ {
try { try {
var_dump($this->fenom->compileCode('{$.const.access?}')->getBody()); var_dump($this->fenom->compileCode('{"string"|append:$.get.one}')->getBody());
} catch (\Exception $e) { } catch (\Exception $e) {
print_r($e->getMessage() . "\n" . $e->getTraceAsString()); print_r($e->getMessage() . "\n" . $e->getTraceAsString());
} }