Completed
Push — master ( f42a52...0a9499 )
by Thierry
02:50 queued 01:16
created

Container::getViewManager()   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\DI;
16
17
use Jaxon\Response\Response;
18
use Jaxon\Request\Support\CallableRepository;
19
use Jaxon\Request\Plugin\CallableClass;
20
use Jaxon\Request\Plugin\CallableDir;
21
use Jaxon\Request\Plugin\CallableFunction;
22
use Jaxon\Request\Plugin\FileUpload;
23
use Jaxon\Request\Handler as RequestHandler;
24
use Jaxon\Request\Factory\RequestFactory;
25
use Jaxon\Request\Factory\ParameterFactory;
26
use Jaxon\Request\Factory\CallableClass\Request as CallableClassRequestFactory;
27
use Jaxon\Request\Factory\CallableClass\Paginator as CallableClassPaginatorFactory;
28
use Jaxon\Request\Support\CallableObject;
29
use Jaxon\Response\Manager as ResponseManager;
30
use Jaxon\Response\Plugin\JQuery as JQueryPlugin;
31
use Jaxon\Plugin\Manager as PluginManager;
32
use Jaxon\Plugin\CodeGenerator;
33
use Jaxon\Contracts\Template\Renderer as TemplateRenderer;
34
use Jaxon\Contracts\Session as SessionContract;
35
use Jaxon\Contracts\Container as ContainerContract;
36
37
use Jaxon\App\App;
38
use Jaxon\App\Bootstrap;
39
40
use Jaxon\Utils\Config\Config;
41
use Jaxon\Utils\Config\Reader as ConfigReader;
42
use Jaxon\Utils\View\Manager as ViewManager;
43
use Jaxon\Utils\View\Renderer as ViewRenderer;
44
use Jaxon\Utils\View\Renderer;
45
use Jaxon\Utils\Dialogs\Dialog;
46
use Jaxon\Utils\Template\Minifier;
47
use Jaxon\Utils\Template\Engine as TemplateEngine;
48
use Jaxon\Utils\Pagination\Paginator;
49
use Jaxon\Utils\Pagination\Renderer as PaginationRenderer;
50
use Jaxon\Utils\Validation\Validator;
51
use Jaxon\Utils\Translation\Translator;
52
53
use Lemon\Event\EventDispatcher;
54
55
class Container
56
{
57
    /**
58
     * The Dependency Injection Container
59
     *
60
     * @var \Pimple\Container
61
     */
62
    private $libContainer = null;
63
64
    /**
65
     * The Dependency Injection Container
66
     *
67
     * @var \Jaxon\Contracts\Container
68
     */
69
    private $appContainer = null;
70
71
    /**
72
     * The class constructor
73
     */
74
    public function __construct()
75
    {
76
        $this->libContainer = new \Pimple\Container();
77
78
        $sTranslationDir = realpath(__DIR__ . '/../../../translations');
79
        $sTemplateDir = realpath(__DIR__ . '/../../../templates');
80
        $this->init($sTranslationDir, $sTemplateDir);
81
    }
82
83
    /**
84
     * Get the container provided by the integrated framework
85
     *
86
     * @return ContainerContract
87
     */
88
    public function getAppContainer()
89
    {
90
        return $this->appContainer;
91
    }
92
93
    /**
94
     * Set the container provided by the integrated framework
95
     *
96
     * @param ContainerContract  $container     The container implementation
97
     *
98
     * @return void
99
     */
100
    public function setAppContainer(ContainerContract $container)
101
    {
102
        $this->appContainer = $container;
103
    }
104
105
    /**
106
     * Set the parameters and create the objects in the dependency injection container
107
     *
108
     * @param string        $sTranslationDir     The translation directory
109
     * @param string        $sTemplateDir        The template directory
110
     *
111
     * @return void
112
     */
113
    private function init($sTranslationDir, $sTemplateDir)
114
    {
115
        /*
116
         * Parameters
117
         */
118
        // Translation directory
119
        $this->libContainer['jaxon.core.translation_dir'] = $sTranslationDir;
120
        // Template directory
121
        $this->libContainer['jaxon.core.template_dir'] = $sTemplateDir;
122
123
        /*
124
         * Core library objects
125
         */
126
        // Global Response
127
        $this->libContainer[Response::class] = function () {
128
            return new Response();
129
        };
130
        // Dialog
131
        $this->libContainer[Dialog::class] = function () {
132
            return new Dialog();
133
        };
134
        // Jaxon App
135
        $this->libContainer[App::class] = function () {
136
            return new App();
137
        };
138
        // Jaxon App bootstrap
139
        $this->libContainer[Bootstrap::class] = function () {
140
            return new Bootstrap();
141
        };
142
143
        /*
144
         * Plugins
145
         */
146
        // Callable objects repository
147
        $this->libContainer[CallableRepository::class] = function () {
148
            return new CallableRepository();
149
        };
150
        // Callable class plugin
151
        $this->libContainer[CallableClass::class] = function ($c) {
152
            return new CallableClass($c[CallableRepository::class]);
153
        };
154
        // Callable dir plugin
155
        $this->libContainer[CallableDir::class] = function ($c) {
156
            return new CallableDir($c[CallableRepository::class]);
157
        };
158
        // Callable function plugin
159
        $this->libContainer[CallableFunction::class] = function () {
160
            return new CallableFunction();
161
        };
162
        // File upload plugin
163
        $this->libContainer[FileUpload::class] = function () {
164
            return new FileUpload();
165
        };
166
        // JQuery response plugin
167
        $this->libContainer[JQueryPlugin::class] = function () {
168
            return new JQueryPlugin();
169
        };
170
171
        /*
172
         * Managers
173
         */
174
        // Plugin Manager
175
        $this->libContainer[PluginManager::class] = function () {
176
            return new PluginManager();
177
        };
178
        // Request Handler
179
        $this->libContainer[RequestHandler::class] = function ($c) {
180
            return new RequestHandler($c[PluginManager::class], $c[ResponseManager::class], $c[FileUpload::class]);
181
        };
182
        // Request Factory
183
        $this->libContainer[RequestFactory::class] = function ($c) {
184
            return new RequestFactory($c[CallableRepository::class]);
185
        };
186
        // Parameter Factory
187
        $this->libContainer[ParameterFactory::class] = function () {
188
            return new ParameterFactory();
189
        };
190
        // Response Manager
191
        $this->libContainer[ResponseManager::class] = function () {
192
            return new ResponseManager();
193
        };
194
        // Code Generator
195
        $this->libContainer[CodeGenerator::class] = function ($c) {
196
            return new CodeGenerator($c[PluginManager::class], $c[TemplateEngine::class]);
197
        };
198
        // View Manager
199
        $this->libContainer[ViewManager::class] = function () {
200
            return new ViewManager();
201
        };
202
        // View Renderer
203
        $this->libContainer[ViewRenderer::class] = function ($c) {
204
            return new ViewRenderer($c[ViewManager::class]);
205
        };
206
207
        /*
208
         * Config
209
         */
210
        $this->libContainer[Config::class] = function () {
211
            return new Config();
212
        };
213
        $this->libContainer[ConfigReader::class] = function () {
214
            return new ConfigReader();
215
        };
216
217
        /*
218
         * Services
219
         */
220
        // Minifier
221
        $this->libContainer[Minifier::class] = function () {
222
            return new Minifier();
223
        };
224
        // Translator
225
        $this->libContainer[Translator::class] = function ($c) {
226
            return new Translator($c['jaxon.core.translation_dir'], $c[Config::class]);
227
        };
228
        // Template engine
229
        $this->libContainer[TemplateEngine::class] = function ($c) {
230
            return new TemplateEngine($c['jaxon.core.template_dir']);
231
        };
232
        // Template Renderer
233
        $this->libContainer[TemplateRenderer::class] = function ($c) {
234
            return $c[TemplateEngine::class];
235
        };
236
        // Validator
237
        $this->libContainer[Validator::class] = function ($c) {
238
            return new Validator($c[Translator::class], $c[Config::class]);
239
        };
240
        // Pagination Paginator
241
        $this->libContainer[Paginator::class] = function ($c) {
242
            return new Paginator($c[PaginationRenderer::class]);
243
        };
244
        // Pagination Renderer
245
        $this->libContainer[PaginationRenderer::class] = function ($c) {
246
            return new PaginationRenderer($c[TemplateRenderer::class]);
247
        };
248
        // Event Dispatcher
249
        $this->libContainer[EventDispatcher::class] = function () {
250
            return new EventDispatcher();
251
        };
252
    }
253
254
    /**
255
     * Get a class instance
256
     *
257
     * @return object        The class instance
258
     */
259
    public function get($sClass)
260
    {
261
        if($this->appContainer != null && $this->appContainer->has($sClass))
262
        {
263
            return $this->appContainer->get($sClass);
264
        }
265
        return $this->libContainer[$sClass];
266
    }
267
268
    /**
269
     * Set a DI closure
270
     *
271
     * @param string                $sClass             The full class name
272
     * @param Closure               $xClosure           The closure
273
     *
274
     * @return void
275
     */
276
    public function set($sClass, $xClosure)
277
    {
278
        $this->libContainer[$sClass] = $xClosure;
279
    }
280
281
    /**
282
     * Set an alias
283
     *
284
     * @param string                $sClass             The class name
285
     * @param string                $sAlias             The alias name
286
     *
287
     * @return void
288
     */
289
    public function alias($sClass, $sAlias)
290
    {
291
        $this->libContainer[$sClass] = function ($c) use ($sAlias) {
292
            return $c[$sAlias];
293
        };
294
    }
295
296
    /**
297
     * Get the plugin manager
298
     *
299
     * @return PluginManager
300
     */
301
    public function getPluginManager()
302
    {
303
        return $this->libContainer[PluginManager::class];
304
    }
305
306
    /**
307
     * Get the request handler
308
     *
309
     * @return RequestHandler
310
     */
311
    public function getRequestHandler()
312
    {
313
        return $this->libContainer[RequestHandler::class];
314
    }
315
316
    /**
317
     * Get the request factory
318
     *
319
     * @return RequestFactory
320
     */
321
    public function getRequestFactory()
322
    {
323
        return $this->libContainer[RequestFactory::class];
324
    }
325
326
    /**
327
     * Get the parameter factory
328
     *
329
     * @return ParameterFactory
330
     */
331
    public function getParameterFactory()
332
    {
333
        return $this->libContainer[ParameterFactory::class];
334
    }
335
336
    /**
337
     * Get the response manager
338
     *
339
     * @return ResponseManager
340
     */
341
    public function getResponseManager()
342
    {
343
        return $this->libContainer[ResponseManager::class];
344
    }
345
346
    /**
347
     * Get the code generator
348
     *
349
     * @return CodeGenerator
350
     */
351
    public function getCodeGenerator()
352
    {
353
        return $this->libContainer[CodeGenerator::class];
354
    }
355
356
    /**
357
     * Get the callable repository
358
     *
359
     * @return CallableRepository
360
     */
361
    public function getCallableRepository()
362
    {
363
        return $this->libContainer[CallableRepository::class];
364
    }
365
366
    /**
367
     * Get the config manager
368
     *
369
     * @return Config
370
     */
371
    public function getConfig()
372
    {
373
        return $this->libContainer[Config::class];
374
    }
375
376
    /**
377
     * Create a new the config manager
378
     *
379
     * @param array             $aOptions           The options array
380
     * @param string            $sKeys              The keys of the options in the array
381
     *
382
     * @return Config            The config manager
383
     */
384
    public function newConfig(array $aOptions = [], $sKeys = '')
385
    {
386
        return new Config($aOptions, $sKeys);
387
    }
388
389
    /**
390
     * Get the dialog wrapper
391
     *
392
     * @return Dialog
393
     */
394
    public function getDialog()
395
    {
396
        return $this->libContainer[Dialog::class];
397
    }
398
399
    /**
400
     * Get the minifier
401
     *
402
     * @return Minifier
403
     */
404
    public function getMinifier()
405
    {
406
        return $this->libContainer[Minifier::class];
407
    }
408
409
    /**
410
     * Get the translator
411
     *
412
     * @return Translator
413
     */
414
    public function getTranslator()
415
    {
416
        return $this->libContainer[Translator::class];
417
    }
418
419
    /**
420
     * Get the template engine
421
     *
422
     * @return Engine
423
     */
424
    public function getTemplateEngine()
425
    {
426
        return $this->libContainer[TemplateEngine::class];
427
    }
428
429
    /**
430
     * Get the template renderer
431
     *
432
     * @return TemplateRenderer
433
     */
434
    public function getTemplateRenderer()
435
    {
436
        return $this->libContainer[TemplateRenderer::class];
437
    }
438
439
    /**
440
     * Get the validator
441
     *
442
     * @return Validator
443
     */
444
    public function getValidator()
445
    {
446
        return $this->libContainer[Validator::class];
447
    }
448
449
    /**
450
     * Get the paginator
451
     *
452
     * @return Paginator
453
     */
454
    public function getPaginator()
455
    {
456
        return $this->libContainer[Paginator::class];
457
    }
458
459
    /**
460
     * Get the event dispatcher
461
     *
462
     * @return EventDispatcher
463
     */
464
    public function getEventDispatcher()
465
    {
466
        return $this->libContainer[EventDispatcher::class];
467
    }
468
469
    /**
470
     * Get the global Response object
471
     *
472
     * @return Response
473
     */
474
    public function getResponse()
475
    {
476
        return $this->libContainer[Response::class];
477
    }
478
479
    /**
480
     * Create a new Jaxon response object
481
     *
482
     * @return Response
483
     */
484
    public function newResponse()
485
    {
486
        return new Response();
487
    }
488
489
    /**
490
     * Get the App instance
491
     *
492
     * @return App
493
     */
494
    public function getApp()
495
    {
496
        return $this->libContainer[App::class];
497
    }
498
499
    /**
500
     * Get the App bootstrap
501
     *
502
     * @return Bootstrap
503
     */
504
    public function getBootstrap()
505
    {
506
        return $this->libContainer[Bootstrap::class];
507
    }
508
509
    /**
510
     * Get the view manager
511
     *
512
     * @return ViewManager
513
     */
514
    public function getViewManager()
515
    {
516
        return $this->libContainer[ViewManager::class];
517
    }
518
519
    /**
520
     * Get the view facade
521
     *
522
     * @return ViewRenderer
523
     */
524
    public function getViewRenderer()
525
    {
526
        return $this->libContainer[ViewRenderer::class];
527
    }
528
529
    /**
530
     * Get the session manager
531
     *
532
     * @return SessionContract
533
     */
534
    public function getSessionManager()
535
    {
536
        return $this->libContainer[SessionContract::class];
537
    }
538
539
    /**
540
     * Set the session manager
541
     *
542
     * @param Closure      $xClosure      A closure to create the session manager instance
543
     *
544
     * @return void
545
     */
546
    public function setSessionManager($xClosure)
547
    {
548
        $this->libContainer[SessionContract::class] = $xClosure;
549
    }
550
551
    /**
552
     * Set the callable class request factory
553
     *
554
     * @param string            $sClassName         The callable class name
555
     * @param CallableObject    $xCallableObject    The corresponding callable object
556
     *
557
     * @return void
558
     */
559
    public function setCallableClassRequestFactory($sClassName, CallableObject $xCallableObject)
560
    {
561
        $this->libContainer[$sClassName . '_RequestFactory'] = function () use ($xCallableObject) {
562
            // $xCallableObject = $c[CallableRepository::class]->getCallableObject($sClassName);
563
            return new CallableClassRequestFactory($xCallableObject);
564
        };
565
    }
566
567
    /**
568
     * Get the callable class request factory
569
     *
570
     * @param string        $sClassName             The callable class name
571
     *
572
     * @return CallableClassRequestFactory
573
     */
574
    public function getCallableClassRequestFactory($sClassName)
575
    {
576
        return $this->libContainer[$sClassName . '_RequestFactory'];
577
    }
578
579
    /**
580
     * Set the callable class paginator factory
581
     *
582
     * @param string            $sClassName         The callable class name
583
     * @param CallableObject    $xCallableObject    The corresponding callable object
584
     *
585
     * @return void
586
     */
587
    public function setCallableClassPaginatorFactory($sClassName, CallableObject $xCallableObject)
588
    {
589
        $this->libContainer[$sClassName . '_PaginatorFactory'] = function () use ($xCallableObject) {
590
            // $xCallableObject = $c[CallableRepository::class]->getCallableObject($sClassName);
591
            return new CallableClassPaginatorFactory($xCallableObject);
592
        };
593
    }
594
595
    /**
596
     * Get the callable class paginator factory
597
     *
598
     * @param string        $sClassName             The callable class name
599
     *
600
     * @return CallableClassPaginatorFactory
601
     */
602
    public function getCallableClassPaginatorFactory($sClassName)
603
    {
604
        return $this->libContainer[$sClassName . '_PaginatorFactory'];
605
    }
606
}
607