10000 [DI] Generate one file per service factory · symfony/symfony@e91ba87 · GitHub
[go: up one dir, main page]

Skip to content

Commit e91ba87

Browse files
[DI] Generate one file per service factory
1 parent e95b438 commit e91ba87

30 files changed

+953
-154
lines changed

src/Symfony/Bridge/Doctrine/ManagerRegistry.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) {
5555
if (isset($this->aliases[$name])) {
5656
$name = $this->aliases[$name];
5757
}
58-
$method = $this->methodMap[$name] ?? 'get'.strtr($name, $this->underscoreMap).'Service'; // BC with DI v3.4
59-
$wrappedInstance = $this->{$method}(false);
58+
if (isset($this->fileMap[$name])) {
59+
$wrappedInstance = $this->requireInScope($this->fileMap[$name], false);
60+
} else { // BC with DI v3.4
61+
$method = $this->methodMap[$name] ?? 'get'.strtr($name, $this->underscoreMap).'Service';
62+
$wrappedInstance = $this->{$method}(false);
63+
}
6064

6165
$manager->setProxyInitializer(null);
6266

src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ public function getProxyFactoryCode(Definition $definition, $id, $factoryCode =
6969
$instantiation = 'return';
7070

7171
if ($definition->isShared()) {
72-
$instantiation .= sprintf(' $this->%s[\'%s\'] =', $definition->isPublic() || !method_exists(ContainerBuilder::class, 'addClassResource') ? 'services' : 'privates', $id);
72+
if (!method_exists(ContainerBuilder::class, 'addClassResource')) {
73+
$instantiation .= sprintf(' $this->%s[\'%s\'] =', $definition->isPublic() ? 'services' : 'privates', $id);
74+
} else {
75+
// BC with DI v3.4
76+
$instantiation .= sprintf(' $this->services[\'%s\'] =', $id);
77+
}
7378
}
7479

7580
if (null === $factoryCode) {

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Container implements ResettableContainerInterface
4646
protected $parameterBag;
4747

4848
protected $services = array();
49+
protected $fileMap = array();
4950
protected $methodMap = array();
5051
protected $aliases = array();
5152
protected $loading = array();
@@ -150,7 +151,7 @@ public function set($id, $service)
150151
throw new InvalidArgumentException('You cannot set service "service_container".');
151152
}
152153

153-
if (isset($this->methodMap[$id])) {
154+
if (isset($this->fileMap[$id]) || isset($this->methodMap[$id])) {
154155
throw new InvalidArgumentException(sprintf('You cannot set the pre-defined service "%s".', $id));
155156
}
156157

@@ -186,19 +187,12 @@ public function has($id)
186187
return true;
187188
}
188189

189-
if (isset($this->methodMap[$id])) {
190-
return true;
191-
}
192-
193-
return false;
190+
return isset($this->fileMap[$id]) || isset($this->methodMap[$id]);
194191
}
195192

196193
/**
197194
* Gets a service.
198195
*
199-
* If a service is defined both through a set() method and
200-
* with a get{$id}Service() method, the former has always precedence.
201-
*
202196
* @param string $id The service identifier
203197
* @param int $invalidBehavior The behavior when the service does not exist
204198
*
@@ -228,32 +222,14 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
228222
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
229223
}
230224

231-
if (isset($this->methodMap[$id])) {
232-
$method = $this->methodMap[$id];
233-
} else {
234-
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
235-
if (!$id) {
236-
throw new ServiceNotFoundException($id);
237-
}
238-
239-
$alternatives = array();
240-
foreach ($this->getServiceIds() as $knownId) {
241-
$lev = levenshtein($id, $knownId);
242-
if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) {
243-
$alternatives[] = $knownId;
244-
}
245-
}
246-
247-
throw new ServiceNotFoundException($id, null, null, $alternatives);
248-
}
249-
250-
return;
251-
}
252-
253225
$this->loading[$id] = true;
254226

255227
try {
256-
$service = $this->$method();
228+
if (isset($this->fileMap[$id])) {
229+
return $this->requireInScope($this->fileMap[$id]);
230+
} elseif (isset($this->methodMap[$id])) {
231+
return $this->{$this->methodMap[$id]}();
232+
}
257233
} catch (\Exception $e) {
258234
unset($this->services[$id]);
259235

@@ -262,7 +238,21 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
262238
unset($this->loading[$id]);
263239
}
264240

265-
return $service;
241+
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
242+
if (!$id) {
243+
throw new ServiceNotFoundException($id);
244+
}
245+
246+
$alternatives = array();
247+
foreach ($this->getServiceIds() as $knownId) {
248+
$lev = levenshtein($id, $knownId);
249+
if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) {
250+
$alternatives[] = $knownId;
251+
}
252+
}
253+
254+
throw new ServiceNotFoundException($id, null, null, $alternatives);
255+
}
266256
}
267257

268258
/**
@@ -300,7 +290,7 @@ public function reset()
300290
*/
301291
public function getServiceIds()
302292
{
303-
return array_unique(array_merge(array('service_container'), array_keys($this->methodMap), array_keys($this->services)));
293+
return array_unique(array_merge(array('service_container'), array_keys($this->fileMap), array_keys($this->methodMap), array_keys($this->services)));
304294
}
305295

306296
/**
@@ -327,6 +317,16 @@ public static function underscore($id)
327317
return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), str_replace('_', '.', $id)));
328318
}
329319

320+
/**
321+
* Creates a service by requiring its factory file.
322+
*
323+
* @return object The service created by the file
324+
*/
325+
protected function requireInScope($file)
326+
{
327+
return require $file;
328+
}
329+
330330
/**
331331
* Fetches a variable from the environment.
332332
*

0 commit comments

Comments
 (0)
0