Last active
May 6, 2018 18:16
-
-
Save yinsee/798bcb1550e9f9562ddfc39c455fd93a to your computer and use it in GitHub Desktop.
Swagger (JSON) to Laravel Migrations and Models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$prefix = 'OMS'; | |
$namespace = 'App\\Models\\' . $prefix; | |
$swagger = json_decode(file_get_contents($argv[1])); | |
mkdir("gen"); | |
mkdir("gen/models"); | |
mkdir("gen/migrations"); | |
foreach ($swagger->definitions as $class => $def) { | |
$tablename = strtolower($prefix . "_" . preg_replace('/([^A-Z])([A-Z])/', "$1_$2", $class)); | |
$is_momentary = false; | |
$timestamps = false; | |
foreach ($def->properties as $key => $value) { | |
if (isset($value->{'$ref'}) || (isset($value->type) && 'array' == $value->type)) { | |
// this is momentary object | |
$is_momentary = true; | |
} | |
if ('createdAt' == $key) { | |
$timestamps = true; | |
} | |
} | |
/// -- models | |
print "Generating model $class\n"; | |
$fp = fopen("gen/models/$class.php", "wt"); | |
$str = <<<EOF1 | |
<?php | |
namespace {$namespace}; | |
use Illuminate\Database\Eloquent\Model; | |
class {$class} extends Model | |
{ | |
protected \$table = '{$tablename}'; | |
EOF1; | |
fwrite($fp, $str); | |
if (!$timestamps) { | |
fputs($fp, " public \$timestamps = false;\n\n"); | |
} | |
if (!$is_momentary) { | |
$casts = []; | |
$fillables = []; | |
$str = ''; | |
foreach ($def->properties as $key => $value) { | |
if (in_array($key, ['createdAt', 'updatedAt', 'id'])) { | |
continue; | |
} | |
$key = ucfirst($key); | |
$fillables[] = "'$key'"; | |
$fieldtype = isset($value->format) ? $value->format : $value->type; | |
if ('int64' == $fieldtype) { | |
$fieldtype = 'integer'; | |
} | |
$casts[] = "'$key' => '$fieldtype'"; | |
} | |
fwrite($fp, " protected \$fillable = [" . implode(", ", $fillables) . "];\n\n"); | |
fwrite($fp, " protected \$casts = [\n " . implode(",\n ", $casts) . "\n ];\n\n"); | |
fwrite($fp, $str); | |
} | |
$str = <<<EOF2 | |
} | |
EOF2; | |
fwrite($fp, $str); | |
fclose($fp); | |
if ($is_momentary) {continue;} | |
/// -- migrations | |
print "Generating migrations {$class}\n"; | |
$fp = fopen("gen/migrations/" . date('Y_m_d_000000') . "_create_" . $tablename . "_table.php", "wt"); | |
$cref = "Create" . ucfirst(strtolower($prefix)) . $class . "Table"; | |
$str = <<<EOF3 | |
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use {$namespace}\\{$class}; | |
class {$cref} extends Migration | |
{ | |
public function up() | |
{ | |
\$tablename = (new $class)->getTable(); | |
Schema::create(\$tablename, function (Blueprint \$table) { | |
EOF3; | |
fwrite($fp, $str); | |
foreach ($def->properties as $key => $value) { | |
if ('createdAt' == $key) { | |
fputs($fp, " \$table->timestamps();\n"); | |
continue; | |
} else if ('updatedAt' == $key) { | |
continue; | |
} | |
$nullable = "->nullable()"; | |
if ('id' == $key) { | |
$fieldtype = 'increments'; | |
$nullable = ''; | |
} else { | |
$fieldtype = isset($value->format) ? $value->format : $value->type; | |
if ('int64' == $fieldtype) { | |
$fieldtype = 'integer'; | |
} | |
} | |
$key = ucfirst($key); | |
if (isset($value->maxLength)) { | |
fputs($fp, " \$table->{$fieldtype}('{$key}', {$value->maxLength})$nullable;\n"); | |
} else { | |
fputs($fp, " \$table->{$fieldtype}('{$key}')$nullable;\n"); | |
} | |
} | |
$str = <<<EOF4 | |
}); | |
} | |
public function down() | |
{ | |
\$tablename = (new $class)->getTable(); | |
Schema::drop(\$tablename); | |
} | |
} | |
EOF4; | |
fwrite($fp, $str); | |
fclose($fp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment