2011-01-09 09:53:46 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
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
|
2011-01-09 09:53:46 +00:00
|
|
|
{
|
2011-03-11 13:50:46 +00:00
|
|
|
private $graph;
|
|
|
|
private $container;
|
|
|
|
private $currentId;
|
|
|
|
private $currentDefinition;
|
|
|
|
private $repeatedPass;
|
|
|
|
private $onlyConstructorArguments;
|
2011-01-09 09:53:46 +00:00
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param boolean $ignoreMethodCalls Sets this Service Reference pass to ignore method calls
|
|
|
|
*/
|
2011-03-04 14:26:00 +00:00
|
|
|
public function __construct($onlyConstructorArguments = false)
|
2011-01-17 22:28:59 +00:00
|
|
|
{
|
2011-03-04 14:26:00 +00:00
|
|
|
$this->onlyConstructorArguments = (Boolean) $onlyConstructorArguments;
|
2011-01-09 09:53:46 +00:00
|
|
|
}
|
|
|
|
|
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-01-09 09:53:46 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Processes a ContainerBuilder object to populate the service reference graph.
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param ContainerBuilder $container
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-09 09:53:46 +00:00
|
|
|
public function process(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
2011-01-17 22:28:59 +00:00
|
|
|
$this->graph = $container->getCompiler()->getServiceReferenceGraph();
|
2011-01-09 09:53:46 +00:00
|
|
|
$this->graph->clear();
|
|
|
|
|
|
|
|
foreach ($container->getDefinitions() as $id => $definition) {
|
2011-01-26 23:14:31 +00:00
|
|
|
if ($definition->isSynthetic() || $definition->isAbstract()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
$this->currentId = $id;
|
|
|
|
$this->currentDefinition = $definition;
|
|
|
|
$this->processArguments($definition->getArguments());
|
2011-01-17 22:28:59 +00:00
|
|
|
|
2011-03-04 14:26:00 +00:00
|
|
|
if (!$this->onlyConstructorArguments) {
|
2011-01-17 22:28:59 +00:00
|
|
|
$this->processArguments($definition->getMethodCalls());
|
2011-03-04 14:26:00 +00:00
|
|
|
$this->processArguments($definition->getProperties());
|
2011-01-17 22:28:59 +00:00
|
|
|
}
|
2011-01-09 09:53:46 +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
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function processArguments(array $arguments)
|
2011-01-09 09:53:46 +00:00
|
|
|
{
|
2011-02-04 18:10:13 +00:00
|
|
|
foreach ($arguments as $argument) {
|
2011-01-09 09:53:46 +00:00
|
|
|
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());
|
2011-03-04 14:26:00 +00:00
|
|
|
$this->processArguments($argument->getProperties());
|
2011-01-09 09:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function getDefinition($id)
|
2011-01-09 09:53:46 +00:00
|
|
|
{
|
|
|
|
while ($this->container->hasAlias($id)) {
|
|
|
|
$id = (string) $this->container->getAlias($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->container->hasDefinition($id)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->container->getDefinition($id);
|
|
|
|
}
|
2011-02-04 18:10:13 +00:00
|
|
|
}
|