1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Di.php |
5
|
|
|
* |
6
|
|
|
* @author Dominik Kocuj |
7
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License |
8
|
|
|
* @copyright Copyright (c) 2017-2018 kocuj.pl |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Kocuj\Di; |
12
|
|
|
|
13
|
|
|
use Kocuj\Di\ArgumentParser\ArgumentParserFactory; |
14
|
|
|
use Kocuj\Di\Container\Container; |
15
|
|
|
use Kocuj\Di\Container\ContainerInterface; |
16
|
|
|
use Kocuj\Di\Service\ServiceFactory; |
17
|
|
|
use Kocuj\Di\Service\ServiceFactoryInterface; |
18
|
|
|
use Kocuj\Di\ServiceIdDecorator\ServiceIdDecorator; |
19
|
|
|
use Kocuj\Di\ServiceIdDecorator\ServiceIdDecoratorInterface; |
20
|
|
|
use Kocuj\Di\Tools\Camelizer\Camelizer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Dependency injection containers library |
24
|
|
|
* |
25
|
|
|
* @package Kocuj\Di |
26
|
|
|
*/ |
27
|
|
|
class Di |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Service identifier decorator |
31
|
|
|
* |
32
|
|
|
* @var ServiceIdDecoratorInterface |
33
|
|
|
*/ |
34
|
|
|
private $serviceIdDecorator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Service factory |
38
|
|
|
* |
39
|
|
|
* @var ServiceFactoryInterface |
40
|
|
|
*/ |
41
|
|
|
private $serviceFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Default dependency injection container for services |
45
|
|
|
* |
46
|
|
|
* @var ContainerInterface |
47
|
|
|
*/ |
48
|
|
|
private $defaultContainer; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor |
52
|
|
|
* |
53
|
|
|
* @codeCoverageIgnore |
54
|
|
|
*/ |
55
|
|
|
public function __construct() |
56
|
|
|
{ |
57
|
|
|
// initialize |
58
|
|
|
$this->serviceIdDecorator = new ServiceIdDecorator(new Camelizer()); |
59
|
|
|
$argumentParserFactory = new ArgumentParserFactory(); |
60
|
|
|
$this->serviceFactory = new ServiceFactory($argumentParserFactory); |
61
|
|
|
// create default container |
62
|
|
|
$this->defaultContainer = $this->create(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create dependency injection container for services |
67
|
|
|
* |
68
|
|
|
* @return ContainerInterface New container |
69
|
|
|
* @codeCoverageIgnore |
70
|
|
|
*/ |
71
|
|
|
public function create(): ContainerInterface |
72
|
|
|
{ |
73
|
|
|
// exit |
74
|
|
|
return new Container($this->serviceIdDecorator, $this->serviceFactory); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Copy container |
79
|
|
|
* |
80
|
|
|
* @param ContainerInterface $fromContainer |
81
|
|
|
* Container from which copy will be made |
82
|
|
|
* @return ContainerInterface Copied container |
83
|
|
|
* @codeCoverageIgnore |
84
|
|
|
*/ |
85
|
|
|
public function copy(ContainerInterface $fromContainer): ContainerInterface |
86
|
|
|
{ |
87
|
|
|
// exit |
88
|
|
|
return clone $fromContainer; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get default dependency injection container for services |
93
|
|
|
* |
94
|
|
|
* @return ContainerInterface Default container |
95
|
|
|
* @codeCoverageIgnore |
96
|
|
|
*/ |
97
|
|
|
public function getDefault(): ContainerInterface |
98
|
|
|
{ |
99
|
|
|
// exit |
100
|
|
|
return $this->defaultContainer; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|