ZendDbSchema
Schema management for Zend Framework
 All Classes Namespaces Functions Pages
Schema.php
1 <?php
30  extends Zend_Tool_Project_Provider_Abstract
31  implements Zend_Tool_Framework_Provider_Pretendable
32 {
46  public function createResource(Zend_Tool_Project_Profile $profile, $schemaName, $contents, $force = false)
47  {
48  if (!is_string($schemaName)) {
49  throw new Zend_Tool_Project_Provider_Exception(
50  'schema name must be a string'
51  );
52  }
53 
54  if ($this->_findfile($profile, $schemaName) !== false && $force == false) {
55  throw new Zend_Tool_Project_Provider_Exception(
56  'File named ' . $schemaName. ' already exists within the models directory.'
57  );
58  }
59 
60  $newFile = $profile->createResourceAt(
61  'schemaDirectory', // where to create at
62  'file', // what to create
63  array(
64  'schemaName' => $schemaName,
65  'fileContents' => $contents) // attrs to initiate with
66  );
67 
68  return $newFile;
69  }
70 
77  public function getContextClasses()
78  {
79  return array(
80  'ZendDbSchema_Tool_Project_Context_Schema_File',
81  'ZendDbSchema_Tool_Project_Context_Schema_Directory'
82  );
83  }
84 
92  protected function _findfile($profile, $name)
93  {
94  // check to see if a model already exists
95  $existingFile = $profile->search(array(
96  'schemaDirectory',
97  'file' => array('filesystemName' => $name))
98  );
99  return $existingFile;
100  }
101 
102 
111  protected function _getSchema($type, $name)
112  {
113  if (empty($name)) {
114  throw new Zend_Tool_Project_Provider_Exception('Schema name must not be empty');
115  }
116 
117  switch ($type) {
118  case 'table':
119  $schema = new ZendDbSchema_Db_Schema_Table($name);
120  break;
121  case 'database':
122  $schema = new ZendDbSchema_Db_Schema_Database($name);
123  break;
124  default:
125  throw new Zend_Tool_Project_Provider_Exception(
126  "Type must be table or database, '{$type}' is given."
127  );
128  }
129  return $schema;
130  }
131 
139  public function pull($type = 'table', $name = '', $force = false)
140  {
141  $profile = $this->_bootstrap();
142 
143  $schema = $this->_getSchema($type, $name);
144  if (!$schema->isExist()) {
145  throw new Zend_Tool_Project_Provider_Exception(
146  "No {$type} with name '{$name}' found"
147  );
148  }
149 
150  $name = $this->_getFilename($type, $name);
151 
152  $writer = new Zend_Config_Writer_Array();
153  $writer->setConfig(new Zend_Config($schema->toArray()));
154 
155  $file = $this->createResource($profile, $name, $writer->render(), $force);
156 
157  if ($this->_registry->getRequest()->isPretend()) {
158  $this->_registry->getResponse()->appendContent(
159  'Would create schema at ' . $file->getPath()
160  );
161  } else {
162  $this->_registry->getResponse()->appendContent(
163  'Creating schema at ' . $file->getPath()
164  );
165  $file->create();
166  $this->_storeProfile();
167  }
168  }
169 
177  public function push($type = 'table', $name = '', $force = false)
178  {
179  $profile = $this->_bootstrap();
180 
181  $schema = $this->_getSchema($type, $name);
182  if ($schema->isExist() && !$force) {
183  throw new Zend_Tool_Project_Provider_Exception(
184  "{$type} with name {$name} already exists in database. User force for overwrite"
185  );
186  }
187 
188  $name = $this->_getFilename($type, $name);
189 
190  if (!$file = $this->_findfile($profile, $name)) {
191  throw new Zend_Tool_Project_Provider_Exception(
192  "Schema file '{$name}' is not found"
193  );
194  }
195 
196  $config = include $file->getPath();
197 
198  $schema->setFromArray($config);
199 
200  if (!$schema->isDirty()) {
201  throw new Zend_Tool_Project_Provider_Exception(
202  "{$type} with name {$name} is not modified"
203  );
204  }
205  if ($this->_registry->getRequest()->isPretend()) {
206  $this->_registry->getResponse()->appendContent(
207  'Would exequte following sql code' .PHP_EOL . $schema->toSql(true)
208  );
209  } else {
210  $this->_registry->getResponse()->appendContent(
211  'Altering database'
212  );
213  $schema->save();
214  $this->_storeProfile();
215  }
216  }
217 
225  public function status($type, $name)
226  {
227  $profile = $this->_bootstrap();
228 
229  $schema = $this->_getSchema($type, $name);
230 
231  $name = $this->_getFilename($type, $name);
232 
233  if (!$file = $this->_findfile($profile, $name)) {
234  throw new Zend_Tool_Project_Provider_Exception(
235  "Schema file '{$name}' is not found"
236  );
237  }
238 
239  $config = include $file->getPath();
240 
241  $schema->setFromArray($config);
242 
243  $status = 'Ok';
244  if ($schema->isDirty()) {
245  $status = 'Dirty';
246  }
247  echo $status;
248  }
249 
257  protected function _getFilename($type, $name)
258  {
259  return $name . '.' . $type . '.php';
260  }
261 
267  protected function _bootstrap()
268  {
269  $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
270 
271  $bootstrapResource = $this->_loadedProfile->search('BootstrapFile');
272 
273  /* @var $zendApp Zend_Application */
274  $zendApp = $bootstrapResource->getApplicationInstance();
275 
276  try {
277  $zendApp->bootstrap('db');
278  Zend_Loader_Autoloader::getInstance()->registerNamespace('ZendDbSchema');
279  } catch (Zend_Application_Exception $e) {
280  throw new Zend_Tool_Project_Provider_Exception('Db resource not available, you might need to configure a DbAdapter.');
281  return;
282  }
283  return $profile;
284  }
285 }