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 <?php
require_once __DIR__.'/../../vendor/autoload.php'; require_once __DIR__.'/../vendor/autoload.php';
$fenom = Fenom::factory(__DIR__.'/templates', __DIR__.'/compiled', 0); $fenom = Fenom::factory(__DIR__.'/templates', __DIR__.'/compiled', 0);
$fenom->display("greeting.tpl", array( $fenom->display("../templates/../fenom.php", array(
"user" => array( "user" => array(
"name" => "Ivka", "name" => "Ivka",
'type' => 'new' 'type' => 'new'

View File

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

View File

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

View File

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

View File

@ -264,7 +264,8 @@ class TemplateTest extends TestCase
{ {
return array( return array(
array('Include {include} template', 'Fenom\Error\CompileException', "Unexpected end of expression"), 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( return array(
array('Include {insert} template', 'Fenom\Error\CompileException', "Unexpected end of expression"), 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"), 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 * @dataProvider providerIncludeInvalid
* @group testIncludeInvalid
*/ */
public function testIncludeInvalid($code, $exception, $message, $options = 0) public function testIncludeInvalid($code, $exception, $message, $options = 0)
{ {