Skip to content

Commit

Permalink
Release v1.0.0-beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sugeng-sulistiyawan committed Apr 4, 2023
1 parent 14b83af commit e997657
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 46 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,16 @@ public $locale = 'en_US';
At the end of every Seeder, if any columns have been forgotten, a message with all the missing columns will appear

```shell
> #################### MISSING COLUMNS ###################
> # TABLE: {{%user}} #
> # name => varchar(255) #
> # age => int(2) #
> ########################################################
> ########################### MISSING COLUMNS ############################
> # #
> # TABLE: {{%user}} #
> # -------------------------------------------------------------------- #
> # - full_name => varchar(1) #
> # - birth_date => date #
> # - thumbnail => varchar(64) #
> # -------------------------------------------------------------------- #
> # #
> ########################################################################
```

### Seeder
Expand Down
31 changes: 25 additions & 6 deletions src/SeederController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,20 @@ public function actionSeed($name = "")
$function = $explode[1] ?? null;

if ($name) {
$func = $function ?? 'run';
$modelClass = str_replace('/', '\\', $name);
$explode = explode('\\', $modelClass);
$modelName = Inflector::camelize(array_pop($explode));
$modelNamespace = implode('\\', $explode);

$modelClass = $modelNamespace ? "{$modelNamespace}\\{$modelName}" : $modelName;
$func = $function ?? 'run';
$seederClasses = [
$modelClass,
"{$modelClass}TableSeeder",
"{$this->seederNamespace}\\{$modelClass}",
"{$this->seederNamespace}\\{$modelClass}TableSeeder",
"{$this->tableSeederNamespace}\\{$modelClass}",
"{$this->tableSeederNamespace}\\{$modelClass}TableSeeder",
$name,
"{$name}TableSeeder",
"{$this->seederNamespace}\\{$name}",
Expand Down Expand Up @@ -211,7 +223,7 @@ public function actionCreate($modelName)
]);
FileHelper::createDirectory($this->tablesPath);

if (!file_exists($file) || $this->confirm("\n'{$className}' already exists, overwrite?\nAll data will be lost irreversibly!")) {
if (!file_exists($file) || $this->confirm("\n'{$file}' already exists, overwrite?\nAll data will be lost irreversibly!")) {
file_put_contents($file, $content, LOCK_EX);
$this->stdout("New seeder created successfully.\n", Console::FG_GREEN);
}
Expand Down Expand Up @@ -319,20 +331,27 @@ protected function generateFakerField($key)
'email' => 'email',
'phone' => 'phoneNumber',
'hp' => 'phoneNumber',
'start_date' => 'dateTime()->format("Y-m-d H:i:s")',
'end_date' => 'dateTime()->format("Y-m-d H:i:s")',
'created_at' => 'dateTime()->format("Y-m-d H:i:s")',
'updated_at' => 'dateTime()->format("Y-m-d H:i:s")',
'token' => 'uuid',
'duration' => 'numberBetween()',

'integer' => 'numberBetween(0, 10)',
'smallint' => 'numberBetween(0, 10)',
'tinyint' => 'numberBetween(0, 10)',
'mediumint' => 'numberBetween(0, 10)',
'int' => 'numberBetween(0, 10)',
'bigint' => 'numberBetween(0, 10)',
'bigint' => 'numberBetween()',
'date' => 'date()',
'datetime' => 'dateTime()',
'timestamp' => 'dateTime()',
'datetime' => 'dateTime()->format("Y-m-d H:i:s")',
'timestamp' => 'dateTime()->format("Y-m-d H:i:s")',
'year' => 'year()',
'time' => 'time()',
];

return ArrayHelper::getValue($faker, $key, 'text');
return ArrayHelper::getValue($faker, $key, 'word');
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/TableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,18 @@ protected function checkMissingColumns($insertedColumns)
}

if (count($missingColumns)) {
echo " > " . str_pad(' MISSING COLUMNS ', 70, '#', STR_PAD_BOTH) . "\n";
echo " > #" . str_pad(' MISSING COLUMNS ', 70, '#', STR_PAD_BOTH) . "#\n";
foreach ($missingColumns as $table => $columns) {
echo " > " . str_pad("# TABLE: $table", 69, ' ') . "#\n";
echo " > #" . str_pad('', 70, ' ') . "#\n";
echo " > #" . str_pad(" TABLE: {$table}", 70, ' ') . "#\n";
echo " > # " . str_pad('', 68, '-') . " #\n";
foreach ($columns as [$tableColumn, $type]) {
echo " > " . str_pad("# $tableColumn => $type", 69, ' ') . "#\n";
echo " > #" . str_pad(" - $tableColumn => $type", 70, ' ') . "#\n";
}
echo " > # " . str_pad('', 68, '-') . " #\n";
echo " > #" . str_pad('', 70, ' ') . "#\n";
}
echo " > " . str_pad('', 70, '#') . "\n";
echo " > #" . str_pad('', 70, '#') . "#\n";
}
}

Expand Down
69 changes: 38 additions & 31 deletions src/views/TableSeeder.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This view is used by diecoding\seeder\SeederController.php.
*
Expand All @@ -18,34 +19,40 @@
echo "\nnamespace {$namespace};\n";
}

$modelClass = $model::class;
$modelName = StringHelper::basename($modelClass);
$varsPre = [];
$check[$modelName] = [$modelName];
$modelClass = $model::class;
$modelName = StringHelper::basename($modelClass);
$foreignVars = [];

$checkClass[$modelClass] = $modelClass;
$checkClassName[$modelName] = [$modelClass];

?>

use diecoding\seeder\TableSeeder;
<?= "use {$modelClass};\n" ?>
<?php
<?php
$i = 0;
foreach ($fields as $column => $properties) {
if ($foreign = $properties->foreign) {
$_class = $foreign::class;
$_className = StringHelper::basename($_class);
if (isset($check[$_className])) {
$_className = $_className . count($check[$_className]);
$foreignClass = $foreign::class;
$foreignClassName = StringHelper::basename($foreignClass);
if (!isset($checkClass[$foreignClass])) {
if (isset($checkClassName[$foreignClassName])) {
$foreignClassName .= count($checkClassName[$foreignClassName]);
$checkClassName[$foreignClassName][] = $foreignClass;

echo "use {$_class} as {$_className};\n";
} else {
echo "use {$_class};\n";
echo "use {$foreignClass} as {$foreignClassName};\n";
} else {
$checkClassName[$foreignClassName] = [$foreignClass];

echo "use {$foreignClass};\n";
}
$checkClass[$foreignClass] = $foreignClass;
}

$check[$_className] = [$_class];
$ref_table_id = $properties->ref_table_id;
$space = $i++ === 0 ? '' : "\t\t";
$vars[$ref_table_id . $column] = '$' . Inflector::variablize($_className);
$varsPre[$ref_table_id . $column] = "{$space}{$vars[$ref_table_id . $column]} = {$_className}::find()->all();\n";
$ref_table_id = $properties->ref_table_id;
$vars[$ref_table_id . $column] = '$' . Inflector::variablize($foreignClassName);
$foreignVars[$foreignClassName] = "{$vars[$ref_table_id .$column]} = {$foreignClassName}::find()->all();\n";
}
} ?>

Expand All @@ -60,26 +67,26 @@ class <?= $className ?> extends TableSeeder
/**
* {@inheritdoc}
*/
function run()
public function run()
{
<?= implode("", $varsPre) ?>
<?= implode(" ", $foreignVars) ?>

$count = 100;
for ($i = 0; $i < $count; $i++) {
for ($i = 0; $i < $count; $i++) {
$this->insert(<?= $modelName ?>::tableName(), [
<?php
$i = 0;
foreach ($fields as $column => $properties) {
$ref_table_id = $properties->ref_table_id;
$space = $i++ === 0 ? '' : "\t\t\t\t";
if (isset($vars[$ref_table_id . $column])) {
echo $space . "'$column' => \$this->faker->randomElement({$vars[$ref_table_id . $column]})->{$properties->ref_table_id},\n";
} else {
echo $space . "'$column' => \$this->faker->{$properties->faker},\n";
}
}
$i = 0;
foreach ($fields as $column => $properties) {
$ref_table_id = $properties->ref_table_id;
$space = $i++ === 0 ? '' : "\t\t\t\t";
if (isset($vars[$ref_table_id . $column])) {
echo $space . "'$column' => \$this->faker->randomElement({$vars[$ref_table_id .$column]})->{$properties->ref_table_id},\n";
} else {
echo $space . "'$column' => \$this->faker->{$properties->faker},\n";
}
}
?>
]);
}
}
}
}

0 comments on commit e997657

Please sign in to comment.