Files
dockerfluxodecaixapro/app/Console/Commands/MakeMigrationModuleCommand.php
T
2026-09-01 10:46:29 -03:00

82 lines
2.6 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;
use Illuminate\Support\Str;
class MakeMigrationModuleCommand extends Command
{
protected static $defaultName = 'make:migration-module';
protected static $defaultDescription = 'Cria uma nova migração dentro de um módulo específico';
protected function configure(): void
{
$this
->addArgument('module', InputArgument::REQUIRED, 'O nome do módulo')
->addArgument('name', InputArgument::REQUIRED, 'O nome da migração (ex: create_posts_table)');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$module = strtolower($input->getArgument('module'));
$name = Str::snake($input->getArgument('name'));
$modulePath = dirname(__DIR__, 3) . "/modules/{$module}";
if (!is_dir($modulePath)) {
$output->writeln("<error>Módulo '{$module}' não encontrado.</error>");
return Command::FAILURE;
}
$migrationsPath = "{$modulePath}/database/migrations";
if (!is_dir($migrationsPath)) {
mkdir($migrationsPath, 0755, true);
}
// Busca o próximo número sequencial para o prefixo (SysCMS usa 001, 002...)
$existingFiles = glob("{$migrationsPath}/*.php");
$nextNumber = str_pad(count($existingFiles) + 1, 3, '0', STR_PAD_LEFT);
$fileName = "{$nextNumber}_{$name}.php";
$filePath = "{$migrationsPath}/{$fileName}";
$className = Str::studly($name);
$template = <<<PHP
<?php
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
class {$className}
{
public function up()
{
Capsule::schema()->create('table_name', function (Blueprint \$table) {
\$table->bigIncrements('id');
// \$table->string('name');
\$table->timestamps();
});
}
public function down()
{
Capsule::schema()->dropIfExists('table_name');
}
}
PHP;
if (file_put_contents($filePath, $template)) {
$output->writeln("<info>Migração criada com sucesso: modules/{$module}/database/migrations/{$fileName}</info>");
return Command::SUCCESS;
}
$output->writeln("<error>Falha ao criar o arquivo de migração.</error>");
return Command::FAILURE;
}
}