Mojopollo\Schema\MakeMigrationJson::parseSchema PHP Метод

parseSchema() публичный Метод

Parses the schema from the json array into a readable format laravel generators extended understand
public parseSchema ( array $data, array $only = [] ) : array
$data array The array containing the json output from file
$only array (optional) array containing specific table/migration names to retrieve from the json file
Результат array The finished parsed schema for use with generators extended
    public function parseSchema(array $data, array $only = [])
    {
        // Final schema
        $schema = [];
        // For every table
        foreach ($data as $tableName => $fields) {
            // If the --only option was used
            if (!empty($only)) {
                // If this tableName is not existing in the only array
                if (!in_array($tableName, $only)) {
                    // Go to next table
                    continue 1;
                }
            }
            // Set migration name / class name
            $migrationName = $this->setMigrationName($tableName);
            // Check if this is a pivot table definition
            if (substr($migrationName, -6) === '_pivot') {
                // Get table names
                $tables = explode('_', $migrationName, 3);
                // Add to the schema array
                $schema[$migrationName] = "{$tables[0]} {$tables[1]}";
                // Go to next table
                continue 1;
            }
            // For every field
            foreach ($fields as $fieldName => $fieldSchema) {
                // Add to the schema array
                $schema[$migrationName][] = "{$fieldName}:{$fieldSchema}";
            }
            // Join all fields for this migration in a single line
            $schema[$migrationName] = implode(', ', $schema[$migrationName]);
        }
        // Return final schema
        return $schema;
    }

Usage Example

 /**
  * Make the json migration
  *
  * @return void
  */
 protected function makeJsonMigration()
 {
     // Set start time of file generation
     $this->startTime = time();
     // Get json array from file
     $jsonArray = $this->makeMigrationJson->jsonFileToArray($this->filePath);
     // Parse only option
     $only = [];
     if (!empty($this->option('only'))) {
         $only = explode(',', $this->option('only'));
         $only = array_map('trim', $only);
     }
     // Parse json and get schema
     $schema = $this->makeMigrationJson->parseSchema($jsonArray, $only);
     // For every migration in the schema
     foreach ($schema as $migrationName => $fieldSchema) {
         // Check if this migration is a pivot table
         if (substr($migrationName, -6) === '_pivot') {
             // Get tables
             $tables = explode(' ', $fieldSchema, 3);
             // Invoke the extended generator command for pivot tables
             $this->call('make:migration:pivot', ['tableOne' => $tables[0], 'tableTwo' => $tables[1]]);
             // Go to next migration
             continue 1;
         }
         // Invoke the extended generator command
         $this->call('make:migration:schema', ['name' => $migrationName, '--schema' => $fieldSchema]);
         // wait 1 second in-between schemas to run the insequence later with "php artisan migrate"
         sleep(1);
     }
     // $this->info(var_export($schema, true));
 }
All Usage Examples Of Mojopollo\Schema\MakeMigrationJson::parseSchema