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

Add Seeder to Console #6

Merged
merged 1 commit into from
Nov 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions Config/Command/DatabaseSeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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();
}
}
54 changes: 54 additions & 0 deletions Config/Command/GenerateSeedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace Config\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Support\Str;

class GenerateSeedCommand extends Command
{
// the name of the command (the part after "bin/console")
protected static $defaultName = 'g:seed';

public function __construct()
{
$this->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;
}
}
20 changes: 20 additions & 0 deletions Config/Command/stubs/seed.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace App\Database\Seeds;

use Illuminate\Database\Seeder;
use App\Models\modelName;

class ClassName extends Seeder
{
/**
* Seed the table's database.
*
* @return void
*/
public function run()
{
//$entity = new modelName();
//$entity->field = 'value';
//$entity->save();
}
}
1 change: 1 addition & 0 deletions Config/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down