Skip to content

Commit 1838bb5

Browse files
authored
Merge pull request #340 from bowphp/refactor/code-base
Change seeder concept
2 parents dab9a1d + 61b6b9a commit 1838bb5

12 files changed

Lines changed: 92 additions & 109 deletions

src/Console/Command.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Bow\Console\Command\Generator\GenerateServiceCommand;
2121
use Bow\Console\Command\Generator\GenerateSessionCommand;
2222
use Bow\Console\Command\Generator\GenerateAppEventCommand;
23-
use Bow\Console\Command\Generator\GenerateWorkerCommand;
2423
use Bow\Console\Command\Generator\GenerateExceptionCommand;
2524
use Bow\Console\Command\Generator\GenerateMessagingCommand;
2625
use Bow\Console\Command\Generator\GenerateMigrationCommand;
@@ -30,6 +29,7 @@
3029
use Bow\Console\Command\Generator\GenerateNotificationCommand;
3130
use Bow\Console\Command\Generator\GenerateConfigurationCommand;
3231
use Bow\Console\Command\Generator\GenerateEventListenerCommand;
32+
use Bow\Console\Command\Generator\GenerateJobCommand;
3333
use Bow\Console\Command\Generator\GenerateRouterResourceCommand;
3434

3535
class Command extends AbstractCommand
@@ -41,7 +41,7 @@ class Command extends AbstractCommand
4141
*/
4242
private array $commands = [
4343
"clear" => ClearCommand::class,
44-
"seed:table" => SeederCommand::class,
44+
"seed:file" => SeederCommand::class,
4545
"seed:all" => SeederCommand::class,
4646
"migration:migrate" => MigrationCommand::class,
4747
"migration:rollback" => MigrationCommand::class,
@@ -57,7 +57,7 @@ class Command extends AbstractCommand
5757
"add:validation" => GenerateValidationCommand::class,
5858
"add:event" => GenerateAppEventCommand::class,
5959
"add:listener" => GenerateEventListenerCommand::class,
60-
"add:producer" => GenerateWorkerCommand::class,
60+
"add:producer" => GenerateJobCommand::class,
6161
"add:command" => GenerateConsoleCommand::class,
6262
"add:message" => GenerateMessagingCommand::class,
6363
"run:console" => ReplCommand::class,

src/Console/Command/Generator/GenerateMigrationCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public function run(string $model): void
6161
]);
6262

6363
// Print console information
64-
echo Color::green('The migration file has been successfully created') . "\n";
64+
echo Color::green("The migration {$this->setting->getMigrationDirectory()}/{$filename} file has been successfully created") . "\n";
6565
}
6666
}

src/Console/Command/Generator/GenerateSeederCommand.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@ class GenerateSeederCommand extends AbstractCommand
2020
*/
2121
public function run(string $seeder): void
2222
{
23-
$seeder = Str::plural($seeder);
23+
$create_at = date("YmdHis");
24+
$class_name = sprintf("%s%s", ucfirst(Str::camel($seeder)), $create_at);
25+
$filename = sprintf("%s-%s", $create_at, $seeder);
2426

2527
$generator = new Generator(
2628
$this->setting->getSeederDirectory(),
27-
$seeder
29+
$filename
2830
);
2931

3032
if ($generator->fileExists()) {
31-
echo "\033[0;31mThe seeder already exists.\033[00m";
33+
echo "\033[0;31mThe seeder {$this->setting->getSeederDirectory()}/{$filename}.php already exists.\033[00m";
3234

3335
exit(1);
3436
}
3537

36-
$generator->write('seeder', ['name' => $seeder]);
38+
$generator->write('seeder', ['className' => $class_name]);
3739

38-
echo "\033[0;32mThe seeder has been created.\033[00m\n";
40+
echo "\033[0;32mThe seeder {$this->setting->getSeederDirectory()}/{$filename}.php has been created.\033[00m\n";
3941

4042
exit(0);
4143
}

src/Console/Command/MigrationCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ protected function makeUp(array $migrations): void
141141
}
142142

143143
foreach ($migrations as $file => $migration) {
144-
if ($this->checkIfMigrationExist($migration)) {
144+
if ($this->checkIfMigrationExists($migration)) {
145145
continue;
146146
}
147147

@@ -174,7 +174,7 @@ protected function makeUp(array $migrations): void
174174
* @return bool
175175
* @throws ConnectionException|QueryBuilderException
176176
*/
177-
private function checkIfMigrationExist(string $migration): bool
177+
private function checkIfMigrationExists(string $migration): bool
178178
{
179179
$result = $this->getMigrationTable()
180180
->where('migration', $migration)

src/Console/Command/SeederCommand.php

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
namespace Bow\Console\Command;
66

7-
use Bow\Console\AbstractCommand;
7+
use Exception;
8+
use Bow\Support\Str;
89
use Bow\Console\Color;
9-
use Bow\Console\Generator;
10+
use Bow\Console\AbstractCommand;
1011
use Bow\Console\Traits\ConsoleTrait;
11-
use Bow\Database\Barry\Model;
12-
use Bow\Database\Database;
13-
use Bow\Support\Str;
14-
use Exception;
1512

1613
class SeederCommand extends AbstractCommand
1714
{
@@ -24,9 +21,15 @@ class SeederCommand extends AbstractCommand
2421
*/
2522
public function all(): void
2623
{
27-
$seeder = $this->setting->getSeederDirectory() . '/_database.php';
24+
$seeder_files = [];
2825

29-
$this->make($seeder);
26+
foreach (glob($this->setting->getSeederDirectory() . '/*.php') as $seeder_file) {
27+
$seeder_files[$seeder_file] = $this->normalizeClassName(explode('.', basename($seeder_file))[0]);
28+
}
29+
30+
foreach ($seeder_files as $seeder_file => $seeder_class_name) {
31+
$this->make($seeder_file, $seeder_class_name);
32+
}
3033
}
3134

3235
/**
@@ -35,34 +38,15 @@ public function all(): void
3538
* @param string $seed_filename
3639
* @return void
3740
*/
38-
private function make(string $seed_filename): void
41+
private function make(string $seed_filename, string $seeder_class_name): void
3942
{
40-
$seeds = include $seed_filename;
41-
42-
$seed_collection = array_merge($seeds);
43-
44-
// Get the database connexion
45-
$connection = $this->arg->getParameters()->get('--connection', config("database.default"));
46-
4743
try {
48-
$connection = Database::connection($connection);
49-
50-
foreach ($seed_collection as $table => $seed) {
51-
if (class_exists($table)) {
52-
$instance = app($table);
53-
if ($instance instanceof Model) {
54-
$table = $instance->getTable();
55-
}
56-
}
57-
58-
$result = $connection->table($table)->insert($seed);
59-
60-
echo Color::green("$result seed" . ($result > 1 ? 's' : '') . " on $table table\n");
61-
}
44+
include_once $seed_filename;
45+
(new $seeder_class_name())->run();
46+
echo Color::green("Seeding completed: $seed_filename\n");
6247
} catch (Exception $e) {
63-
echo Color::red($e->getMessage());
64-
65-
exit(1);
48+
echo Color::red("Seeding failed for: $seed_filename");
49+
echo Color::red("\n" . $e->getMessage());
6650
}
6751
}
6852

@@ -72,22 +56,37 @@ private function make(string $seed_filename): void
7256
* @param string|null $seeder_name
7357
* @return void
7458
*/
75-
public function table(?string $seeder_name = null): void
59+
public function file(?string $seeder_class_name = null): void
7660
{
77-
if (is_null($seeder_name)) {
78-
$this->throwFailsCommand('Specify the seeder table name', 'help seed');
61+
if (is_null($seeder_class_name)) {
62+
$this->throwFailsCommand('Specify the seeder file name', 'help seed');
63+
}
64+
65+
$seeder_files = [];
66+
67+
foreach (glob($this->setting->getSeederDirectory() . '/*.php') as $seeder_file) {
68+
$interal_class_base_name = $this->normalizeClassName(explode('.', basename($seeder_file))[0]);
69+
if ($seeder_class_name != $interal_class_base_name) {
70+
continue;
71+
}
72+
$seeder_files[$seeder_file] = $interal_class_base_name;
73+
break;
7974
}
8075

81-
$seeder_name = trim($seeder_name);
76+
foreach ($seeder_files as $file => $seeder_class_name) {
77+
echo Color::green("Seeding: $file");
8278

83-
if (!file_exists($this->setting->getSeederDirectory() . "/{$seeder_name}.php")) {
84-
echo Color::red("Seeder $seeder_name not exists.");
79+
$this->make($file, $seeder_class_name);
8580

86-
exit(1);
81+
echo Color::green("Seeding completed: $file");
8782
}
83+
}
84+
85+
private function normalizeClassName(string $seeder_class_name): string
86+
{
87+
$time = explode('-', $seeder_class_name)[0];
88+
$seeder_class_name = str_replace($time, '', $seeder_class_name);
8889

89-
$this->make(
90-
$this->setting->getSeederDirectory() . "/{$seeder_name}.php"
91-
);
90+
return Str::camel($seeder_class_name) . $time;
9291
}
9392
}

src/Console/Console.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ public function call(?string $command): mixed
238238
if (!in_array($command, array_keys($commands))) {
239239
// Try to execute the custom command
240240
if (array_key_exists($this->arg->getRawCommand(), static::$registers) || array_key_exists($command, static::$registers)) {
241-
$this->executeCustomCommand($this->arg->getRawCommand() ?? $command);
242-
exit(0);
241+
return $this->executeCustomCommand($this->arg->getRawCommand() ?? $command);
243242
}
244243
}
245244

@@ -251,14 +250,12 @@ public function call(?string $command): mixed
251250

252251
if (!$this->arg->getAction()) {
253252
if ($target == 'help') {
254-
$this->help($command);
255-
exit(0);
253+
return $this->help($command);
256254
}
257255
}
258256

259257
try {
260-
call_user_func_array([$this, $command], [$this->arg->getRawCommand()]);
261-
exit(0);
258+
return call_user_func_array([$this, $command], [$this->arg->getRawCommand()]);
262259
} catch (Exception $e) {
263260
echo Color::red(sprintf("$command command failed with: %s\n", $e->getMessage()));
264261
exit(1);
@@ -379,7 +376,7 @@ private function seed(): void
379376
{
380377
$action = $this->arg->getAction();
381378

382-
if (!in_array($action, ['all', 'table'])) {
379+
if (!in_array($action, ['all', 'file'])) {
383380
$this->throwFailsCommand('This action is not exists', 'help seed');
384381
}
385382

@@ -555,8 +552,8 @@ private function help(?string $command = null): int
555552
\033[0;33mclear:all\033[00m Clear all cache information
556553
557554
\033[0;32mSEED\033[00m Make seeding
558-
\033[0;33mseed:table\033[00m [name] Make seeding for one table
559-
\033[0;33mseed:all\033[00m Make seeding for all
555+
\033[0;33mseed:file\033[00m [class_name] Make seeding for one file
556+
\033[0;33mseed:all\033[00m Make seeding for all
560557
561558
\033[0;32mRUN\033[00m Launch process
562559
\033[0;33mrun:console\033[00m show psysh php REPL for debug you code.
@@ -660,7 +657,7 @@ private function help(?string $command = null): int
660657
\n\033[0;32mMake table seeding\033[00m\n
661658
662659
\033[0;33m$\033[00m php \033[0;34mbow\033[00m seed:all\033[00m Make seeding for all
663-
\033[0;33m$\033[00m php \033[0;34mbow\033[00m seed:table\033[00m table_name Make seeding for one table
660+
\033[0;33m$\033[00m php \033[0;34mbow\033[00m seed:file\033[00m class_name Make seeding for one file
664661
665662
U;
666663
break;

src/Console/Generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function write(string $type, array $data = []): bool
122122
], $data)
123123
);
124124

125-
return (bool)file_put_contents($this->getPath(), $template);
125+
return (bool) file_put_contents($this->getPath(), $template);
126126
}
127127

128128
/**

src/Console/stubs/seeder.stub

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
use Faker\Factory as FakerFactory;
44

5-
/**
6-
* The {name} seeder
7-
*
8-
* @see https://fakerphp.github.io for all documentation
9-
*/
10-
$faker = FakerFactory::create();
11-
12-
$seed = [
13-
'name' => $faker->name(),
14-
'created_at' => date('Y-m-d H:i:s'),
15-
'updated_at' => date('Y-m-d H:i:s')
16-
];
17-
18-
return ['{name}' => $seed];
5+
class {className}
6+
{
7+
public function run()
8+
{
9+
$faker = FakerFactory::create();
10+
11+
// Write the seeding here
12+
}
13+
}

tests/Console/CustomCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static function setUpBeforeClass(): void
2121
public function test_create_the_custom_command_from_static_calling()
2222
{
2323
Console::register("command", CustomCommand::class);
24+
2425
static::$console->call("command");
2526

2627
$content = $this->getFileContent();

tests/Console/GeneratorDeepTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ public function test_generate_seeder_stubs()
118118
{
119119
$generator = new Generator(TESTING_RESOURCE_BASE_DIRECTORY, 'fake_seeder');
120120
$content = $generator->makeStubContent('seeder', [
121-
'num' => 1,
122-
'name' => "fakes"
121+
'className' => "fakes"
123122
]);
124123
$this->assertNotNull($content);
125124
$this->assertMatchesSnapshot($content);

0 commit comments

Comments
 (0)