Skip to content

Commit 924e16f

Browse files
[DI] Generate one file per service factory
1 parent fca85f8 commit 924e16f

File tree

10 files changed

+701
-188
lines changed

10 files changed

+701
-188
lines changed

Container.php

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Container implements ResettableContainerInterface
5555
protected $parameterBag;
5656

5757
protected $services = array();
58+
protected $fileMap = array();
5859
protected $methodMap = array();
5960
protected $aliases = array();
6061
protected $loading = array();
@@ -203,7 +204,7 @@ public function set($id, $service)
203204
} else {
204205
@trigger_error(sprintf('Setting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
205206
}
206-
} elseif (isset($this->methodMap[$id])) {
207+
} elseif (isset($this->fileMap[$id]) || isset($this->methodMap[$id])) {
207208
if (null === $service) {
208209
@trigger_error(sprintf('Unsetting the "%s" pre-defined service is deprecated since Symfony 3.3 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
209210
} else {
@@ -235,7 +236,7 @@ public function has($id)
235236
return true;
236237
}
237238

238-
if (isset($this->methodMap[$id])) {
239+
if (isset($this->fileMap[$id]) || isset($this->methodMap[$id])) {
239240
return true;
240241
}
241242

@@ -299,49 +300,48 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
299300
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
300301
}
301302

302-
if (isset($this->methodMap[$id])) {
303-
$method = $this->methodMap[$id];
304-
} elseif (--$i && $id !== $normalizedId = $this->normalizeId($id)) {
305-
$id = $normalizedId;
306-
continue;
307-
} elseif (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) {
308-
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
309-
// and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
310-
@trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
311-
// $method is set to the right value, proceed
312-
} else {
313-
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
314-
if (!$id) {
315-
throw new ServiceNotFoundException($id);
316-
}
317-
318-
$alternatives = array();
319-
foreach ($this->getServiceIds() as $knownId) {
320-
$lev = levenshtein($id, $knownId);
321-
if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) {
322-
$alternatives[] = $knownId;
323-
}
324-
}
325-
326-
throw new ServiceNotFoundException($id, null, null, $alternatives);
327-
}
328-
329-
return;
330-
}
331-
332303
$this->loading[$id] = true;
333304

334305
try {
335-
$service = $this->$method();
306+
if (isset($this->fileMap[$id])) {
307+
return $this->load($this->fileMap[$id]);
308+
} elseif (isset($this->methodMap[$id])) {
309+
return $this->{$this->methodMap[$id]}();
310+
} elseif (--$i && $id !== $normalizedId = $this->normalizeId($id)) {
311+
$id = $normalizedId;
312+
continue;
313+
} elseif (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) {
314+
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
315+
// and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
316+
@trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
317+
318+
return $this->{$method}();
319+
}
320+
321+
break;
336322
} catch (\Exception $e) {
337323
unset($this->services[$id]);
338324

339325
throw $e;
340326
} finally {
341327
unset($this->loading[$id]);
342328
}
329+
}
330+
331+
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
332+
if (!$id) {
333+
throw new ServiceNotFoundException($id);
334+
}
335+
336+
$alternatives = array();
337+
foreach ($this->getServiceIds() as $knownId) {
338+
$lev = levenshtein($id, $knownId);
339+
if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) {
340+
$alternatives[] = $knownId;
341+
}
342+
}
343343

344-
return $service;
344+
throw new ServiceNotFoundException($id, null, null, $alternatives);
345345
}
346346
}
347347

@@ -401,7 +401,7 @@ public function getServiceIds()
401401
}
402402
$ids[] = 'service_container';
403403

404-
return array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->services)));
404+
return array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->fileMap), array_keys($this->services)));
405405
}
406406

407407
/**
@@ -428,6 +428,16 @@ public static function underscore($id)
428428
return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), str_replace('_', '.', $id)));
429429
}
430430

431+
/**
432+
* Creates a service by requiring its factory file.
433+
*
434+
* @return object The service created by the file
435+
*/
436+
protected function load($file)
437+
{
438+
return require $file;
439+
}
440+
431441
/**
432442
* Fetches a variable from the environment.
433443
*

0 commit comments

Comments
 (0)