More convenient constructor for SasaeEnvironment

This commit is contained in:
flash 2023-08-24 22:57:41 +00:00
parent 2d53e14ba4
commit 7ce1241e42
2 changed files with 18 additions and 6 deletions

View file

@ -5,9 +5,12 @@
namespace Sasae;
use InvalidArgumentException;
use UnexpectedValueException;
use Index\Version;
use Sasae\Cache\SasaeFilesystemCache;
use Sasae\Extension\SasaeExtension;
use Sasae\Loader\SasaeFilesystemLoader;
use Twig\Environment as TwigEnvironment;
use Twig\TwigFilter;
use Twig\TwigFunction;
@ -28,17 +31,26 @@ class SasaeEnvironment {
private static ?Version $twigVersion = null;
/**
* @param TwigLoaderInterface $loader A template loader instance.
* @param ?TwigCacheInterface $cache A caching driver.
* @param TwigLoaderInterface|string $loader A template loader instance or a path.
* @param TwigCacheInterface|array<string>|string|null $cache A caching driver.
* @param string $charset Character for templates.
* @param bool $debug Debug mode.
*/
public function __construct(
TwigLoaderInterface $loader,
?TwigCacheInterface $cache = null,
TwigLoaderInterface|string $loader,
TwigCacheInterface|array|string|null $cache = null,
string $charset = 'utf-8',
bool $debug = false
) {
if(is_string($loader))
$loader = new SasaeFilesystemLoader($loader);
if(is_array($cache)) {
if(empty($cache))
throw new InvalidArgumentException('If $cache is an array, it may not be empty.');
$cache = SasaeFilesystemCache::create(array_shift($cache), array_shift($cache));
}
$this->env = new TwigEnvironment($loader, [
'debug' => $debug,
'cache' => $cache,

View file

@ -20,8 +20,8 @@ use Sasae\Loader\SasaeFilesystemLoader;
final class SasaeTest extends TestCase {
public function testEverything(): void {
$env = new SasaeEnvironment(
new SasaeFilesystemLoader(__DIR__),
SasaeFilesystemCache::create('SasaeTest', XString::random(8))
__DIR__,
['SasaeTest', XString::random(8)]
);
$this->assertFalse($env->isDebug());