public function run($segments = [], $quiet = false) { $name = array_shift($segments); // If a table already exists then we don't need a migration $this->load->database(); if ($this->db->table_exists($name)) { $this->table_exists = true; } // If we didn't attach any fields, then we // need to generate the barebones here. $this->fields = CLI::option('fields'); // If we don't have any fields, and no model // exists, then we need to provide some default fields. if (empty($this->fields)) { if (!$this->table_exists) { $this->fields = "id:int id:id title:string created_on:datetime modified_on:datetime"; } } // Perform the steps. if (!$this->table_exists) { $this->makeMigration($name); } $this->makeSeed($name); $this->makeModel($name); $this->makeController($name); $this->readme(); }
public function __construct() { parent::__construct(); $this->load->config('forge'); // Detect if we're genning in a module if ($module = CLI::option('module')) { $this->module = $module; $folders = config_item('modules_locations'); if (is_array($folders)) { $this->module_path = $folders[0] . strtolower($module) . '/'; } } // Should we overwrite files? if (CLI::option('overwrite')) { $this->overwrite = true; } }
/** * Grabs the fields from the CLI options and gets them ready for * use within the views. */ protected function prepareFields() { $fields = CLI::option('fields'); if (empty($fields)) { // If we have a model, we can get our fields from there if (!empty($this->options['model'])) { $fields = $this->getFieldsFromModel($this->options['model']); if (empty($fields)) { return NULL; } } else { return NULL; } } $fields = explode(' ', $fields); $new_fields = []; foreach ($fields as $field) { $pop = [NULL, NULL, NULL]; list($field, $type, $size) = array_merge(explode(':', $field), $pop); $type = strtolower($type); // Ignore list if (in_array($field, ['created_on', 'modified_on'])) { continue; } // Strings if (in_array($type, ['char', 'varchar', 'string'])) { $new_fields[] = ['name' => $field, 'type' => 'text']; } else { if ($type == 'text') { $new_fields[] = ['name' => $field, 'type' => 'textarea']; } else { if (in_array($type, ['tinyint', 'int', 'bigint', 'mediumint', 'float', 'double', 'number'])) { $new_fields[] = ['name' => $field, 'type' => 'number']; } else { if (in_array($type, ['date', 'datetime', 'time'])) { $new_fields[] = ['name' => $field, 'type' => $type]; } } } } } return $new_fields; }
/** * The primary method that calls the correct generator and * makes it run. */ public function run($command) { $quiet = false; $segments = explode(" ", $command); // Get rid of the 'forge' command if ($segments[0] == 'forge') { array_shift($segments); } $command = trim(str_ireplace("forge", '', array_shift($segments))); $dir = $this->locateGenerator($command); $class_name = ucfirst($command) . 'Generator'; if (!file_exists($dir . $class_name . '.php')) { return CLI::error("Generator file not found for: {$class_name}"); } require_once $dir . $class_name . '.php'; if (!class_exists($class_name, false)) { return CLI::error("No class `{$class_name}` found in generator file."); } // Should we run the process quietly? if (CLI::option('q') || CLI::option('quiet')) { $quiet = true; } CLI::write('Invoked ' . CLI::color($class_name, 'yellow')); $class = new $class_name(); $class->run($segments, $quiet); }