Completed
Push — master ( 331d03...c48bd4 )
by Thierry
01:44
created

Container::getParameterFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\DI;
16
17
use Lemon\Event\EventDispatcher;
18
use Jaxon\Sentry\View\Renderer;
19
20
class Container
21
{
22
    // The Dependency Injection Container
23
    private $coreContainer = null;
24
25
    // The Dependency Injection Container
26
    private $sentryContainer = null;
27
28
    // The only instance of the Container (Singleton)
29
    private static $xInstance = null;
30
31
    public static function getInstance()
32
    {
33
        if(!self::$xInstance)
34
        {
35
            self::$xInstance = new Container();
36
        }
37
        return self::$xInstance;
38
    }
39
40
    private function __construct()
41
    {
42
        $this->coreContainer = new \Pimple\Container();
43
44
        $sTranslationDir = realpath(__DIR__ . '/../../translations');
45
        $sTemplateDir = realpath(__DIR__ . '/../../templates');
46
        $this->init($sTranslationDir, $sTemplateDir);
47
    }
48
49
    /**
50
     * Get the container provided by the integrated framework
51
     *
52
     * @return ContainerInterface
53
     */
54
    public function getSentryContainer()
55
    {
56
        return $this->sentryContainer;
57
    }
58
59
    /**
60
     * Set the container provided by the integrated framework
61
     *
62
     * @param ContainerInterface  $container     The container implementation
63
     *
64
     * @return void
65
     */
66
    public function setSentryContainer(ContainerInterface $container)
67
    {
68
        $this->sentryContainer = $container;
69
    }
70
71
    /**
72
     * Set the parameters and create the objects in the dependency injection container
73
     *
74
     * @param string        $sTranslationDir     The translation directory
75
     * @param string        $sTemplateDir        The template directory
76
     *
77
     * @return void
78
     */
79
    private function init($sTranslationDir, $sTemplateDir)
80
    {
81
        /*
82
         * Parameters
83
         */
84
        // Translation directory
85
        $this->coreContainer['jaxon.core.translation_dir'] = $sTranslationDir;
86
        // Template directory
87
        $this->coreContainer['jaxon.core.template_dir'] = $sTemplateDir;
88
89
        /*
90
         * Managers
91
         */
92
        // Plugin Manager
93
        $this->coreContainer['jaxon.core.plugin_manager'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
            return new \Jaxon\Plugin\Manager();
95
        };
96
        // Request Manager
97
        $this->coreContainer['jaxon.core.request_manager'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
            return new \Jaxon\Request\Manager();
99
        };
100
        // Request Factory
101
        $this->coreContainer['jaxon.core.request_factory'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
            return new \Jaxon\Factory\Request();
103
        };
104
        // Parameter Factory
105
        $this->coreContainer['jaxon.core.parameter_factory'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
            return new \Jaxon\Factory\Parameter();
107
        };
108
        // Response Manager
109
        $this->coreContainer['jaxon.core.response_manager'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
            return new \Jaxon\Response\Manager();
111
        };
112
113
        /*
114
         * Services
115
         */
116
        // Config manager
117
        $this->coreContainer['jaxon.core.config'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
            return new \Jaxon\Utils\Config\Config();
119
        };
120
        // Minifier
121
        $this->coreContainer['jaxon.core.minifier'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
            return new \Jaxon\Utils\Template\Minifier();
123
        };
124
        // Translator
125
        $this->coreContainer['jaxon.core.translator'] = function ($c) {
126
            return new \Jaxon\Utils\Translation\Translator($c['jaxon.core.translation_dir'], $c['jaxon.core.config']);
127
        };
128
        // Template engine
129
        $this->coreContainer['jaxon.core.template'] = function ($c) {
130
            return new \Jaxon\Utils\Template\Template($c['jaxon.core.template_dir']);
131
        };
132
        // Validator
133
        $this->coreContainer['jaxon.core.validator'] = function ($c) {
134
            return new \Jaxon\Utils\Validation\Validator($c['jaxon.core.translator'], $c['jaxon.core.config']);
135
        };
136
        // Pagination Renderer
137
        $this->coreContainer['jaxon.pagination.renderer'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
            return new \Jaxon\Utils\Pagination\Renderer();
139
        };
140
        // Pagination Paginator
141
        $this->coreContainer['jaxon.pagination.paginator'] = function ($c) {
142
            return new \Jaxon\Utils\Pagination\Paginator($c['jaxon.pagination.renderer']);
143
        };
144
        // Event Dispatcher
145
        $this->coreContainer['jaxon.core.events'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146
            return new EventDispatcher();
147
        };
148
149
        /*
150
         * Core library objects
151
         */
152
        // Global Response
153
        $this->coreContainer['jaxon.core.response'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
            return new \Jaxon\Response\Response();
155
        };
156
        // Jaxon Core
157
        $this->coreContainer['jaxon.core.jaxon'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158
            return new \Jaxon\Jaxon();
159
        };
160
        // View Renderer Facade
161
        $this->coreContainer['jaxon.sentry.view.renderer'] = function ($c) {
162
            $aRenderers = $c['jaxon.view.data.renderers'];
163
            $sDefaultNamespace = $c['jaxon.view.data.namespace.default'];
164
            return new \Jaxon\Sentry\View\Facade($aRenderers, $sDefaultNamespace);
165
        };
166
    }
167
168
    /**
169
     * Get a class instance
170
     *
171
     * @return object        The class instance
172
     */
173
    public function get($sClass)
174
    {
175
        if($this->sentryContainer != null && $this->sentryContainer->has($sClass))
176
        {
177
            return $this->sentryContainer->get($sClass);
178
        }
179
        return $this->coreContainer[$sClass];
180
    }
181
182
    /**
183
     * Set a DI closure
184
     *
185
     * @param string                $sClass             The full class name
186
     * @param Closure               $xClosure           The closure
187
     *
188
     * @return void
189
     */
190
    public function set($sClass, $xClosure)
191
    {
192
        $this->coreContainer[$sClass] = $xClosure;
193
    }
194
195
    /**
196
     * Get the plugin manager
197
     *
198
     * @return object        The plugin manager
199
     */
200
    public function getPluginManager()
201
    {
202
        return $this->coreContainer['jaxon.core.plugin_manager'];
203
    }
204
205
    /**
206
     * Get the request manager
207
     *
208
     * @return object        The request manager
209
     */
210
    public function getRequestManager()
211
    {
212
        return $this->coreContainer['jaxon.core.request_manager'];
213
    }
214
215
    /**
216
     * Get the request factory
217
     *
218
     * @return object        The request factory
219
     */
220
    public function getRequestFactory()
221
    {
222
        return $this->coreContainer['jaxon.core.request_factory'];
223
    }
224
225
    /**
226
     * Get the parameter factory
227
     *
228
     * @return object        The parameter factory
229
     */
230
    public function getParameterFactory()
231
    {
232
        return $this->coreContainer['jaxon.core.parameter_factory'];
233
    }
234
235
    /**
236
     * Get the response manager
237
     *
238
     * @return object        The response manager
239
     */
240
    public function getResponseManager()
241
    {
242
        return $this->coreContainer['jaxon.core.response_manager'];
243
    }
244
245
    /**
246
     * Get the config manager
247
     *
248
     * @return Jaxon\Utils\Config\Config            The config manager
249
     */
250
    public function getConfig()
251
    {
252
        return $this->coreContainer['jaxon.core.config'];
253
    }
254
255
    /**
256
     * Create a new the config manager
257
     *
258
     * @return Jaxon\Utils\Config\Config            The config manager
259
     */
260
    public function newConfig()
261
    {
262
        return new \Jaxon\Utils\Config\Config();
263
    }
264
265
    /**
266
     * Get the minifier
267
     *
268
     * @return object        The minifier
269
     */
270
    public function getMinifier()
271
    {
272
        return $this->coreContainer['jaxon.core.minifier'];
273
    }
274
275
    /**
276
     * Get the translator
277
     *
278
     * @return object        The translator
279
     */
280
    public function getTranslator()
281
    {
282
        return $this->coreContainer['jaxon.core.translator'];
283
    }
284
285
    /**
286
     * Get the template engine
287
     *
288
     * @return object        The template engine
289
     */
290
    public function getTemplate()
291
    {
292
        return $this->coreContainer['jaxon.core.template'];
293
    }
294
295
    /**
296
     * Get the validator
297
     *
298
     * @return object        The validator
299
     */
300
    public function getValidator()
301
    {
302
        return $this->coreContainer['jaxon.core.validator'];
303
    }
304
305
    /**
306
     * Get the paginator
307
     *
308
     * @return object        The paginator
309
     */
310
    public function getPaginator()
311
    {
312
        return $this->coreContainer['jaxon.pagination.paginator'];
313
    }
314
315
    /**
316
     * Set the pagination renderer
317
     *
318
     * @param object        $xRenderer              The pagination renderer
319
     *
320
     * @return void
321
     */
322
    public function setPaginationRenderer($xRenderer)
323
    {
324
        $this->coreContainer['jaxon.pagination.renderer'] = $xRenderer;
325
    }
326
327
    /**
328
     * Get the event dispatcher
329
     *
330
     * @return object        The event dispatcher
331
     */
332
    public function getEventDispatcher()
333
    {
334
        return $this->coreContainer['jaxon.core.events'];
335
    }
336
337
    /**
338
     * Get the Global Response object
339
     *
340
     * @return object        The Global Response object
341
     */
342
    public function getResponse()
343
    {
344
        return $this->coreContainer['jaxon.core.response'];
345
    }
346
347
    /**
348
     * Create a new Jaxon response object
349
     *
350
     * @return \Jaxon\Response\Response        The new Jaxon response object
351
     */
352
    public function newResponse()
353
    {
354
        return new \Jaxon\Response\Response();
355
    }
356
357
    /**
358
     * Get the main Jaxon object
359
     *
360
     * @return object        The Jaxon object
361
     */
362
    public function getJaxon()
363
    {
364
        return $this->coreContainer['jaxon.core.jaxon'];
365
    }
366
367
    /**
368
     * Get the Jaxon library version number
369
     *
370
     * @return string        The version number
371
     */
372
    public function getVersion()
373
    {
374
        return $this->getJaxon()->getVersion();
375
    }
376
377
    /**
378
     * Get the Sentry instance
379
     *
380
     * @return object        The Sentry instance
381
     */
382
    public function getSentry()
383
    {
384
        return $this->coreContainer['jaxon.sentry'];
385
    }
386
387
    /**
388
     * Set the Sentry instance
389
     *
390
     * @param object                $xSentry            The Sentry instance
391
     *
392
     * @return void
393
     */
394
    public function setSentry($xSentry)
395
    {
396
        $this->coreContainer['jaxon.sentry'] = $xSentry;
397
    }
398
399
    /**
400
     * Get the Armada instance
401
     *
402
     * @return object        The Armada instance
403
     */
404
    public function getArmada()
405
    {
406
        return $this->coreContainer['jaxon.armada'];
407
    }
408
409
    /**
410
     * Set the Armada instance
411
     *
412
     * @param object                $xArmada            The Armada instance
413
     *
414
     * @return void
415
     */
416
    public function setArmada($xArmada)
417
    {
418
        $this->coreContainer['jaxon.armada'] = $xArmada;
419
    }
420
421
    /**
422
     * Set the view renderers data
423
     *
424
     * @param array                $aRenderers          Array of renderer names with namespace as key
425
     *
426
     * @return void
427
     */
428
    public function initViewRenderers($aRenderers)
429
    {
430
        $this->coreContainer['jaxon.view.data.renderers'] = $aRenderers;
431
    }
432
433
    /**
434
     * Set the view namespaces data
435
     *
436
     * @param array                $aNamespaces         Array of namespaces with renderer name as key
437
     *
438
     * @return void
439
     */
440
    public function initViewNamespaces($aNamespaces, $sDefaultNamespace)
441
    {
442
        $this->coreContainer['jaxon.view.data.namespaces'] = $aNamespaces;
443
        $this->coreContainer['jaxon.view.data.namespace.default'] = $sDefaultNamespace;
444
    }
445
446
    /**
447
     * Add a view renderer
448
     *
449
     * @param string                $sId                The unique identifier of the view renderer
450
     * @param Closure               $xClosure           A closure to create the view instance
451
     *
452
     * @return void
453
     */
454
    public function addViewRenderer($sId, $xClosure)
455
    {
456
        // Return the non-initialiazed view renderer
457
        $this->coreContainer['jaxon.sentry.view.base.' . $sId] = $xClosure;
458
459
        // Return the initialized view renderer
460
        $this->coreContainer['jaxon.sentry.view.' . $sId] = function ($c) use ($sId) {
461
            // Get the defined renderer
462
            $renderer = $c['jaxon.sentry.view.base.' . $sId];
463
            // Init the renderer with the template namespaces
464
            $aNamespaces = $this->coreContainer['jaxon.view.data.namespaces'];
465
            if(key_exists($sId, $aNamespaces))
466
            {
467
                foreach($aNamespaces[$sId] as $ns)
468
                {
469
                    $renderer->addNamespace($ns['namespace'], $ns['directory'], $ns['extension']);
470
                }
471
            }
472
            return $renderer;
473
        };
474
    }
475
476
    /**
477
     * Get the view object
478
     *
479
     * @param string                $sId                The unique identifier of the view renderer
480
     *
481
     * @return object        The view object
482
     */
483
    public function getViewRenderer($sId = '')
484
    {
485
        if(!$sId)
486
        {
487
            // Return the view renderer facade
488
            return $this->coreContainer['jaxon.sentry.view.renderer'];
489
        }
490
        // Return the view renderer with the given id
491
        return $this->coreContainer['jaxon.sentry.view.' . $sId];
492
    }
493
494
    /**
495
     * Get the session object
496
     *
497
     * @return object        The session object
498
     */
499
    public function getSessionManager()
500
    {
501
        return $this->coreContainer['jaxon.armada.session'];
502
    }
503
504
    /**
505
     * Set the session
506
     *
507
     * @param Closure               $xClosure           A closure to create the session instance
508
     *
509
     * @return void
510
     */
511
    public function setSessionManager($xClosure)
512
    {
513
        $this->coreContainer['jaxon.armada.session'] = $xClosure;
514
    }
515
}
516