More convenient constructor for SasaeEnvironment
This commit is contained in:
parent
2d53e14ba4
commit
7ce1241e42
2 changed files with 18 additions and 6 deletions
|
@ -5,9 +5,12 @@
|
||||||
|
|
||||||
namespace Sasae;
|
namespace Sasae;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
use UnexpectedValueException;
|
use UnexpectedValueException;
|
||||||
use Index\Version;
|
use Index\Version;
|
||||||
|
use Sasae\Cache\SasaeFilesystemCache;
|
||||||
use Sasae\Extension\SasaeExtension;
|
use Sasae\Extension\SasaeExtension;
|
||||||
|
use Sasae\Loader\SasaeFilesystemLoader;
|
||||||
use Twig\Environment as TwigEnvironment;
|
use Twig\Environment as TwigEnvironment;
|
||||||
use Twig\TwigFilter;
|
use Twig\TwigFilter;
|
||||||
use Twig\TwigFunction;
|
use Twig\TwigFunction;
|
||||||
|
@ -28,17 +31,26 @@ class SasaeEnvironment {
|
||||||
private static ?Version $twigVersion = null;
|
private static ?Version $twigVersion = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param TwigLoaderInterface $loader A template loader instance.
|
* @param TwigLoaderInterface|string $loader A template loader instance or a path.
|
||||||
* @param ?TwigCacheInterface $cache A caching driver.
|
* @param TwigCacheInterface|array<string>|string|null $cache A caching driver.
|
||||||
* @param string $charset Character for templates.
|
* @param string $charset Character for templates.
|
||||||
* @param bool $debug Debug mode.
|
* @param bool $debug Debug mode.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
TwigLoaderInterface $loader,
|
TwigLoaderInterface|string $loader,
|
||||||
?TwigCacheInterface $cache = null,
|
TwigCacheInterface|array|string|null $cache = null,
|
||||||
string $charset = 'utf-8',
|
string $charset = 'utf-8',
|
||||||
bool $debug = false
|
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, [
|
$this->env = new TwigEnvironment($loader, [
|
||||||
'debug' => $debug,
|
'debug' => $debug,
|
||||||
'cache' => $cache,
|
'cache' => $cache,
|
||||||
|
|
|
@ -20,8 +20,8 @@ use Sasae\Loader\SasaeFilesystemLoader;
|
||||||
final class SasaeTest extends TestCase {
|
final class SasaeTest extends TestCase {
|
||||||
public function testEverything(): void {
|
public function testEverything(): void {
|
||||||
$env = new SasaeEnvironment(
|
$env = new SasaeEnvironment(
|
||||||
new SasaeFilesystemLoader(__DIR__),
|
__DIR__,
|
||||||
SasaeFilesystemCache::create('SasaeTest', XString::random(8))
|
['SasaeTest', XString::random(8)]
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertFalse($env->isDebug());
|
$this->assertFalse($env->isDebug());
|
||||||
|
|
Reference in a new issue