-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
ContainerBuilder.php
246 lines (217 loc) · 6.43 KB
/
ContainerBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* PHP-DI
*
* @link http://mnapoli.github.io/PHP-DI/
* @copyright Matthieu Napoli (http://mnapoli.fr/)
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
*/
namespace DI;
use DI\Definition\DefinitionManager;
use DI\Definition\Source\AnnotationDefinitionSource;
use DI\Definition\Source\ChainableDefinitionSource;
use DI\Definition\Source\PHPFileDefinitionSource;
use DI\Definition\Source\ReflectionDefinitionSource;
use Doctrine\Common\Cache\Cache;
use InvalidArgumentException;
use ProxyManager\Configuration;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
/**
* Helper to create and configure a Container.
*
* With the default options, the container created is appropriate for the development environment.
*
* @since 3.2
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class ContainerBuilder
{
/**
* Name of the container class, used to create the container.
* @var string
*/
private $containerClass;
/**
* @var boolean
*/
private $useReflection = true;
/**
* @var boolean
*/
private $useAnnotations = true;
/**
* @var Cache
*/
private $cache;
/**
* If true, write the proxies to disk to improve performances.
* @var boolean
*/
private $writeProxiesToFile = false;
/**
* Directory where to write the proxies (if $writeProxiesToFile is enabled).
* @var string
*/
private $proxyDirectory;
/**
* If PHP-DI is wrapped in another container, this references the wrapper.
* @var ContainerInterface
*/
private $wrapperContainer;
/**
* Files of definitions for the container.
* @var string[]
*/
private $files = array();
/**
* Build a container configured for the dev environment.
*
* @return Container
*/
public static function buildDevContainer()
{
$builder = new self();
return $builder->build();
}
/**
* @param string $containerClass Name of the container class, used to create the container.
*/
public function __construct($containerClass = 'DI\Container')
{
$this->containerClass = $containerClass;
}
/**
* @return Container
*/
public function build()
{
// Definition sources
$source = null;
foreach ($this->files as $file) {
$newSource = new PHPFileDefinitionSource($file);
// Chain file sources
if ($source) {
$newSource->chain($source);
}
$source = $newSource;
}
if ($this->useAnnotations) {
if ($source) {
$source->chain(new AnnotationDefinitionSource());
} else {
$source = new AnnotationDefinitionSource();
}
} elseif ($this->useReflection) {
if ($source) {
$source->chain(new ReflectionDefinitionSource());
} else {
$source = new ReflectionDefinitionSource();
}
}
// Definition manager
$definitionManager = new DefinitionManager($source);
if ($this->cache) {
$definitionManager->setCache($this->cache);
}
// Proxy factory
$proxyFactory = $this->buildProxyFactory();
$containerClass = $this->containerClass;
return new $containerClass($definitionManager, $proxyFactory, $this->wrapperContainer);
}
/**
* Enable or disable the use of reflection
*
* By default, enabled
* @param boolean $bool
* @return ContainerBuilder
*/
public function useReflection($bool)
{
$this->useReflection = $bool;
return $this;
}
/**
* Enable or disable the use of annotations
*
* By default, enabled
* @param $bool
* @return ContainerBuilder
*/
public function useAnnotations($bool)
{
$this->useAnnotations = $bool;
return $this;
}
/**
* Enables the use of a cache for the definitions
*
* @param Cache $cache Cache backend to use
* @return ContainerBuilder
*/
public function setDefinitionCache(Cache $cache)
{
$this->cache = $cache;
return $this;
}
/**
* Configure the proxy generation
*
* For dev environment, use writeProxiesToFile(false) (default configuration)
* For production environment, use writeProxiesToFile(true, 'tmp/proxies')
*
* @param boolean $writeToFile If true, write the proxies to disk to improve performances
* @param string|null $proxyDirectory Directory where to write the proxies
* @return ContainerBuilder
*
* @throws InvalidArgumentException when writeToFile is set to true and the proxy directory is null
*/
public function writeProxiesToFile($writeToFile, $proxyDirectory = null)
{
$this->writeProxiesToFile = $writeToFile;
if ($writeToFile && $proxyDirectory === null) {
throw new InvalidArgumentException(
"The proxy directory must be specified if you want to write proxies on disk"
);
}
$this->proxyDirectory = $proxyDirectory;
return $this;
}
/**
* If PHP-DI's container is wrapped by another container, we can
* set this so that PHP-DI will use the wrapper rather than itself for building objects.
*
* @param ContainerInterface $otherContainer
* @return $this
*/
public function wrapContainer(ContainerInterface $otherContainer)
{
$this->wrapperContainer = $otherContainer;
return $this;
}
/**
* Add a file containing definitions to the container.
*
* @param string $file
*/
public function addDefinitions($file)
{
$this->files[] = $file;
}
/**
* @return LazyLoadingValueHolderFactory
*/
private function buildProxyFactory()
{
$config = new Configuration();
// TODO use non-deprecated method
$config->setAutoGenerateProxies(true);
if ($this->writeProxiesToFile) {
$config->setProxiesTargetDir($this->proxyDirectory);
spl_autoload_register($config->getProxyAutoloader());
} else {
$config->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
}
return new LazyLoadingValueHolderFactory($config);
}
}