Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. XmlFileLoader.php

function XmlFileLoader::processAnonymousServices

Processes anonymous services.

1 call to XmlFileLoader::processAnonymousServices()
XmlFileLoader::loadXml in vendor/symfony/dependency-injection/Loader/XmlFileLoader.php

File

vendor/symfony/dependency-injection/Loader/XmlFileLoader.php, line 498

Class

XmlFileLoader
XmlFileLoader loads XML files service definitions.

Namespace

Symfony\Component\DependencyInjection\Loader

Code

private function processAnonymousServices(\DOMDocument $xml, string $file, ?\DOMNode $root = null) : void {
    $definitions = [];
    $count = 0;
    $suffix = '~' . ContainerBuilder::hash($file);
    $xpath = new \DOMXPath($xml);
    $xpath->registerNamespace('container', self::NS);
    // anonymous services as arguments/properties
    if (false !== ($nodes = $xpath->query('.//container:argument[@type="service"][not(@id)]|.//container:property[@type="service"][not(@id)]|.//container:bind[not(@id)]|.//container:factory[not(@service)]|.//container:configurator[not(@service)]', $root))) {
        foreach ($nodes as $node) {
            if ($services = $this->getChildren($node, 'service')) {
                // give it a unique name
                $id = \sprintf('.%d_%s', ++$count, preg_replace('/^.*\\\\/', '', $services[0]->getAttribute('class')) . $suffix);
                $node->setAttribute('id', $id);
                $node->setAttribute('service', $id);
                $definitions[$id] = [
                    $services[0],
                    $file,
                ];
                $services[0]->setAttribute('id', $id);
                // anonymous services are always private
                // we could not use the constant false here, because of XML parsing
                $services[0]->setAttribute('public', 'false');
            }
        }
    }
    // anonymous services "in the wild"
    if (false !== ($nodes = $xpath->query('.//container:services/container:service[not(@id)]', $root))) {
        foreach ($nodes as $node) {
            throw new InvalidArgumentException(\sprintf('Top-level services must have "id" attribute, none found in "%s" at line %d.', $file, $node->getLineNo()));
        }
    }
    // resolve definitions
    uksort($definitions, 'strnatcmp');
    foreach (array_reverse($definitions) as $id => [
        $domElement,
        $file,
    ]) {
        if (null !== ($definition = $this->parseDefinition($domElement, $file, new Definition()))) {
            $this->setDefinition($id, $definition);
        }
    }
}
RSS feed
Powered by Drupal