public function getDatabase() { if (null === $this->database) { $xtad = new XmlToAppData($this->getPlatform()); $xtad->setGeneratorConfig($this->getConfig()); $appData = $xtad->parseString($this->schema); $this->database = $appData->getDatabase(); // does final initialization } return $this->database; }
/** * Gets all matching XML schema files and loads them into data models for class. * @return void */ protected function loadDataModels() { $ads = array(); $totalNbTables = 0; $dataModelFiles = $this->getSchemas(); $defaultPlatform = $this->getGeneratorConfig()->getConfiguredPlatform(); // Make a transaction for each file foreach ($dataModelFiles as $schema) { $dmFilename = $schema->getPathName(); $this->log('Processing: ' . $schema->getFileName()); $dom = new DomDocument('1.0', 'UTF-8'); $dom->load($dmFilename); $this->includeExternalSchemas($dom, $schema->getPath()); // normalize (or transform) the XML document using XSLT if ($this->getGeneratorConfig()->getBuildProperty('schemaTransform') && $this->xsl) { $this->log('Transforming ' . $dmFilename . ' using stylesheet ' . $this->xsl->getPath()); if (!class_exists('\\XSLTProcessor')) { $this->log('Could not perform XLST transformation. Make sure PHP has been compiled/configured to support XSLT.'); } else { // normalize the document using normalizer stylesheet $xslDom = new DomDocument('1.0', 'UTF-8'); $xslDom->load($this->xsl->getAbsolutePath()); $xsl = new \XsltProcessor(); $xsl->importStyleSheet($xslDom); $dom = $xsl->transformToDoc($dom); } } // validate the XML document using XSD schema if ($this->validate && $this->xsd) { $this->log(' Validating XML using schema ' . $this->xsd->getPath()); if (!$dom->schemaValidate($this->xsd->getAbsolutePath())) { throw new EngineException(<<<EOT XML schema file ({$xmlFile->getPath}()) does not validate. See warnings above for reasons validation failed (make sure error_reporting is set to show E_WARNING if you don't see any) EOT , $this->getLocation()); } } $xmlParser = new XmlToAppData($defaultPlatform, $this->dbEncoding); $xmlParser->setGeneratorConfig($this->getGeneratorConfig()); $ad = $xmlParser->parseString($dom->saveXML(), $dmFilename); $nbTables = $ad->getDatabase(null, false)->countTables(); $totalNbTables += $nbTables; $this->log(sprintf(' %d tables processed successfully', $nbTables)); $ad->setName($dmFilename); $ads[] = $ad; } $this->log(sprintf('%d tables found in %d schema files.', $totalNbTables, count($dataModelFiles))); if (empty($ads)) { throw new BuildException('No schema files were found (matching your schema fileset definition).'); } foreach ($ads as $ad) { // map schema filename with database name $this->dataModelDbMap[$ad->getName()] = $ad->getDatabase(null, false)->getName(); } if (count($ads) > 1 && $this->getGeneratorConfig()->getBuildProperty('packageObjectModel')) { $ad = $this->joinDataModels($ads); $this->dataModels = array($ad); } else { $this->dataModels = $ads; } foreach ($this->dataModels as &$ad) { $ad->doFinalInitialization(); } $this->dataModelsLoaded = true; }
public function testSetPackageOverridesNamespaceAutoPackage() { $schema = <<<EOF <database name="DB" namespace="NS1"> <table name="table" namespace="NS2" package="foo"> <column name="id" primaryKey="true" /> <column name="title1" type="VARCHAR" /> </table> </database> EOF; $config = new GeneratorConfig(); $config->setBuildProperties(array('propel.namespace.autoPackage' => 'true')); $xmlToAppData = new XmlToAppData(new DefaultPlatform()); $xmlToAppData->setGeneratorConfig($config); $table = $xmlToAppData->parseString($schema)->getDatabase('DB')->getTable('table'); $this->assertEquals('foo', $table->getPackage()); }
/** * Gets all matching XML schema files and loads them into data models for class. * @return void */ protected function loadDataModels() { $ads = array(); $totalNbTables = 0; $this->log('Loading XML schema files...'); // Get all matched files from schemaFilesets foreach ($this->schemaFilesets as $fs) { $ds = $fs->getDirectoryScanner($this->project); $srcDir = $fs->getDir($this->project); $dataModelFiles = $ds->getIncludedFiles(); sort($dataModelFiles); $defaultPlatform = $this->getGeneratorConfig()->getConfiguredPlatform(); // Make a transaction for each file foreach ($dataModelFiles as $dmFilename) { $this->log("Processing: " . $dmFilename, Project::MSG_VERBOSE); $xmlFile = new PhingFile($srcDir, $dmFilename); $dom = new DomDocument('1.0', 'UTF-8'); $dom->load($xmlFile->getAbsolutePath()); // modify schema to include any external schemas (and remove the external-schema nodes) $this->includeExternalSchemas($dom, $srcDir); // normalize (or transform) the XML document using XSLT if ($this->getGeneratorConfig()->getBuildProperty('schemaTransform') && $this->xslFile) { $this->log("Transforming " . $dmFilename . " using stylesheet " . $this->xslFile->getPath(), Project::MSG_VERBOSE); if (!class_exists('XSLTProcessor')) { $this->log("Could not perform XLST transformation. Make sure PHP has been compiled/configured to support XSLT.", Project::MSG_ERR); } else { // normalize the document using normalizer stylesheet $xslDom = new DomDocument('1.0', 'UTF-8'); $xslDom->load($this->xslFile->getAbsolutePath()); $xsl = new XsltProcessor(); $xsl->importStyleSheet($xslDom); $dom = $xsl->transformToDoc($dom); } } // validate the XML document using XSD schema if ($this->validate && $this->xsdFile) { $this->log(" Validating XML using schema " . $this->xsdFile->getPath(), Project::MSG_VERBOSE); if (!$dom->schemaValidate($this->xsdFile->getAbsolutePath())) { throw new EngineException("XML schema file (" . $xmlFile->getPath() . ") does not validate. See warnings above for reasons validation failed (make sure error_reporting is set to show E_WARNING if you don't see any).", $this->getLocation()); } } $xmlParser = new XmlToAppData($defaultPlatform, $this->getTargetPackage(), $this->dbEncoding); $xmlParser->setGeneratorConfig($this->getGeneratorConfig()); $ad = $xmlParser->parseString($dom->saveXML(), $xmlFile->getAbsolutePath()); $nbTables = $ad->getDatabase(null, false)->countTables(); $totalNbTables += $nbTables; $this->log(sprintf(' %d tables processed successfully', $nbTables), Project::MSG_VERBOSE); $ad->setName($dmFilename); $ads[] = $ad; } $this->log(sprintf('%d tables found in %d schema files.', $totalNbTables, count($dataModelFiles))); } if (empty($ads)) { throw new BuildException("No schema files were found (matching your schema fileset definition)."); } foreach ($ads as $ad) { // map schema filename with database name $this->dataModelDbMap[$ad->getName()] = $ad->getDatabase(null, false)->getName(); } if (count($ads) > 1 && $this->packageObjectModel) { $ad = $this->joinDataModels($ads); $this->dataModels = array($ad); } else { $this->dataModels = $ads; } foreach ($this->dataModels as &$ad) { $ad->doFinalInitialization(); } $this->dataModelsLoaded = true; }