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/ReplaceAliasByActualDefinitionPass.php
Nicolas Grekas db995dfaf8 Merge branch '3.2'
* 3.2: (27 commits)
  Improve tracking of environment variables in the case of private services
  [DI] Align AutowirePass with 2.8
  property constraints can be added in child classes
  added test for staticClassLoader in LazyLoadingMetadatafactory
  fixed PHPUnit setUp and tearDown method visibility
  spelling fixes
  Readd Symfony version status in the toolbar
  [Security] LdapUserProvider should not throw an exception if the UID key does not exist in an LDAP entry
  make sure that null can be the invalid value
  [VarDumper] Improve dump of AMQP* Object
  Fix annotations cache folder path
  [FrameworkBundle] Wire ArrayCache for annotation reader at bootstrap
  Ignore missing 'debug.file_link_formatter' service in Debug bundle
  [VarDumper] Fixed dumping of terminated generator
  bumped Symfony version to 3.2.4
  updated VERSION for 3.2.3
  updated CHANGELOG for 3.2.3
  bumped Symfony version to 2.8.18
  updated VERSION for 2.8.17
  updated CHANGELOG for 2.8.17
  ...
2017-02-14 17:35:24 +01:00

91 lines
3.2 KiB
PHP

<?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\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
/**
* Replaces aliases with actual service definitions, effectively removing these
* aliases.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ReplaceAliasByActualDefinitionPass extends AbstractRecursivePass
{
private $replacements;
/**
* Process the Container to replace aliases with service definitions.
*
* @param ContainerBuilder $container
*
* @throws InvalidArgumentException if the service definition does not exist
*/
public function process(ContainerBuilder $container)
{
// First collect all alias targets that need to be replaced
$seenAliasTargets = array();
$replacements = array();
foreach ($container->getAliases() as $definitionId => $target) {
$targetId = (string) $target;
// Special case: leave this target alone
if ('service_container' === $targetId) {
continue;
}
// Check if target needs to be replaces
if (isset($replacements[$targetId])) {
$container->setAlias($definitionId, $replacements[$targetId]);
}
// No need to process the same target twice
if (isset($seenAliasTargets[$targetId])) {
continue;
}
// Process new target
$seenAliasTargets[$targetId] = true;
try {
$definition = $container->getDefinition($targetId);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e);
}
if ($definition->isPublic()) {
continue;
}
// Remove private definition and schedule for replacement
$definition->setPublic(true);
$container->setDefinition($definitionId, $definition);
$container->removeDefinition($targetId);
$replacements[$targetId] = $definitionId;
}
$this->replacements = $replacements;
parent::process($container);
$this->replacements = array();
}
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
{
if ($value instanceof Reference && isset($this->replacements[$referenceId = (string) $value])) {
// Perform the replacement
$newId = $this->replacements[$referenceId];
$value = new Reference($newId, $value->getInvalidBehavior());
$this->container->log($this, sprintf('Changed reference of service "%s" previously pointing to "%s" to "%s".', $this->currentId, $referenceId, $newId));
}
return parent::processValue($value, $isRoot);
}
}