migrate to php8

This commit is contained in:
ivan shalganov 2023-02-23 21:50:12 +01:00
parent b04c38a533
commit e8647cfb02
6 changed files with 40 additions and 14 deletions

View File

@ -422,7 +422,7 @@ class Fenom
string|Fenom\ProviderInterface $source, string|Fenom\ProviderInterface $source,
string $compile_dir = '/tmp', string $compile_dir = '/tmp',
int|array $options = 0 int|array $options = 0
): Fenom ): static
{ {
if (is_string($source)) { if (is_string($source)) {
$provider = new Fenom\Provider($source); $provider = new Fenom\Provider($source);
@ -1056,7 +1056,8 @@ class Fenom
/** @var Fenom\Template $tpl */ /** @var Fenom\Template $tpl */
$tpl = $this->_storage[$key]; $tpl = $this->_storage[$key];
if (($this->_options & self::AUTO_RELOAD) && !$tpl->isValid()) { 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 { } else {
return $tpl; return $tpl;
} }
@ -1099,6 +1100,10 @@ class Fenom
protected function _load(array|string $template, int $opts): Render protected function _load(array|string $template, int $opts): Render
{ {
$file_name = $this->getCompileName($template, $opts); $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)) { if (is_file($this->_compile_dir . "/" . $file_name)) {
$fenom = $this; // used in template $fenom = $this; // used in template
$_tpl = include($this->_compile_dir . "/" . $file_name); $_tpl = include($this->_compile_dir . "/" . $file_name);
@ -1108,9 +1113,12 @@ class Fenom
&& $_tpl instanceof Fenom\Render && $_tpl instanceof Fenom\Render
&& $_tpl->isValid()) { && $_tpl->isValid()) {
return $_tpl; 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}");
} }
/** /**

View File

@ -0,0 +1,10 @@
<?php
namespace Fenom\Error;
/**
* @package Fenom\Error
*/
class TemplateException extends \Exception
{
}

View File

@ -10,6 +10,7 @@
namespace Fenom; namespace Fenom;
use Fenom; use Fenom;
use Fenom\Error\TemplateException;
/** /**
* Primitive template * Primitive template
@ -210,10 +211,15 @@ class Render extends \ArrayObject
* Execute template and write into output * Execute template and write into output
* @param array $values for template * @param array $values for template
* @return array * @return array
* @throws TemplateException
*/ */
public function display(array $values): array public function display(array $values): array
{ {
try {
$this->_code->__invoke($values, $this); $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; return $values;
} }

View File

@ -241,7 +241,7 @@ class TestCase extends \PHPUnit\Framework\TestCase
); );
} }
public function providerVariables() public static function providerVariables()
{ {
return array( return array(
array('$one', 1), array('$one', 1),
@ -334,6 +334,13 @@ class Helper
public function getArray() { public function getArray() {
return array(1,2,3); return array(1,2,3);
} }
/**
* @throws \Exception
*/
public static function throws() {
throw new \Exception("helper exception");
}
} }
function helper_func($string, $pad = 10) { function helper_func($string, $pad = 10) {

View File

@ -44,14 +44,9 @@ class RenderTest extends TestCase
$this->assertSame("It is render's function fetch", self::$render->fetch(array("render" => "fetch"))); $this->assertSame("It is render's function fetch", self::$render->fetch(array("render" => "fetch")));
} }
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage template error
*/
public function testFetchException() public function testFetchException()
{ {
$this->expectException(\RuntimeException::class); $this->expectException(Fenom\Error\TemplateException::class);
$this->expectExceptionMessage("template error");
$render = new Render(Fenom::factory("."), function () { $render = new Render(Fenom::factory("."), function () {
echo "error"; echo "error";
throw new \RuntimeException("template error"); throw new \RuntimeException("template error");

View File

@ -24,7 +24,7 @@ class FenomTest extends \Fenom\TestCase
$time = $this->tpl('temp.tpl', 'Template 1 a'); $time = $this->tpl('temp.tpl', 'Template 1 a');
$fenom = new Fenom($provider = new \Fenom\Provider(FENOM_RESOURCES . '/template')); $fenom = new Fenom($provider = new \Fenom\Provider(FENOM_RESOURCES . '/template'));
$fenom->setCompileDir(FENOM_RESOURCES . '/compile'); $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($provider, $tpl->getProvider());
$this->assertSame('temp.tpl', $tpl->getBaseName()); $this->assertSame('temp.tpl', $tpl->getBaseName());
$this->assertSame('temp.tpl', $tpl->getName()); $this->assertSame('temp.tpl', $tpl->getName());
@ -40,7 +40,7 @@ class FenomTest extends \Fenom\TestCase
FENOM_RESOURCES . '/compile', FENOM_RESOURCES . '/compile',
Fenom::AUTO_ESCAPE 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($provider, $tpl->getProvider());
$this->assertSame('temp.tpl', $tpl->getBaseName()); $this->assertSame('temp.tpl', $tpl->getBaseName());
$this->assertSame('temp.tpl', $tpl->getName()); $this->assertSame('temp.tpl', $tpl->getName());