Skip to content

Commit

Permalink
Generate database seed command creation
Browse files Browse the repository at this point in the history
  • Loading branch information
cable8mm committed Mar 14, 2024
1 parent 56ebb92 commit 1e7814e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ The Xeed is to generate new model, seed, database seed, factory and migration fi

- [x] Database testing is supported
- [x] Generate models for Laravel
- [ ] Generate seed files for Laravel
- [ ] Generate database seed files for Laravel
- [x] Generate seed files for Laravel
- [x] Generate database seed files for Laravel
- [ ] Generate factories for Laravel
- [ ] Generate migrations for Laravel

Expand Down Expand Up @@ -41,6 +41,20 @@ bin/console models
# Generate all models from database in `dist/app/Models` folder
```

### Generate `Seeder`s

```sh
bin/console seeders
# Generate all seeds from database in `dist/database/seeders` folder
```

### Generate `DatabaseSeeder`

```sh
bin/console database
# Generate database seed from database in `dist/database/seeders` folder
```

## How to contribute

### Development
Expand Down
2 changes: 2 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require __DIR__.'/../vendor/autoload.php';

use Cable8mm\Xeed\Command\GenerateDatabaseSeederCommand;
use Cable8mm\Xeed\Command\GenerateModelsCommand;
use Cable8mm\Xeed\Command\GenerateSeedersCommand;
use Symfony\Component\Console\Application;
Expand All @@ -13,5 +14,6 @@ $application = new Application();

$application->add(new GenerateModelsCommand());
$application->add(new GenerateSeedersCommand());
$application->add(new GenerateDatabaseSeederCommand());

$application->run();
46 changes: 46 additions & 0 deletions src/Command/GenerateDatabaseSeederCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Cable8mm\Xeed\Command;

use Cable8mm\Xeed\DB;
use Cable8mm\Xeed\Generators\DatabaseSeederGenerator;
use Cable8mm\Xeed\Support\Inflector;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'generate-database-seeder',
description: 'Generate seeders. run `bin/console generate-database-seeder`',
hidden: false,
aliases: ['database']
)]
class GenerateDatabaseSeederCommand extends Command
{
protected function configure()
{
$dotenv = \Dotenv\Dotenv::createImmutable(getcwd());
$dotenv->safeLoad();
}

/**
* Generate models.
*
* Run `bin/console generate-seeders` or `bin/console seeders`
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$tables = DB::getInstance()->attach()->getTables();

$classes = [];

foreach ($tables as $table) {
$classes[] = Inflector::classify($table->name);
}

DatabaseSeederGenerator::make($classes)->run();

return Command::SUCCESS;
}
}

0 comments on commit 1e7814e

Please sign in to comment.