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

Breadcrumb

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

function YamlDumper::addService

1 call to YamlDumper::addService()
YamlDumper::addServices in vendor/symfony/dependency-injection/Dumper/YamlDumper.php

File

vendor/symfony/dependency-injection/Dumper/YamlDumper.php, line 56

Class

YamlDumper
YamlDumper dumps a service container as a YAML string.

Namespace

Symfony\Component\DependencyInjection\Dumper

Code

private function addService(string $id, Definition $definition) : string {
    $code = "    {$id}:\n";
    if ($class = $definition->getClass()) {
        if (str_starts_with($class, '\\')) {
            $class = substr($class, 1);
        }
        $code .= \sprintf("        class: %s\n", $this->dumper
            ->dump($class));
    }
    if (!$definition->isPrivate()) {
        $code .= \sprintf("        public: %s\n", $definition->isPublic() ? 'true' : 'false');
    }
    $tagsCode = '';
    $tags = $definition->getTags();
    $tags['container.error'] = array_map(fn($e) => [
        'message' => $e,
    ], $definition->getErrors());
    foreach ($tags as $name => $tags) {
        foreach ($tags as $attributes) {
            $att = [];
            foreach ($attributes as $key => $value) {
                $att[] = \sprintf('%s: %s', $this->dumper
                    ->dump($key), $this->dumper
                    ->dump($value));
            }
            $att = $att ? ': { ' . implode(', ', $att) . ' }' : '';
            $tagsCode .= \sprintf("            - %s%s\n", $this->dumper
                ->dump($name), $att);
        }
    }
    if ($tagsCode) {
        $code .= "        tags:\n" . $tagsCode;
    }
    if ($definition->getFile()) {
        $code .= \sprintf("        file: %s\n", $this->dumper
            ->dump($definition->getFile()));
    }
    if ($definition->isSynthetic()) {
        $code .= "        synthetic: true\n";
    }
    if ($definition->isDeprecated()) {
        $code .= "        deprecated:\n";
        foreach ($definition->getDeprecation('%service_id%') as $key => $value) {
            if ('' !== $value) {
                $code .= \sprintf("            %s: %s\n", $key, $this->dumper
                    ->dump($value));
            }
        }
    }
    if ($definition->isAutowired()) {
        $code .= "        autowire: true\n";
    }
    if ($definition->isAutoconfigured()) {
        $code .= "        autoconfigure: true\n";
    }
    if ($definition->isAbstract()) {
        $code .= "        abstract: true\n";
    }
    if ($definition->isLazy()) {
        $code .= "        lazy: true\n";
    }
    if ($definition->getArguments()) {
        $code .= \sprintf("        arguments: %s\n", $this->dumper
            ->dump($this->dumpValue($definition->getArguments()), 0));
    }
    if ($definition->getProperties()) {
        $code .= \sprintf("        properties: %s\n", $this->dumper
            ->dump($this->dumpValue($definition->getProperties()), 0));
    }
    if ($definition->getMethodCalls()) {
        $code .= \sprintf("        calls:\n%s\n", $this->dumper
            ->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
    }
    if (!$definition->isShared()) {
        $code .= "        shared: false\n";
    }
    if (null !== ($decoratedService = $definition->getDecoratedService())) {
        [
            $decorated,
            $renamedId,
            $priority,
        ] = $decoratedService;
        $code .= \sprintf("        decorates: %s\n", $decorated);
        if (null !== $renamedId) {
            $code .= \sprintf("        decoration_inner_name: %s\n", $renamedId);
        }
        if (0 !== $priority) {
            $code .= \sprintf("        decoration_priority: %s\n", $priority);
        }
        $decorationOnInvalid = $decoratedService[3] ?? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
        if (\in_array($decorationOnInvalid, [
            ContainerInterface::IGNORE_ON_INVALID_REFERENCE,
            ContainerInterface::NULL_ON_INVALID_REFERENCE,
        ])) {
            $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE === $decorationOnInvalid ? 'null' : 'ignore';
            $code .= \sprintf("        decoration_on_invalid: %s\n", $invalidBehavior);
        }
    }
    if ($callable = $definition->getFactory()) {
        if (\is_array($callable) && [
            'Closure',
            'fromCallable',
        ] !== $callable && $definition->getClass() === $callable[0]) {
            $code .= \sprintf("        constructor: %s\n", $callable[1]);
        }
        else {
            $code .= \sprintf("        factory: %s\n", $this->dumper
                ->dump($this->dumpCallable($callable), 0));
        }
    }
    if ($callable = $definition->getConfigurator()) {
        $code .= \sprintf("        configurator: %s\n", $this->dumper
            ->dump($this->dumpCallable($callable), 0));
    }
    return $code;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal