Merge pull request #85 from bzick/develop

Develop to Master
This commit is contained in:
Ivan Shalganov 2014-06-29 15:07:49 +04:00
commit e96004b166
7 changed files with 140 additions and 52 deletions

View File

@ -119,6 +119,7 @@ $fenom->addModifier('my_modifier', function ($variable, $param1, $param2) {
```php
$fenom->addTest($name, $code);
?>
```
# Add template provider
@ -160,4 +161,4 @@ $this->setCacheDir("redis://hash/compiled/");
* `$cache = fopen("redis://hash/compiled/XnsbfeDnrd.php", "w");`
* `fwrite($cache, "... <template content> ...");`
* `fclose($cache);`
* `rename("redis://hash/compiled/XnsbfeDnrd.php", "redis://hash/compiled/main.php");`
* `rename("redis://hash/compiled/XnsbfeDnrd.php", "redis://hash/compiled/main.php");`

View File

@ -32,6 +32,7 @@ Documentation
* [macro](./tags/macro.md) and `import` — template functions
* [autoescape](./tags/autoescape.md) — escape template fragment
* [raw](./tags/raw.md) — unescape template fragment
* [unset](./tags/unset.md) — unset a given variables
* or [add](./ext/extend.md#add-tags) yours

12
docs/tags/unset.md Normal file
View File

@ -0,0 +1,12 @@
Tag {unset}
===========
Unset a given variables.
```smarty
{unset $a} unset single variable
{unset $a $b $c.d.e} multiple unset
```

View File

@ -282,6 +282,10 @@ class Fenom
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::ignoreOpen',
'close' => 'Fenom\Compiler::nope'
),
'unset' => array(
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagUnset'
)
);

View File

@ -134,17 +134,23 @@ class Compiler
*/
public static function foreachOpen(Tokenizer $tokens, Tag $scope)
{
$p = array("index" => false, "first" => false, "last" => false);
$key = null;
$before = $body = array();
$p = array("index" => false, "first" => false, "last" => false);
$key = null;
$before = $body = array();
$prepend = "";
if ($tokens->is(T_VARIABLE)) {
$from = $scope->tpl->parseTerm($tokens);
$prepend = "";
$from = $scope->tpl->parseTerm($tokens, $is_var);
if($is_var) {
$check = '!empty('.$from.')';
} else {
$scope["var"] = $scope->tpl->tmpVar();
$prepend = $scope["var"].' = (array)('.$from.')';
$from = $check = $scope["var"];
}
} elseif ($tokens->is('[')) {
$from = $scope->tpl->parseArray($tokens);
$uid = '$v' . $scope->tpl->i++;
$prepend = $uid . ' = ' . $from . ';';
$from = $uid;
$count = 0;
$from = $scope->tpl->parseArray($tokens, $count);
$check = $count;
} else {
throw new UnexpectedTokenException($tokens, null, "tag {foreach}");
}
@ -189,9 +195,9 @@ class Compiler
$body = $body ? implode("; ", $body) . ";" : "";
$scope["after"] = $scope["after"] ? implode("; ", $scope["after"]) . ";" : "";
if ($key) {
return "$prepend if($from) { $before foreach($from as $key => $value) { $body";
return "$prepend if($check) { $before foreach($from as $key => $value) { $body";
} else {
return "$prepend if($from) { $before foreach($from as $value) { $body";
return "$prepend if($check) { $before foreach($from as $value) { $body";
}
}
@ -1001,4 +1007,19 @@ class Compiler
{
$tag->tpl->ignore('ignore');
}
/**
* Tag {unset ...}
* @param Tokenizer $tokens
* @param Tag $tag
* @return string
*/
public static function tagUnset(Tokenizer $tokens, Tag $tag)
{
$unset = array();
while($tokens->valid()) {
$unset[] = $tag->tpl->parseVariable($tokens);
}
return 'unset('.implode(", ", $unset).')';
}
}

View File

@ -133,7 +133,7 @@ class Template extends Render
/**
* @param string $tag
* @return bool|\Fenom\Scope
* @return bool|\Fenom\Tag
*/
public function getParentScope($tag)
{
@ -1274,50 +1274,35 @@ class Template extends Render
* [1, 2.3, 5+7/$var, 'string', "str {$var+3} ing", $var2, []]
*
* @param Tokenizer $tokens
* @throws UnexpectedTokenException
* @param int $count amount of elements
* @throws Error\UnexpectedTokenException
* @return string
*/
public function parseArray(Tokenizer $tokens)
public function parseArray(Tokenizer $tokens, &$count = 0)
{
if ($tokens->is("[")) {
$_arr = "array(";
$key = $val = false;
$arr = array();
$tokens->next();
while ($tokens->valid()) {
if ($tokens->is(',') && $val) {
$key = true;
$val = false;
$_arr .= $tokens->getAndNext() . ' ';
} elseif ($tokens->is(
Tokenizer::MACRO_SCALAR,
T_VARIABLE,
T_STRING,
T_EMPTY,
T_ISSET,
"(",
"#"
) && !$val
) {
$_arr .= $this->parseExpr($tokens);
$key = false;
$val = true;
} elseif ($tokens->is('"') && !$val) {
$_arr .= $this->parseQuote($tokens);
$key = false;
$val = true;
} elseif ($tokens->is(T_DOUBLE_ARROW) && $val) {
$_arr .= ' ' . $tokens->getAndNext() . ' ';
$key = true;
$val = false;
} elseif (!$val && $tokens->is('[')) {
$_arr .= $this->parseArray($tokens);
$key = false;
$val = true;
} elseif ($tokens->is(']') && (!$key || $tokens->prev[0] === ',')) {
if ($tokens->is(']')) {
$tokens->next();
return $_arr . ')';
return 'array(' . implode(', ', $arr) . ')';
}
if ($tokens->is('[')) {
$arr[] = $this->parseArray($tokens);
$count++;
} else {
break;
$expr = $this->parseExpr($tokens);
if($tokens->is(T_DOUBLE_ARROW)) {
$tokens->next();
$arr[] = $expr.' => '.$this->parseExpr($tokens);
} else {
$arr[] = $expr;
}
$count++;
}
if($tokens->is(',')) {
$tokens->next();
}
}
}

View File

@ -572,6 +572,38 @@ class TemplateTest extends TestCase
);
}
public static function providerArrays()
{
return array(
array('{var $arr = []}', array()),
array('{var $arr = [1]}', array(1)),
array('{var $arr = [1,]}', array(1)),
array('{var $arr = [1, 2, 3, 5]}', array(1, 2, 3, 5)),
array('{var $arr = [1, true, false, null, -1, 1.1, -2.2, 5, "str"]}', array(1, true, false, null, -1, 1.1, -2.2, 5, "str")),
array('{var $arr = [5 => 1, "two" => 2, 3]}', array(5 => 1, "two" => 2, 3)),
array('{var $arr = [1 + 1, 2 * 2, 3 / 3 + 7,]}', array(1 + 1, 2 * 2, 3 / 3 + 7)),
array('{var $arr = [$zero, $two => $one, $num.3 => $.const.PHP_VERSION]}', array(0, 2 => 1, "three" => PHP_VERSION)),
array('{var $arr = [5 - 1 => 1, "two"|up => "two"|low, 3 => count([1,2])]}', array(4 => 1, "TWO" => "two", 3 => 2)),
array('{var $arr = [[1]]}', array(array(1))),
array('{var $arr = [[],[]]}', array(array(),array())),
array('{var $arr = [1, [2, 3], 5]}', array(1, array(2, 3), 5)),
array('{var $arr = [1, [true, false, null, -1, 1.1, -2.2, 5], "str"]}', array(1, array(true, false, null, -1, 1.1, -2.2, 5), "str")),
array('{var $arr = [5 => [1, "two" => 2], 3]}', array(5 => array(1, "two" => 2), 3)),
array('{var $arr = [1 + 1, [2 * 2, 3 / 3 + 7,],]}', array(1 + 1, array(2 * 2, 3 / 3 + 7))),
array('{var $arr = [$zero, [$two => $one, $num.3 => $.const.PHP_VERSION]]}', array(0, array(2 => 1, "three" => PHP_VERSION))),
array('{var $arr = [5 - 1 => 1, ["two"|up => ("two"|low ~ "..."), 3 => count([1,2])]]}', array(4 => 1, array("TWO" => "two...", 3 => 2))),
);
}
public static function providerUnset() {
return array(
array('{var $a = 5} {unset $a} {if $a is not set}not set{/if}', 'not set'),
array('{var $a = ["b" => 5, "c" => 6]} {unset $a.b} {if $a.b is not set}not set{/if} but c is {$a.c}', 'not set but c is 6'),
array('{var $a = ["b" => 5, "c" => 6]} {unset $a.b $a.c} {if $a.b is not set}not set{/if} {if $a.c is not set}not set{/if}', 'not set not set'),
);
}
public static function providerTernary()
{
$a = array(
@ -683,6 +715,7 @@ class TemplateTest extends TestCase
),
array('Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
array('Foreach: {foreach [] as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
array('Foreach: {foreach $unexists as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
array(
'Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {foreachelse} empty {/foreach} end',
$a,
@ -1268,19 +1301,22 @@ class TemplateTest extends TestCase
);
}
/**
* @group sb
*/
public function _testSandbox()
{
try {
var_dump(
$this->fenom->setOptions(0)->compileCode(
"{autoescape true}{test_block_function:raw}{\$html}{/test_block_function}{/autoescape}"
$this->fenom->compileCode(
'{unset $a $a.c $b}'
)->getBody()
);
} catch (\Exception $e) {
print_r($e->getMessage() . "\n" . $e->getTraceAsString());
while ($e->getPrevious()) {
$e = $e->getPrevious();
print_r("\n\n" . $e->getMessage() . "\n" . $e->getTraceAsString());
print_r("\n\n" . $e->getMessage() . " in {$e->getFile()}:{$e->getLine()}\n" . $e->getTraceAsString());
}
}
exit;
@ -1416,6 +1452,34 @@ class TemplateTest extends TestCase
$this->execError($code, $exception, $message, $options);
}
/**
* @dataProvider providerArrays
* @group arrays
*/
public function testArrays($code, $vars)
{
$v = $this->getVars();
$v['vars'] = $vars;
$this->exec($code.'{if $arr === $vars}equal{/if}', $v, 'equal');
}
/**
* @dataProvider providerUnset
* @group unset
*/
public function testUnset($code, $result)
{
$this->exec($code, $this->getVars(), $result);
}
/**
* @dataProvider providerCreateVarInvalid
*/
// public function testCreateVarInvalid($code, $exception, $message, $options = 0)
// {
// $this->execError($code, $exception, $message, $options);
// }
/**
* @group ternary
* @dataProvider providerTernary