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