/** * printTasks * * Prints an index of all the available tasks in the CLI instance * * @return void */ public function printTasks($task = null, $full = false) { $task = Doctrine::classify(str_replace('-', '_', $task)); $tasks = $this->getLoadedTasks(); echo $this->_formatter->format("Doctrine Command Line Interface", 'HEADER') . "\n\n"; foreach ($tasks as $taskName) { if ($task != null && strtolower($task) != strtolower($taskName)) { continue; } $className = 'Doctrine_Task_' . $taskName; $taskInstance = new $className(); $taskInstance->taskName = str_replace('_', '-', Doctrine::tableize($taskName)); $syntax = $this->_scriptName . ' ' . $taskInstance->getTaskName(); echo $this->_formatter->format($syntax, 'INFO'); if ($full) { echo " - " . $taskInstance->getDescription() . "\n"; $args = null; $requiredArguments = $taskInstance->getRequiredArgumentsDescriptions(); if (!empty($requiredArguments)) { foreach ($requiredArguments as $name => $description) { $args .= $this->_formatter->format($name, "ERROR"); if (isset($this->_config[$name])) { $args .= " - " . $this->_formatter->format($this->_config[$name], 'COMMENT'); } else { $args .= " - " . $description; } $args .= "\n"; } } $optionalArguments = $taskInstance->getOptionalArgumentsDescriptions(); if (!empty($optionalArguments)) { foreach ($optionalArguments as $name => $description) { $args .= $name . ' - ' . $description . "\n"; } } if ($args) { echo "\n" . $this->_formatter->format('Arguments:', 'HEADER') . "\n" . $args; } } echo "\n"; } }
/** * doMigrate * * Perform migration for a migration class. Executes the up or down method then processes the changes * * @param string $direction * @return void */ protected function doMigrate($direction) { if (!method_exists($this, $direction)) { return; } $this->{$direction}(); foreach ($this->_changes as $type => $changes) { $process = new Doctrine_Migration_Process(); $funcName = 'process' . Doctrine::classify($type); if (!empty($changes)) { $process->{$funcName}($changes); } } }
/** * setOption * * @param string $key * @param string $value * @return void */ public function setOption($key, $value) { $name = 'set' . Doctrine::classify($key); if (method_exists($this, $name)) { $this->{$name}($value); } else { $key = '_' . $key; $this->{$key} = $value; } }
public function buildRecord($table, $tableColumns, $className = '', $fileName = '') { if (empty($fileName)) { if (empty($this->path)) { $errMsg = 'No build target directory set.'; throw new Doctrine_Import_Builder_Exception($errMsg); } if (is_writable($this->path) === false) { $errMsg = 'Build target directory ' . $this->path . ' is not writable.'; throw new Doctrine_Import_Builder_Exception($errMsg); } $fileName = $this->path . DIRECTORY_SEPARATOR . $className . $this->suffix; } $created = date('l dS \\of F Y h:i:s A'); if (empty($className)) { $className = Doctrine::classify($table); } $content = sprintf(self::$tpl, $created, $className, $this->buildDefinition($table, $tableColumns)); $bytes = file_put_contents($fileName, $content); if ($bytes === false) { throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $fileName); } }
/** * _processRow * * @param string $rowKey * @param string $row * @return void */ protected function _processRow($rowKey, $row) { $obj = $this->_importedObjects[$rowKey]; foreach ($row as $key => $value) { if ($obj->getTable()->hasField($key)) { $obj->set($key, $value); } else { if (method_exists($obj, 'set' . Doctrine::classify($key))) { $func = 'set' . Doctrine::classify($key); $obj->{$func}($value); } else { if ($obj->getTable()->hasRelation($key)) { if (is_array($value)) { if (isset($value[0])) { foreach ($value as $link) { if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::ONE) { $obj->set($key, $this->_getImportedObject($link, $obj, $key)); } else { if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) { $relation = $obj->{$key}; $relation[] = $this->_getImportedObject($link, $obj, $key); } } } } else { $obj->{$key}->fromArray($value); } } else { $obj->set($key, $this->_getImportedObject($value, $obj, $key)); } } else { throw new Doctrine_Data_Exception('Invalid fixture element "' . $key . '" under "' . $rowKey . '"'); } } } } }
protected function _processRow($rowKey, $row) { $obj = $this->_importedObjects[$rowKey]; foreach ($row as $key => $value) { if ($obj->getTable()->hasColumn($key)) { $obj->set($key, $value); } else { if (method_exists($obj, 'set' . Doctrine::classify($key))) { $func = 'set' . Doctrine::classify($key); $obj->{$func}($value); } else { if ($obj->getTable()->hasRelation($key)) { if (is_array($value)) { if (isset($value[0])) { foreach ($value as $link) { if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::ONE) { $obj->set($key, $this->_getImportedObject($link)); } else { if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) { $relation = $obj->{$key}; $relation[] = $this->_getImportedObject($link); } } } } else { $obj->{$key}->fromArray($value); } } else { $obj->set($key, $this->_getImportedObject($value)); } } } } } }
/** * generateMigrationsFromModels * * @param string $modelsPath * @return void */ public function generateMigrationsFromModels($modelsPath = null, $modelLoading = null) { if ($modelsPath !== null) { $models = Doctrine::filterInvalidModels(Doctrine::loadModels($modelsPath, $modelLoading)); } else { $models = Doctrine::getLoadedModels(); } $foreignKeys = array(); foreach ($models as $model) { $export = Doctrine::getTable($model)->getExportableFormat(); $foreignKeys[$export['tableName']] = $export['options']['foreignKeys']; $up = $this->buildCreateTable($export); $down = $this->buildDropTable($export); $className = 'Add' . Doctrine::classify($export['tableName']); $this->generateMigrationClass($className, array(), $up, $down); } if (!empty($foreignKeys)) { $className = 'ApplyForeignKeyConstraints'; $up = ''; $down = ''; foreach ($foreignKeys as $tableName => $definitions) { $tableForeignKeyNames[$tableName] = array(); foreach ($definitions as $definition) { $definition['name'] = $tableName . '_' . $definition['foreignTable'] . '_' . $definition['local'] . '_' . $definition['foreign']; $up .= $this->buildCreateForeignKey($tableName, $definition); $down .= $this->buildDropForeignKey($tableName, $definition); } } $this->generateMigrationClass($className, array(), $up, $down); } return true; }
public function buildAccessors(array $options, array $columns) { $ret = ''; foreach ($columns as $name => $column) { // getters $ret .= "\n public function get" . Doctrine::classify($name) . "(\$load = true)\n"; $ret .= " {\n"; $ret .= " return \$this->get('{$name}', \$load);\n"; $ret .= " }\n"; // setters $ret .= "\n public function set" . Doctrine::classify($name) . "(\${$name}, \$load = true)\n"; $ret .= " {\n"; $ret .= " return \$this->set('{$name}', \${$name}, \$load);\n"; $ret .= " }\n"; } return $ret; }
/** * importSchema * * method for importing existing schema to Doctrine_Record classes * * @param string $directory * @param array $databases * @return array the names of the imported classes */ public function importSchema($directory, array $databases = array(), array $options = array()) { $connections = Doctrine_Manager::getInstance()->getConnections(); foreach ($connections as $name => $connection) { // Limit the databases to the ones specified by $databases. // Check only happens if array is not empty if (!empty($databases) && !in_array($name, $databases)) { continue; } $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); $builder->setOptions($options); $classes = array(); foreach ($connection->import->listTables() as $table) { $builder->buildRecord(array('tableName' => $table, 'className' => Doctrine::classify($table)), $connection->import->listTableColumns($table), array()); $classes[] = Doctrine::classify($table); } } return $classes; }
/** * import * * method for importing existing schema to Doctrine_Record classes * * @param string $directory * @param array $databases * @return array the names of the imported classes */ public function import($directory, array $databases = array()) { $builder = new Doctrine_Import_Builder(); $builder->setTargetPath($directory); $classes = array(); foreach ($this->listTables() as $table) { $builder->buildRecord($table, $this->listTableColumns($table)); $classes[] = Doctrine::classify($table); } return $classes; }