You are here

class AnalyzeServiceReferencesPass in Service Container 7

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

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>

Hierarchy

Expanded class hierarchy of AnalyzeServiceReferencesPass

4 files declare their use of AnalyzeServiceReferencesPass
AnalyzeServiceReferencesPassTest.php in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/Compiler/AnalyzeServiceReferencesPassTest.php
CheckCircularReferencesPassTest.php in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/Compiler/CheckCircularReferencesPassTest.php
InlineServiceDefinitionsPassTest.php in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/Compiler/InlineServiceDefinitionsPassTest.php
RemoveUnusedDefinitionsPassTest.php in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/Compiler/RemoveUnusedDefinitionsPassTest.php

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Compiler/AnalyzeServiceReferencesPass.php, line 27

Namespace

Symfony\Component\DependencyInjection\Compiler
View source
class AnalyzeServiceReferencesPass implements RepeatablePassInterface {
  private $graph;
  private $container;
  private $currentId;
  private $currentDefinition;
  private $repeatedPass;
  private $onlyConstructorArguments;

  /**
   * Constructor.
   *
   * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
   */
  public function __construct($onlyConstructorArguments = false) {
    $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
  }

  /**
   * {@inheritdoc}
   */
  public function setRepeatedPass(RepeatedPass $repeatedPass) {
    $this->repeatedPass = $repeatedPass;
  }

  /**
   * Processes a ContainerBuilder object to populate the service reference graph.
   *
   * @param ContainerBuilder $container
   */
  public function process(ContainerBuilder $container) {
    $this->container = $container;
    $this->graph = $container
      ->getCompiler()
      ->getServiceReferenceGraph();
    $this->graph
      ->clear();
    foreach ($container
      ->getDefinitions() as $id => $definition) {
      if ($definition
        ->isSynthetic() || $definition
        ->isAbstract()) {
        continue;
      }
      $this->currentId = $id;
      $this->currentDefinition = $definition;
      $this
        ->processArguments($definition
        ->getArguments());
      if ($definition
        ->getFactoryService(false)) {
        $this
          ->processArguments(array(
          new Reference($definition
            ->getFactoryService(false)),
        ));
      }
      if (is_array($definition
        ->getFactory())) {
        $this
          ->processArguments($definition
          ->getFactory());
      }
      if (!$this->onlyConstructorArguments) {
        $this
          ->processArguments($definition
          ->getMethodCalls());
        $this
          ->processArguments($definition
          ->getProperties());
        if ($definition
          ->getConfigurator()) {
          $this
            ->processArguments(array(
            $definition
              ->getConfigurator(),
          ));
        }
      }
    }
    foreach ($container
      ->getAliases() as $id => $alias) {
      $this->graph
        ->connect($id, $alias, (string) $alias, $this
        ->getDefinition((string) $alias), null);
    }
  }

  /**
   * 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);
      }
      elseif ($argument instanceof Reference) {
        $this->graph
          ->connect($this->currentId, $this->currentDefinition, $this
          ->getDefinitionId((string) $argument), $this
          ->getDefinition((string) $argument), $argument);
      }
      elseif ($argument instanceof Definition) {
        $this
          ->processArguments($argument
          ->getArguments());
        $this
          ->processArguments($argument
          ->getMethodCalls());
        $this
          ->processArguments($argument
          ->getProperties());
        if (is_array($argument
          ->getFactory())) {
          $this
            ->processArguments($argument
            ->getFactory());
        }
        if ($argument
          ->getFactoryService(false)) {
          $this
            ->processArguments(array(
            new Reference($argument
              ->getFactoryService(false)),
          ));
        }
      }
    }
  }

  /**
   * 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|null The definition related to the supplied id
   */
  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)) {
      return;
    }
    return $id;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnalyzeServiceReferencesPass::$container private property
AnalyzeServiceReferencesPass::$currentDefinition private property
AnalyzeServiceReferencesPass::$currentId private property
AnalyzeServiceReferencesPass::$graph private property
AnalyzeServiceReferencesPass::$onlyConstructorArguments private property
AnalyzeServiceReferencesPass::$repeatedPass private property
AnalyzeServiceReferencesPass::getDefinition private function Returns a service definition given the full name or an alias.
AnalyzeServiceReferencesPass::getDefinitionId private function
AnalyzeServiceReferencesPass::process public function Processes a ContainerBuilder object to populate the service reference graph. Overrides CompilerPassInterface::process
AnalyzeServiceReferencesPass::processArguments private function Processes service definitions for arguments to find relationships for the service graph.
AnalyzeServiceReferencesPass::setRepeatedPass public function Sets the RepeatedPass interface. Overrides RepeatablePassInterface::setRepeatedPass
AnalyzeServiceReferencesPass::__construct public function Constructor.