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

Breadcrumb

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

function XmlFileLoader::getArgumentsAsPhp

2 calls to XmlFileLoader::getArgumentsAsPhp()
XmlFileLoader::parseDefinition in vendor/symfony/dependency-injection/Loader/XmlFileLoader.php
Parses an individual Definition.
XmlFileLoader::parseParameters in vendor/symfony/dependency-injection/Loader/XmlFileLoader.php

File

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

Class

XmlFileLoader
XmlFileLoader loads XML files service definitions.

Namespace

Symfony\Component\DependencyInjection\Loader

Code

private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file, bool $isChildDefinition = false) : array {
    $arguments = [];
    foreach ($this->getChildren($node, $name) as $arg) {
        if ($arg->hasAttribute('name')) {
            $arg->setAttribute('key', $arg->getAttribute('name'));
        }
        // this is used by ChildDefinition to overwrite a specific
        // argument of the parent definition
        if ($arg->hasAttribute('index')) {
            $key = ($isChildDefinition ? 'index_' : '') . $arg->getAttribute('index');
        }
        elseif (!$arg->hasAttribute('key')) {
            // Append an empty argument, then fetch its key to overwrite it later
            $arguments[] = null;
            $keys = array_keys($arguments);
            $key = array_pop($keys);
        }
        else {
            $key = $arg->getAttribute('key');
        }
        switch ($arg->getAttribute('key-type')) {
            case 'binary':
                if (false === ($key = base64_decode($key, true))) {
                    throw new InvalidArgumentException(\sprintf('Tag "<%s>" with key-type="binary" does not have a valid base64 encoded key in "%s".', $name, $file));
                }
                break;
            case 'constant':
                try {
                    $key = \constant(trim($key));
                } catch (\Error) {
                    throw new InvalidArgumentException(\sprintf('The key "%s" is not a valid constant in "%s".', $key, $file));
                }
                break;
        }
        $trim = $arg->hasAttribute('trim') && XmlUtils::phpize($arg->getAttribute('trim'));
        $onInvalid = $arg->getAttribute('on-invalid');
        $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
        if ('ignore' == $onInvalid) {
            $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
        }
        elseif ('ignore_uninitialized' == $onInvalid) {
            $invalidBehavior = ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE;
        }
        elseif ('null' == $onInvalid) {
            $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
        }
        switch ($type = $arg->getAttribute('type')) {
            case 'service':
                if ('' === $arg->getAttribute('id')) {
                    throw new InvalidArgumentException(\sprintf('Tag "<%s>" with type="service" has no or empty "id" attribute in "%s".', $name, $file));
                }
                $arguments[$key] = new Reference($arg->getAttribute('id'), $invalidBehavior);
                break;
            case 'expression':
                if (!class_exists(Expression::class)) {
                    throw new \LogicException('The type="expression" attribute cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".');
                }
                $arguments[$key] = new Expression($arg->nodeValue);
                break;
            case 'collection':
                $arguments[$key] = $this->getArgumentsAsPhp($arg, $name, $file);
                break;
            case 'iterator':
                $arg = $this->getArgumentsAsPhp($arg, $name, $file);
                $arguments[$key] = new IteratorArgument($arg);
                break;
            case 'closure':
            case 'service_closure':
                if ('' !== $arg->getAttribute('id')) {
                    $arg = new Reference($arg->getAttribute('id'), $invalidBehavior);
                }
                else {
                    $arg = $this->getArgumentsAsPhp($arg, $name, $file);
                }
                $arguments[$key] = match ($type) {    'service_closure' => new ServiceClosureArgument($arg),
                    'closure' => (new Definition('Closure'))->setFactory([
                        'Closure',
                        'fromCallable',
                    ])
                        ->addArgument($arg),
                
                };
                break;
            case 'service_locator':
                $arg = $this->getArgumentsAsPhp($arg, $name, $file);
                $arguments[$key] = new ServiceLocatorArgument($arg);
                break;
            case 'tagged':
                trigger_deprecation('symfony/dependency-injection', '7.2', 'Type "tagged" is deprecated for tag <%s>, use "tagged_iterator" instead in "%s".', $name, $file);
            // no break
            case 'tagged_iterator':
            case 'tagged_locator':
                $forLocator = 'tagged_locator' === $type;
                if (!$arg->getAttribute('tag')) {
                    throw new InvalidArgumentException(\sprintf('Tag "<%s>" with type="%s" has no or empty "tag" attribute in "%s".', $name, $type, $file));
                }
                $excludes = array_column($this->getChildren($arg, 'exclude'), 'nodeValue');
                if ($arg->hasAttribute('exclude')) {
                    if (\count($excludes) > 0) {
                        throw new InvalidArgumentException('You cannot use both the attribute "exclude" and <exclude> tags at the same time.');
                    }
                    $excludes = [
                        $arg->getAttribute('exclude'),
                    ];
                }
                $arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag'), $arg->getAttribute('index-by') ?: null, $arg->getAttribute('default-index-method') ?: null, $forLocator, $arg->getAttribute('default-priority-method') ?: null, $excludes, !$arg->hasAttribute('exclude-self') || XmlUtils::phpize($arg->getAttribute('exclude-self')));
                if ($forLocator) {
                    $arguments[$key] = new ServiceLocatorArgument($arguments[$key]);
                }
                break;
            case 'binary':
                if (false === ($value = base64_decode($arg->nodeValue))) {
                    throw new InvalidArgumentException(\sprintf('Tag "<%s>" with type="binary" is not a valid base64 encoded string.', $name));
                }
                $arguments[$key] = $value;
                break;
            case 'abstract':
                $arguments[$key] = new AbstractArgument($arg->nodeValue);
                break;
            case 'string':
                $arguments[$key] = $trim ? trim($arg->nodeValue) : $arg->nodeValue;
                break;
            case 'constant':
                $arguments[$key] = \constant(trim($arg->nodeValue));
                break;
            default:
                $arguments[$key] = XmlUtils::phpize($trim ? trim($arg->nodeValue) : $arg->nodeValue);
        }
    }
    return $arguments;
}
RSS feed
Powered by Drupal