Passed
Push — feature/code_improvement ( 94564c...8399ab )
by Thierry
02:36
created

Container::setCallableClassRequestFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
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
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\Utils\DI;
16
17
use Jaxon\Response\Response;
18
use Jaxon\Request\Support\CallableRegistry;
19
use Jaxon\Request\Support\CallableRepository;
20
use Jaxon\Request\Plugin\CallableClass;
21
use Jaxon\Request\Plugin\CallableDir;
22
use Jaxon\Request\Plugin\CallableFunction;
23
use Jaxon\Request\Plugin\FileUpload;
24
use Jaxon\Request\Support\FileUpload as FileUploadSupport;
25
use Jaxon\Request\Handler\Handler as RequestHandler;
26
use Jaxon\Request\Factory\RequestFactory;
27
use Jaxon\Request\Factory\ParameterFactory;
28
use Jaxon\Request\Factory\CallableClass\Request as CallableClassRequestFactory;
29
use Jaxon\Request\Factory\CallableClass\Paginator as CallableClassPaginatorFactory;
30
use Jaxon\Request\Support\CallableObject;
31
use Jaxon\Response\Manager as ResponseManager;
32
use Jaxon\Response\Plugin\JQuery as JQueryPlugin;
33
use Jaxon\Plugin\Manager as PluginManager;
34
use Jaxon\Plugin\CodeGenerator;
35
use Jaxon\Contracts\Template\Renderer as TemplateRenderer;
36
use Jaxon\Contracts\Session as SessionContract;
37
use Jaxon\Contracts\Container as ContainerContract;
38
39
use Jaxon\App\App;
40
use Jaxon\App\Bootstrap;
41
42
use Jaxon\Utils\Config\Config;
43
use Jaxon\Utils\Config\Reader as ConfigReader;
44
use Jaxon\Utils\View\Manager as ViewManager;
45
use Jaxon\Utils\View\Renderer as ViewRenderer;
46
use Jaxon\Utils\View\Renderer;
47
use Jaxon\Utils\Dialogs\Dialog;
48
use Jaxon\Utils\Template\Minifier;
49
use Jaxon\Utils\Template\Engine as TemplateEngine;
50
use Jaxon\Utils\Pagination\Paginator;
51
use Jaxon\Utils\Pagination\Renderer as PaginationRenderer;
52
use Jaxon\Utils\Validation\Validator;
53
use Jaxon\Utils\Translation\Translator;
54
use Jaxon\Utils\Http\URI;
55
56
use Lemon\Event\EventDispatcher;
57
use Closure;
58
59
class Container
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Container
Loading history...
60
{
61
    /**
62
     * The Dependency Injection Container
63
     *
64
     * @var \Pimple\Container
65
     */
66
    private $libContainer = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
67
68
    /**
69
     * The Dependency Injection Container
70
     *
71
     * @var \Jaxon\Contracts\Container
72
     */
73
    private $appContainer = null;
74
75
    /**
76
     * The class constructor
77
     */
78
    public function __construct()
79
    {
80
        $this->libContainer = new \Pimple\Container();
81
82
        $sTranslationDir = realpath(__DIR__ . '/../../../translations');
83
        $sTemplateDir = realpath(__DIR__ . '/../../../templates');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
84
        $this->init($sTranslationDir, $sTemplateDir);
85
    }
86
87
    /**
88
     * Get the container provided by the integrated framework
89
     *
90
     * @return ContainerContract
91
     */
92
    public function getAppContainer()
93
    {
94
        return $this->appContainer;
95
    }
96
97
    /**
98
     * Set the container provided by the integrated framework
99
     *
100
     * @param ContainerContract  $container     The container implementation
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
101
     *
102
     * @return void
103
     */
104
    public function setAppContainer(ContainerContract $container)
105
    {
106
        $this->appContainer = $container;
107
    }
108
109
    /**
110
     * Set the parameters and create the objects in the dependency injection container
111
     *
112
     * @param string        $sTranslationDir     The translation directory
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
113
     * @param string        $sTemplateDir        The template directory
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 8 found
Loading history...
114
     *
115
     * @return void
116
     */
117
    private function init($sTranslationDir, $sTemplateDir)
118
    {
119
        /*
120
         * Parameters
121
         */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
122
        // Translation directory
123
        $this->libContainer['jaxon.core.translation_dir'] = $sTranslationDir;
124
        // Template directory
125
        $this->libContainer['jaxon.core.template_dir'] = $sTemplateDir;
126
127
        /*
128
         * Core library objects
129
         */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
130
        // Global Response
131
        $this->libContainer[Response::class] = function() {
132
            return new Response();
133
        };
134
        // Dialog
135
        $this->libContainer[Dialog::class] = function() {
136
            return new Dialog();
137
        };
138
        // Jaxon App
139
        $this->libContainer[App::class] = function() {
140
            return new App();
141
        };
142
        // Jaxon App bootstrap
143
        $this->libContainer[Bootstrap::class] = function() {
144
            return new Bootstrap();
145
        };
146
147
        /*
148
         * Plugins
149
         */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
150
        // Callable objects repository
151
        $this->libContainer[CallableRepository::class] = function() {
152
            return new CallableRepository();
153
        };
154
        // Callable objects registry
155
        $this->libContainer[CallableRegistry::class] = function($c) {
156
            return new CallableRegistry($c[CallableRepository::class]);
157
        };
158
        // Callable class plugin
159
        $this->libContainer[CallableClass::class] = function($c) {
160
            return new CallableClass($c[CallableRegistry::class], $c[CallableRepository::class]);
161
        };
162
        // Callable dir plugin
163
        $this->libContainer[CallableDir::class] = function($c) {
164
            return new CallableDir($c[CallableRegistry::class]);
165
        };
166
        // Callable function plugin
167
        $this->libContainer[CallableFunction::class] = function() {
168
            return new CallableFunction();
169
        };
170
        // File upload support
171
        $this->libContainer[FileUploadSupport::class] = function() {
172
            return new FileUploadSupport();
173
        };
174
        // File upload plugin
175
        $this->libContainer[FileUpload::class] = function($c) {
176
            return new FileUpload($c[FileUploadSupport::class]);
177
        };
178
        // JQuery response plugin
179
        $this->libContainer[JQueryPlugin::class] = function() {
180
            return new JQueryPlugin();
181
        };
182
183
        /*
184
         * Managers
185
         */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
186
        // Plugin Manager
187
        $this->libContainer[PluginManager::class] = function() {
188
            return new PluginManager();
189
        };
190
        // Request Handler
191
        $this->libContainer[RequestHandler::class] = function($c) {
192
            return new RequestHandler($c[PluginManager::class], $c[ResponseManager::class], $c[FileUpload::class]);
193
        };
194
        // Request Factory
195
        $this->libContainer[RequestFactory::class] = function($c) {
196
            return new RequestFactory($c[CallableRegistry::class]);
197
        };
198
        // Parameter Factory
199
        $this->libContainer[ParameterFactory::class] = function() {
200
            return new ParameterFactory();
201
        };
202
        // Response Manager
203
        $this->libContainer[ResponseManager::class] = function() {
204
            return new ResponseManager();
205
        };
206
        // Code Generator
207
        $this->libContainer[CodeGenerator::class] = function($c) {
208
            return new CodeGenerator($c[PluginManager::class], $c[TemplateEngine::class]);
209
        };
210
        // View Manager
211
        $this->libContainer[ViewManager::class] = function() {
212
            return new ViewManager();
213
        };
214
        // View Renderer
215
        $this->libContainer[ViewRenderer::class] = function($c) {
216
            return new ViewRenderer($c[ViewManager::class]);
217
        };
218
219
        /*
220
         * Config
221
         */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
222
        $this->libContainer[Config::class] = function() {
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
223
            return new Config();
224
        };
225
        $this->libContainer[ConfigReader::class] = function() {
226
            return new ConfigReader();
227
        };
228
229
        /*
230
         * Services
231
         */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
232
        // Minifier
233
        $this->libContainer[Minifier::class] = function() {
234
            return new Minifier();
235
        };
236
        // Translator
237
        $this->libContainer[Translator::class] = function($c) {
238
            return new Translator($c['jaxon.core.translation_dir'], $c[Config::class]);
239
        };
240
        // Template engine
241
        $this->libContainer[TemplateEngine::class] = function($c) {
242
            return new TemplateEngine($c['jaxon.core.template_dir']);
243
        };
244
        // Template Renderer
245
        $this->libContainer[TemplateRenderer::class] = function($c) {
246
            return $c[TemplateEngine::class];
247
        };
248
        // Validator
249
        $this->libContainer[Validator::class] = function($c) {
250
            return new Validator($c[Translator::class], $c[Config::class]);
251
        };
252
        // Pagination Paginator
253
        $this->libContainer[Paginator::class] = function($c) {
254
            return new Paginator($c[PaginationRenderer::class]);
255
        };
256
        // Pagination Renderer
257
        $this->libContainer[PaginationRenderer::class] = function($c) {
258
            return new PaginationRenderer($c[TemplateRenderer::class]);
259
        };
260
        // Event Dispatcher
261
        $this->libContainer[EventDispatcher::class] = function() {
262
            return new EventDispatcher();
263
        };
264
        // URI decoder
265
        $this->libContainer[URI::class] = function() {
266
            return new URI();
267
        };
268
    }
269
270
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sClass should have a doc-comment as per coding-style.
Loading history...
271
     * Get a class instance
272
     *
273
     * @return object        The class instance
274
     */
275
    public function get($sClass)
276
    {
277
        if($this->appContainer != null && $this->appContainer->has($sClass))
278
        {
279
            return $this->appContainer->get($sClass);
280
        }
281
        return $this->libContainer[$sClass];
282
    }
283
284
    /**
285
     * Set a DI closure
286
     *
287
     * @param string                $sClass             The full class name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 16 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 13 found
Loading history...
288
     * @param Closure               $xClosure           The closure
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 15 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 11 found
Loading history...
289
     *
290
     * @return void
291
     */
292
    public function set($sClass, Closure $xClosure)
293
    {
294
        $this->libContainer[$sClass] = $xClosure;
295
    }
296
297
    /**
298
     * Set an alias
299
     *
300
     * @param string                $sClass             The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 16 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
301
     * @param string                $sAlias             The alias name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 16 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
302
     *
303
     * @return void
304
     */
305
    public function alias($sClass, $sAlias)
306
    {
307
        $this->libContainer[$sClass] = function($c) use ($sAlias) {
308
            return $c[$sAlias];
309
        };
310
    }
311
312
    /**
313
     * Get the plugin manager
314
     *
315
     * @return PluginManager
316
     */
317
    public function getPluginManager()
318
    {
319
        return $this->libContainer[PluginManager::class];
320
    }
321
322
    /**
323
     * Get the request handler
324
     *
325
     * @return RequestHandler
326
     */
327
    public function getRequestHandler()
328
    {
329
        return $this->libContainer[RequestHandler::class];
330
    }
331
332
    /**
333
     * Get the request factory
334
     *
335
     * @return RequestFactory
336
     */
337
    public function getRequestFactory()
338
    {
339
        return $this->libContainer[RequestFactory::class];
340
    }
341
342
    /**
343
     * Get the parameter factory
344
     *
345
     * @return ParameterFactory
346
     */
347
    public function getParameterFactory()
348
    {
349
        return $this->libContainer[ParameterFactory::class];
350
    }
351
352
    /**
353
     * Get the response manager
354
     *
355
     * @return ResponseManager
356
     */
357
    public function getResponseManager()
358
    {
359
        return $this->libContainer[ResponseManager::class];
360
    }
361
362
    /**
363
     * Get the code generator
364
     *
365
     * @return CodeGenerator
366
     */
367
    public function getCodeGenerator()
368
    {
369
        return $this->libContainer[CodeGenerator::class];
370
    }
371
372
    /**
373
     * Get the callable registry
374
     *
375
     * @return CallableRegistry
376
     */
377
    public function getCallableRegistry()
378
    {
379
        return $this->libContainer[CallableRegistry::class];
380
    }
381
382
    /**
383
     * Get the config manager
384
     *
385
     * @return Config
386
     */
387
    public function getConfig()
388
    {
389
        return $this->libContainer[Config::class];
390
    }
391
392
    /**
393
     * Create a new the config manager
394
     *
395
     * @param array             $aOptions           The options array
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 13 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 11 found
Loading history...
396
     * @param string            $sKeys              The keys of the options in the array
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 14 found
Loading history...
397
     *
398
     * @return Config            The config manager
399
     */
400
    public function newConfig(array $aOptions = [], $sKeys = '')
401
    {
402
        return new Config($aOptions, $sKeys);
403
    }
404
405
    /**
406
     * Get the dialog wrapper
407
     *
408
     * @return Dialog
409
     */
410
    public function getDialog()
411
    {
412
        return $this->libContainer[Dialog::class];
413
    }
414
415
    /**
416
     * Get the minifier
417
     *
418
     * @return Minifier
419
     */
420
    public function getMinifier()
421
    {
422
        return $this->libContainer[Minifier::class];
423
    }
424
425
    /**
426
     * Get the translator
427
     *
428
     * @return Translator
429
     */
430
    public function getTranslator()
431
    {
432
        return $this->libContainer[Translator::class];
433
    }
434
435
    /**
436
     * Get the template engine
437
     *
438
     * @return TemplateEngine
439
     */
440
    public function getTemplateEngine()
441
    {
442
        return $this->libContainer[TemplateEngine::class];
443
    }
444
445
    /**
446
     * Get the template renderer
447
     *
448
     * @return TemplateRenderer
449
     */
450
    public function getTemplateRenderer()
451
    {
452
        return $this->libContainer[TemplateRenderer::class];
453
    }
454
455
    /**
456
     * Get the validator
457
     *
458
     * @return Validator
459
     */
460
    public function getValidator()
461
    {
462
        return $this->libContainer[Validator::class];
463
    }
464
465
    /**
466
     * Get the paginator
467
     *
468
     * @return Paginator
469
     */
470
    public function getPaginator()
471
    {
472
        return $this->libContainer[Paginator::class];
473
    }
474
475
    /**
476
     * Get the event dispatcher
477
     *
478
     * @return EventDispatcher
479
     */
480
    public function getEventDispatcher()
481
    {
482
        return $this->libContainer[EventDispatcher::class];
483
    }
484
485
    /**
486
     * Get the global Response object
487
     *
488
     * @return Response
489
     */
490
    public function getResponse()
491
    {
492
        return $this->libContainer[Response::class];
493
    }
494
495
    /**
496
     * Create a new Jaxon response object
497
     *
498
     * @return Response
499
     */
500
    public function newResponse()
501
    {
502
        return new Response();
503
    }
504
505
    /**
506
     * Get the App instance
507
     *
508
     * @return App
509
     */
510
    public function getApp()
511
    {
512
        return $this->libContainer[App::class];
513
    }
514
515
    /**
516
     * Get the App bootstrap
517
     *
518
     * @return Bootstrap
519
     */
520
    public function getBootstrap()
521
    {
522
        return $this->libContainer[Bootstrap::class];
523
    }
524
525
    /**
526
     * Get the view manager
527
     *
528
     * @return ViewManager
529
     */
530
    public function getViewManager()
531
    {
532
        return $this->libContainer[ViewManager::class];
533
    }
534
535
    /**
536
     * Get the view facade
537
     *
538
     * @return ViewRenderer
539
     */
540
    public function getViewRenderer()
541
    {
542
        return $this->libContainer[ViewRenderer::class];
543
    }
544
545
    /**
546
     * Get the session manager
547
     *
548
     * @return SessionContract
549
     */
550
    public function getSessionManager()
551
    {
552
        return $this->libContainer[SessionContract::class];
553
    }
554
555
    /**
556
     * Set the session manager
557
     *
558
     * @param Closure      $xClosure      A closure to create the session manager instance
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 6 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 6 found
Loading history...
559
     *
560
     * @return void
561
     */
562
    public function setSessionManager(Closure $xClosure)
563
    {
564
        $this->libContainer[SessionContract::class] = $xClosure;
565
    }
566
567
    /**
568
     * Set the callable class request factory
569
     *
570
     * @param string            $sClassName         The callable class name
0 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 9 found
Loading history...
571
     * @param CallableObject    $xCallableObject    The corresponding callable object
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
572
     *
573
     * @return void
574
     */
575
    public function setCallableClassRequestFactory($sClassName, CallableObject $xCallableObject)
576
    {
577
        $this->libContainer[$sClassName . '_RequestFactory'] = function() use ($xCallableObject) {
578
            return new CallableClassRequestFactory($xCallableObject);
579
        };
580
    }
581
582
    /**
583
     * Get the callable class request factory
584
     *
585
     * @param string        $sClassName             The callable class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
586
     *
587
     * @return CallableClassRequestFactory
588
     */
589
    public function getCallableClassRequestFactory($sClassName)
590
    {
591
        return $this->libContainer[$sClassName . '_RequestFactory'];
592
    }
593
}
594