Skip to content

Commit

Permalink
mod
Browse files Browse the repository at this point in the history
  • Loading branch information
ShibuyaKosuke committed May 29, 2021
1 parent eb9c4bd commit b62458e
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Console/RuleExportCommend.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use ShibuyaKosuke\LaravelDdlExport\Facades\CreateView;
use ShibuyaKosuke\LaravelDdlExport\Facades\Table;
use ShibuyaKosuke\LaravelDdlExport\Facades\Type;
use ShibuyaKosuke\LaravelDdlExport\Helpers\Arr;
use ShibuyaKosuke\LaravelDdlExport\Models\Contracts\ColumnInterface;
use ShibuyaKosuke\LaravelDdlExport\Models\Contracts\ConstraintInterface;
use ShibuyaKosuke\LaravelDdlExport\Models\Contracts\TableInterface;
Expand Down Expand Up @@ -42,7 +41,7 @@ public function handle(): void
$response = Table::all()->mapWithKeys(function (TableInterface $table) {
return [
$table->name => $table->columns
->mapWithKeys(static function (ColumnInterface $column) {
->map(static function (ColumnInterface $column) {
/** @var ConstraintInterface $constraint */
$foreign = ($constraint = $column->foreign) ?
sprintf(
Expand All @@ -64,16 +63,29 @@ public function handle(): void
if ($foreign) {
$rules[] = $foreign;
}
return [$column->name => $rules];
$rules = array_map(function ($item) {
return "'" . $item . "'";
}, $rules);
return [sprintf("'%s'", $column->name) . ' => [' . implode(', ', $rules) . '],'];
})
];
})->toArray();

CreateView::down();

$res = '[' . PHP_EOL;
foreach ($response as $tableName => $columns) {
$res .= " '" . $tableName . '\' => [' . PHP_EOL;
foreach ($columns as $column) {
$res .= ' ' . $column[0] . PHP_EOL;
}
$res .= ' ],' . PHP_EOL;
}
$res .= ']';

File::put(
config_path('rules.php'),
sprintf("<?php\n\nreturn %s;\n", Arr::export($response))
sprintf("<?php\n\nreturn %s;\n", $res)
);
}
}

0 comments on commit b62458e

Please sign in to comment.