Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate seeders command creation #10

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
require __DIR__.'/../vendor/autoload.php';

use Cable8mm\Xeed\Command\GenerateModelsCommand;
use Cable8mm\Xeed\Command\GenerateSeedersCommand;
use Symfony\Component\Console\Application;

$application = new Application();

// ... register commands

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

$application->run();
2 changes: 2 additions & 0 deletions database/seeders/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
41 changes: 41 additions & 0 deletions src/Command/GenerateSeedersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Cable8mm\Xeed\Command;

use Cable8mm\Xeed\DB;
use Cable8mm\Xeed\Generators\SeederGenerator;
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-seeders',
description: 'Generate seeders. run `bin/console generate-seeders`',
hidden: false,
aliases: ['seeders']
)]
class GenerateSeedersCommand 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();

foreach ($tables as $table) {
SeederGenerator::make($table)->run();
}

return Command::SUCCESS;
}
}
11 changes: 9 additions & 2 deletions src/Generators/SeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Cable8mm\Xeed\Generators;

use Cable8mm\Xeed\Interfaces\Generator;
use Cable8mm\Xeed\Support\Inflector;
use Cable8mm\Xeed\Support\Path;
use Cable8mm\Xeed\Table;

/**
* Generator for `dist/database/seeders/*.php`.
Expand Down Expand Up @@ -58,15 +60,20 @@ public function run(): void
/**
* Factory method.
*
* @param string $class The model class name
* @param string|Table $class The model class name
* @param string $namespace The model namespace
* @param string $dist The path to the dist folder
*/
public static function make(
string $class,
string|Table $class,
?string $namespace = null,
?string $dist = null
): static {

if ($class instanceof Table) {
$class = Inflector::classify($class->name);
}

return new self($class, $namespace, $dist);
}
}
Loading