1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Container.php - Jaxon data container |
5
|
|
|
* |
6
|
|
|
* Provide container service for Jaxon utils class instances. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Jaxon\Utils; |
16
|
|
|
|
17
|
|
|
use Lemon\Event\EventDispatcher; |
18
|
|
|
use Jaxon\Sentry\View\Renderer; |
19
|
|
|
|
20
|
|
|
class Container |
21
|
|
|
{ |
22
|
|
|
// The Dependency Injection Container |
23
|
|
|
private $di = null; |
24
|
|
|
|
25
|
|
|
// The only instance of the Container (Singleton) |
26
|
|
|
private static $xInstance = null; |
27
|
|
|
|
28
|
|
|
public static function getInstance() |
29
|
|
|
{ |
30
|
|
|
if(!self::$xInstance) |
31
|
|
|
{ |
32
|
|
|
self::$xInstance = new Container(); |
33
|
|
|
} |
34
|
|
|
return self::$xInstance; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function __construct() |
38
|
|
|
{ |
39
|
|
|
$this->di = new \Pimple\Container(); |
40
|
|
|
|
41
|
|
|
$sTranslationDir = realpath(__DIR__ . '/../../translations'); |
42
|
|
|
$sTemplateDir = realpath(__DIR__ . '/../../templates'); |
43
|
|
|
$this->init($sTranslationDir, $sTemplateDir); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set the parameters and create the objects in the dependency injection container |
48
|
|
|
* |
49
|
|
|
* @param string $sTranslationDir The translation directory |
50
|
|
|
* @param string $sTemplateDir The template directory |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
private function init($sTranslationDir, $sTemplateDir) |
55
|
|
|
{ |
56
|
|
|
/* |
57
|
|
|
* Parameters |
58
|
|
|
*/ |
59
|
|
|
// Translation directory |
60
|
|
|
$this->di['translation_dir'] = $sTranslationDir; |
61
|
|
|
// Template directory |
62
|
|
|
$this->di['template_dir'] = $sTemplateDir; |
63
|
|
|
|
64
|
|
|
/* |
65
|
|
|
* Managers |
66
|
|
|
*/ |
67
|
|
|
// Plugin Manager |
68
|
|
|
$this->di['plugin_manager'] = function ($c) { |
69
|
|
|
return new \Jaxon\Plugin\Manager(); |
70
|
|
|
}; |
71
|
|
|
// Request Manager |
72
|
|
|
$this->di['request_manager'] = function ($c) { |
73
|
|
|
return new \Jaxon\Request\Manager(); |
74
|
|
|
}; |
75
|
|
|
// Request Factory |
76
|
|
|
$this->di['request_factory'] = function ($c) { |
77
|
|
|
return new \Jaxon\Request\Factory(); |
78
|
|
|
}; |
79
|
|
|
// Response Manager |
80
|
|
|
$this->di['response_manager'] = function ($c) { |
81
|
|
|
return new \Jaxon\Response\Manager(); |
82
|
|
|
}; |
83
|
|
|
|
84
|
|
|
/* |
85
|
|
|
* Services |
86
|
|
|
*/ |
87
|
|
|
// Config manager |
88
|
|
|
$this->di['config'] = function ($c) { |
89
|
|
|
return new Config\Config(); |
90
|
|
|
}; |
91
|
|
|
// Minifier |
92
|
|
|
$this->di['minifier'] = function ($c) { |
93
|
|
|
return new Template\Minifier(); |
94
|
|
|
}; |
95
|
|
|
// Translator |
96
|
|
|
$this->di['translator'] = function ($c) { |
97
|
|
|
return new Translation\Translator($c['translation_dir'], $c['config']); |
98
|
|
|
}; |
99
|
|
|
// Template engine |
100
|
|
|
$this->di['template'] = function ($c) { |
101
|
|
|
return new Template\Template($c['template_dir']); |
102
|
|
|
}; |
103
|
|
|
// Validator |
104
|
|
|
$this->di['validator'] = function ($c) { |
105
|
|
|
return new Validation\Validator($c['translator'], $c['config']); |
106
|
|
|
}; |
107
|
|
|
// Pagination Renderer |
108
|
|
|
$this->di['pagination.renderer'] = function ($c) { |
109
|
|
|
return new Pagination\Renderer(); |
110
|
|
|
}; |
111
|
|
|
// Pagination Paginator |
112
|
|
|
$this->di['pagination.paginator'] = function ($c) { |
113
|
|
|
return new Pagination\Paginator($c['pagination.renderer']); |
114
|
|
|
}; |
115
|
|
|
// Event Dispatcher |
116
|
|
|
$this->di['events'] = function ($c) { |
117
|
|
|
return new EventDispatcher(); |
118
|
|
|
}; |
119
|
|
|
|
120
|
|
|
/* |
121
|
|
|
* Core library objects |
122
|
|
|
*/ |
123
|
|
|
// Global Response |
124
|
|
|
$this->di['response'] = function ($c) { |
125
|
|
|
return new \Jaxon\Response\Response(); |
126
|
|
|
}; |
127
|
|
|
// Jaxon Core |
128
|
|
|
$this->di['jaxon'] = function ($c) { |
129
|
|
|
return new \Jaxon\Jaxon(); |
130
|
|
|
}; |
131
|
|
|
// View Renderer Facade |
132
|
|
|
$this->di['sentry.view.renderer'] = function ($c) { |
133
|
|
|
$aRenderers = $c['view.data.renderers']; |
134
|
|
|
$sDefaultNamespace = $c['view.data.namespace.default']; |
135
|
|
|
return new \Jaxon\Sentry\View\Facade($aRenderers, $sDefaultNamespace); |
136
|
|
|
}; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the plugin manager |
141
|
|
|
* |
142
|
|
|
* @return object The plugin manager |
143
|
|
|
*/ |
144
|
|
|
public function getPluginManager() |
145
|
|
|
{ |
146
|
|
|
return $this->di['plugin_manager']; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the request manager |
151
|
|
|
* |
152
|
|
|
* @return object The request manager |
153
|
|
|
*/ |
154
|
|
|
public function getRequestManager() |
155
|
|
|
{ |
156
|
|
|
return $this->di['request_manager']; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the request factory |
161
|
|
|
* |
162
|
|
|
* @return object The request factory |
163
|
|
|
*/ |
164
|
|
|
public function getRequestFactory() |
165
|
|
|
{ |
166
|
|
|
return $this->di['request_factory']; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the response manager |
171
|
|
|
* |
172
|
|
|
* @return object The response manager |
173
|
|
|
*/ |
174
|
|
|
public function getResponseManager() |
175
|
|
|
{ |
176
|
|
|
return $this->di['response_manager']; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the config manager |
181
|
|
|
* |
182
|
|
|
* @return Jaxon\Utils\Config\Config The config manager |
183
|
|
|
*/ |
184
|
|
|
public function getConfig() |
185
|
|
|
{ |
186
|
|
|
return $this->di['config']; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Create a new the config manager |
191
|
|
|
* |
192
|
|
|
* @return Jaxon\Utils\Config\Config The config manager |
193
|
|
|
*/ |
194
|
|
|
public function newConfig() |
195
|
|
|
{ |
196
|
|
|
return new Config\Config(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the minifier |
201
|
|
|
* |
202
|
|
|
* @return object The minifier |
203
|
|
|
*/ |
204
|
|
|
public function getMinifier() |
205
|
|
|
{ |
206
|
|
|
return $this->di['minifier']; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the translator |
211
|
|
|
* |
212
|
|
|
* @return object The translator |
213
|
|
|
*/ |
214
|
|
|
public function getTranslator() |
215
|
|
|
{ |
216
|
|
|
return $this->di['translator']; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get the template engine |
221
|
|
|
* |
222
|
|
|
* @return object The template engine |
223
|
|
|
*/ |
224
|
|
|
public function getTemplate() |
225
|
|
|
{ |
226
|
|
|
return $this->di['template']; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get the validator |
231
|
|
|
* |
232
|
|
|
* @return object The validator |
233
|
|
|
*/ |
234
|
|
|
public function getValidator() |
235
|
|
|
{ |
236
|
|
|
return $this->di['validator']; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get the paginator |
241
|
|
|
* |
242
|
|
|
* @return object The paginator |
243
|
|
|
*/ |
244
|
|
|
public function getPaginator() |
245
|
|
|
{ |
246
|
|
|
return $this->di['pagination.paginator']; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Set the pagination renderer |
251
|
|
|
* |
252
|
|
|
* @param object $xRenderer The pagination renderer |
253
|
|
|
* |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
|
|
public function setPaginationRenderer($xRenderer) |
257
|
|
|
{ |
258
|
|
|
$this->di['pagination.renderer'] = $xRenderer; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get the event dispatcher |
263
|
|
|
* |
264
|
|
|
* @return object The event dispatcher |
265
|
|
|
*/ |
266
|
|
|
public function getEventDispatcher() |
267
|
|
|
{ |
268
|
|
|
return $this->di['events']; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Get the Global Response object |
273
|
|
|
* |
274
|
|
|
* @return object The Global Response object |
275
|
|
|
*/ |
276
|
|
|
public function getResponse() |
277
|
|
|
{ |
278
|
|
|
return $this->di['response']; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Create a new Jaxon response object |
283
|
|
|
* |
284
|
|
|
* @return \Jaxon\Response\Response The new Jaxon response object |
285
|
|
|
*/ |
286
|
|
|
public function newResponse() |
287
|
|
|
{ |
288
|
|
|
return new \Jaxon\Response\Response(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get the main Jaxon object |
293
|
|
|
* |
294
|
|
|
* @return object The Jaxon object |
295
|
|
|
*/ |
296
|
|
|
public function getJaxon() |
297
|
|
|
{ |
298
|
|
|
return $this->di['jaxon']; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Get the Jaxon library version number |
303
|
|
|
* |
304
|
|
|
* @return string The version number |
305
|
|
|
*/ |
306
|
|
|
public function getVersion() |
307
|
|
|
{ |
308
|
|
|
return $this->getJaxon()->getVersion(); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Get the Sentry instance |
313
|
|
|
* |
314
|
|
|
* @return object The Sentry instance |
315
|
|
|
*/ |
316
|
|
|
public function getSentry() |
317
|
|
|
{ |
318
|
|
|
return $this->di['sentry']; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Set the Sentry instance |
323
|
|
|
* |
324
|
|
|
* @param object $xSentry The Sentry instance |
325
|
|
|
* |
326
|
|
|
* @return void |
327
|
|
|
*/ |
328
|
|
|
public function setSentry($xSentry) |
329
|
|
|
{ |
330
|
|
|
$this->di['sentry'] = $xSentry; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Get the Armada instance |
335
|
|
|
* |
336
|
|
|
* @return object The Armada instance |
337
|
|
|
*/ |
338
|
|
|
public function getArmada() |
339
|
|
|
{ |
340
|
|
|
return $this->di['armada']; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Set the Armada instance |
345
|
|
|
* |
346
|
|
|
* @param object $xArmada The Armada instance |
347
|
|
|
* |
348
|
|
|
* @return void |
349
|
|
|
*/ |
350
|
|
|
public function setArmada($xArmada) |
351
|
|
|
{ |
352
|
|
|
$this->di['armada'] = $xArmada; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Set the view renderers data |
357
|
|
|
* |
358
|
|
|
* @param array $aRenderers Array of renderer names with namespace as key |
359
|
|
|
* |
360
|
|
|
* @return void |
361
|
|
|
*/ |
362
|
|
|
public function initViewRenderers($aRenderers) |
363
|
|
|
{ |
364
|
|
|
$this->di['view.data.renderers'] = $aRenderers; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Set the view namespaces data |
369
|
|
|
* |
370
|
|
|
* @param array $aNamespaces Array of namespaces with renderer name as key |
371
|
|
|
* |
372
|
|
|
* @return void |
373
|
|
|
*/ |
374
|
|
|
public function initViewNamespaces($aNamespaces, $sDefaultNamespace) |
375
|
|
|
{ |
376
|
|
|
$this->di['view.data.namespaces'] = $aNamespaces; |
377
|
|
|
$this->di['view.data.namespace.default'] = $sDefaultNamespace; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Add a view renderer |
382
|
|
|
* |
383
|
|
|
* @param string $sId The unique identifier of the view renderer |
384
|
|
|
* @param Closure $xClosure A closure to create the view instance |
385
|
|
|
* |
386
|
|
|
* @return void |
387
|
|
|
*/ |
388
|
|
|
public function addViewRenderer($sId, $xClosure) |
389
|
|
|
{ |
390
|
|
|
// Return the non-initialiazed view renderer |
391
|
|
|
$this->di['sentry.view.base.' . $sId] = $xClosure; |
392
|
|
|
|
393
|
|
|
// Return the initialized view renderer |
394
|
|
|
$this->di['sentry.view.' . $sId] = function ($c) use ($sId) { |
395
|
|
|
// Get the defined renderer |
396
|
|
|
$renderer = $c['sentry.view.base.' . $sId]; |
397
|
|
|
// Init the renderer with the template namespaces |
398
|
|
|
$aNamespaces = $this->di['view.data.namespaces']; |
399
|
|
|
if(key_exists($sId, $aNamespaces)) |
400
|
|
|
{ |
401
|
|
|
foreach($aNamespaces[$sId] as $ns) |
402
|
|
|
{ |
403
|
|
|
$renderer->addNamespace($ns['namespace'], $ns['directory'], $ns['extension']); |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
return $renderer; |
407
|
|
|
}; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Get the view object |
412
|
|
|
* |
413
|
|
|
* @param string $sId The unique identifier of the view renderer |
414
|
|
|
* |
415
|
|
|
* @return object The view object |
416
|
|
|
*/ |
417
|
|
|
public function getViewRenderer($sId = '') |
418
|
|
|
{ |
419
|
|
|
if(!$sId) |
420
|
|
|
{ |
421
|
|
|
// Return the view renderer facade |
422
|
|
|
return $this->di['sentry.view.renderer']; |
423
|
|
|
} |
424
|
|
|
// Return the view renderer with the given id |
425
|
|
|
return $this->di['sentry.view.' . $sId]; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Get the session object |
430
|
|
|
* |
431
|
|
|
* @return object The session object |
432
|
|
|
*/ |
433
|
|
|
public function getSessionManager() |
434
|
|
|
{ |
435
|
|
|
return $this->di['armada.session']; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Set the session |
440
|
|
|
* |
441
|
|
|
* @param Closure $xClosure A closure to create the session instance |
442
|
|
|
* |
443
|
|
|
* @return void |
444
|
|
|
*/ |
445
|
|
|
public function setSessionManager($xClosure) |
446
|
|
|
{ |
447
|
|
|
$this->di['armada.session'] = $xClosure; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|