-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Generate one file per service factory #23741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ class Container implements ResettableContainerInterface | |
protected $parameterBag; | ||
|
||
protected $services = array(); | ||
protected $fileMap = array(); | ||
protected $methodMap = array(); | ||
protected $aliases = array(); | ||
protected $loading = array(); | ||
|
@@ -203,7 +204,7 @@ public function set($id, $service) | |
} else { | ||
@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); | ||
} | ||
} elseif (isset($this->methodMap[$id])) { | ||
} elseif (isset($this->fileMap[$id]) || isset($this->methodMap[$id])) { | ||
if (null === $service) { | ||
@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); | ||
} else { | ||
|
@@ -235,7 +236,7 @@ public function has($id) | |
return true; | ||
} | ||
|
||
if (isset($this->methodMap[$id])) { | ||
if (isset($this->fileMap[$id]) || isset($this->methodMap[$id])) { | ||
return true; | ||
} | ||
|
||
|
@@ -299,49 +300,48 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE | |
throw new ServiceCircularReferenceException($id, array_keys($this->loading)); | ||
} | ||
|
||
if (isset($this->methodMap[$id])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the diff in this method is better viewed ignoring whitespaces |
||
$method = $this->methodMap[$id]; | ||
} elseif (--$i && $id !== $normalizedId = $this->normalizeId($id)) { | ||
$id = $normalizedId; | ||
continue; | ||
} elseif (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) { | ||
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder, | ||
// and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper) | ||
@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); | ||
// $method is set to the right value, proceed | ||
} else { | ||
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) { | ||
if (!$id) { | ||
throw new ServiceNotFoundException($id); | ||
} | ||
|
||
$alternatives = array(); | ||
foreach ($this->getServiceIds() as $knownId) { | ||
$lev = levenshtein($id, $knownId); | ||
if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) { | ||
$alternatives[] = $knownId; | ||
} | ||
} | ||
|
||
throw new ServiceNotFoundException($id, null, null, $alternatives); | ||
} | ||
|
||
return; | ||
} | ||
|
||
$this->loading[$id] = true; | ||
|
||
try { | ||
$service = $this->$method(); | ||
if (isset($this->fileMap[$id])) { | ||
return $this->load($this->fileMap[$id]); | ||
} elseif (isset($this->methodMap[$id])) { | ||
return $this->{$this->methodMap[$id]}(); | ||
} elseif (--$i && $id !== $normalizedId = $this->normalizeId($id)) { | ||
$id = $normalizedId; | ||
continue; | ||
} elseif (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) { | ||
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder, | ||
// and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper) | ||
@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); | ||
|
||
return $this->{$method}(); | ||
} | ||
|
||
break; | ||
} catch (\Exception $e) { | ||
unset($this->services[$id]); | ||
|
||
throw $e; | ||
} finally { | ||
unset($this->loading[$id]); | ||
} | ||
} | ||
|
||
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) { | ||
if (!$id) { | ||
throw new ServiceNotFoundException($id); | ||
} | ||
|
||
$alternatives = array(); | ||
foreach ($this->getServiceIds() as $knownId) { | ||
$lev = levenshtein($id, $knownId); | ||
if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) { | ||
$alternatives[] = $knownId; | ||
} | ||
} | ||
|
||
return $service; | ||
throw new ServiceNotFoundException($id, null, null, $alternatives); | ||
} | ||
} | ||
|
||
|
@@ -401,7 +401,7 @@ public function getServiceIds() | |
} | ||
$ids[] = 'service_container'; | ||
|
||
return array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->services))); | ||
return array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->fileMap), array_keys($this->services))); | ||
} | ||
|
||
/** | ||
|
@@ -428,6 +428,16 @@ public static function underscore($id) | |
return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), str_replace('_', '.', $id))); | ||
} | ||
|
||
/** | ||
* Creates a service by requiring its factory file. | ||
* | ||
* @return object The service created by the file | ||
*/ | ||
protected function load($file) | ||
{ | ||
return require $file; | ||
} | ||
|
||
/** | ||
* Fetches a variable from the environment. | ||
* | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the format is already tested elsewhere, no need to duplicate that effort, it just makes updating tests more complex (same below)