1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asmaster\EquipTwig\Configuration; |
4
|
|
|
|
5
|
|
|
use Auryn\Injector; |
6
|
|
|
use Equip\Configuration\EnvTrait; |
7
|
|
|
use Equip\Configuration\ConfigurationInterface; |
8
|
|
|
use Equip\Responder\FormattedResponder; |
9
|
|
|
use Asmaster\EquipTwig\TwigFormatter; |
10
|
|
|
|
11
|
|
|
class TwigConfiguration implements ConfigurationInterface |
12
|
|
|
{ |
13
|
|
|
use EnvTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Injector $injector |
17
|
|
|
*/ |
18
|
|
|
public function apply(Injector $injector) |
19
|
|
|
{ |
20
|
2 |
|
$injector->prepare(FormattedResponder::class, function (FormattedResponder $responder) { |
21
|
1 |
|
return $responder->withValue(TwigFormatter::class, 1.0); |
22
|
2 |
|
}); |
23
|
|
|
|
24
|
2 |
|
$injector->prepare(\Twig_Loader_Filesystem::class, [$this, 'prepareFilesystem']); |
25
|
|
|
|
26
|
2 |
|
$injector->define(\Twig_Environment::class, [ |
27
|
2 |
|
'loader' => \Twig_Loader_Filesystem::class, |
28
|
2 |
|
':options' => $this->prepareOptions() |
29
|
2 |
|
]); |
30
|
2 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \Twig_Loader_Filesystem $loader |
34
|
|
|
*/ |
35
|
1 |
|
public function prepareFilesystem(\Twig_Loader_Filesystem $loader) |
36
|
|
|
{ |
37
|
1 |
|
$templates = $this->getRootDir() . DIRECTORY_SEPARATOR . $this->env->getValue('TWIG_TEMPLATES'); |
38
|
|
|
|
39
|
1 |
|
$loader->addPath($templates); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array $options |
44
|
|
|
*/ |
45
|
2 |
|
public function prepareOptions() |
46
|
|
|
{ |
47
|
2 |
|
$env = $this->env; |
48
|
|
|
|
49
|
2 |
|
if ($cacheDir = $env->getValue('TWIG_CACHE')) { |
50
|
|
|
$cacheDir = $this->getRootDir() . DIRECTORY_SEPARATOR . $env->getValue('TWIG_CACHE'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$options = [ |
54
|
2 |
|
'debug' => $env->getValue('TWIG_DEBUG') ?: false, |
55
|
2 |
|
'auto_reload' => $env->getValue('TWIG_AUTO_RELOAD') ?: true, |
56
|
2 |
|
'strict_variables' => $env->getValue('TWIG_STRICT_VARIABLES') ?: false, |
57
|
|
|
'cache' => $cacheDir |
58
|
2 |
|
]; |
59
|
|
|
|
60
|
2 |
|
return $options; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string $rootDir |
65
|
|
|
*/ |
66
|
1 |
|
protected function getRootDir() |
67
|
|
|
{ |
68
|
1 |
|
$rootDir = dirname(dirname(dirname(dirname(dirname(__DIR__))))); |
69
|
|
|
|
70
|
1 |
|
return $rootDir; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|