/** * Parse the field definition line and build the necessary data structures * @param string $line The field line to be parsed * @param string $model The name of the model to which the field belongs * @param string $type The type of the definition: model/table * @return void */ public function parseFieldDefinitionLine($line, $model, $type) { // Is this a model definition, or a table definition? // We won't add anything to the model list in case of a table definition $isModelDefinition = $type == 'model'; // Parse the field definition $parsed = $this->fieldParser->parse(trim($line)); // Check for errors if (!$parsed) { throw new ParseError("Could not parse field line. Check for errors like misplaced quotes."); } // Check if the field is timestamps if ($parsed['type'] == 'timestamps') { $isModelDefinition && $this->modelList->setTimestamps($model); $this->migrationList->setTimestamps($model); return; } // Check if the field is softDeletes if ($parsed['type'] == 'softDeletes') { $isModelDefinition && $this->modelList->setSoftDeletes($model); $this->migrationList->setSoftDeletes($model); return; } // Check if field type is increments if ($parsed['type'] == 'increments') { $isModelDefinition && $this->modelList->setPrimaryKey($model, $parsed['name']); $this->migrationList->setPrimaryKey($model, $parsed['name']); return; } // For other fields, add them to the current migration $this->migrationList->addColumn($model, $parsed); }
/** * For a given table, generate the migration object with the appropriate * parameters * @param string $tableName The name of the table * @param array $columns An array of SchemaExtractor Column objects */ private function createTableMigration($tableName, $columns) { // Get the parsed columns $parsedColumns = $this->schemaExtractor->extract($columns, $this->dbType); // Get the model name form the table name $modelName = ucwords(Pluralizer::singular($tableName)); // Create a new migration $this->migrationList->create($modelName, $tableName); // Now, proceed towards adding columns foreach ($parsedColumns as $column) { $type = $this->getLaravelColumnType($column); // For primary keys, we simply set pK if ($type == 'increments') { $this->migrationList->setPrimaryKey($modelName, $column->name); continue; } $c = array('name' => $column->name, 'type' => $type, 'parameters' => $this->getLaravelColumnParameters($column->parameters, $type), 'default' => is_null($column->defaultValue) ? '' : $column->defaultValue, 'unsigned' => $column->unsigned, 'nullable' => $column->null, 'primary' => $column->index == 'primary', 'unique' => $column->index == 'unique', 'index' => $column->index == 'multicolumn'); $this->migrationList->addColumn($modelName, $c); } }