示例#1
0
/**
 * Extract files
 *
 * @param string $schemaDir  Schema directory
 * @param string $schemaFile Schema files
 * @param bool   $delOldDir  Delete old directory
 *
 * @return void
 */
function extractFiles($schemaDir, $schemaFile, $delOldDir = false)
{
    $baseDir = "modules/hprimxml/xsd";
    $destinationDir = "{$baseDir}/{$schemaDir}";
    $archivePath = "{$baseDir}/{$schemaFile}";
    if ($delOldDir && file_exists($destinationDir)) {
        if (CMbPath::remove($destinationDir)) {
            echo "<div class='info'>Suppression de {$destinationDir}</div>";
        } else {
            echo "<div class='error'>Impossible de supprimer le dossier {$destinationDir}</div>";
            return;
        }
    }
    if (false != ($nbFiles = CMbPath::extract($archivePath, $destinationDir))) {
        echo "<div class='info'>Extraction de {$nbFiles} fichiers pour {$schemaDir}</div>";
    } else {
        echo "<div class='error'>Impossible d'extraire l'archive {$schemaFile}</div>";
        return;
    }
    if (CAppUI::conf("hprimxml concatenate_xsd")) {
        $rootFiles = glob("{$destinationDir}/msg*.xsd");
        $includeFiles = array_diff(glob("{$destinationDir}/*.xsd"), $rootFiles);
        foreach ($rootFiles as $rootFile) {
            $xsd = new CHPrimXMLSchema();
            $xsd->loadXML(file_get_contents($rootFile));
            $xpath = new DOMXPath($xsd);
            $importFiles = array();
            foreach ($includeFiles as $includeFile) {
                $include = new DOMDOcument();
                $include->loadXML(file_get_contents($includeFile));
                $isImport = false;
                foreach ($importFiles as $key => $value) {
                    if (strpos($includeFile, $key) !== false) {
                        $isImport = true;
                        break;
                    }
                }
                foreach ($include->documentElement->childNodes as $child) {
                    $impNode = $xsd->importNode($child, true);
                    $existing = false;
                    if (in_array($impNode->nodeName, array("xsd:simpleType", "xsd:complexType"))) {
                        $name = $impNode->getAttribute('name');
                        $existing = $xpath->query("//{$impNode->nodeName}[@name='{$name}']")->length > 0;
                    }
                    if ($isImport) {
                        $xsd->documentElement->setAttribute("xmlns:insee", "http://www.hprim.org/inseeXML");
                    }
                    if (!$existing) {
                        $xsd->documentElement->appendChild($impNode);
                    }
                }
            }
            $xsd->purgeImportedNamespaces();
            $xsd->purgeIncludes();
            file_put_contents(substr($rootFile, 0, -4) . ".xml", $xsd->saveXML());
            echo "<div class='info'>Schéma concatené</div>";
        }
    }
}
 /**
  * Check schema
  *
  * @return bool
  */
 function checkSchema()
 {
     if (!is_dir($this->schemapath)) {
         $msg = "HPRIMXML schemas are missing. Please extract them from archive in '{$this->schemapath}/' directory";
         trigger_error($msg, E_USER_WARNING);
         return false;
     }
     if (!is_file($this->schemafilename)) {
         $schema = new CHPrimXMLSchema();
         $schema->importSchemaPackage($this->schemapath);
         $schema->purgeIncludes();
         $schema->purgeImportedNamespaces();
         $schema->save($this->schemafilename);
     }
     return true;
 }