1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\service; |
3
|
|
|
|
4
|
|
|
use \Twig_Environment; |
5
|
|
|
use \Twig_SimpleFunction; |
6
|
|
|
use keeko\core\auth\AuthManager; |
7
|
|
|
use keeko\core\kernel\AbstractKernel; |
8
|
|
|
use keeko\core\package\ModuleManager; |
9
|
|
|
use keeko\core\package\PackageManager; |
10
|
|
|
use keeko\core\preferences\PreferenceLoader; |
11
|
|
|
use keeko\core\security\Firewall; |
12
|
|
|
use Puli\Repository\Api\ResourceRepository; |
13
|
|
|
use Puli\TwigExtension\PuliExtension; |
14
|
|
|
use Puli\TwigExtension\PuliTemplateLoader; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
16
|
|
|
use Symfony\Component\Translation\Loader\JsonFileLoader; |
17
|
|
|
|
18
|
|
|
class ServiceContainer { |
19
|
|
|
|
20
|
|
|
/** @var PackageManager */ |
21
|
|
|
private $packageManager; |
22
|
|
|
|
23
|
|
|
/** @var ModuleManager */ |
24
|
|
|
private $moduleManager; |
25
|
|
|
|
26
|
|
|
/** @var AuthManager */ |
27
|
|
|
private $authManager; |
28
|
|
|
|
29
|
|
|
/** @var PreferenceLoader */ |
30
|
|
|
private $preferenceLoader; |
31
|
|
|
|
32
|
|
|
/** @var Firewall */ |
33
|
|
|
private $firewall; |
34
|
|
|
|
35
|
|
|
/** @var KeekoTranslator */ |
36
|
|
|
private $translator; |
37
|
|
|
|
38
|
|
|
/** @var ResourceRepository */ |
39
|
|
|
private $puli; |
40
|
|
|
|
41
|
|
|
/** @var EventDispatcher */ |
42
|
|
|
private $dispatcher; |
43
|
|
|
|
44
|
|
|
/** @var AbstractKernel */ |
45
|
|
|
private $kernel; |
46
|
|
|
|
47
|
|
|
/** @var Twig_Environment */ |
48
|
|
|
private $twig; |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
public function __construct(AbstractKernel $kernel) { |
52
|
|
|
$this->kernel = $kernel; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the kernel |
57
|
|
|
* |
58
|
|
|
* @return AbstractKernel |
59
|
|
|
*/ |
60
|
|
|
public function getKernel() { |
61
|
|
|
return $this->kernel; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the event dispatcher |
66
|
|
|
* |
67
|
|
|
* @return EventDispatcher |
68
|
|
|
*/ |
69
|
|
|
public function getDispatcher() { |
70
|
|
|
if ($this->dispatcher === null) { |
71
|
|
|
$this->dispatcher = new EventDispatcher(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->dispatcher; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the package manager |
79
|
|
|
* |
80
|
|
|
* @return PackageManager |
81
|
|
|
*/ |
82
|
|
|
public function getPackageManager() { |
83
|
|
|
if ($this->packageManager === null) { |
84
|
|
|
$this->packageManager = new PackageManager($this); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->packageManager; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the module manager |
92
|
|
|
* |
93
|
|
|
* @return ModuleManager |
94
|
|
|
*/ |
95
|
|
|
public function getModuleManager() { |
96
|
|
|
if ($this->moduleManager === null) { |
97
|
|
|
$this->moduleManager = new ModuleManager($this); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->moduleManager; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the auth manager |
105
|
|
|
* |
106
|
|
|
* @return AuthManager |
107
|
|
|
*/ |
108
|
|
|
public function getAuthManager() { |
109
|
|
|
if ($this->authManager === null) { |
110
|
|
|
$this->authManager = new AuthManager(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->authManager; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the preference loader |
118
|
|
|
* |
119
|
|
|
* @return PreferenceLoader |
120
|
|
|
*/ |
121
|
|
|
public function getPreferenceLoader() { |
122
|
|
|
if ($this->preferenceLoader === null) { |
123
|
|
|
$this->preferenceLoader = new PreferenceLoader(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->preferenceLoader; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the firewall |
131
|
|
|
* |
132
|
|
|
* @return Firewall |
133
|
|
|
*/ |
134
|
|
|
public function getFirewall() { |
135
|
|
|
if ($this->firewall === null) { |
136
|
|
|
$this->firewall = new Firewall($this); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->firewall; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the keeko translation service |
144
|
|
|
* |
145
|
|
|
* @return KeekoTranslator |
146
|
|
|
*/ |
147
|
|
|
public function getTranslator() { |
148
|
|
|
// TODO: how to get the language |
149
|
|
|
if ($this->translator === null) { |
150
|
|
|
$app = $this->getKernel()->getApplication(); |
|
|
|
|
151
|
|
|
$lang = $app->getLocalization()->getLanguage()->getAlpha2(); |
|
|
|
|
152
|
|
|
$this->translator = new KeekoTranslator($this, $lang); |
153
|
|
|
$this->translator->addLoader('json', new JsonFileLoader()); |
154
|
|
|
$this->translator->setFallbackLocales(['en']); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->translator; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns an instance to the puli repository |
162
|
|
|
* |
163
|
|
|
* @return ResourceRepository |
164
|
|
|
*/ |
165
|
|
|
public function getResourceRepository() { |
166
|
|
|
if ($this->puli === null) { |
167
|
|
|
$factoryClass = PULI_FACTORY_CLASS; |
168
|
|
|
$factory = new $factoryClass(); |
|
|
|
|
169
|
|
|
|
170
|
|
|
$this->puli = $factory->createRepository(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->puli; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns twig |
178
|
|
|
* |
179
|
|
|
* @return Twig_Environment |
180
|
|
|
*/ |
181
|
|
|
public function getTwig() { |
182
|
|
|
if ($this->twig === null) { |
183
|
|
|
$repo = $this->getResourceRepository(); |
|
|
|
|
184
|
|
|
$loader = new PuliTemplateLoader($repo); |
|
|
|
|
185
|
|
|
$this->twig = new Twig_Environment($loader); |
186
|
|
|
|
187
|
|
|
// puli extension |
188
|
|
|
$this->twig->addExtension(new PuliExtension($repo)); |
189
|
|
|
|
190
|
|
|
// translator function |
191
|
|
|
$translator = $this->getServiceContainer()->getTranslator(); |
|
|
|
|
192
|
|
|
$trans = function($key, $params = [], $domain = null) use ($translator) { |
|
|
|
|
193
|
|
|
return $translator->trans($key, $params, $domain); |
194
|
|
|
}; |
195
|
|
|
$this->twig->addFunction(new Twig_SimpleFunction('t', $trans)); |
196
|
|
|
|
197
|
|
|
// firewall |
198
|
|
|
$firewall = $this->getServiceContainer()->getFirewall(); |
|
|
|
|
199
|
|
|
$this->twig->addFunction(new Twig_SimpleFunction('hasPermission', function ($module, $action) use ($firewall) { |
200
|
|
|
return $firewall->hasPermission($module, $action); |
201
|
|
|
})); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this->twig; |
205
|
|
|
} |
206
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.