/** * Gets the full path to the file for the current class. * @return string */ public function getClassFilePath() { return ClassTools::getFilePath($this->getPackage(), $this->getClassname()); }
public function getClassFilePath() { return ClassTools::getFilePath('lib.model.om', $this->getClassname()); }
/** * Gets the file path to the parent class. * @return string */ protected function getParentClassFilePath() { return ClassTools::getFilePath($this->getParentClasspath()); }
/** * Lists data model classes and builds an associative array className => classPath * To be used for autoloading * @return array */ protected function getClassMap() { $phpconfClassmap = array(); $generatorConfig = $this->getGeneratorConfig(); foreach ($this->getDataModels() as $dataModel) { foreach ($dataModel->getDatabases() as $database) { $classMap = array(); foreach ($database->getTables() as $table) { if (!$table->isForReferenceOnly()) { // ----------------------------------------------------- // Add TableMap class, // Peer, Object & Query stub classes, // and Peer, Object & Query base classes // ----------------------------------------------------- // (this code is based on PropelOMTask) foreach (array('tablemap', 'peerstub', 'objectstub', 'querystub', 'peer', 'object', 'query') as $target) { $builder = $generatorConfig->getConfiguredBuilder($table, $target); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } // ----------------------------------------------------- // Add children classes for object and query, // as well as base child query, // for single tabel inheritance tables. // ----------------------------------------------------- if ($col = $table->getChildrenColumn()) { if ($col->isEnumeratedClasses()) { foreach ($col->getChildren() as $child) { foreach (array('objectmultiextend', 'queryinheritance', 'queryinheritancestub') as $target) { $builder = $generatorConfig->getConfiguredBuilder($table, $target); $builder->setChild($child); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } } } } // ----------------------------------------------------- // Add base classes for alias tables (undocumented) // ----------------------------------------------------- $baseClass = $table->getBaseClass(); if ($baseClass !== null) { $className = ClassTools::classname($baseClass); if (!isset($classMap[$className])) { $classPath = ClassTools::getFilePath($baseClass); $this->log('Adding class mapping: ' . $className . ' => ' . $classPath); $classMap[$className] = $classPath; } } $basePeer = $table->getBasePeer(); if ($basePeer !== null) { $className = ClassTools::classname($basePeer); if (!isset($classMap[$className])) { $classPath = ClassTools::getFilePath($basePeer); $this->log('Adding class mapping: ' . $className . ' => ' . $classPath); $classMap[$className] = $classPath; } } // ---------------------------------------------- // Add classes for interface // ---------------------------------------------- if ($table->getInterface()) { $builder = $generatorConfig->getConfiguredBuilder($table, 'interface'); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } // ---------------------------------------------- // Add classes from old treeMode implementations // ---------------------------------------------- if ($table->treeMode() == 'MaterializedPath') { foreach (array('nodepeerstub', 'nodestub', 'nodepeer', 'node') as $target) { $builder = $generatorConfig->getConfiguredBuilder($table, $target); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } } if ($table->treeMode() == 'NestedSet') { foreach (array('nestedset', 'nestedsetpeer') as $target) { $builder = $generatorConfig->getConfiguredBuilder($table, $target); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } } } // if (!$table->isReferenceOnly()) } $phpconfClassmap = array_merge($phpconfClassmap, $classMap); } } return $phpconfClassmap; }
protected function addClassClose(&$script) { parent::addClassClose($script); $behaviors = $this->getTable()->getAttribute('behaviors'); if ($behaviors) { $behavior_file_name = 'Base' . $this->getTable()->getPhpName() . 'Behaviors'; $behavior_file_path = ClassTools::getFilePath($this->getStubObjectBuilder()->getPackage() . '.om', $behavior_file_name); $behavior_include_script = <<<EOF if (sfProjectConfiguration::getActive() instanceof sfApplicationConfiguration) { include_once '%s'; } EOF; $script .= sprintf($behavior_include_script, $behavior_file_path); } }
protected function addClassClose(&$script) { parent::addClassClose($script); $behavior_file_name = 'Base' . $this->getTable()->getPhpName() . 'Behaviors'; $behavior_file_path = ClassTools::getFilePath($this->getStubObjectBuilder()->getPackage() . '.om', $behavior_file_name); $absolute_behavior_file_path = sfConfig::get('sf_root_dir') . '/' . $behavior_file_path; if (file_exists($absolute_behavior_file_path)) { unlink($absolute_behavior_file_path); } $behaviors = $this->getTable()->getAttribute('behaviors'); if ($behaviors) { file_put_contents($absolute_behavior_file_path, sprintf("<?php\nsfPropelBehavior::add('%s', %s);\n", $this->getTable()->getPhpName(), var_export(unserialize($behaviors), true))); $behavior_include_script = <<<EOF if (sfProjectConfiguration::getActive() instanceof sfApplicationConfiguration) { include_once '%s'; } EOF; $script .= sprintf($behavior_include_script, $behavior_file_path); } }
/** * Returns the path to the current model's behaviors configuration file. * * @param boolean $absolute * * @return string */ protected function getBehaviorsFilePath($absolute = false) { $base = $absolute ? sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR : ''; return $base . ClassTools::getFilePath($this->getTable()->getPackage() . '.om', sprintf('Base%sBehaviors', $this->getTable()->getPhpName())); }
public function main() { // check to make sure task received all correct params $this->validate(); $basepath = $this->getOutputDirectory(); // Get new Capsule context $generator = $this->createContext(); $generator->put("basepath", $basepath); // make available to other templates $targetPlatform = $this->getTargetPlatform(); // convenience for embedding in strings below // we need some values that were loaded into the template context $basePrefix = $generator->get('basePrefix'); $project = $generator->get('project'); foreach ($this->getDataModels() as $dataModel) { $this->log("Processing Datamodel : " . $dataModel->getName()); foreach ($dataModel->getDatabases() as $database) { $this->log(" - processing database : " . $database->getName()); $generator->put("platform", $database->getPlatform()); foreach ($database->getTables() as $table) { if (!$table->isForReferenceOnly()) { if ($table->getPackage()) { $package = $table->getPackage(); } else { $package = $this->targetPackage; } $pkbase = "{$package}.om"; $pkpeer = "{$package}"; $pkmap = "{$package}.map"; // make these available $generator->put("package", $package); $generator->put("pkbase", $pkbase); //$generator->put("pkpeer", $pkpeer); -- we're not using this yet $generator->put("pkmap", $pkmap); foreach (array($pkpeer, $pkmap, $pkbase) as $pk) { $path = strtr($pk, '.', '/'); $f = new PhingFile($this->getOutputDirectory(), $path); if (!$f->exists()) { if (!$f->mkdirs()) { throw new Exception("Error creating directories: " . $f->getPath()); } } } // for each package $this->log("\t+ " . $table->getName()); $generator->put("table", $table); // Create the Base Peer class $this->log("\t\t-> " . $basePrefix . $table->getPhpName() . "Peer"); $path = ClassTools::getFilePath($pkbase, $basePrefix . $table->getPhpName() . "Peer"); $generator->parse("om/{$targetPlatform}/Peer.tpl", $path); // Create the Base object class $this->log("\t\t-> " . $basePrefix . $table->getPhpName()); $path = ClassTools::getFilePath($pkbase, $basePrefix . $table->getPhpName()); $generator->parse("om/{$targetPlatform}/Object.tpl", $path); if ($table->isTree()) { // Create the Base NodePeer class $this->log("\t\t-> " . $basePrefix . $table->getPhpName() . "NodePeer"); $path = ClassTools::getFilePath($pkbase, $basePrefix . $table->getPhpName() . "NodePeer"); $generator->parse("om/{$targetPlatform}/NodePeer.tpl", $path); // Create the Base Node class if the table is a tree $this->log("\t\t-> " . $basePrefix . $table->getPhpName() . "Node"); $path = ClassTools::getFilePath($pkbase, $basePrefix . $table->getPhpName() . "Node"); $generator->parse("om/{$targetPlatform}/Node.tpl", $path); } // Create MapBuilder class if this table is not an alias if (!$table->isAlias()) { $this->log("\t\t-> " . $table->getPhpName() . "MapBuilder"); $path = ClassTools::getFilePath($pkmap, $table->getPhpName() . "MapBuilder"); $generator->parse("om/{$targetPlatform}/MapBuilder.tpl", $path); } // if !$table->isAlias() // Create [empty] stub Peer class if it does not already exist $path = ClassTools::getFilePath($package, $table->getPhpName() . "Peer"); $_f = new PhingFile($basepath, $path); if (!$_f->exists()) { $this->log("\t\t-> " . $table->getPhpName() . "Peer"); $generator->parse("om/{$targetPlatform}/ExtensionPeer.tpl", $path); } else { $this->log("\t\t-> (exists) " . $table->getPhpName() . "Peer"); } // Create [empty] stub object class if it does not already exist $path = ClassTools::getFilePath($package, $table->getPhpName()); $_f = new PhingFile($basepath, $path); if (!$_f->exists()) { $this->log("\t\t-> " . $table->getPhpName()); $generator->parse("om/{$targetPlatform}/ExtensionObject.tpl", $path); } else { $this->log("\t\t-> (exists) " . $table->getPhpName()); } if ($table->isTree()) { // Create [empty] stub Node Peer class if it does not already exist $path = ClassTools::getFilePath($package, $table->getPhpName() . "NodePeer"); $_f = new PhingFile($basepath, $path); if (!$_f->exists()) { $this->log("\t\t-> " . $table->getPhpName() . "NodePeer"); $generator->parse("om/{$targetPlatform}/ExtensionNodePeer.tpl", $path); } else { $this->log("\t\t-> (exists) " . $table->getPhpName() . "NodePeer"); } // Create [empty] stub Node class if it does not already exist $path = ClassTools::getFilePath($package, $table->getPhpName() . "Node"); $_f = new PhingFile($basepath, $path); if (!$_f->exists()) { $this->log("\t\t-> " . $table->getPhpName() . "Node"); $generator->parse("om/{$targetPlatform}/ExtensionNode.tpl", $path); } else { $this->log("\t\t-> (exists) " . $table->getPhpName() . "Node"); } } // Create [empty] interface if it does not already exist if ($table->getInterface()) { $path = ClassTools::getFilePath($table->getInterface()); $_f = new PhingFile($basepath, $path); if (!$_f->exists()) { $_dir = new PhingFile(dirname($_f->getAbsolutePath())); $_dir->mkdirs(); $this->log("\t\t-> " . $table->getInterface()); $generator->parse("om/{$targetPlatform}/Interface.tpl", $path); } else { $this->log("\t\t-> (exists) " . $table->getInterface()); } } // If table has enumerated children (uses inheritance) then create the empty child stub classes // if they don't already exist. if ($table->getChildrenColumn()) { $col = $table->getChildrenColumn(); if ($col->isEnumeratedClasses()) { foreach ($col->getChildren() as $child) { $childpkg = $child->getPackage() ? $child->getPackage() : $package; $generator->put("child", $child); $generator->put("package", $childpkg); $path = ClassTools::getFilePath($childpkg, $child->getClassName()); $_f = new PhingFile($basepath, $path); if (!$_f->exists()) { $this->log("\t\t-> " . $child->getClassName()); $generator->parse("om/{$targetPlatform}/MultiExtendObject.tpl", $path); } else { $this->log("\t\t-> (exists) " . $child->getClassName()); } } // foreach } // if col->is enumerated } // if tbl->getChildrenCol } // if !$table->isForReferenceOnly() } // foreach table } // foreach database } // foreach dataModel }
protected function addClassClose(&$script) { parent::addClassClose($script); $behaviors = $this->getTable()->getAttribute('behaviors'); if ($behaviors) { $behavior_file_name = 'Base' . $this->getTable()->getPhpName() . 'Behaviors'; $behavior_file_path = ClassTools::getFilePath($this->getStubObjectBuilder()->getPackage() . '.om', $behavior_file_name); $script .= sprintf("\ninclude_once '%s';\n", $behavior_file_path); } }
/** * Lists data model classes and builds an associative array className => classPath * To be used for autoloading * @return array */ protected function getClassMap() { $phpconfClassmap = array(); $generatorConfig = $this->getGeneratorConfig(); foreach ($this->getDataModels() as $dataModel) { foreach ($dataModel->getDatabases() as $database) { $classMap = array(); foreach ($database->getTables() as $table) { if (!$table->isForReferenceOnly()) { // Classes that I'm assuming do not need to be mapped (because they will be required by subclasses): // - base peer and object classes // - interfaces // - base node peer and object classes // ----------------------------------------------------- // Add Peer & Object stub classes and MapBuilder classes // ----------------------------------------------------- // (this code is based on PropelOMTask) foreach (array('tablemap', 'peerstub', 'objectstub') as $target) { $builder = $generatorConfig->getConfiguredBuilder($table, $target); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } if ($col = $table->getChildrenColumn()) { if ($col->isEnumeratedClasses()) { foreach ($col->getChildren() as $child) { $builder = $generatorConfig->getConfiguredBuilder($table, 'objectmultiextend'); $builder->setChild($child); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } } } $baseClass = $table->getBaseClass(); if ($baseClass !== null) { $className = ClassTools::classname($baseClass); if (!isset($classMap[$className])) { $classPath = ClassTools::getFilePath($baseClass); $this->log('Adding class mapping: ' . $className . ' => ' . $classPath); $classMap[$className] = $classPath; } } $basePeer = $table->getBasePeer(); if ($basePeer !== null) { $className = ClassTools::classname($basePeer); if (!isset($classMap[$className])) { $classPath = ClassTools::getFilePath($basePeer); $this->log('Adding class mapping: ' . $className . ' => ' . $classPath); $classMap[$className] = $classPath; } } // ------------------------ // Create tree Node classes // ------------------------ if ('MaterializedPath' == $table->treeMode()) { foreach (array('nodepeerstub', 'nodestub') as $target) { $builder = $generatorConfig->getConfiguredBuilder($table, $target); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } } } // if (!$table->isReferenceOnly()) } $phpconfClassmap = array_merge($phpconfClassmap, $classMap); } } return $phpconfClassmap; }
/** * The main method does the work of the task. */ public function main() { // Check to make sure the input and output files were specified and that the input file exists. if (!$this->xmlConfFile || !$this->xmlConfFile->exists()) { throw new BuildException("No valid xmlConfFile specified.", $this->getLocation()); } if (!$this->outputFile) { throw new BuildException("No outputFile specified.", $this->getLocation()); } if (!$this->outputClassmapFile) { // We'll create a default one for BC $this->outputClassmapFile = 'classmap-' . $this->outputFile; } // Create a PHP array from the XML file $xmlDom = new DOMDocument(); $xmlDom->load($this->xmlConfFile->getAbsolutePath()); $xml = simplexml_load_string($xmlDom->saveXML()); $phpconf = self::simpleXmlToArray($xml); $phpconfClassmap = array(); // $this->log(var_export($phpconf,true)); // Create a map of all PHP classes and their filepaths for this data model $generatorConfig = $this->getGeneratorConfig(); foreach ($this->getDataModels() as $dataModel) { foreach ($dataModel->getDatabases() as $database) { $classMap = array(); // $this->log("Processing class mappings in database: " . $database->getName()); //print the tables foreach ($database->getTables() as $table) { if (!$table->isForReferenceOnly()) { // $this->log("\t+ " . $table->getName()); // Classes that I'm assuming do not need to be mapped (because they will be required by subclasses): // - base peer and object classes // - interfaces // - base node peer and object classes // ----------------------------------------------------------------------------------------- // Add Peer & Object stub classes and MapBuilder classes // ----------------------------------------------------------------------------------------- // (this code is based on PropelOMTask) foreach (array('mapbuilder', 'peerstub', 'objectstub') as $target) { $builder = $generatorConfig->getConfiguredBuilder($table, $target); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } if ($table->getChildrenColumn()) { $col = $table->getChildrenColumn(); if ($col->isEnumeratedClasses()) { foreach ($col->getChildren() as $child) { $builder = $generatorConfig->getConfiguredBuilder($table, 'objectmultiextend'); $builder->setChild($child); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } } } $baseClass = $table->getBaseClass(); if ($baseClass !== null) { $className = ClassTools::classname($baseClass); if (!isset($classMap[$className])) { $classPath = ClassTools::getFilePath($baseClass); $this->log('Adding class mapping: ' . $className . ' => ' . $classPath); $classMap[$className] = $classPath; } } $basePeer = $table->getBasePeer(); if ($basePeer !== null) { $className = ClassTools::classname($basePeer); if (!isset($classMap[$className])) { $classPath = ClassTools::getFilePath($basePeer); $this->log('Adding class mapping: ' . $className . ' => ' . $classPath); $classMap[$className] = $classPath; } } // ----------------------------------------------------------------------------------------- // Create tree Node classes // ----------------------------------------------------------------------------------------- if ('MaterializedPath' == $table->treeMode()) { foreach (array('nodepeerstub', 'nodestub') as $target) { $builder = $generatorConfig->getConfiguredBuilder($table, $target); $this->log("Adding class mapping: " . $builder->getClassname() . ' => ' . $builder->getClassFilePath()); $classMap[$builder->getClassname()] = $builder->getClassFilePath(); } } // if Table->treeMode() == 'MaterializedPath' } // if (!$table->isReferenceOnly()) } $phpconfClassmap['propel']['datasources'][$database->getName()]['classes'] = $classMap; } } // $phpconf['propel']['classes'] = $classMap; $phpconf['propel']['generator_version'] = $this->getGeneratorConfig()->getBuildProperty('version'); // Write resulting PHP data to output file: $outfile = new PhingFile($this->outputDirectory, $this->outputFile); $output = '<' . '?' . "php\n"; $output .= "// This file generated by Propel " . $phpconf['propel']['generator_version'] . " convert-props target" . ($this->getGeneratorConfig()->getBuildProperty('addTimestamp') ? " on " . strftime("%c") : '') . "\n"; $output .= "// from XML runtime conf file " . $this->xmlConfFile->getPath() . "\n"; $output .= "return array_merge_recursive("; $output .= var_export($phpconf, true); $output .= ", include(dirname(__FILE__) . DIRECTORY_SEPARATOR . '" . $this->outputClassmapFile . "'));"; $this->log("Creating PHP runtime conf file: " . $outfile->getPath()); if (!file_put_contents($outfile->getAbsolutePath(), $output)) { throw new BuildException("Error creating output file: " . $outfile->getAbsolutePath(), $this->getLocation()); } $outfile = new PhingFile($this->outputDirectory, $this->outputClassmapFile); $output = '<' . '?' . "php\n"; $output .= "// This file generated by Propel " . $phpconf['propel']['generator_version'] . " convert-props target" . ($this->getGeneratorConfig()->getBuildProperty('addTimestamp') ? " on " . strftime("%c") : '') . "\n"; $output .= "return "; $output .= var_export($phpconfClassmap, true); $output .= ";"; $this->log("Creating PHP classmap runtime file: " . $outfile->getPath()); if (!file_put_contents($outfile->getAbsolutePath(), $output)) { throw new BuildException("Error creating output file: " . $outfile->getAbsolutePath(), $this->getLocation()); } }