|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
namespace Xervice\Twig; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
use Xervice\Core\Factory\AbstractFactory; |
|
9
|
|
|
use Xervice\Twig\Business\Loader\PathInjector; |
|
10
|
|
|
use Xervice\Twig\Business\Loader\PathInjectorInterface; |
|
11
|
|
|
use Xervice\Twig\Business\Loader\XerviceLoader; |
|
12
|
|
|
use Xervice\Twig\Business\Path\PathCollection; |
|
13
|
|
|
use Xervice\Twig\Business\Twig\Extensions\TwigExtensionCollection; |
|
14
|
|
|
use Xervice\Twig\Business\Twig\TwigEnvironmentProvider; |
|
15
|
|
|
use Xervice\Twig\Business\Twig\TwigEnvironmentProviderInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @method \Xervice\Twig\TwigConfig getConfig() |
|
19
|
|
|
*/ |
|
20
|
|
|
class TwigFactory extends AbstractFactory |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Twig_Environment |
|
24
|
|
|
*/ |
|
25
|
|
|
private $twigEnvironment; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return \Xervice\Twig\Business\Twig\TwigEnvironmentProviderInterface |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public function createTwigEnvironmentProvider(): TwigEnvironmentProviderInterface |
|
31
|
|
|
{ |
|
32
|
2 |
|
return new TwigEnvironmentProvider( |
|
33
|
2 |
|
$this->createTwigEnvironment(), |
|
34
|
2 |
|
$this->getTwigExtensionCollection() |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return \Twig_Environment |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function createTwigEnvironment(): \Twig_Environment |
|
42
|
|
|
{ |
|
43
|
2 |
|
return new \Twig_Environment( |
|
44
|
2 |
|
$this->createXerviceLoader(), |
|
45
|
|
|
[ |
|
46
|
2 |
|
'debug' => $this->getConfig()->isDebug(), |
|
47
|
2 |
|
'charset' => $this->getConfig()->getCharset(), |
|
48
|
2 |
|
'base_template_class' => $this->getConfig()->getBaseTemplateClass(), |
|
49
|
2 |
|
'strict_variables' => $this->getConfig()->isStrictVariables(), |
|
50
|
2 |
|
'autoescape' => $this->getConfig()->getAutoescape(), |
|
51
|
2 |
|
'cache' => $this->getConfig()->isCache() ? $this->getConfig()->getCachePath() : false, |
|
52
|
2 |
|
'auto_reload' => $this->getConfig()->isAutoReload(), |
|
53
|
2 |
|
'optimizations' => $this->getConfig()->getOptimization() |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return \Xervice\Twig\Business\Loader\PathInjector |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function createPathInjector(): PathInjectorInterface |
|
62
|
|
|
{ |
|
63
|
2 |
|
return new PathInjector( |
|
64
|
2 |
|
$this->getTwigEnvironment()->getLoader() |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return \Twig_LoaderInterface |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function createXerviceLoader(): \Twig_LoaderInterface |
|
72
|
|
|
{ |
|
73
|
2 |
|
return new XerviceLoader( |
|
74
|
2 |
|
$this->createTwigFilesystemLoader(), |
|
75
|
2 |
|
$this->getPathProviderCollection() |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return \Twig_Loader_Filesystem |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function createTwigFilesystemLoader(): \Twig_Loader_Filesystem |
|
83
|
|
|
{ |
|
84
|
2 |
|
return new \Twig_Loader_Filesystem(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/*** |
|
88
|
|
|
* @return \Twig_Environment |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function getTwigEnvironment(): \Twig_Environment |
|
91
|
|
|
{ |
|
92
|
2 |
|
if ($this->twigEnvironment === null) { |
|
93
|
2 |
|
$this->twigEnvironment = $this->createTwigEnvironmentProvider()->getTwigEnvironment(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
return $this->twigEnvironment; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return \Xervice\Twig\Business\Path\PathCollection |
|
101
|
|
|
*/ |
|
102
|
2 |
|
public function getPathProviderCollection(): PathCollection |
|
103
|
|
|
{ |
|
104
|
2 |
|
return $this->getDependency(TwigDependencyProvider::PATH_PROVIDER_COLLECTION); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return \Xervice\Twig\Business\Twig\Extensions\TwigExtensionCollection |
|
109
|
|
|
*/ |
|
110
|
2 |
|
public function getTwigExtensionCollection(): TwigExtensionCollection |
|
111
|
|
|
{ |
|
112
|
2 |
|
return $this->getDependency(TwigDependencyProvider::TWIG_EXTENSIONS); |
|
113
|
|
|
} |
|
114
|
|
|
} |