private function XmlFileLoader::processAnonymousServices

Processes anonymous services

Parameters

SimpleXMLElement $xml:

string $file:

1 call to XmlFileLoader::processAnonymousServices()
XmlFileLoader::load in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
Loads an XML file.

File

drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php, line 240

Class

XmlFileLoader
XmlFileLoader loads XML files service definitions.

Namespace

Symfony\Component\DependencyInjection\Loader

Code

private function processAnonymousServices(SimpleXMLElement $xml, $file) {
  $definitions = array();
  $count = 0;

  // anonymous services as arguments/properties
  if (false !== ($nodes = $xml
    ->xpath('//container:argument[@type="service"][not(@id)]|//container:property[@type="service"][not(@id)]'))) {
    foreach ($nodes as $node) {

      // give it a unique name
      $node['id'] = sprintf('%s_%d', md5($file), ++$count);
      $definitions[(string) $node['id']] = array(
        $node->service,
        $file,
        false,
      );
      $node->service['id'] = (string) $node['id'];
    }
  }

  // anonymous services "in the wild"
  if (false !== ($nodes = $xml
    ->xpath('//container:services/container:service[not(@id)]'))) {
    foreach ($nodes as $node) {

      // give it a unique name
      $node['id'] = sprintf('%s_%d', md5($file), ++$count);
      $definitions[(string) $node['id']] = array(
        $node,
        $file,
        true,
      );
      $node->service['id'] = (string) $node['id'];
    }
  }

  // resolve definitions
  krsort($definitions);
  foreach ($definitions as $id => $def) {

    // anonymous services are always private
    $def[0]['public'] = false;
    $this
      ->parseDefinition($id, $def[0], $def[1]);
    $oNode = dom_import_simplexml($def[0]);
    if (true === $def[2]) {
      $nNode = new \DOMElement('_services');
      $oNode->parentNode
        ->replaceChild($nNode, $oNode);
      $nNode
        ->setAttribute('id', $id);
    }
    else {
      $oNode->parentNode
        ->removeChild($oNode);
    }
  }
}