1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexMasterov\EquipTwig\Configuration; |
4
|
|
|
|
5
|
|
|
use AlexMasterov\EquipTwig\Loader\FilesystemLoader; |
6
|
|
|
use AlexMasterov\EquipTwig\TwigFormatter; |
7
|
|
|
use Auryn\Injector; |
8
|
|
|
use Equip\Configuration\ConfigurationInterface; |
9
|
|
|
use Equip\Configuration\EnvTrait; |
10
|
|
|
use Equip\Env; |
11
|
|
|
use Twig_Environment; |
12
|
|
|
|
13
|
|
|
final class TwigConfiguration implements ConfigurationInterface |
14
|
|
|
{ |
15
|
|
|
use EnvTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Injector $injector |
19
|
|
|
*/ |
20
|
|
|
public function apply(Injector $injector) |
21
|
|
|
{ |
22
|
|
|
$env = $this->env; |
23
|
|
|
$options = $this->options($env); |
24
|
|
|
|
25
|
|
|
$injector->define(Twig_Environment::class, [ |
26
|
|
|
'loader' => FilesystemLoader::class, |
27
|
|
|
':options' => $options |
28
|
|
|
]); |
29
|
|
|
|
30
|
|
|
list($path,$fileExtensions) = $this->filesystemConfig($env); |
31
|
|
|
|
32
|
|
|
$injector->define(FilesystemLoader::class, [ |
33
|
|
|
':path' => $path, |
34
|
|
|
':fileExtensions' => $fileExtensions |
35
|
|
|
]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Env $env |
40
|
|
|
* |
41
|
|
|
* @return array Configuration options from environment variables |
42
|
|
|
*/ |
43
|
|
|
private function options(Env $env) |
44
|
|
|
{ |
45
|
|
|
$options = [ |
46
|
|
|
'debug' => $env->getValue('TWIG_DEBUG', false), |
47
|
|
|
'auto_reload' => $env->getValue('TWIG_AUTO_RELOAD', true), |
48
|
|
|
'strict_variables' => $env->getValue('TWIG_STRICT_VARIABLES', false), |
49
|
|
|
'cache' => $env->getValue('TWIG_CACHE', false) |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
return $options; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Env $env |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
private function filesystemConfig(Env $env) |
61
|
|
|
{ |
62
|
|
|
$path = $env->getValue('TWIG_TEMPLATES'); |
63
|
|
|
$fileExtensions = $env->getValue('TWIG_FILE_EXTENSIONS', ['html.twig', 'twig']); |
64
|
|
|
|
65
|
|
|
if (is_string($fileExtensions)) { |
66
|
|
|
$fileExtensions = explode(',', $fileExtensions); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return [$path, $fileExtensions]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|