namespace Symfony\Component\DependencyInjection\Compiler;
+use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
+use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ExpressionLanguage;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
*
* @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;
private $expressionLanguage;
/**
*/
public function setRepeatedPass(RepeatedPass $repeatedPass)
{
- $this->repeatedPass = $repeatedPass;
+ // no-op for BC
}
/**
* Processes a ContainerBuilder object to populate the service reference graph.
- *
- * @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
$this->container = $container;
$this->graph = $container->getCompiler()->getServiceReferenceGraph();
$this->graph->clear();
+ $this->lazy = false;
- foreach ($container->getDefinitions() as $id => $definition) {
- if ($definition->isSynthetic() || $definition->isAbstract()) {
- continue;
- }
+ foreach ($container->getAliases() as $id => $alias) {
+ $targetId = $this->getDefinitionId((string) $alias);
+ $this->graph->connect($id, $alias, $targetId, $this->getDefinition($targetId), null);
+ }
- $this->currentId = $id;
- $this->currentDefinition = $definition;
+ parent::process($container);
+ }
- $this->processArguments($definition->getArguments());
- if (is_array($definition->getFactory())) {
- $this->processArguments($definition->getFactory());
- }
+ protected function processValue($value, $isRoot = false)
+ {
+ $lazy = $this->lazy;
- if (!$this->onlyConstructorArguments) {
- $this->processArguments($definition->getMethodCalls());
- $this->processArguments($definition->getProperties());
- if ($definition->getConfigurator()) {
- $this->processArguments(array($definition->getConfigurator()));
- }
- }
- }
+ if ($value instanceof ArgumentInterface) {
+ $this->lazy = true;
+ parent::processValue($value->getValues());
+ $this->lazy = $lazy;
- foreach ($container->getAliases() as $id => $alias) {
- $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
+ return $value;
}
- }
+ if ($value instanceof Expression) {
+ $this->getExpressionLanguage()->compile((string) $value, array('this' => '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
- */
- private function processArguments(array $arguments)
- {
- foreach ($arguments as $argument) {
- if (is_array($argument)) {
- $this->processArguments($argument);
- } elseif ($argument instanceof Expression) {
- $this->getExpressionLanguage()->compile((string) $argument, array('this' => 'container'));
- } elseif ($argument instanceof Reference) {
- $this->graph->connect(
- $this->currentId,
- $this->currentDefinition,
- $this->getDefinitionId((string) $argument),
- $this->getDefinition((string) $argument),
- $argument
- );
- } 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());
- }
+ return $value;
+ }
+ if ($value instanceof Reference) {
+ $targetId = $this->getDefinitionId((string) $value);
+ $targetDefinition = $this->getDefinition($targetId);
+
+ $this->graph->connect(
+ $this->currentId,
+ $this->currentDefinition,
+ $targetId,
+ $targetDefinition,
+ $value,
+ $this->lazy || ($targetDefinition && $targetDefinition->isLazy()),
+ ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior()
+ );
+
+ 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;
+
+ $this->processValue($value->getFactory());
+ $this->processValue($value->getArguments());
+
+ if (!$this->onlyConstructorArguments) {
+ $this->processValue($value->getProperties());
+ $this->processValue($value->getMethodCalls());
+ $this->processValue($value->getConfigurator());
+ }
+ $this->lazy = $lazy;
+
+ return $value;
}
/**
*/
private function getDefinition($id)
{
- $id = $this->getDefinitionId($id);
-
return null === $id ? null : $this->container->getDefinition($id);
}
return;
}
- return $id;
+ return $this->container->normalizeId($id);
}
private function getExpressionLanguage()
{
if (null === $this->expressionLanguage) {
+ if (!class_exists(ExpressionLanguage::class)) {
+ throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
+ }
+
$providers = $this->container->getExpressionLanguageProviders();
$this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
if ('""' === substr_replace($arg, '', 1, -1)) {
$id = stripcslashes(substr($arg, 1, -1));
+ $id = $this->getDefinitionId($id);
$this->graph->connect(
$this->currentId,
$this->currentDefinition,
- $this->getDefinitionId($id),
+ $id,
$this->getDefinition($id)
);
}