This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Compiler/AnalyzeServiceReferencesPass.php

99 lines
3.1 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.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\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Run this pass before passes that need to know more about the relation of
* your services.
*
* This class will populate the ServiceReferenceGraph with information. You can
* retrieve the graph in other passes from the compiler.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
2011-01-17 22:28:59 +00:00
class AnalyzeServiceReferencesPass implements RepeatablePassInterface
{
protected $graph;
protected $container;
protected $currentId;
protected $currentDefinition;
protected $repeatedPass;
2011-01-17 22:28:59 +00:00
protected $ignoreMethodCalls;
2011-01-17 22:28:59 +00:00
public function __construct($ignoreMethodCalls = false)
{
$this->ignoreMethodCalls = (Boolean) $ignoreMethodCalls;
}
2011-01-17 22:28:59 +00:00
public function setRepeatedPass(RepeatedPass $repeatedPass) {
$this->repeatedPass = $repeatedPass;
}
public function process(ContainerBuilder $container)
{
$this->container = $container;
2011-01-17 22:28:59 +00:00
$this->graph = $container->getCompiler()->getServiceReferenceGraph();
$this->graph->clear();
foreach ($container->getDefinitions() as $id => $definition) {
$this->currentId = $id;
$this->currentDefinition = $definition;
$this->processArguments($definition->getArguments());
2011-01-17 22:28:59 +00:00
if (!$this->ignoreMethodCalls) {
$this->processArguments($definition->getMethodCalls());
}
}
foreach ($container->getAliases() as $id => $alias) {
$this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
}
}
protected function processArguments(array $arguments)
{
foreach ($arguments as $k => $argument) {
if (is_array($argument)) {
$this->processArguments($argument);
} else if ($argument instanceof Reference) {
$this->graph->connect(
$this->currentId,
$this->currentDefinition,
(string) $argument,
$this->getDefinition((string) $argument),
$argument
);
} else if ($argument instanceof Definition) {
$this->processArguments($argument->getArguments());
$this->processArguments($argument->getMethodCalls());
}
}
}
protected function getDefinition($id)
{
while ($this->container->hasAlias($id)) {
$id = (string) $this->container->getAlias($id);
}
if (!$this->container->hasDefinition($id)) {
return null;
}
return $this->container->getDefinition($id);
}
}