Completed
Push — master ( 3ab6e3...8e1449 )
by Thierry
01:54
created

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