Completed
Push — master ( 5c3876...859b6d )
by Thierry
01:35
created

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