From e8647cfb029ded46219bb4a7a5a367309f6db7f3 Mon Sep 17 00:00:00 2001 From: ivan shalganov Date: Thu, 23 Feb 2023 21:50:12 +0100 Subject: [PATCH] migrate to php8 --- src/Fenom.php | 16 ++++++++++++---- src/Fenom/Error/TemplateException.php | 10 ++++++++++ src/Fenom/Render.php | 8 +++++++- tests/TestCase.php | 9 ++++++++- tests/cases/Fenom/RenderTest.php | 7 +------ tests/cases/FenomTest.php | 4 ++-- 6 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 src/Fenom/Error/TemplateException.php diff --git a/src/Fenom.php b/src/Fenom.php index a5bf65a..182ebf5 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -422,7 +422,7 @@ class Fenom string|Fenom\ProviderInterface $source, string $compile_dir = '/tmp', int|array $options = 0 - ): Fenom + ): static { if (is_string($source)) { $provider = new Fenom\Provider($source); @@ -1056,11 +1056,12 @@ class Fenom /** @var Fenom\Template $tpl */ $tpl = $this->_storage[$key]; if (($this->_options & self::AUTO_RELOAD) && !$tpl->isValid()) { - return $this->_storage[$key] = $this->compile($template, true, $options); + $this->compile($template, true, $options); + return $this->_storage[$key] = $this->_load($template, $options); } else { return $tpl; } - } elseif ($this->_options & (self::FORCE_COMPILE | self::DISABLE_CACHE)) { + } elseif ($this->_options & (self::FORCE_COMPILE | self::DISABLE_CACHE)) { return $this->compile($template, !($this->_options & self::DISABLE_CACHE), $options); } else { return $this->_storage[$key] = $this->_load($template, $options); @@ -1099,6 +1100,10 @@ class Fenom protected function _load(array|string $template, int $opts): Render { $file_name = $this->getCompileName($template, $opts); + $tpl = null; + if (!is_file($this->_compile_dir . "/" . $file_name)) { + $tpl = $this->compile($template, true, $opts); + } if (is_file($this->_compile_dir . "/" . $file_name)) { $fenom = $this; // used in template $_tpl = include($this->_compile_dir . "/" . $file_name); @@ -1108,9 +1113,12 @@ class Fenom && $_tpl instanceof Fenom\Render && $_tpl->isValid()) { return $_tpl; + } else if ($tpl) { + return $tpl; } } - return $this->compile($template, true, $opts); + throw new CompileException("failed to store cache of " . var_export($template, true) . + " to {$file_name}"); } /** diff --git a/src/Fenom/Error/TemplateException.php b/src/Fenom/Error/TemplateException.php new file mode 100644 index 0000000..f17cd8f --- /dev/null +++ b/src/Fenom/Error/TemplateException.php @@ -0,0 +1,10 @@ +_code->__invoke($values, $this); + try { + $this->_code->__invoke($values, $this); + } catch (\Throwable $e) { + throw new Fenom\Error\TemplateException("unhandled exception in the template `{$this->getName()}`: {$e->getMessage()}", 0, $e); + } return $values; } diff --git a/tests/TestCase.php b/tests/TestCase.php index 76adc5a..bbc4284 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -241,7 +241,7 @@ class TestCase extends \PHPUnit\Framework\TestCase ); } - public function providerVariables() + public static function providerVariables() { return array( array('$one', 1), @@ -334,6 +334,13 @@ class Helper public function getArray() { return array(1,2,3); } + + /** + * @throws \Exception + */ + public static function throws() { + throw new \Exception("helper exception"); + } } function helper_func($string, $pad = 10) { diff --git a/tests/cases/Fenom/RenderTest.php b/tests/cases/Fenom/RenderTest.php index aab8ccc..c9f3eee 100644 --- a/tests/cases/Fenom/RenderTest.php +++ b/tests/cases/Fenom/RenderTest.php @@ -44,14 +44,9 @@ class RenderTest extends TestCase $this->assertSame("It is render's function fetch", self::$render->fetch(array("render" => "fetch"))); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage template error - */ public function testFetchException() { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage("template error"); + $this->expectException(Fenom\Error\TemplateException::class); $render = new Render(Fenom::factory("."), function () { echo "error"; throw new \RuntimeException("template error"); diff --git a/tests/cases/FenomTest.php b/tests/cases/FenomTest.php index 2764350..6c9e025 100644 --- a/tests/cases/FenomTest.php +++ b/tests/cases/FenomTest.php @@ -24,7 +24,7 @@ class FenomTest extends \Fenom\TestCase $time = $this->tpl('temp.tpl', 'Template 1 a'); $fenom = new Fenom($provider = new \Fenom\Provider(FENOM_RESOURCES . '/template')); $fenom->setCompileDir(FENOM_RESOURCES . '/compile'); - $this->assertInstanceOf('Fenom\Template', $tpl = $fenom->getTemplate('temp.tpl')); + $this->assertInstanceOf('Fenom\Render', $tpl = $fenom->getTemplate('temp.tpl')); $this->assertSame($provider, $tpl->getProvider()); $this->assertSame('temp.tpl', $tpl->getBaseName()); $this->assertSame('temp.tpl', $tpl->getName()); @@ -40,7 +40,7 @@ class FenomTest extends \Fenom\TestCase FENOM_RESOURCES . '/compile', Fenom::AUTO_ESCAPE ); - $this->assertInstanceOf('Fenom\Template', $tpl = $fenom->getTemplate('temp.tpl')); + $this->assertInstanceOf('Fenom\Render', $tpl = $fenom->getTemplate('temp.tpl')); $this->assertSame($provider, $tpl->getProvider()); $this->assertSame('temp.tpl', $tpl->getBaseName()); $this->assertSame('temp.tpl', $tpl->getName());