diff --git a/Config/Command/DatabaseSeedCommand.php b/Config/Command/DatabaseSeedCommand.php index 476cbc6..90095fa 100644 --- a/Config/Command/DatabaseSeedCommand.php +++ b/Config/Command/DatabaseSeedCommand.php @@ -5,6 +5,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Illuminate\Support\Str; class DatabaseSeedCommand extends Command { @@ -25,9 +26,27 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { - $seed = new \App\Database\Seeds\DatabaseSeeder; - $seed->run(); + $seeds = glob($this->seedPath . '*.php'); + + foreach ($seeds as $seed) { + $file = pathinfo($seed); + $filename = $file['filename']; + + if ($filename !== ""): + + $className = Str::studly(str_replace(".php","",$filename)); + $this->seed($filename, $className); + $output->writeln("> ".str_replace(".php","",$file['basename']) . ' seeded successfully'); + + endif; + } $output->writeln("Database seed complete"); } + + protected function seed($filename, $className) { + $class = "\App\Database\Seeds\\".$className; + $seed = new $class; + $seed->run(); + } } \ No newline at end of file diff --git a/Config/Command/GenerateSeedCommand.php b/Config/Command/GenerateSeedCommand.php new file mode 100644 index 0000000..5b7d0c1 --- /dev/null +++ b/Config/Command/GenerateSeedCommand.php @@ -0,0 +1,54 @@ +seedsPath = dirname(dirname(__DIR__)) . "/App/Database/Seeds/"; + parent::__construct(); + } + + protected function configure() + { + $this + ->setDescription("Create a new seed file") + ->setHelp("Create a new seed file") + ->addArgument('model', InputArgument::REQUIRED, 'model name') + ->addArgument("name", InputArgument::OPTIONAL, "seed name"); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $modelName = Str::studly(Str::singular($input->getArgument("model"))); + $seedName = $input->getArgument("name") ?? $modelName; + + $className = Str::studly(Str::singular($seedName)."Seeder"); + + $filename = $className . ".php"; + $file = $this->seedsPath . $filename; + if (!file_exists($file)): + + touch($file); + + $fileContent = \file_get_contents(__DIR__ . '/stubs/seed.stub'); + + $fileContent = str_replace(["ClassName", "modelName"], [$className, $modelName], $fileContent); + + file_put_contents($file, $fileContent); + + $output->writeln($filename . ' generated successfully'); + else: + return $seedName." already exists"; + endif; + } + } \ No newline at end of file diff --git a/Config/Command/stubs/seed.stub b/Config/Command/stubs/seed.stub new file mode 100644 index 0000000..e40ac11 --- /dev/null +++ b/Config/Command/stubs/seed.stub @@ -0,0 +1,20 @@ +field = 'value'; + //$entity->save(); + } +} diff --git a/Config/Console.php b/Config/Console.php index 9b78046..1088745 100644 --- a/Config/Console.php +++ b/Config/Console.php @@ -19,6 +19,7 @@ public function __construct() { $this->app->add(new \Config\Command\GenerateModelCommand()); $this->app->add(new \Config\Command\GenerateHelperCommand()); $this->app->add(new \Config\Command\GenerateControllerCommand()); + $this->app->add(new \Config\Command\GenerateSeedCommand()); // Delete Commands $this->app->add(new \Config\Command\DeleteModelCommand());