private function XmlFileLoader::validateSchema

Validates a documents XML schema.

Parameters

\DOMDocument $dom:

string $file:

Throws

RuntimeException When extension references a non-existent XSD file

InvalidArgumentException When XML doesn't validate its XSD schema

1 call to XmlFileLoader::validateSchema()
XmlFileLoader::validate in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
Validates an XML document.

File

drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php, line 307

Class

XmlFileLoader
XmlFileLoader loads XML files service definitions.

Namespace

Symfony\Component\DependencyInjection\Loader

Code

private function validateSchema(\DOMDocument $dom, $file) {
  $schemaLocations = array(
    '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()) {
        $path = str_replace($extension
          ->getNamespace(), 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_class($extension), $path));
        }
        $schemaLocations[$items[$i]] = $path;
      }
    }
  }
  $tmpfiles = array();
  $imports = '';
  foreach ($schemaLocations as $namespace => $location) {
    $parts = explode('/', $location);
    if (0 === stripos($location, 'phar://')) {
      $tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
      if ($tmpfile) {
        copy($location, $tmpfile);
        $tmpfiles[] = $tmpfile;
        $parts = explode('/', str_replace('\\', '/', $tmpfile));
      }
    }
    $drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts) . '/' : '';
    $location = 'file:///' . $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;
  $current = libxml_use_internal_errors(true);
  libxml_clear_errors();
  $valid = @$dom
    ->schemaValidateSource($source);
  foreach ($tmpfiles as $tmpfile) {
    @unlink($tmpfile);
  }
  if (!$valid) {
    throw new InvalidArgumentException(implode("\n", $this
      ->getXmlErrors($current)));
  }
  libxml_use_internal_errors($current);
}