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