Beispiel #1
0
 function import($sFile)
 {
     if (!$this->bSpycReady) {
         return self::SPYC_CLASS_NOT_FOUND;
     }
     if (!file_exists($sFile)) {
         return self::YAML_FILE_NOT_FOUND;
     }
     $this->aTables = SPYC::YAMLload(file_get_contents($sFile));
     if (!is_array($this->aTables)) {
         return self::YAML_FILE_IS_INVALID;
     }
     uses('model' . DS . 'model');
     $oDB = $this->oDb;
     $aAllowedTables = $oDB->listSources();
     foreach ($this->aTables as $table => $records) {
         if (!in_array($oDB->config['prefix'] . $table, $aAllowedTables)) {
             return self::TABLE_NOT_FOUND;
         }
         $temp_model = new Model(false, $table);
         foreach ($records as $record_num => $record_value) {
             if (!isset($record_value['id'])) {
                 $record_value['id'] = $record_num;
             }
             if (!$temp_model->save($record_value)) {
                 return array('error' => array('table' => $table, 'record' => $record_value));
             }
         }
     }
     return true;
 }
Beispiel #2
0
 /**
  * Loads the YAML file with the YAML schema and parses it into self::aTasks
  *
  * @param string $sFile Path to the YAML file
  * @return mixed True on success and an Error code ( see class constants ) on failure
  */
 function load($sFile)
 {
     if (!$this->bSpycReady) {
         return self::SPYC_CLASS_NOT_FOUND;
     }
     if (!file_exists($sFile)) {
         return self::YAML_FILE_NOT_FOUND;
     }
     $this->aSchema = SPYC::YAMLload(file_get_contents($sFile));
     $this->aTasks = array();
     $this->aTasks['UP'] = $this->aTasks['DOWN'] = array();
     if (!is_array($this->aSchema) || !isset($this->aSchema['UP']) || !isset($this->aSchema['DOWN'])) {
         return self::YAML_FILE_IS_INVALID;
     }
     foreach ($this->aSchema as $sDirection => $sAction) {
         foreach ($this->aSchema[$sDirection] as $sAction => $aElement) {
             foreach ($aElement as $sName => $aProperties) {
                 if ($sAction == 'create_table' || $sAction == 'create_tables') {
                     $this->aTasks[$sDirection][] = $this->create_table($sName, $aProperties);
                 } elseif ($sAction == 'drop_table' || $sAction == 'drop_tables') {
                     $this->aTasks[$sDirection][] = $this->drop_table($aProperties);
                 } elseif ($sAction == 'rename_table' || $sAction == 'rename_tables') {
                     $this->aTasks[$sDirection][] = $this->rename_table($sName, $aProperties['name']);
                 } elseif ($sAction == 'merge_table' || $sAction == 'merge_tables') {
                     $this->aTasks[$sDirection] = am($this->aTasks[$sDirection], $this->merge_table($sName, $aProperties));
                 } elseif ($sAction == 'truncate_table' || $sAction == 'truncate_tables') {
                     $this->aTasks[$sDirection][] = $this->truncate_table($sName);
                 } elseif ($sAction == 'add_fields' || $sAction == 'add_field' || $sAction == 'add_columns' || $sAction == 'add_column') {
                     foreach ($aProperties as $sN => $aV) {
                         $this->aTasks[$sDirection][] = $this->add_field($sName, array($sN => $aV));
                     }
                 } elseif ($sAction == 'alter_field' || $sAction == 'alter_fields' || $sAction == 'alter_column' || $sAction == 'alter_columns') {
                     foreach ($aProperties as $sN => $aV) {
                         $this->aTasks[$sDirection][] = $this->alter_field($sName, array($sN => $aV));
                     }
                 } elseif ($sAction == 'drop_field' || $sAction == 'drop_fields' || $sAction == 'drop_column' || $sAction == 'drop_columns') {
                     foreach ($aProperties as $sField) {
                         $this->aTasks[$sDirection][] = $this->drop_field($sName, $sField);
                     }
                 } elseif ($sAction == 'query' || $sAction == 'queries') {
                     $this->aTasks[$sDirection][] = $aElement;
                 }
             }
         }
     }
     $this->bLoaded = true;
     return true;
 }
Beispiel #3
0
 /**
  * Load from file.
  *
  * PHP, YAML and JSON formats supported
  *
  * @param string $filename
  * @return bool
  */
 public static function load($filename, $merge = true)
 {
     if (file_exists(CONFIGS . $filename)) {
         $filename = CONFIGS . $filename;
     } elseif (!file_exists($filename)) {
         return false;
     }
     if (preg_match('/\\.php$/', $filename)) {
         include $filename;
     } elseif (preg_match('/\\.yml$/', $filename)) {
         if (App::import('Vendor', 'Yaml.Spyc')) {
             $config = SPYC::YAMLload(file_get_contents($filename));
         }
     } elseif (preg_match('/\\.js(on)?$/', $filename)) {
         $config = json_decode(file_get_contents($filename), true);
     } else {
         return;
         // null
     }
     if (isset($config) && is_array($config)) {
         if ($merge) {
             self::$_data = self::_merge(self::$_data, $config);
             self::_refresh();
         }
         return $config;
     }
     return true;
 }