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

Breadcrumb

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

function XmlDumper::addService

2 calls to XmlDumper::addService()
XmlDumper::addServices in vendor/symfony/dependency-injection/Dumper/XmlDumper.php
XmlDumper::convertParameters in vendor/symfony/dependency-injection/Dumper/XmlDumper.php

File

vendor/symfony/dependency-injection/Dumper/XmlDumper.php, line 90

Class

XmlDumper
XmlDumper dumps a service container as an XML string.

Namespace

Symfony\Component\DependencyInjection\Dumper

Code

private function addService(Definition $definition, ?string $id, \DOMElement $parent) : void {
    $service = $this->document
        ->createElement('service');
    if (null !== $id) {
        $service->setAttribute('id', $id);
    }
    if ($class = $definition->getClass()) {
        if (str_starts_with($class, '\\')) {
            $class = substr($class, 1);
        }
        $service->setAttribute('class', $class);
    }
    if (!$definition->isShared()) {
        $service->setAttribute('shared', 'false');
    }
    if ($definition->isPublic()) {
        $service->setAttribute('public', 'true');
    }
    if ($definition->isSynthetic()) {
        $service->setAttribute('synthetic', 'true');
    }
    if ($definition->isLazy()) {
        $service->setAttribute('lazy', 'true');
    }
    if (null !== ($decoratedService = $definition->getDecoratedService())) {
        [
            $decorated,
            $renamedId,
            $priority,
        ] = $decoratedService;
        $service->setAttribute('decorates', $decorated);
        $decorationOnInvalid = $decoratedService[3] ?? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
        if (\in_array($decorationOnInvalid, [
            ContainerInterface::IGNORE_ON_INVALID_REFERENCE,
            ContainerInterface::NULL_ON_INVALID_REFERENCE,
        ], true)) {
            $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE === $decorationOnInvalid ? 'null' : 'ignore';
            $service->setAttribute('decoration-on-invalid', $invalidBehavior);
        }
        if (null !== $renamedId) {
            $service->setAttribute('decoration-inner-name', $renamedId);
        }
        if (0 !== $priority) {
            $service->setAttribute('decoration-priority', $priority);
        }
    }
    $tags = $definition->getTags();
    $tags['container.error'] = array_map(fn($e) => [
        'message' => $e,
    ], $definition->getErrors());
    foreach ($tags as $name => $tags) {
        foreach ($tags as $attributes) {
            $tag = $this->document
                ->createElement('tag');
            // Check if we have recursive attributes
            if (array_filter($attributes, \is_array(...))) {
                $tag->setAttribute('name', $name);
                $this->addTagRecursiveAttributes($tag, $attributes);
            }
            else {
                if (!\array_key_exists('name', $attributes)) {
                    $tag->setAttribute('name', $name);
                }
                else {
                    $tag->appendChild($this->document
                        ->createTextNode($name));
                }
                foreach ($attributes as $key => $value) {
                    $tag->setAttribute($key, $value ?? '');
                }
            }
            $service->appendChild($tag);
        }
    }
    if ($definition->getFile()) {
        $file = $this->document
            ->createElement('file');
        $file->appendChild($this->document
            ->createTextNode($definition->getFile()));
        $service->appendChild($file);
    }
    if ($parameters = $definition->getArguments()) {
        $this->convertParameters($parameters, 'argument', $service);
    }
    if ($parameters = $definition->getProperties()) {
        $this->convertParameters($parameters, 'property', $service, 'name');
    }
    $this->addMethodCalls($definition->getMethodCalls(), $service);
    if ($callable = $definition->getFactory()) {
        if (\is_array($callable) && [
            'Closure',
            'fromCallable',
        ] !== $callable && $definition->getClass() === $callable[0]) {
            $service->setAttribute('constructor', $callable[1]);
        }
        else {
            $factory = $this->document
                ->createElement('factory');
            if (\is_array($callable) && $callable[0] instanceof Definition) {
                $this->addService($callable[0], null, $factory);
                $factory->setAttribute('method', $callable[1]);
            }
            elseif (\is_array($callable)) {
                if (null !== $callable[0]) {
                    $factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
                }
                $factory->setAttribute('method', $callable[1]);
            }
            else {
                $factory->setAttribute('function', $callable);
            }
            $service->appendChild($factory);
        }
    }
    if ($definition->isDeprecated()) {
        $deprecation = $definition->getDeprecation('%service_id%');
        $deprecated = $this->document
            ->createElement('deprecated');
        $deprecated->appendChild($this->document
            ->createTextNode($definition->getDeprecation('%service_id%')['message']));
        $deprecated->setAttribute('package', $deprecation['package']);
        $deprecated->setAttribute('version', $deprecation['version']);
        $service->appendChild($deprecated);
    }
    if ($definition->isAutowired()) {
        $service->setAttribute('autowire', 'true');
    }
    if ($definition->isAutoconfigured()) {
        $service->setAttribute('autoconfigure', 'true');
    }
    if ($definition->isAbstract()) {
        $service->setAttribute('abstract', 'true');
    }
    if ($callable = $definition->getConfigurator()) {
        $configurator = $this->document
            ->createElement('configurator');
        if (\is_array($callable) && $callable[0] instanceof Definition) {
            $this->addService($callable[0], null, $configurator);
            $configurator->setAttribute('method', $callable[1]);
        }
        elseif (\is_array($callable)) {
            $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
            $configurator->setAttribute('method', $callable[1]);
        }
        else {
            $configurator->setAttribute('function', $callable);
        }
        $service->appendChild($configurator);
    }
    $parent->appendChild($service);
}
RSS feed
Powered by Drupal