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
|
|
|
const PREFIX = 'TWIG_'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Injector $injector |
21
|
|
|
*/ |
22
|
1 |
|
public function apply(Injector $injector) |
23
|
|
|
{ |
24
|
1 |
|
$env = $this->env; |
25
|
|
|
|
26
|
1 |
|
$path = $env->getValue('TWIG_TEMPLATES'); |
27
|
|
|
|
28
|
1 |
|
$injector->define(FilesystemLoader::class, [ |
29
|
1 |
|
':path' => $path, |
30
|
1 |
|
':fileExtensions' => $this->fileExtensions($env), |
31
|
|
|
]); |
32
|
|
|
|
33
|
1 |
|
$injector->define(Twig_Environment::class, [ |
34
|
1 |
|
'loader' => FilesystemLoader::class, |
35
|
1 |
|
':options' => $this->options($env), |
36
|
|
|
]); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Env $env |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
1 |
|
private function fileExtensions(Env $env) |
45
|
|
|
{ |
46
|
1 |
|
$fileExtensions = $env->getValue('TWIG_FILE_EXTENSIONS', 'html.twig,twig'); |
47
|
|
|
|
48
|
1 |
|
return explode(',', $fileExtensions); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Env $env |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
1 |
|
private function options(Env $env) |
57
|
|
|
{ |
58
|
1 |
|
static $options = [ |
59
|
|
|
'debug' => false, |
60
|
|
|
'strict_variables' => false, |
61
|
|
|
'cache' => false, |
62
|
|
|
'auto_reload' => false, |
63
|
|
|
'charset' => 'UTF-8', |
64
|
|
|
'autoescape' => 'html', |
65
|
|
|
'base_template_class' => 'Twig_Template', |
66
|
|
|
'optimizations' => -1, |
67
|
|
|
]; |
68
|
|
|
|
69
|
1 |
|
foreach ($this->envTwig($env) as $option => $value) { |
70
|
1 |
|
list(, $option) = explode('_', strtolower($option), 2); |
71
|
1 |
|
if (isset($options[$option])) { |
72
|
1 |
|
$options[$option] = $value; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return $options; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Env $env |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
private function envTwig(Env $env) |
85
|
|
|
{ |
86
|
1 |
|
$twigFilter = static function ($value) { |
87
|
1 |
|
return stristr($value, self::PREFIX); |
88
|
1 |
|
}; |
89
|
|
|
|
90
|
1 |
|
return array_filter($env->toArray(), $twigFilter, ARRAY_FILTER_USE_KEY); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|