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

79 lines
2.5 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 MakeCommandCommand extends Command
{
protected static $defaultName = 'make:command';
protected static $defaultDescription = 'Cria um novo comando para o syscli';
protected function configure(): void
{
$this
->addArgument('name', InputArgument::REQUIRED, 'O nome da classe do comando (ex: TestCommand)')
->addArgument('command', InputArgument::REQUIRED, 'O comando a ser digitado (ex: app:test)');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$name = $input->getArgument('name');
$commandName = $input->getArgument('command');
// Garantir que termina com Command
if (!str_ends_with($name, 'Command')) {
$name .= 'Command';
}
$commandsPath = __DIR__;
$filePath = $commandsPath . '/' . $name . '.php';
if (file_exists($filePath)) {
$output->writeln("<error>O comando '{$name}' já existe.</error>");
return Command::FAILURE;
}
$template = <<<PHP
<?php
namespace App\Console\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class {$name} extends Command
{
protected static \$defaultName = '{$commandName}';
protected static \$defaultDescription = 'Descrição do comando {$commandName}';
protected function configure(): void
{
// Configure argumentos e opções aqui
}
protected function execute(InputInterface \$input, OutputInterface \$output): int
{
// Sua lógica aqui
\$output->writeln('<info>Executando comando {$commandName}...</info>');
return Command::SUCCESS;
}
}
PHP;
if (file_put_contents($filePath, $template)) {
$output->writeln("<info>Comando '{$name}' criado com sucesso em app/Console/Commands/</info>");
$output->writeln("<comment>Lembre-se de registrar o comando no syscli se necessário.</comment>");
return Command::SUCCESS;
} else {
$output->writeln("<error>Falha ao criar o arquivo do comando.</error>");
return Command::FAILURE;
}
}
}