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

129 lines
4.1 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.
*
2011-04-27 06:25:26 +01:00
* @param Boolean $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 = (Boolean) $onlyConstructorArguments;
}
2011-02-13 18:06:41 +00:00
/**
* {@inheritDoc}
*/
2011-01-17 22:28:59 +00:00
public function setRepeatedPass(RepeatedPass $repeatedPass) {
$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());
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());
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);
} 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());
$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.
* @return Definition The definition related to the supplied id
*/
private 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);
}
}