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

145 lines
4.6 KiB
PHP
Raw Normal View History

<?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\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
{
private $graph;
private $container;
private $currentId;
private $currentDefinition;
private $repeatedPass;
private $onlyConstructorArguments;
2011-02-13 18:06:41 +00:00
/**
* Constructor.
*
* @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
2011-02-13 18:06:41 +00:00
*/
public function __construct($onlyConstructorArguments = false)
2011-01-17 22:28:59 +00:00
{
$this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
}
2011-02-13 18:06:41 +00:00
/**
* {@inheritdoc}
2011-02-13 18:06:41 +00:00
*/
2011-12-13 07:50:54 +00:00
public function setRepeatedPass(RepeatedPass $repeatedPass)
{
2011-01-17 22:28:59 +00:00
$this->repeatedPass = $repeatedPass;
}
2011-02-13 18:06:41 +00:00
/**
* Processes a ContainerBuilder object to populate the service reference graph.
*
* @param ContainerBuilder $container
2011-02-13 18:06:41 +00:00
*/
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) {
2011-01-26 23:14:31 +00:00
if ($definition->isSynthetic() || $definition->isAbstract()) {
continue;
}
$this->currentId = $id;
$this->currentDefinition = $definition;
$this->processArguments($definition->getArguments());
if ($definition->getFactoryService()) {
$this->processArguments(array(new Reference($definition->getFactoryService())));
}
2011-01-17 22:28:59 +00:00
if (!$this->onlyConstructorArguments) {
2011-01-17 22:28:59 +00:00
$this->processArguments($definition->getMethodCalls());
$this->processArguments($definition->getProperties());
if ($definition->getConfigurator()) {
$this->processArguments(array($definition->getConfigurator()));
}
2011-01-17 22:28:59 +00:00
}
}
foreach ($container->getAliases() as $id => $alias) {
$this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
}
}
2011-02-13 18:06:41 +00:00
/**
* 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);
2011-12-18 13:42:59 +00:00
} elseif ($argument instanceof Reference) {
$this->graph->connect(
$this->currentId,
$this->currentDefinition,
$this->getDefinitionId((string) $argument),
$this->getDefinition((string) $argument),
$argument
);
2011-12-18 13:42:59 +00:00
} elseif ($argument instanceof Definition) {
$this->processArguments($argument->getArguments());
$this->processArguments($argument->getMethodCalls());
$this->processArguments($argument->getProperties());
}
}
}
2011-02-13 18:06:41 +00:00
/**
* Returns a service definition given the full name or an alias.
*
* @param string $id A full id or alias for a service definition.
2011-12-13 07:50:54 +00:00
*
* @return Definition|null The definition related to the supplied id
2011-02-13 18:06:41 +00:00
*/
private function getDefinition($id)
{
$id = $this->getDefinitionId($id);
return null === $id ? null : $this->container->getDefinition($id);
}
private function getDefinitionId($id)
{
while ($this->container->hasAlias($id)) {
$id = (string) $this->container->getAlias($id);
}
if (!$this->container->hasDefinition($id)) {
2014-04-16 08:15:58 +01:00
return;
}
return $id;
}
}