-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Factorize compiler passes around new AbstractRecursivePass #21327
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
abstract class AbstractRecursivePass implements CompilerPassInterface | ||
{ | ||
protected $container; | ||
protected $currentId; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$this->container = $container; | ||
|
||
try { | ||
$this->processValue($container->getDefinitions(), true); | ||
} finally { | ||
$this->container = null; | ||
} | ||
} | ||
|
||
/** | ||
* Processes a value found in a definition tree. | ||
* | ||
* @param mixed $value | ||
* @param bool $isRoot | ||
* | ||
* @return mixed The processed value | ||
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. Can be removed as mixed does not really help :) 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. I fear that people won't notice that the return value is here at all. |
||
*/ | ||
protected function processValue($value, $isRoot = false) | ||
{ | ||
if (is_array($value)) { | ||
foreach ($value as $k => $v) { | ||
if ($isRoot) { | ||
$this->currentId = $k; | ||
} | ||
if ($v !== $processedValue = $this->processValue($v, $isRoot)) { | ||
$value[$k] = $processedValue; | ||
} | ||
} | ||
} elseif ($value instanceof ArgumentInterface) { | ||
$value->setValues($this->processValue($value->getValues())); | ||
} elseif ($value instanceof Definition) { | ||
$value->setArguments($this->processValue($value->getArguments())); | ||
$value->setProperties($this->processValue($value->getProperties())); | ||
$value->setMethodCalls($this->processValue($value->getMethodCalls())); | ||
|
||
if ($v = $value->getFactory()) { | ||
$value->setFactory($this->processValue($v)); | ||
} | ||
if ($v = $value->getConfigurator()) { | ||
$value->setConfigurator($this->processValue($v)); | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,14 +25,13 @@ | |
* | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
*/ | ||
class AnalyzeServiceReferencesPass implements RepeatablePassInterface | ||
class AnalyzeServiceReferencesPass extends AbstractRecursivePass implements RepeatablePassInterface | ||
{ | ||
private $graph; | ||
private $container; | ||
private $currentId; | ||
private $currentDefinition; | ||
private $repeatedPass; | ||
private $onlyConstructorArguments; | ||
private $lazy; | ||
|
||
/** | ||
* @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls | ||
|
@@ -60,68 +59,60 @@ public function process(ContainerBuilder $container) | |
$this->container = $container; | ||
$this->graph = $container->getCompiler()->getServiceReferenceGraph(); | ||
$this->graph->clear(); | ||
|
||
foreach ($container->getDefinitions() as $id => $definition) { | ||
if ($definition->isSynthetic() || $definition->isAbstract()) { | ||
continue; | ||
} | ||
|
||
$this->currentId = $id; | ||
$this->currentDefinition = $definition; | ||
|
||
$this->processArguments($definition->getArguments()); | ||
if (is_array($definition->getFactory())) { | ||
$this->processArguments($definition->getFactory()); | ||
} | ||
|
||
if (!$this->onlyConstructorArguments) { | ||
$this->processArguments($definition->getMethodCalls()); | ||
$this->processArguments($definition->getProperties()); | ||
if ($definition->getConfigurator()) { | ||
$this->processArguments(array($definition->getConfigurator())); | ||
} | ||
} | ||
} | ||
$this->lazy = false; | ||
|
||
foreach ($container->getAliases() as $id => $alias) { | ||
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. this is moved before the processing of definitions instead of after. Is it expected ? And does it make any difference ? 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. given that this pass is ready-only, that doesn't make any difference yes |
||
$this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null); | ||
} | ||
|
||
parent::process($container); | ||
} | ||
|
||
/** | ||
* Processes service definitions for arguments to find relationships for the service graph. | ||
* | ||
* @param array $arguments An array of Reference or Definition objects relating to service definitions | ||
* @param bool $lazy Whether the references nested in the arguments should be considered lazy or not | ||
*/ | ||
private function processArguments(array $arguments, $lazy = false) | ||
protected function processValue($value, $isRoot = false) | ||
{ | ||
foreach ($arguments as $argument) { | ||
if (is_array($argument)) { | ||
$this->processArguments($argument, $lazy); | ||
} elseif ($argument instanceof ArgumentInterface) { | ||
$this->processArguments($argument->getValues(), true); | ||
} elseif ($argument instanceof Reference) { | ||
$targetDefinition = $this->getDefinition((string) $argument); | ||
|
||
$this->graph->connect( | ||
$this->currentId, | ||
$this->currentDefinition, | ||
$this->getDefinitionId((string) $argument), | ||
$targetDefinition, | ||
$argument, | ||
$lazy || ($targetDefinition && $targetDefinition->isLazy()) | ||
); | ||
} elseif ($argument instanceof Definition) { | ||
$this->processArguments($argument->getArguments()); | ||
$this->processArguments($argument->getMethodCalls()); | ||
$this->processArguments($argument->getProperties()); | ||
|
||
if (is_array($argument->getFactory())) { | ||
$this->processArguments($argument->getFactory()); | ||
} | ||
$lazy = $this->lazy; | ||
|
||
if ($value instanceof ArgumentInterface) { | ||
$this->lazy = true; | ||
parent::processValue($value); | ||
$this->lazy = $lazy; | ||
|
||
return $value; | ||
} | ||
if ($value instanceof Reference) { | ||
$targetDefinition = $this->getDefinition((string) $value); | ||
|
||
$this->graph->connect( | ||
$this->currentId, | ||
$this->currentDefinition, | ||
$this->getDefinitionId((string) $value), | ||
$targetDefinition, | ||
$value, | ||
$this->lazy || ($targetDefinition && $targetDefinition->isLazy()) | ||
); | ||
|
||
return $value; | ||
} | ||
if (!$value instanceof Definition) { | ||
return parent::processValue($value, $isRoot); | ||
} | ||
if ($isRoot) { | ||
if ($value->isSynthetic() || $value->isAbstract()) { | ||
return $value; | ||
} | ||
$this->currentDefinition = $value; | ||
} | ||
$this->lazy = false; | ||
|
||
if ($this->onlyConstructorArguments) { | ||
$this->processValue($value->getFactory()); | ||
$this->processValue($value->getArguments()); | ||
} else { | ||
parent::processValue($value, $isRoot); | ||
} | ||
$this->lazy = $lazy; | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
|
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.
Wouldn't it be "better" to create getters for those?
Uh oh!
There was an error while loading. Please reload this page.
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.
Some passes set the value also. They are "local vars passed as properties". Cleaned after use. Just saves boilerplates this way. I'd prefer keep them asis.