1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Stack package. |
4
|
|
|
* |
5
|
|
|
* (c) Andrzej Kostrzewa <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Stack\DI; |
12
|
|
|
|
13
|
|
|
use Interop\Container\ContainerInterface; |
14
|
|
|
use Stack\DI\Definition\Source\Annotation; |
15
|
|
|
use Stack\DI\Definition\Source\Autowiring; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Helper to create and configure a Container. |
19
|
|
|
* |
20
|
|
|
* With the default options, the container created is appropriate for the development environment. |
21
|
|
|
* |
22
|
|
|
* Example: |
23
|
|
|
* |
24
|
|
|
* $builder = new ContainerBuilder(); |
25
|
|
|
* $container = $builder->build(); |
26
|
|
|
* |
27
|
|
|
* @author Andrzej Kostrzewa <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class ContainerBuilder |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $containerClass; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
private $useAutowiring = true; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $useAnnotation = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $definitionSources = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ContainerInterface |
53
|
|
|
*/ |
54
|
|
|
private $delegateContainer; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* ContainerBuilder constructor. |
58
|
|
|
* |
59
|
|
|
* @param string $containerClass |
60
|
|
|
*/ |
61
|
|
|
public function __construct($containerClass = 'Stack\DI\Container') |
62
|
|
|
{ |
63
|
|
|
$this->containerClass = $containerClass; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add definitions to the container. |
68
|
|
|
* |
69
|
|
|
* @param array $definitions |
70
|
|
|
* |
71
|
|
|
* @throws \InvalidArgumentException |
72
|
|
|
* |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function addDefinitions($definitions) |
76
|
|
|
{ |
77
|
|
View Code Duplication |
if (!is_array($definitions)) { |
|
|
|
|
78
|
|
|
throw new \InvalidArgumentException(sprintf( |
79
|
|
|
'%s parameter must be an array, %s given', |
80
|
|
|
'ContainerBuilder::addDefinitions()', |
81
|
|
|
is_object($definitions) ? get_class($definitions) : gettype($definitions) |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->definitionSources = array_merge($this->definitionSources, $definitions); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Build and return a container. |
92
|
|
|
* |
93
|
|
|
* @return Container |
94
|
|
|
*/ |
95
|
|
|
public function build() |
96
|
|
|
{ |
97
|
|
|
$definitionSource = null; |
98
|
|
|
if ($this->useAnnotation) { |
99
|
|
|
$definitionSource = new Annotation($this->definitionSources); |
100
|
|
|
} elseif ($this->useAutowiring) { |
101
|
|
|
$definitionSource = new Autowiring($this->definitionSources); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$containerClass = $this->containerClass; |
105
|
|
|
|
106
|
|
|
return new $containerClass($definitionSource, $this->delegateContainer); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Build a container configured for the dev environment. |
111
|
|
|
* |
112
|
|
|
* @return Container |
113
|
|
|
*/ |
114
|
|
|
public static function buildDevContainer() |
115
|
|
|
{ |
116
|
|
|
$builder = new self(); |
117
|
|
|
|
118
|
|
|
return $builder->build(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Enable or disable the use of autowiring to guess injections. |
123
|
|
|
* |
124
|
|
|
* Enabled by default. |
125
|
|
|
* |
126
|
|
|
* @param $bool |
127
|
|
|
* |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function useAutowiring($bool) |
131
|
|
|
{ |
132
|
|
|
$this->useAutowiring = $bool; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Enable or disable the use of annotations to guess injections. |
139
|
|
|
* |
140
|
|
|
* Disabled by default. |
141
|
|
|
* |
142
|
|
|
* @param $bool |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function useAnnotation($bool) |
147
|
|
|
{ |
148
|
|
|
$this->useAnnotation = $bool; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Delegate the container for dependencies. |
155
|
|
|
* |
156
|
|
|
* @param ContainerInterface $delegateContainer |
157
|
|
|
* |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function setDelegateContainer(ContainerInterface $delegateContainer) |
161
|
|
|
{ |
162
|
|
|
$this->delegateContainer = $delegateContainer; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.