|
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
|
|
|
use Puli\Discovery\Api\Discovery; |
|
18
|
|
|
use Puli\UrlGenerator\Api\UrlGenerator; |
|
19
|
|
|
|
|
20
|
|
|
class ServiceContainer { |
|
21
|
|
|
|
|
22
|
|
|
/** @var PackageManager */ |
|
23
|
|
|
private $packageManager; |
|
24
|
|
|
|
|
25
|
|
|
/** @var ModuleManager */ |
|
26
|
|
|
private $moduleManager; |
|
27
|
|
|
|
|
28
|
|
|
/** @var AuthManager */ |
|
29
|
|
|
private $authManager; |
|
30
|
|
|
|
|
31
|
|
|
/** @var PreferenceLoader */ |
|
32
|
|
|
private $preferenceLoader; |
|
33
|
|
|
|
|
34
|
|
|
/** @var Firewall */ |
|
35
|
|
|
private $firewall; |
|
36
|
|
|
|
|
37
|
|
|
/** @var KeekoTranslator */ |
|
38
|
|
|
private $translator; |
|
39
|
|
|
|
|
40
|
|
|
/** @var EventDispatcher */ |
|
41
|
|
|
private $dispatcher; |
|
42
|
|
|
|
|
43
|
|
|
/** @var AbstractKernel */ |
|
44
|
|
|
private $kernel; |
|
45
|
|
|
|
|
46
|
|
|
/** @var Twig_Environment */ |
|
47
|
|
|
private $twig; |
|
48
|
|
|
|
|
49
|
|
|
/** @var Puli\GeneratedPuliFactory */ |
|
50
|
|
|
private $puliFactory; |
|
51
|
|
|
|
|
52
|
|
|
/** @var ResourceRepository */ |
|
53
|
|
|
private $resourceRepository; |
|
54
|
|
|
|
|
55
|
|
|
/** @var Discovery */ |
|
56
|
|
|
private $resourceDiscovery; |
|
57
|
|
|
|
|
58
|
|
|
/** @var UrlGenerator */ |
|
59
|
|
|
private $urlGenerator; |
|
60
|
|
|
|
|
61
|
|
|
/** @var ExtensionRegistry */ |
|
62
|
|
|
private $extensionRegistry; |
|
63
|
|
|
|
|
64
|
|
|
public function __construct(AbstractKernel $kernel) { |
|
65
|
|
|
$this->kernel = $kernel; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns the kernel |
|
70
|
|
|
* |
|
71
|
|
|
* @return AbstractKernel |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getKernel() { |
|
74
|
|
|
return $this->kernel; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the event dispatcher |
|
79
|
|
|
* |
|
80
|
|
|
* @return EventDispatcher |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getDispatcher() { |
|
83
|
|
|
if ($this->dispatcher === null) { |
|
84
|
|
|
$this->dispatcher = new EventDispatcher(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->dispatcher; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns the package manager |
|
92
|
|
|
* |
|
93
|
|
|
* @return PackageManager |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getPackageManager() { |
|
96
|
|
|
if ($this->packageManager === null) { |
|
97
|
|
|
$this->packageManager = new PackageManager($this); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->packageManager; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns the module manager |
|
105
|
|
|
* |
|
106
|
|
|
* @return ModuleManager |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getModuleManager() { |
|
109
|
|
|
if ($this->moduleManager === null) { |
|
110
|
|
|
$this->moduleManager = new ModuleManager($this); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this->moduleManager; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the auth manager |
|
118
|
|
|
* |
|
119
|
|
|
* @return AuthManager |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getAuthManager() { |
|
122
|
|
|
if ($this->authManager === null) { |
|
123
|
|
|
$this->authManager = new AuthManager(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $this->authManager; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns the preference loader |
|
131
|
|
|
* |
|
132
|
|
|
* @return PreferenceLoader |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getPreferenceLoader() { |
|
135
|
|
|
if ($this->preferenceLoader === null) { |
|
136
|
|
|
$this->preferenceLoader = new PreferenceLoader(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $this->preferenceLoader; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns the firewall |
|
144
|
|
|
* |
|
145
|
|
|
* @return Firewall |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getFirewall() { |
|
148
|
|
|
if ($this->firewall === null) { |
|
149
|
|
|
$this->firewall = new Firewall($this); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $this->firewall; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Returns the keeko translation service |
|
157
|
|
|
* |
|
158
|
|
|
* @return KeekoTranslator |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getTranslator() { |
|
161
|
|
|
// TODO: how to get the language |
|
162
|
|
|
if ($this->translator === null) { |
|
163
|
|
|
$app = $this->getKernel()->getApplication(); |
|
|
|
|
|
|
164
|
|
|
$lang = $app->getLocalization()->getLanguage()->getAlpha2(); |
|
|
|
|
|
|
165
|
|
|
$this->translator = new KeekoTranslator($this, $lang); |
|
166
|
|
|
$this->translator->addLoader('json', new JsonFileLoader()); |
|
167
|
|
|
$this->translator->setFallbackLocales(['en']); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $this->translator; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* |
|
175
|
|
|
* @return Puli\GeneratedPuliFactory |
|
176
|
|
|
*/ |
|
177
|
|
|
private function getPuliFactory() { |
|
178
|
|
|
if ($this->puliFactory === null) { |
|
179
|
|
|
$factoryClass = PULI_FACTORY_CLASS; |
|
|
|
|
|
|
180
|
|
|
$this->puliFactory = new $factoryClass(); |
|
181
|
|
|
} |
|
182
|
|
|
return $this->puliFactory; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Returns an instance to the puli repository |
|
187
|
|
|
* |
|
188
|
|
|
* @return ResourceRepository |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getResourceRepository() { |
|
191
|
|
|
if ($this->resourceRepository === null) { |
|
192
|
|
|
$this->resourceRepository = $this->getPuliFactory()->createRepository(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return $this->resourceRepository; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Returns an instance to the puli discovery |
|
200
|
|
|
* |
|
201
|
|
|
* @return Discovery |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getResourceDiscovery() { |
|
204
|
|
|
if ($this->resourceDiscovery === null) { |
|
205
|
|
|
$repo = $this->getResourceRepository(); |
|
|
|
|
|
|
206
|
|
|
$this->resourceDiscovery = $this->getPuliFactory()->createDiscovery($repo); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $this->resourceDiscovery; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Returns the url generator for puli resources |
|
214
|
|
|
* |
|
215
|
|
|
* @return UrlGenerator |
|
216
|
|
|
*/ |
|
217
|
|
|
public function getUrlGenerator() { |
|
218
|
|
|
if ($this->urlGenerator === null) { |
|
219
|
|
|
$discovery = $this->getResourceDiscovery(); |
|
|
|
|
|
|
220
|
|
|
$this->urlGenerator = $this->getPuliFactory()->createUrlGenerator($discovery); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return $this->urlGenerator; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Returns twig |
|
228
|
|
|
* |
|
229
|
|
|
* @return Twig_Environment |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getTwig() { |
|
232
|
|
|
if ($this->twig === null) { |
|
233
|
|
|
$repo = $this->getResourceRepository(); |
|
|
|
|
|
|
234
|
|
|
$loader = new PuliTemplateLoader($repo); |
|
|
|
|
|
|
235
|
|
|
$this->twig = new Twig_Environment($loader); |
|
236
|
|
|
|
|
237
|
|
|
// puli extension |
|
238
|
|
|
$generator = $this->getUrlGenerator(); |
|
239
|
|
|
$this->twig->addExtension(new PuliExtension($repo, $generator)); |
|
240
|
|
|
|
|
241
|
|
|
// translator function |
|
242
|
|
|
$translator = $this->getTranslator(); |
|
243
|
|
|
$trans = function($key, $params = [], $domain = null) use ($translator) { |
|
|
|
|
|
|
244
|
|
|
return $translator->trans($key, $params, $domain); |
|
245
|
|
|
}; |
|
246
|
|
|
$this->twig->addFunction(new Twig_SimpleFunction('t', $trans)); |
|
247
|
|
|
|
|
248
|
|
|
// firewall |
|
249
|
|
|
$firewall = $this->getFirewall(); |
|
250
|
|
|
$this->twig->addFunction(new Twig_SimpleFunction('hasPermission', function ($module, $action) use ($firewall) { |
|
251
|
|
|
return $firewall->hasPermission($module, $action); |
|
252
|
|
|
})); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
return $this->twig; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Returns the extension registry |
|
260
|
|
|
* |
|
261
|
|
|
* @return ExtensionRegistry |
|
262
|
|
|
*/ |
|
263
|
|
|
public function getExtensionRegistry() { |
|
264
|
|
|
if ($this->extensionRegistry === null) { |
|
265
|
|
|
$this->extensionRegistry = new ExtensionRegistry(); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return $this->extensionRegistry; |
|
269
|
|
|
} |
|
270
|
|
|
} |
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.