コード例 #1
0
ファイル: Db.php プロジェクト: netconstructor/Centurion
 /**
  * Inspect db for Zend_Db adapter.
  * Connect to a database and generate models.
  *
  * @param string $tablePrefix 
  * @param string $environment 
  * @return void
  */
 public function inspect($tablePrefix = self::DEFAULT_TABLE_PREFIX, $environment = self::DEFAULT_ENVIRONMENT)
 {
     $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
     $buildDirectory = $this->_loadedProfile->search('buildDirectory');
     $tables = $this->_getDbImport()->getAdapter()->listTables();
     $dependentTables = array();
     $classes = array();
     foreach ($tables as $key => $table) {
         $metadata = $this->_getDbImport()->getAdapter()->describeTable($table);
         $primary = array();
         $className = Centurion_Inflector::modelize($table);
         $classNamePrefix = sprintf('%s%s', $this->_formatPrefix($tablePrefix), $className);
         $relations[$table] = array();
         foreach ($metadata as $columnName => $columnValue) {
             if ($columnValue[self::PRIMARY] === true) {
                 array_push($primary, $columnName);
             }
         }
         $localRelations = $this->_getDbImport()->listTableRelations($table);
         foreach ($localRelations as $localRelationKey => $localRelation) {
             $referenceClassName = Centurion_Inflector::modelize($localRelation['table']);
             if (!array_key_exists($referenceClassName, $dependentTables)) {
                 $dependentTables[$referenceClassName] = array();
             }
             $relations[$table][strtolower($referenceClassName)] = array('columns' => $localRelation['local'], 'refColumns' => $localRelation['foreign'], 'refTableClass' => sprintf('%s%s', $this->_formatPrefix($tablePrefix), $referenceClassName));
             $key = strtolower(Centurion_Inflector::pluralize($className));
             $dependentTables[$referenceClassName][$key] = $classNamePrefix;
         }
         $existingModelFile = $this->_loadedProfile->search(array('dataDirectory', 'buildDirectory', 'buildFile' => array('fileName' => $className)));
         if (false !== $existingModelFile) {
             $existingModelFile->delete();
         }
         $buildFile = $buildDirectory->createResource('BuildFile', array('name' => $table, 'primary' => $primary, 'cols' => array_keys($metadata), 'metadata' => $metadata, 'className' => $classNamePrefix, 'fileName' => $className, 'referenceMap' => $relations[$table]));
         $classes[$className] = $buildFile;
     }
     foreach ($dependentTables as $key => $dependentTable) {
         if (!array_key_exists($key, $classes)) {
             continue;
         }
         $classes[$key]->getModelClass()->getProperty('_dependentTables')->setDefaultValue($dependentTable);
     }
     if ($this->_registry->getRequest()->isPretend()) {
         foreach ($classes as $key => $class) {
             $this->_registry->getResponse()->appendContent(sprintf('Would create model at %s: %s', $buildDirectory->getPath(), $key));
             $this->_registry->getResponse()->appendContent($class->getContents());
         }
     } else {
         if (!file_exists($buildDirectory->getPath())) {
             throw new Centurion_Exception('Build directory does not exist.');
         }
         if (!is_writable($buildDirectory->getPath())) {
             throw new Centurion_Exception('Build directory is not writable.');
         }
         foreach ($classes as $key => $class) {
             $this->_registry->getResponse()->appendContent(sprintf('Creating model at %s: %s', $buildDirectory->getPath(), $key));
             $class->create();
         }
         $this->_storeProfile();
     }
 }