Completed
Push — master ( 8e1449...c61dd9 )
by Thierry
01:56
created

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