diff --git a/src/SasaeEnvironment.php b/src/SasaeEnvironment.php index abd93fd..02ac85b 100644 --- a/src/SasaeEnvironment.php +++ b/src/SasaeEnvironment.php @@ -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|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, diff --git a/tests/SasaeTest.php b/tests/SasaeTest.php index bd98012..82a8f69 100644 --- a/tests/SasaeTest.php +++ b/tests/SasaeTest.php @@ -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());