75 lines
2.1 KiB
PHP
Executable File
75 lines
2.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
class MakeSeedCommand extends Command
|
|
{
|
|
protected static $defaultName = 'make:seed';
|
|
protected static $defaultDescription = 'Cria uma nova classe de seeder';
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this
|
|
->addArgument('name', InputArgument::REQUIRED, 'O nome da classe seeder (ex: UserSeeder)');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$name = $input->getArgument('name');
|
|
|
|
// Garantir que termina com Seeder
|
|
if (!str_ends_with($name, 'Seeder')) {
|
|
$name .= 'Seeder';
|
|
}
|
|
|
|
$seedersPath = dirname(__DIR__, 3) . '/database/seeders';
|
|
|
|
if (!is_dir($seedersPath)) {
|
|
mkdir($seedersPath, 0755, true);
|
|
}
|
|
|
|
$filePath = $seedersPath . '/' . $name . '.php';
|
|
|
|
if (file_exists($filePath)) {
|
|
$output->writeln("<error>O seeder '{$name}' já existe.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$template = <<<PHP
|
|
<?php
|
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
class {$name}
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run(): void
|
|
{
|
|
// Capsule::table('users')->insert([
|
|
// 'user_name' => 'Admin',
|
|
// 'user_email' => 'admin@example.com',
|
|
// 'user_password' => password_hash('password', PASSWORD_DEFAULT),
|
|
// ]);
|
|
}
|
|
}
|
|
PHP;
|
|
|
|
if (file_put_contents($filePath, $template)) {
|
|
$output->writeln("<info>Seeder criado com sucesso:</info> database/seeders/{$name}.php");
|
|
return Command::SUCCESS;
|
|
} else {
|
|
$output->writeln("<error>Falha ao criar o arquivo do seeder.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
}
|
|
}
|