You are here

public function XmlFileLoader::validateSchema in Service Container 7.2

Same name and namespace in other branches
  1. 7 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php \Symfony\Component\DependencyInjection\Loader\XmlFileLoader::validateSchema()

Validates a documents XML schema.

Parameters

\DOMDocument $dom:

Return value

bool

Throws

RuntimeException When extension references a non-existent XSD file

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php, line 433

Class

XmlFileLoader
XmlFileLoader loads XML files service definitions.

Namespace

Symfony\Component\DependencyInjection\Loader

Code

public function validateSchema(\DOMDocument $dom) {
  $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;
  $valid = @$dom
    ->schemaValidateSource($source);
  foreach ($tmpfiles as $tmpfile) {
    @unlink($tmpfile);
  }
  return $valid;
}