From 504905604628e9a37bb2b9e3c736d0eb5e056002 Mon Sep 17 00:00:00 2001 From: Ivan Shalganov Date: Mon, 20 Jan 2014 11:54:28 +0400 Subject: [PATCH] Fix #58, small improvements --- sandbox/fenom.php | 4 ++-- src/Fenom/Compiler.php | 28 ++++++++++++++-------------- src/Fenom/Provider.php | 10 ++++++---- src/Fenom/Template.php | 3 --- tests/cases/Fenom/TemplateTest.php | 6 ++++-- 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/sandbox/fenom.php b/sandbox/fenom.php index 9a4e780..e2afb39 100644 --- a/sandbox/fenom.php +++ b/sandbox/fenom.php @@ -1,10 +1,10 @@ display("greeting.tpl", array( +$fenom->display("../templates/../fenom.php", array( "user" => array( "name" => "Ivka", 'type' => 'new' diff --git a/src/Fenom/Compiler.php b/src/Fenom/Compiler.php index 0fe0c30..3d257fa 100644 --- a/src/Fenom/Compiler.php +++ b/src/Fenom/Compiler.php @@ -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() . '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() . 'exchangeArray('.$var.'); unset('.$var.');'; + } else { + return $var.' = (array)$tpl; ?>' . $inc->getBody() . '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() . 'exchangeArray('.$var.'); unset('.$var.');'; - } else { - return '$tpl->getStorage()->getTemplate(' . $cname . ')->display((array)$tpl);'; - } + return '$tpl->getStorage()->getTemplate(' . $cname . ')->display((array)$tpl);'; } } diff --git a/src/Fenom/Provider.php b/src/Fenom/Provider.php index 0e66bcf..f73f0d5 100644 --- a/src/Fenom/Provider.php +++ b/src/Fenom/Provider.php @@ -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; } diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index 95f1621..8fb8349 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -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); } diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index e2a2317..bd5791d 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -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) {