This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.php
159 lines (146 loc) · 4.04 KB
/
store.php
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
<?php
/**
* LICENSE: ##LICENSE##
*
* @category Anahita_Dev
* @package Com_Migrator
* @subpackage Controller
* @author Arash Sanieyan <ash@anahitapolis.com>
* @author Rastin Mehr <rastin@anahitapolis.com>
* @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
* @version SVN: $Id: view.php 11985 2012-01-12 10:53:20Z asanieyan $
* @link http://www.anahitapolis.com
*/
/**
* Abstract Controller
*
* @category Anahita_Dev
* @package Com_Migrator
* @subpackage Store
* @author Arash Sanieyan <ash@anahitapolis.com>
* @author Rastin Mehr <rastin@anahitapolis.com>
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
* @link http://www.anahitapolis.com
*/
class ComMigratorStore extends KObject
{
/**
* Return the instance of the migrator store
*
* @return ComMigratorStore
*/
static public function getInstance()
{
static $instance;
if ( !$instance )
{
$instance = new ComMigratorStore();
}
return $instance;
}
/**
* Entity
*
* @var string
*/
protected $_entity;
/**
* Versions
*
* @var KConfig
*/
protected $_versions;
/**
* Constructor.
*
* @param KConfig $config An optional KConfig object with configuration options.
*
* @return void
*/
public function __construct(KConfig $config = null)
{
$this->_versions = $this->_loadVersions();
}
/**
* Return the version of a name
*
* @param string $name The name of the component
*
* @return int
*/
public function getVersion($name)
{
return pick($this->_versions->$name, 0);
}
/**
* Deletes a migration
*
* @return void
*/
public function delete($name)
{
if ( isset($this->_versions[$name]) )
{
unset($this->_versions[$name]);
$this->save();
}
}
/**
* Return the version of a name
*
* @param string $name The name of the component
* @param int $version Store a version for a component
*
* @return int
*/
public function setVersion($name, $version)
{
$this->_versions[$name] = $version;
return $this;
}
/**
* Store the version
*
* @return void
*/
protected function _loadVersions()
{
$db = KService::get('koowa:database.adapter.mysqli');
//convert the migration
try {
$db->execute('RENAME TABLE #__migrator_migraitons TO #__migrator_migrations');
} catch(Exception $e) {}
$sql = <<<EOF
CREATE TABLE IF NOT EXISTS `#__migrator_migrations` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`migrations` TEXT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;
EOF;
$db->execute($sql);
$db = KService::get('koowa:database.adapter.mysqli');
$entity = $db->select("SELECT * FROM #__migrator_migrations", KDatabase::FETCH_OBJECT);
if ( !$entity ) {
$db->insert('migrator_migrations', array('migrations'=>''));
$entity = $db->select("SELECT * FROM #__migrator_migrations", KDatabase::FETCH_OBJECT);
}
$this->_entity = $entity;
$reg = new JRegistry();
$reg->loadINI($this->_entity->migrations);
return new KConfig($reg->toArray());
}
/**
* save all the version
*
* @return void
*/
public function save()
{
$db = KService::get('koowa:database.adapter.mysqli');
$reg = new JRegistry();
$reg->loadArray((array)KConfig::unbox($this->_versions));
$this->_entity->migrations = $reg->toString();
$db->update('migrator_migrations', (array)$this->_entity,' WHERE id = '.$this->_entity->id);
}
}