Work on migrations diff and fixes.
This commit is contained in:
parent
ddefc7f96d
commit
a6b194b928
@ -596,6 +596,8 @@ final class Doctrine
|
||||
*/
|
||||
private static $_loadedModelFiles = array();
|
||||
|
||||
private static $_pathModels = array();
|
||||
|
||||
/**
|
||||
* __construct
|
||||
*
|
||||
@ -607,6 +609,16 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -32,4 +32,124 @@
|
||||
*/
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user