ZendDbSchema
Schema management for Zend Framework
 All Classes Namespaces Functions Pages
Skeleton.php
1 <?php
30 {
34  protected $_adapter;
35 
39  protected $_version;
40 
44  protected $_class;
45 
51  public function __construct(Zend_CodeGenerator_Php_Class $class = null)
52  {
53  if (!$class) {
54  $class = new Zend_CodeGenerator_Php_Class();
55  }
56  $class->setExtendedClass('ZendDbSchema_Db_Migration_Change');
57 
58  $this->_class = $class;
59  }
60 
66  public function setMethodUp($body = '// rollup')
67  {
68  $up = new Zend_CodeGenerator_Php_Method();
69  $up->setName('up')->setBody($body);
70 
71  $this->_class->setMethod($up);
72  }
73 
79  public function setMethodDown($body = '// rolldown')
80  {
81  $down = new Zend_CodeGenerator_Php_Method();
82  $down->setName('down')->setBody($body);
83 
84  $this->_class->setMethod($down);
85  }
86 
93  public function addSchema(array $tablesList = array(), array $databasesList = array())
94  {
95  if (empty($databasesList)) {
96  $config = $this->getAdapter()->getConfig();
97  $databasesList[] = $config['dbname'];
98  }
99  $databasesConfig = array();
100  foreach ($databasesList as $database) {
101  $schema = new ZendDbSchema_Db_Schema_Database($database);
102  $databasesConfig[$database] = $schema->toArray();
103  }
104 
105  $databases = new Zend_CodeGenerator_Php_Property();
106  $databases->setName('_databasesConfig')
107  ->setVisibility(Zend_CodeGenerator_Php_Property::VISIBILITY_PROTECTED)
108  ->setDefaultValue($databasesConfig)
109  ->setDocBlock("Databases configuration \n@var array");
110 
111  $this->_class->setProperty($databases);
112 
113  if (!empty($tablesList)) {
114  $tablesList = array_intersect(
115  $this->getAdapter()->listTables(),
116  $tablesList
117  );
118  } else {
119  $tablesList = $this->getAdapter()->listTables();
120  }
121 
122  $tablesConfig = array();
123  foreach ($tablesList as $table) {
124  $schema = new ZendDbSchema_Db_Schema_Table($table);
125  $tablesConfig[$table] = $schema->toArray();
126  }
127  $tables = new Zend_CodeGenerator_Php_Property();
128  $tables->setName('_tablesConfig')
129  ->setVisibility(Zend_CodeGenerator_Php_Property::VISIBILITY_PROTECTED)
130  ->setDefaultValue($tablesConfig)
131  ->setDocBlock("Tables configuration \n@var array");
132 
133  $this->_class->setProperty($tables);
134  }
135 
142  public function setVersion($version)
143  {
144  $this->_version = rtrim($version, '.php');
145 
146  $this->getClass()->setName(self::getClassName($version));
147 
148  return $this;
149  }
150 
156  public function getVersion()
157  {
158  if (!$this->_version) {
159  $this->setVersion(self::generateVersion());
160  }
161  return $this->_version;
162  }
163 
169  public function getFile()
170  {
171  $file = new Zend_CodeGenerator_Php_File();
172  $file->setClass($this->getClass());
173  $file->setFilename(self::getFilename($this->getVersion()));
174 
175  return $file;
176  }
177 
183  public function write()
184  {
185  $this->getFile()->write();
186 
187  return $this;
188  }
189 
195  public function getClass()
196  {
197  return $this->_class;
198  }
199 
206  public function getAdapter()
207  {
208  if (!$this->_adapter) {
210  $this->setAdapter($adapter);
211  }
212  return $this->_adapter;
213  }
214 
222  {
223  $this->_adapter = $adapter;
224  return $this;
225  }
226 
233  public static function getClassName($version)
234  {
235  return 'Migration_' . basename($version, '.php');
236  }
237 
243  public static function generateVersion()
244  {
245  list($sec, $msec) = explode(".", microtime(true));
246  return date('Ymd_His_') . substr($msec, 0, 2);
247  }
248 
254  public static function getFilename($version)
255  {
256  return $version . '.php';
257  }
258 }