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

Breadcrumb

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

function XmlFileLoader::parseImport

Parses an import and adds the routes in the resource to the RouteCollection.

Throws

\InvalidArgumentException When the XML is invalid

1 call to XmlFileLoader::parseImport()
XmlFileLoader::parseNode in vendor/symfony/routing/Loader/XmlFileLoader.php
Parses a node from a loaded XML file.

File

vendor/symfony/routing/Loader/XmlFileLoader.php, line 152

Class

XmlFileLoader
XmlFileLoader loads XML routing files.

Namespace

Symfony\Component\Routing\Loader

Code

protected function parseImport(RouteCollection $collection, \DOMElement $node, string $path, string $file) : void {
    
    /** @var \DOMElement $resourceElement */
    if (!($resource = $node->getAttribute('resource') ?: null) && ($resourceElement = $node->getElementsByTagName('resource')[0] ?? null)) {
        $resource = [];
        
        /** @var \DOMAttr $attribute */
        foreach ($resourceElement->attributes as $attribute) {
            $resource[$attribute->name] = $attribute->value;
        }
    }
    if (!$resource) {
        throw new \InvalidArgumentException(\sprintf('The <import> element in file "%s" must have a "resource" attribute or element.', $path));
    }
    $type = $node->getAttribute('type');
    $prefix = $node->getAttribute('prefix');
    $schemes = $node->hasAttribute('schemes') ? preg_split('/[\\s,\\|]++/', $node->getAttribute('schemes'), -1, \PREG_SPLIT_NO_EMPTY) : null;
    $methods = $node->hasAttribute('methods') ? preg_split('/[\\s,\\|]++/', $node->getAttribute('methods'), -1, \PREG_SPLIT_NO_EMPTY) : null;
    $trailingSlashOnRoot = $node->hasAttribute('trailing-slash-on-root') ? XmlUtils::phpize($node->getAttribute('trailing-slash-on-root')) : true;
    $namePrefix = $node->getAttribute('name-prefix') ?: null;
    [
        $defaults,
        $requirements,
        $options,
        $condition,
        ,
        $prefixes,
        $hosts,
    ] = $this->parseConfigs($node, $path);
    if ('' !== $prefix && $prefixes) {
        throw new \InvalidArgumentException(\sprintf('The <route> element in file "%s" must not have both a "prefix" attribute and <prefix> child nodes.', $path));
    }
    $exclude = [];
    foreach ($node->childNodes as $child) {
        if ($child instanceof \DOMElement && $child->localName === $exclude && self::NAMESPACE_URI === $child->namespaceURI) {
            $exclude[] = $child->nodeValue;
        }
    }
    if ($node->hasAttribute('exclude')) {
        if ($exclude) {
            throw new \InvalidArgumentException('You cannot use both the attribute "exclude" and <exclude> tags at the same time.');
        }
        $exclude = [
            $node->getAttribute('exclude'),
        ];
    }
    $this->setCurrentDir(\dirname($path));
    
    /** @var RouteCollection[] $imported */
    $imported = $this->import($resource, '' !== $type ? $type : null, false, $file, $exclude) ?: [];
    if (!\is_array($imported)) {
        $imported = [
            $imported,
        ];
    }
    foreach ($imported as $subCollection) {
        $this->addPrefix($subCollection, $prefixes ?: $prefix, $trailingSlashOnRoot);
        if (null !== $hosts) {
            $this->addHost($subCollection, $hosts);
        }
        if (null !== $condition) {
            $subCollection->setCondition($condition);
        }
        if (null !== $schemes) {
            $subCollection->setSchemes($schemes);
        }
        if (null !== $methods) {
            $subCollection->setMethods($methods);
        }
        if (null !== $namePrefix) {
            $subCollection->addNamePrefix($namePrefix);
        }
        $subCollection->addDefaults($defaults);
        $subCollection->addRequirements($requirements);
        $subCollection->addOptions($options);
        $collection->addCollection($subCollection);
    }
}

API Navigation

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