Fix #58, small improvements

This commit is contained in:
Ivan Shalganov 2014-01-20 11:54:28 +04:00
parent 19e0898da6
commit 5049056046
5 changed files with 26 additions and 25 deletions

View File

@ -1,10 +1,10 @@
<?php
require_once __DIR__.'/../../vendor/autoload.php';
require_once __DIR__.'/../vendor/autoload.php';
$fenom = Fenom::factory(__DIR__.'/templates', __DIR__.'/compiled', 0);
$fenom->display("greeting.tpl", array(
$fenom->display("../templates/../fenom.php", array(
"user" => array(
"name" => "Ivka",
'type' => 'new'

View File

@ -27,7 +27,7 @@ class Compiler
* @static
* @param Tokenizer $tokens
* @param Template $tpl
* @throws InvalidUsageException
* @throws \LogicException
* @return string
*/
public static function tagInclude(Tokenizer $tokens, Template $tpl)
@ -35,24 +35,24 @@ class Compiler
$name = false;
$cname = $tpl->parsePlainArg($tokens, $name);
$p = $tpl->parseParams($tokens);
if ($p) { // if we have additionally variables
if ($name && ($tpl->getStorage()->getOptions() & \Fenom::FORCE_INCLUDE)) {
if ($name) {
if($tpl->getStorage()->getOptions() & \Fenom::FORCE_INCLUDE) {
$inc = $tpl->getStorage()->compile($name, false);
$tpl->addDepend($inc);
$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);';
if($p) {
return $var.' = (array)$tpl; $tpl->exchangeArray(' . self::toArray($p) . '+'.$var.'); ?>' . $inc->getBody() . '<?php $tpl->exchangeArray('.$var.'); unset('.$var.');';
} else {
return $var.' = (array)$tpl; ?>' . $inc->getBody() . '<?php $tpl->exchangeArray('.$var.'); unset('.$var.');';
}
} elseif(!$tpl->getStorage()->templateExists($name)) {
throw new \LogicException("Template $name not found");
}
}
if($p) {
return '$tpl->getStorage()->getTemplate(' . $cname . ')->display(' . self::toArray($p) . '+(array)$tpl);';
} else {
if ($name && ($tpl->getStorage()->getOptions() & \Fenom::FORCE_INCLUDE)) {
$inc = $tpl->getStorage()->compile($name, false);
$tpl->addDepend($inc);
$var = $tpl->tmpVar();
return $var.' = (array)$tpl; ?>' . $inc->getBody() . '<?php $tpl->exchangeArray('.$var.'); unset('.$var.');';
} else {
return '$tpl->getStorage()->getTemplate(' . $cname . ')->display((array)$tpl);';
}
return '$tpl->getStorage()->getTemplate(' . $cname . ')->display((array)$tpl);';
}
}

View File

@ -84,7 +84,7 @@ class Provider implements ProviderInterface
public function getSource($tpl, &$time)
{
$tpl = $this->_getTemplatePath($tpl);
clearstatcache(null, $tpl);
clearstatcache(true, $tpl);
$time = filemtime($tpl);
return file_get_contents($tpl);
}
@ -96,7 +96,7 @@ class Provider implements ProviderInterface
*/
public function getLastModified($tpl)
{
clearstatcache(null, $tpl = $this->_getTemplatePath($tpl));
clearstatcache(true, $tpl = $this->_getTemplatePath($tpl));
return filemtime($tpl);
}
@ -132,6 +132,7 @@ class Provider implements ProviderInterface
*/
protected function _getTemplatePath($tpl)
{
if (($path = realpath($this->_path . "/" . $tpl)) && strpos($path, $this->_path) === 0) {
return $path;
} else {
@ -145,7 +146,8 @@ class Provider implements ProviderInterface
*/
public function templateExists($tpl)
{
return file_exists($this->_path . "/" . $tpl);
return ($path = realpath($this->_path . "/" . $tpl)) && strpos($path, $this->_path) === 0;
// return file_exists($this->_path . "/" . $tpl);
}
/**
@ -157,7 +159,7 @@ class Provider implements ProviderInterface
public function verify(array $templates)
{
foreach ($templates as $template => $mtime) {
clearstatcache(null, $template = $this->_path . '/' . $template);
clearstatcache(true, $template = $this->_path . '/' . $template);
if (@filemtime($template) !== $mtime) {
return false;
}

View File

@ -1350,9 +1350,6 @@ class Template extends Render
$static = stripslashes(substr($str, 1, -1));
return $str;
}
} elseif ($tokens->is(Tokenizer::MACRO_STRING)) {
$static = $tokens->getAndNext();
return '"' . addslashes($static) . '"';
} else {
return $this->parseExpr($tokens);
}

View File

@ -264,7 +264,8 @@ class TemplateTest extends TestCase
{
return array(
array('Include {include} template', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Include {include another="welcome.tpl"} template', 'Fenom\Error\CompileException', "Unexpected token '='"),
array('Include {include another="welcome.tpl"} template', 'Fenom\Error\CompileException', "Unexpected token 'another'"),
array('Include {include "../../TestCase.php"} template', 'Fenom\Error\SecurityException', "Template ../../TestCase.php not found"),
);
}
@ -290,7 +291,7 @@ class TemplateTest extends TestCase
{
return array(
array('Include {insert} template', 'Fenom\Error\CompileException', "Unexpected end of expression"),
array('Include {insert another="welcome.tpl"} template', 'Fenom\Error\CompileException', "Template another not found"),
array('Include {insert another="welcome.tpl"} template', 'Fenom\Error\CompileException', "Unexpected token 'another'"),
array('Include {insert $tpl} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
array('Include {insert "$tpl"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
array('Include {insert "{$tpl}"} template', 'Fenom\Error\CompileException', "Tag {insert} accept only static template name"),
@ -849,6 +850,7 @@ class TemplateTest extends TestCase
/**
* @dataProvider providerIncludeInvalid
* @group testIncludeInvalid
*/
public function testIncludeInvalid($code, $exception, $message, $options = 0)
{