Spaces:
Sleeping
Sleeping
File size: 19,241 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 | <?php
/*
* $Id: AbstractPropelDataModelTask.php 536 2007-01-10 14:30:38Z heltem $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://propel.phpdb.org>.
*/
//include_once 'phing/tasks/ext/CapsuleTask.php';
require_once 'phing/TaskPhing.php';
include_once 'propel/engine/database/model/AppData.php';
include_once 'propel/engine/database/model/DatabasePropel.php';
include_once 'propel/engine/database/transform/XmlToAppData.php';
/**
* An abstract base Propel task to perform work related to the XML schema file.
*
* The subclasses invoke templates to do the actual writing of the resulting files.
*
* @author Hans Lellelid <hans@xmpl.org> (Propel)
* @author Jason van Zyl <jvanzyl@zenplex.com> (Torque)
* @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
* @package propel.phing
*/
abstract class AbstractPropelDataModelTask extends TaskPhing {
/**
* Fileset of XML schemas which represent our data models.
* @var array Fileset[]
*/
protected $schemaFilesets = array();
/**
* Data models that we collect. One from each XML schema file.
*/
protected $dataModels = array();
/**
* Have datamodels been initialized?
* @var boolean
*/
private $dataModelsLoaded = false;
/**
* Map of data model name to database name.
* Should probably stick to the convention
* of them being the same but I know right now
* in a lot of cases they won't be.
*/
protected $dataModelDbMap;
/**
* Hashtable containing the names of all the databases
* in our collection of schemas.
*/
protected $databaseNames; // doesn't seem to be used anywhere
/**
* The target database(s) we are generating SQL
* for. Right now we can only deal with a single
* target, but we will support multiple targets
* soon.
*/
protected $targetDatabase;
/**
* DB encoding to use for XmlToAppData object
*/
protected $dbEncoding = 'iso-8859-1';
/**
* Target PHP package to place the generated files in.
*/
protected $targetPackage;
/**
* @var Mapper
*/
protected $mapperElement;
/**
* Destination directory for results of template scripts.
* @var PhingFile
*/
protected $outputDirectory;
/**
* Path where Capsule looks for templates.
* @var PhingFile
*/
protected $templatePath;
/**
* Whether to package the datamodels or not
* @var PhingFile
*/
protected $packageObjectModel;
/**
* Whether to perform validation (XSD) on the schema.xml file(s).
* @var boolean
*/
protected $validate;
/**
* The XSD schema file to use for validation.
* @var PhingFile
*/
protected $xsdFile;
/**
* XSL file to use to normalize (or otherwise transform) schema before validation.
* @var PhingFile
*/
protected $xslFile;
/**
* Return the data models that have been
* processed.
*
* @return List data models
*/
public function getDataModels()
{
if (!$this->dataModelsLoaded) $this->loadDataModels();
return $this->dataModels;
}
/**
* Return the data model to database name map.
*
* @return Hashtable data model name to database name map.
*/
public function getDataModelDbMap()
{
if (!$this->dataModelsLoaded) $this->loadDataModels();
return $this->dataModelDbMap;
}
/**
* Adds a set of xml schema files (nested fileset attribute).
*
* @param set a Set of xml schema files
*/
public function addSchemaFileset(Fileset $set)
{
$this->schemaFilesets[] = $set;
}
/**
* Get the current target database.
*
* @return String target database(s)
*/
public function getTargetDatabase()
{
return $this->targetDatabase;
}
/**
* Set the current target database. (e.g. mysql, oracle, ..)
*
* @param v target database(s)
*/
public function setTargetDatabase($v)
{
$this->targetDatabase = $v;
}
/**
* Get the current target package.
*
* @return string target PHP package.
*/
public function getTargetPackage()
{
return $this->targetPackage;
}
/**
* Set the current target package. This is where generated PHP classes will
* live.
*
* @param string $v target PHP package.
*/
public function setTargetPackage($v)
{
$this->targetPackage = $v;
}
/**
* Set the packageObjectModel switch on/off
*
* @param string $v The build.property packageObjectModel
*/
public function setPackageObjectModel($v)
{
$this->packageObjectModel = ($v === '1' ? true : false);
}
/**
* Set whether to perform validation on the datamodel schema.xml file(s).
* @param boolean $v
*/
public function setValidate($v)
{
$this->validate = $v;
}
/**
* Set the XSD schema to use for validation of any datamodel schema.xml file(s).
* @param $v PhingFile
*/
public function setXsd(PhingFile $v)
{
$this->xsdFile = $v;
}
/**
* Set the normalization XSLT to use to transform datamodel schema.xml file(s) before validation and parsing.
* @param $v PhingFile
*/
public function setXsl(PhingFile $v)
{
$this->xslFile = $v;
}
/**
* [REQUIRED] Set the path where Capsule will look
* for templates using the file template
* loader.
* @return void
* @throws Exception
*/
public function setTemplatePath($templatePath) {
$resolvedPath = "";
$tok = strtok($templatePath, ",");
while ( $tok ) {
// resolve relative path from basedir and leave
// absolute path untouched.
$fullPath = $this->project->resolveFile($tok);
$cpath = $fullPath->getCanonicalPath();
if ($cpath === false) {
$this->log("Template directory does not exist: " . $fullPath->getAbsolutePath());
} else {
$resolvedPath .= $cpath;
}
$tok = strtok(",");
if ( $tok ) {
$resolvedPath .= ",";
}
}
$this->templatePath = $resolvedPath;
}
/**
* Get the path where Velocity will look
* for templates using the file template
* loader.
* @return string
*/
public function getTemplatePath() {
return $this->templatePath;
}
/**
* [REQUIRED] Set the output directory. It will be
* created if it doesn't exist.
* @param PhingFile $outputDirectory
* @return void
* @throws Exception
*/
public function setOutputDirectory(PhingFile $outputDirectory) {
try {
if (!$outputDirectory->exists()) {
$this->log("Output directory does not exist, creating: " . $outputDirectory->getPath(),PROJECT_MSG_VERBOSE);
if (!$outputDirectory->mkdirs()) {
throw new IOException("Unable to create Ouptut directory: " . $outputDirectory->getAbsolutePath());
}
}
$this->outputDirectory = $outputDirectory->getCanonicalPath();
} catch (IOException $ioe) {
throw new BuildException($ioe);
}
}
/**
* Set the current target database encoding.
*
* @param v target database encoding
*/
public function setDbEncoding($v)
{
$this->dbEncoding = $v;
}
/**
* Get the output directory.
* @return string
*/
public function getOutputDirectory() {
return $this->outputDirectory;
}
/**
* Nested creator, creates one Mapper for this task.
*
* @return Mapper The created Mapper type object.
* @throws BuildException
*/
public function createMapper() {
if ($this->mapperElement !== null) {
throw new BuildException("Cannot define more than one mapper.", $this->location);
}
$this->mapperElement = new Mapper($this->project);
return $this->mapperElement;
}
/**
* Maps the passed in name to a new filename & returns resolved File object.
* @param string $from
* @return PhingFile Resolved File object.
* @throws BuilException - if no Mapper element se
* - if unable to map new filename.
*/
protected function getMappedFile($from)
{
if(!$this->mapperElement) {
throw new BuildException("This task requires you to use a <mapper/> element to describe how filename changes should be handled.");
}
$mapper = $this->mapperElement->getImplementation();
$mapped = $mapper->main($from);
if (!$mapped) {
throw new BuildException("Cannot create new filename based on: " . $from);
}
// Mappers always return arrays since it's possible for some mappers to map to multiple names.
$outFilename = array_shift($mapped);
$outFile = new PhingFile($this->getOutputDirectory(), $outFilename);
return $outFile;
}
/**
* Get the Platform class based on the target database type.
* @return Platform Class that implements the Platform interface.
*/
protected function getPlatformForTargetDatabase()
{
$classpath = $this->getPropelProperty("platformClass");
if (empty($classpath)) {
throw new BuildException("Unable to find class path for '$propname' property.");
}
// This is a slight hack to workaround camel case inconsistencies for the DDL classes.
// Basically, we want to turn ?.?.?.sqliteDDLBuilder into ?.?.?.SqliteDDLBuilder
$lastdotpos = strrpos($classpath, '.');
if ($lastdotpos) $classpath{$lastdotpos+1} = strtoupper($classpath{$lastdotpos+1});
else ucfirst($classpath);
if (empty($classpath)) {
throw new BuildException("Unable to find class path for '$propname' property.");
}
$clazz = Phing::import($classpath);
return new $clazz();
}
/**
* Gets all matching XML schema files and loads them into data models for class.
* @return void
*/
protected function loadDataModels()
{
$ads = array();
// Get all matched files from schemaFilesets
foreach($this->schemaFilesets as $fs) {
$ds = $fs->getDirectoryScanner($this->project);
$srcDir = $fs->getDir($this->project);
$dataModelFiles = $ds->getIncludedFiles();
$platform = $this->getPlatformForTargetDatabase();
// Make a transaction for each file
foreach($dataModelFiles as $dmFilename) {
$this->log("Processing: ".$dmFilename);
$xmlFile = new PhingFile($srcDir, $dmFilename);
$dom = new DomDocument('1.0', 'UTF-8');
$dom->load($xmlFile->getAbsolutePath());
// normalize (or transform) the XML document using XSLT
if ($this->xslFile) {
$this->log("Transforming " . $xmlFile->getPath() . " 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
$xslStyleSheet = new DOMDocument();
$xslStyleSheet->load($this->xslFile->getAbsolutePath());
$xsl = new XsltProcessor();
$xsl->importStyleSheet($xslStyleSheet);
$transformed = $xsl->transformToDoc($dom);
$newXmlFilename = substr($xmlFile->getName(), 0, strrpos($xmlFile->getName(), '.')) . '-transformed.xml';
// now overwrite previous vars to point to newly transformed file
$xmlFile = new PhingFile($srcDir, $newXmlFilename);
$transformed->save($xmlFile->getAbsolutePath());
$this->log("\t- Using new (post-transformation) XML file: " . $xmlFile->getPath(), PROJECT_MSG_VERBOSE);
$dom = new DomDocument('1.0', 'UTF-8');
$dom->load($xmlFile->getAbsolutePath());
}
}
// validate the XML document using XSD schema
if ($this->validate && $this->xsdFile) {
$this->log("Validating XML doc (".$xmlFile->getPath().") using schema file " . $this->xsdFile->getPath(), PROJECT_MSG_VERBOSE);
if (!$dom->schemaValidate($this->xsdFile->getAbsolutePath())) {
throw new BuildException("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)."); throw new EngineException("XML schema does not validate (using schema file $xsdFile). 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($platform, $this->getTargetPackage(), $this->dbEncoding);
$ad = $xmlParser->parseFile($xmlFile->getAbsolutePath());
$ad->setName($dmFilename); // <-- Important: use the original name, not the -transformed name.
$ads[] = $ad;
}
}
if (empty($ads)) {
throw new BuildException("No schema files were found (matching your schema fileset definition).");
}
if (!$this->packageObjectModel) {
$this->dataModels = $ads;
$this->databaseNames = array(); // doesn't seem to be used anywhere
$this->dataModelDbMap = array();
// Different datamodels may state the same database
// names, we just want the unique names of databases.
foreach($this->dataModels as $dm) {
$database = $dm->getDatabase();
$this->dataModelDbMap[$dm->getName()] = $database->getName();
$this->databaseNames[$database->getName()] = $database->getName(); // making list of *unique* dbnames.
}
} else {
$this->joinDatamodels($ads);
$this->dataModels[0]->getDatabases(); // calls doFinalInitialization()
}
$this->dataModelsLoaded = true;
}
/**
* Joins the datamodels collected from schema.xml files into one big datamodel
*
* This applies only when the the packageObjectModel option is set. We need to
* join the datamodels in this case to allow for foreign keys that point to
* tables in different packages.
*
* @param array $ads The datamodels to join
*/
protected function joinDatamodels($ads) {
foreach($ads as $ad) {
$db = $ad->getDatabase(null, false);
$this->dataModelDbMap[$ad->getName()] = $db->getName();
}
foreach ($ads as $addAd) {
$ad = &$this->dataModels[0];
if (!isset($ad)) {
$addAd->setName('JoinedDataModel');
$ad = $addAd;
continue;
}
foreach ($addAd->getDatabases(false) as $addDb) {
$addDbName = $addDb->getName();
if (!$package = $addDb->getPackage()) {
throw new BuildException('No package found for database "' . $addDbName . '" in ' . $addAd->getName() . '. The propel.packageObjectModel property requires the package attribute to be set for each database.');
}
$db = $ad->getDatabase($addDbName, false);
if (!$db) {
$ad->addDatabase($addDb);
continue;
}
foreach ($addDb->getTables() as $addTable) {
$table = $db->getTable($addTable->getName());
if ($table) {
throw new BuildException('Duplicate table found: ' . $addDbName . '.');
}
$db->addTable($addTable);
}
}
}
}
/**
* Creates a new Capsule context with some basic properties set.
* (Capsule is a simple PHP encapsulation system -- aka a php "template" class.)
* @return Capsule
*/
protected function createContext() {
$context = new Capsule();
// Make sure the output directory exists, if it doesn't
// then create it.
$outputDir = new PhingFile($this->outputDirectory);
if (!$outputDir->exists()) {
$this->log("Output directory does not exist, creating: " . $outputDir->getAbsolutePath());
$outputDir->mkdirs();
}
// Place our set of data models into the context along
// with the names of the databases as a convenience for now.
$context->put("targetDatabase", $this->targetDatabase);
$context->put("targetPackage", $this->targetPackage);
$context->put("now", strftime("%c"));
$this->log("Target database type: " . $this->targetDatabase);
$this->log("Target package: " . $this->targetPackage);
$this->log("Using template path: " . $this->templatePath);
$this->log("Output directory: " . $this->outputDirectory);
$context->setTemplatePath($this->templatePath);
$context->setOutputDirectory($this->outputDirectory);
$this->populateContextProperties($context);
return $context;
}
/**
* Fetches the propel.xxx properties from project, renaming the propel.xxx properties to just xxx.
*
* Also, renames any xxx.yyy properties to xxxYyy as PHP doesn't like the xxx.yyy syntax.
*
* @return array Assoc array of properties.
*/
protected function getPropelProperties()
{
$allProps = $this->getProject()->getProperties();
$renamedPropelProps = array();
foreach ($allProps as $key => $propValue) {
if (strpos($key, "propel.") === 0) {
$newKey = substr($key, strlen("propel."));
$j = strpos($newKey, '.');
while ($j !== false) {
$newKey = substr($newKey, 0, $j) . ucfirst(substr($newKey, $j + 1));
$j = strpos($newKey, '.');
}
$renamedPropelProps[$newKey] = $propValue;
}
}
return $renamedPropelProps;
}
/**
* Fetches a single propel.xxx property from project, using "converted" property names.
* @see getPropelProperties()
* @param string $name Name of property to fetch (in converted CamelCase)
* @return string The value of the property (or NULL if not set)
*/
protected function getPropelProperty($name)
{
$props = $this->getPropelProperties();
if (isset($props[$name])) {
return $props[$name];
}
return null; // just to be explicit
}
/**
* Adds the propel.xxx properties to the passed Capsule context, changing names to just xxx.
*
* Also, move xxx.yyy properties to xxxYyy as PHP doesn't like the xxx.yyy syntax.
*
* @param Capsule $context
* @see getPropelProperties()
*/
public function populateContextProperties(Capsule $context)
{
foreach ($this->getPropelProperties() as $key => $propValue) {
$this->log('Adding property ${' . $key . '} to context', PROJECT_MSG_DEBUG);
$context->put($key, $propValue);
}
}
/**
* Checks this class against Basic requrements of any propel datamodel task.
*
* @throws BuildException - if schema fileset was not defined
* - if no output directory was specified
*/
protected function validate()
{
if (empty($this->schemaFilesets)) {
throw new BuildException("You must specify a fileset of XML schemas.", $this->getLocation());
}
// Make sure the output directory is set.
if ($this->outputDirectory === null) {
throw new BuildException("The output directory needs to be defined!", $this->getLocation());
}
if ($this->validate) {
if (!$this->xsdFile) {
throw new BuildException("'validate' set to TRUE, but no XSD specified (use 'xsd' attribute).", $this->getLocation());
}
}
}
}
|