/** * First build the model. * * @param $table * @param $baseModel * @param $describes * @param $foreignKeys * @param string $namespace * @param string $prefix */ public function buildModel($table, $baseModel, $describes, $foreignKeys, $namespace = '', $prefix = '') { $this->table = StringUtils::removePrefix($table, $prefix); $this->baseModel = $baseModel; $this->foreignKeys = $this->filterAndSeparateForeignKeys($foreignKeys['all'], $table); $foreignKeysByTable = $foreignKeys['ordered']; if (!empty($namespace)) { $this->namespace = "\n" . 'namespace ' . $namespace . ';'; } $this->class = StringUtils::prettifyTableName($table, $prefix); $this->timestampFields = $this->getTimestampFields($this->baseModel); $describe = $describes[$table]; // main loop foreach ($describe as $field) { if ($this->isPrimaryKey($field)) { $this->primaryKey = $field->Field; $this->incrementing = $this->isIncrementing($field); continue; } if ($this->isTimestampField($field)) { $this->timestamps = true; continue; } if ($this->isDate($field)) { $this->dates[] = $field->Field; } if ($this->isHidden($field)) { $this->hidden[] = $field->Field; continue; } if ($this->isForeignKey($table, $field->Field)) { continue; } $this->fillable[] = $field->Field; } // relations $this->relations = new Relations($table, $this->foreignKeys, $describes, $foreignKeysByTable, $prefix, $namespace); }
/** * Write the actual TableName.php file. * * @param $table * @param Model $model * * @throws Exception * * @return array */ protected function writeFile($table, $model) { $filename = StringUtils::prettifyTableName($table, $this->prefix) . '.php'; if (!is_dir($this->path)) { $oldUMask = umask(0); echo 'creating path: ' . $this->path . LF; mkdir($this->path, 0777, true); umask($oldUMask); if (!is_dir($this->path)) { throw new Exception('dir ' . $this->path . ' could not be created'); } } $result = file_put_contents($this->path . '/' . $filename, $model); return ['filename' => $this->path . '/' . $filename, 'result' => $result]; }