Merge branch 'origin/master'

Conflicts:
	src/Fenom/Compiler.php
	src/Fenom/Template.php
	tests/cases/Fenom/TemplateTest.php
	tests/cases/Fenom/TokenizerTest.php
	tests/cases/FenomTest.php
This commit is contained in:
Ivan Shalganov
2013-09-15 16:39:29 +04:00
12 changed files with 87 additions and 19 deletions

View File

@ -35,7 +35,7 @@ class Fenom
const FORCE_COMPILE = 0x100;
const AUTO_ESCAPE = 0x200;
const DISABLE_CACHE = 0x400;
const FORCE_VERIFY = 0x800; // reserved
const FORCE_VERIFY = 0x800;
const AUTO_TRIM = 0x1000; // reserved
const DENY_STATICS = 0x2000; // reserved
@ -288,10 +288,14 @@ class Fenom
* Set compile directory
*
* @param string $dir directory to store compiled templates in
* @throws LogicException
* @return Fenom
*/
public function setCompileDir($dir)
{
if(!is_writable($dir)) {
throw new LogicException("Cache directory $dir is not writable");
}
$this->_compile_dir = $dir;
return $this;
}
@ -796,7 +800,7 @@ class Fenom
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 $tpl");
throw new \RuntimeException("Can't to move $tpl_tmp to $file_name");
}
}
return $template;

View File

@ -39,7 +39,8 @@ class Compiler
if ($name && ($tpl->getStorage()->getOptions() & \Fenom::FORCE_INCLUDE)) {
$inc = $tpl->getStorage()->compile($name, false);
$tpl->addDepend($inc);
return '$_tpl = (array)$tpl; $tpl->exchangeArray(' . self::toArray($p) . '+$_tpl); ?>' . $inc->getBody() . '<?php $tpl->exchangeArray($_tpl); unset($_tpl);';
$var = $tpl->tmpVar();
return $var.' = (array)$tpl; $tpl->exchangeArray(' . self::toArray($p) . '+'.$var.'); ?>' . $inc->getBody() . '<?php $tpl->exchangeArray('.$var.'); unset('.$var.');';
} else {
return '$tpl->getStorage()->getTemplate(' . $cname . ')->display(' . self::toArray($p) . '+(array)$tpl);';
}
@ -47,7 +48,8 @@ class Compiler
if ($name && ($tpl->getStorage()->getOptions() & \Fenom::FORCE_INCLUDE)) {
$inc = $tpl->getStorage()->compile($name, false);
$tpl->addDepend($inc);
return '$_tpl = (array)$tpl; ?>' . $inc->getBody() . '<?php $tpl->exchangeArray($_tpl); unset($_tpl);';
$var = $tpl->tmpVar();
return $var.' = (array)$tpl; ?>' . $inc->getBody() . '<?php $tpl->exchangeArray('.$var.'); unset('.$var.');';
} else {
return '$tpl->getStorage()->getTemplate(' . $cname . ')->display((array)$tpl);';
}
@ -332,10 +334,12 @@ class Compiler
*/
public static function switchOpen(Tokenizer $tokens, Scope $scope)
{
$scope["expr"] = $scope->tpl->parseExpr($tokens);
$expr = $scope->tpl->parseExpr($tokens);
$scope["case"] = array();
$scope["last"] = array();
$scope["default"] = '';
$scope["var"] = $scope->tpl->tmpVar();
$scope["expr"] = $scope["var"].' = strval('.$expr.')';
// lazy init
return '';
}
@ -409,13 +413,16 @@ class Compiler
public static function switchClose(Tokenizer $tokens, Scope $scope)
{
self::_caseResort($scope);
$expr = $scope["expr"];
$code = "";
$expr = $scope["var"];
$code = $scope["expr"].";\n";
$default = $scope["default"];
foreach ($scope["case"] as $case => $content) {
if(is_numeric($case)) {
$case = "'$case'";
}
$code .= "if($expr == $case) {\n?>$content<?php\n} else";
}
$code .= " {\n?>$default<?php\n}";
$code .= " {\n?>$default<?php\n}\nunset(".$scope["var"].")";
return $code;
}