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

Breadcrumb

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

function XmlFileLoader::validateSchema

Validates a documents XML schema.

Throws

RuntimeException When extension references a non-existent XSD file

1 call to XmlFileLoader::validateSchema()
XmlFileLoader::parseFileToDOM in vendor/symfony/dependency-injection/Loader/XmlFileLoader.php
Parses an XML file to a \DOMDocument.

File

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

Class

XmlFileLoader
XmlFileLoader loads XML files service definitions.

Namespace

Symfony\Component\DependencyInjection\Loader

Code

public function validateSchema(\DOMDocument $dom) : bool {
    $schemaLocations = [
        'http://symfony.com/schema/dic/services' => str_replace('\\', '/', __DIR__ . '/schema/dic/services/services-1.0.xsd'),
    ];
    if ($element = $dom->documentElement
        ->getAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')) {
        $items = preg_split('/\\s+/', $element);
        for ($i = 0, $nb = \count($items); $i < $nb; $i += 2) {
            if (!$this->container
                ->hasExtension($items[$i])) {
                continue;
            }
            if (($extension = $this->container
                ->getExtension($items[$i])) && false !== $extension->getXsdValidationBasePath()) {
                $ns = $extension->getNamespace();
                $path = str_replace([
                    $ns,
                    str_replace('http://', 'https://', $ns),
                ], str_replace('\\', '/', $extension->getXsdValidationBasePath()) . '/', $items[$i + 1]);
                if (!is_file($path)) {
                    throw new RuntimeException(\sprintf('Extension "%s" references a non-existent XSD file "%s".', get_debug_type($extension), $path));
                }
                $schemaLocations[$items[$i]] = $path;
            }
        }
    }
    $tmpfiles = [];
    $imports = '';
    foreach ($schemaLocations as $namespace => $location) {
        $parts = explode('/', $location);
        $locationstart = 'file:///';
        if (0 === stripos($location, 'phar://')) {
            $tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
            if ($tmpfile) {
                copy($location, $tmpfile);
                $tmpfiles[] = $tmpfile;
                $parts = explode('/', str_replace('\\', '/', $tmpfile));
            }
            else {
                array_shift($parts);
                $locationstart = 'phar:///';
            }
        }
        elseif ('\\' === \DIRECTORY_SEPARATOR && str_starts_with($location, '\\\\')) {
            $locationstart = '';
        }
        $drive = '\\' === \DIRECTORY_SEPARATOR ? array_shift($parts) . '/' : '';
        $location = $locationstart . $drive . implode('/', array_map('rawurlencode', $parts));
        $imports .= \sprintf('  <xsd:import namespace="%s" schemaLocation="%s" />' . "\n", $namespace, $location);
    }
    $source = <<<EOF
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema xmlns="http://symfony.com/schema"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://symfony.com/schema"
    elementFormDefault="qualified">

    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
{<span class="php-variable">$imports</span>}
</xsd:schema>
EOF;
    if ($this->shouldEnableEntityLoader()) {
        $disableEntities = libxml_disable_entity_loader(false);
        $valid = @$dom->schemaValidateSource($source);
        libxml_disable_entity_loader($disableEntities);
    }
    else {
        $valid = @$dom->schemaValidateSource($source);
    }
    foreach ($tmpfiles as $tmpfile) {
        @unlink($tmpfile);
    }
    return $valid;
}

API Navigation

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