You are here

class ResolveReferencesToAliasesPass in Service Container 7.2

Same name and namespace in other branches
  1. 7 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php \Symfony\Component\DependencyInjection\Compiler\ResolveReferencesToAliasesPass

Replaces all references to aliases with references to the actual service.

@author Johannes M. Schmitt <schmittjoh@gmail.com>

Hierarchy

Expanded class hierarchy of ResolveReferencesToAliasesPass

1 file declares its use of ResolveReferencesToAliasesPass
ResolveReferencesToAliasesPassTest.php in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveReferencesToAliasesPassTest.php

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php, line 24

Namespace

Symfony\Component\DependencyInjection\Compiler
View source
class ResolveReferencesToAliasesPass implements CompilerPassInterface {
  private $container;

  /**
   * Processes the ContainerBuilder to replace references to aliases with actual service references.
   *
   * @param ContainerBuilder $container
   */
  public function process(ContainerBuilder $container) {
    $this->container = $container;
    foreach ($container
      ->getDefinitions() as $definition) {
      if ($definition
        ->isSynthetic() || $definition
        ->isAbstract()) {
        continue;
      }
      $definition
        ->setArguments($this
        ->processArguments($definition
        ->getArguments()));
      $definition
        ->setMethodCalls($this
        ->processArguments($definition
        ->getMethodCalls()));
      $definition
        ->setProperties($this
        ->processArguments($definition
        ->getProperties()));
    }
    foreach ($container
      ->getAliases() as $id => $alias) {
      $aliasId = (string) $alias;
      if ($aliasId !== ($defId = $this
        ->getDefinitionId($aliasId))) {
        $container
          ->setAlias($id, new Alias($defId, $alias
          ->isPublic()));
      }
    }
  }

  /**
   * Processes the arguments to replace aliases.
   *
   * @param array $arguments An array of References
   *
   * @return array An array of References
   */
  private function processArguments(array $arguments) {
    foreach ($arguments as $k => $argument) {
      if (is_array($argument)) {
        $arguments[$k] = $this
          ->processArguments($argument);
      }
      elseif ($argument instanceof Reference) {
        $defId = $this
          ->getDefinitionId($id = (string) $argument);
        if ($defId !== $id) {
          $arguments[$k] = new Reference($defId, $argument
            ->getInvalidBehavior(), $argument
            ->isStrict());
        }
      }
    }
    return $arguments;
  }

  /**
   * Resolves an alias into a definition id.
   *
   * @param string $id The definition or alias id to resolve
   *
   * @return string The definition id with aliases resolved
   */
  private function getDefinitionId($id) {
    $seen = array();
    while ($this->container
      ->hasAlias($id)) {
      if (isset($seen[$id])) {
        throw new ServiceCircularReferenceException($id, array_keys($seen));
      }
      $seen[$id] = true;
      $id = (string) $this->container
        ->getAlias($id);
    }
    return $id;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResolveReferencesToAliasesPass::$container private property
ResolveReferencesToAliasesPass::getDefinitionId private function Resolves an alias into a definition id.
ResolveReferencesToAliasesPass::process public function Processes the ContainerBuilder to replace references to aliases with actual service references. Overrides CompilerPassInterface::process
ResolveReferencesToAliasesPass::processArguments private function Processes the arguments to replace aliases.