-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Optimize ReplaceAliasByActualDefinitionPass #18265
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,6 @@ class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface | |
{ | ||
private $compiler; | ||
private $formatter; | ||
private $sourceId; | ||
|
||
/** | ||
* Process the Container to replace aliases with service definitions. | ||
|
@@ -36,105 +35,99 @@ class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface | |
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
// Setup | ||
$this->compiler = $container->getCompiler(); | ||
$this->formatter = $this->compiler->getLoggingFormatter(); | ||
|
||
foreach ($container->getAliases() as $id => $alias) { | ||
$aliasId = (string) $alias; | ||
|
||
if ('service_container' === $aliasId) { | ||
// First collect all alias targets that need to be replaced | ||
$seenAliasTargets = array(); | ||
$replacements = array(); | ||
foreach ($container->getAliases() as $definitionId => $target) { | ||
$targetId = (string) $target; | ||
// Special case: leave this target alone | ||
if ('service_container' === $targetId) { | ||
continue; | ||
} | ||
|
||
// Check if target needs to be replaces | ||
if (isset($replacements[$targetId])) { | ||
$container->setAlias($definitionId, $replacements[$targetId]); | ||
} | ||
// No neeed to process the same target twice | ||
if (isset($seenAliasTargets[$targetId])) { | ||
continue; | ||
} | ||
// Process new target | ||
$seenAliasTargets[$targetId] = true; | ||
try { | ||
$definition = $container->getDefinition($aliasId); | ||
$definition = $container->getDefinition($targetId); | ||
} catch (InvalidArgumentException $e) { | ||
throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $id, $alias), null, $e); | ||
throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e); | ||
} | ||
|
||
if ($definition->isPublic()) { | ||
continue; | ||
} | ||
|
||
// Remove private definition and schedule for replacement | ||
$definition->setPublic(true); | ||
$container->setDefinition($id, $definition); | ||
$container->removeDefinition($aliasId); | ||
|
||
$this->updateReferences($container, $aliasId, $id); | ||
|
||
// we have to restart the process due to concurrent modification of | ||
// the container | ||
$this->process($container); | ||
|
||
break; | ||
$container->setDefinition($definitionId, $definition); | ||
$container->removeDefinition($targetId); | ||
$replacements[$targetId] = $definitionId; | ||
} | ||
} | ||
|
||
/** | ||
* Updates references to remove aliases. | ||
* | ||
* @param ContainerBuilder $container The container | ||
* @param string $currentId The alias identifier being replaced | ||
* @param string $newId The id of the service the alias points to | ||
*/ | ||
private function updateReferences($container, $currentId, $newId) | ||
{ | ||
foreach ($container->getAliases() as $id => $alias) { | ||
if ($currentId === (string) $alias) { | ||
$container->setAlias($id, $newId); | ||
} | ||
} | ||
|
||
foreach ($container->getDefinitions() as $id => $definition) { | ||
$this->sourceId = $id; | ||
|
||
$definition->setArguments( | ||
$this->updateArgumentReferences($definition->getArguments(), $currentId, $newId) | ||
); | ||
|
||
$definition->setMethodCalls( | ||
$this->updateArgumentReferences($definition->getMethodCalls(), $currentId, $newId) | ||
); | ||
|
||
$definition->setProperties( | ||
$this->updateArgumentReferences($definition->getProperties(), $currentId, $newId) | ||
); | ||
|
||
$definition->setFactoryService($this->updateFactoryServiceReference($definition->getFactoryService(), $currentId, $newId)); | ||
// Now replace target instances in all definitions | ||
foreach ($container->getDefinitions() as $definitionId => $definition) { | ||
$definition->setArguments($this->updateArgumentReferences($replacements, $definitionId, $definition->getArguments())); | ||
$definition->setMethodCalls($this->updateArgumentReferences($replacements, $definitionId, $definition->getMethodCalls())); | ||
$definition->setProperties($this->updateArgumentReferences($replacements, $definitionId, $definition->getProperties())); | ||
$definition->setFactoryService($this->updateFactoryReferenceId($replacements, $definition->getFactoryService())); | ||
} | ||
} | ||
|
||
/** | ||
* Updates argument references. | ||
* Recursively updates references in an array. | ||
* | ||
* @param array $arguments An array of Arguments | ||
* @param string $currentId The alias identifier | ||
* @param string $newId The identifier the alias points to | ||
* @param array $replacements Table of aliases to replace | ||
* @param string $definitionId Identifier of this definition | ||
* @param array $arguments Where to replace the aliases | ||
* | ||
* @return array | ||
*/ | ||
private function updateArgumentReferences(array $arguments, $currentId, $newId) | ||
private function updateArgumentReferences(array $replacements, $definitionId, array $arguments) | ||
{ | ||
foreach ($arguments as $k => $argument) { | ||
// Handle recursion step | ||
if (is_array($argument)) { | ||
$arguments[$k] = $this->updateArgumentReferences($argument, $currentId, $newId); | ||
} elseif ($argument instanceof Reference) { | ||
if ($currentId === (string) $argument) { | ||
$arguments[$k] = new Reference($newId, $argument->getInvalidBehavior()); | ||
$this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $this->sourceId, $currentId, $newId)); | ||
} | ||
$arguments[$k] = $this->updateArgumentReferences($replacements, $definitionId, $argument); | ||
continue; | ||
} | ||
// Skip arguments that don't need replacement | ||
if (!$argument instanceof Reference) { | ||
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. please add empty lines between the different 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. better? |
||
continue; | ||
} | ||
$referenceId = (string) $argument; | ||
if (!isset($replacements[$referenceId])) { | ||
continue; | ||
} | ||
// Perform the replacement | ||
$newId = $replacements[$referenceId]; | ||
$arguments[$k] = new Reference($newId, $argument->getInvalidBehavior()); | ||
$this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $definitionId, $referenceId, $newId)); | ||
} | ||
|
||
return $arguments; | ||
} | ||
|
||
private function updateFactoryServiceReference($factoryService, $currentId, $newId) | ||
/** | ||
* Returns the updated reference for the factory service. | ||
* | ||
* @param array $replacements Table of aliases to replace | ||
* @param string|null $referenceId Factory service reference identifier | ||
* | ||
* @return string|null | ||
*/ | ||
private function updateFactoryReferenceId(array $replacements, $referenceId) | ||
{ | ||
if (null === $factoryService) { | ||
if (null === $referenceId) { | ||
return; | ||
} | ||
|
||
return $currentId === $factoryService ? $newId : $factoryService; | ||
return isset($replacements[$referenceId]) ? $replacements[$referenceId] : $referenceId; | ||
} | ||
} |
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.
please don't remove empty lines. there are here for readability of the source code
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.
Can we compromise on section-breaking comments? (see new commit) 😄