From a6b194b92833a753da7824b13e19183cdf273bbf Mon Sep 17 00:00:00 2001 From: jwage Date: Fri, 25 Jan 2008 03:18:51 +0000 Subject: [PATCH] Work on migrations diff and fixes. --- lib/Doctrine.php | 16 +++++ lib/Doctrine/Lib.php | 32 +++++++++ lib/Doctrine/Migration/Diff.php | 122 +++++++++++++++++++++++++++++++- lib/Doctrine/Task/Dql.php | 4 +- 4 files changed, 171 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine.php b/lib/Doctrine.php index 5a271b4f6..b5378e916 100644 --- a/lib/Doctrine.php +++ b/lib/Doctrine.php @@ -596,6 +596,8 @@ final class Doctrine */ private static $_loadedModelFiles = array(); + private static $_pathModels = array(); + /** * __construct * @@ -606,7 +608,17 @@ final class Doctrine { throw new Doctrine_Exception('Doctrine is static class. No instances can be created.'); } + + public static function getLoadedModelFiles() + { + return self::$_loadedModelFiles; + } + public static function getPathModels() + { + return self::$_pathModels; + } + /** * getPath * returns the doctrine root @@ -646,6 +658,8 @@ final class Doctrine if ($manager->getAttribute(Doctrine::ATTR_MODEL_LOADING) === Doctrine::MODEL_LOADING_CONSERVATIVE) { self::$_loadedModelFiles[$e[0]] = $file->getPathName(); + self::$_pathModels[$file->getPathName()][$e[0]] = $e[0]; + $loadedModels[] = $e[0]; } else { $declaredBefore = get_declared_classes(); @@ -658,6 +672,8 @@ final class Doctrine foreach ($foundClasses as $className) { if (self::isValidModelClass($className) && !in_array($className, $loadedModels)) { $loadedModels[] = $className; + + self::$_pathModels[$file->getPathName()][$className] = $className; } } } diff --git a/lib/Doctrine/Lib.php b/lib/Doctrine/Lib.php index 0ef78a29c..8243743b4 100644 --- a/lib/Doctrine/Lib.php +++ b/lib/Doctrine/Lib.php @@ -396,6 +396,38 @@ class Doctrine_Lib } } + public static function copyDirectory($source, $dest) + { + // Simple copy for a file + if (is_file($source)) { + return copy($source, $dest); + } + + // Make destination directory + if ( ! is_dir($dest)) { + mkdir($dest); + } + + // Loop through the folder + $dir = dir($source); + while (false !== $entry = $dir->read()) { + // Skip pointers + if ($entry == '.' || $entry == '..') { + continue; + } + + // Deep copy directories + if ($dest !== "$source/$entry") { + self::copyDirectory("$source/$entry", "$dest/$entry"); + } + } + + // Clean up + $dir->close(); + + return true; + } + /** * isValidClassName * diff --git a/lib/Doctrine/Migration/Diff.php b/lib/Doctrine/Migration/Diff.php index 160e4ec2c..5bbc2f38d 100644 --- a/lib/Doctrine/Migration/Diff.php +++ b/lib/Doctrine/Migration/Diff.php @@ -31,5 +31,125 @@ * @author Jonathan H. Wage */ class Doctrine_Migration_Diff -{ +{ + protected $_from, + $_to, + $_changes = array(), + $_migrationsPath; + + public function __construct($from = null, $to = null) + { + $this->_from = $from; + $this->_to = $to; + } + + protected function getUniqueId() + { + return md5($this->_from.$this->_to); + } + + public function setMigrationsPath($migrationsPath) + { + $this->_migrationsPath = $migrationsPath; + } + + public function generate() + { + $from = $this->_generateModels('From', $this->_from); + $to = $this->_generateModels('To', $this->_to); + + $differences = $this->_diff($from, $to); + + print_r($differences); + } + + protected function _diff($from, $to) + { + $fromTmpPath = sys_get_temp_dir() . $this->getUniqueId() . '_from'; + $toTmpPath = sys_get_temp_dir() . $this->getUniqueId() . '_to'; + + if ( ! file_exists($fromTmpPath)) { + $fromModels = Doctrine::loadModels($from); + $fromInfo = $this->_buildModelInformation($fromModels); + + file_put_contents($fromTmpPath, serialize($fromInfo)); + } else { + if ( ! file_exists($toTmpPath)) { + $toModels = Doctrine::loadModels($to); + $toInfo = $this->_buildModelInformation($toModels); + + file_put_contents($toTmpPath, serialize($toInfo)); + } else { + $fromInfo = unserialize(file_get_contents($fromTmpPath)); + $toInfo = unserialize(file_get_contents($toTmpPath)); + + $this->_buildChanges($fromInfo, $toInfo); + + // clean up + unlink($fromTmpPath); + unlink($toTmpPath); + Doctrine_Lib::removeDirectories(sys_get_temp_dir() . 'from_doctrine_tmp_dirs'); + Doctrine_Lib::removeDirectories(sys_get_temp_dir() . 'to_doctrine_tmp_dirs'); + } + } + } + + protected function _buildChanges($from, $to) + { + foreach ($to as $key => $model) { + $columns = $model['columns']; + + foreach ($columns as $columnKey => $column) { + //if (isset($to[$key]['columns'][$columnKey])) + } + } + } + + protected function _buildModelInformation(array $models) + { + $info = array(); + + foreach ($models as $key => $model) { + $info[$model] = Doctrine::getTable($model)->getExportableFormat(); + } + + return $info; + } + + protected function _generateModels($prefix, $item) + { + $path = sys_get_temp_dir() . $prefix . '_doctrine_tmp_dirs'; + + if ( is_dir($item)) { + $files = glob($item . DIRECTORY_SEPARATOR . '*.*'); + + if (isset($files[0])) { + $pathInfo = pathinfo($files[0]); + $extension = $pathInfo['extension']; + } + + if ($extension === 'yml') { + Doctrine::generateModelsFromYaml($item, $path); + + return $path; + } else if ($extension === 'php') { + + Doctrine_Lib::copyDirectory($item, $path); + + return $path; + } else { + throw new Doctrine_Migration_Exception('No php or yml files found at path: "' . $item . '"'); + } + } else { + try { + $connection = Doctrine_Manager::getInstance()->getConnection($item); + + Doctrine::generateModelsFromDb($path, array($item)); + + return $path; + } catch (Exception $e) { + throw new Doctrine_Migration_Exception('Could not generate models from connection: ' . $e->getMessage()); + } + } + } } \ No newline at end of file diff --git a/lib/Doctrine/Task/Dql.php b/lib/Doctrine/Task/Dql.php index 72584be10..b1baedfe7 100644 --- a/lib/Doctrine/Task/Dql.php +++ b/lib/Doctrine/Task/Dql.php @@ -51,10 +51,10 @@ class Doctrine_Task_Dql extends Doctrine_Task $results = $query->query($dql, $params); - $this->printResults($results); + $this->_printResults($results); } - protected function printResults($data) + protected function _printResults($data) { $array = $data->toArray(true);