First Commit
This commit is contained in:
Executable
+59
@@ -0,0 +1,59 @@
|
||||
<?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 ModuleEnableCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'module:enable';
|
||||
protected static $defaultDescription = 'Ativa um módulo no arquivo module.json';
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->addArgument('module', InputArgument::REQUIRED, 'O nome do módulo a ser ativado');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$moduleName = $input->getArgument('module');
|
||||
$modulePath = dirname(__DIR__, 3) . '/modules/' . $moduleName . '/module.json';
|
||||
|
||||
if (!file_exists($modulePath)) {
|
||||
$output->writeln("<error>Módulo '{$moduleName}' não encontrado ou arquivo module.json inexistente.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$config = json_decode(file_get_contents($modulePath), true);
|
||||
|
||||
if ($config === null) {
|
||||
$output->writeln("<error>Erro ao ler o arquivo module.json do módulo '{$moduleName}'.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$isActive = $config['module_active'] ?? $config['module_ative'] ?? false;
|
||||
if ($isActive === true) {
|
||||
$output->writeln("<comment>O módulo '{$moduleName}' já está ativado.</comment>");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
// Maintain the key already used in the file if possible
|
||||
if (isset($config['module_ative'])) {
|
||||
$config['module_ative'] = true;
|
||||
} else {
|
||||
$config['module_active'] = true;
|
||||
}
|
||||
|
||||
if (file_put_contents($modulePath, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES))) {
|
||||
$output->writeln("<info>Módulo '{$moduleName}' ativado com sucesso!</info>");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$output->writeln("<error>Falha ao atualizar o arquivo module.json do módulo '{$moduleName}'.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user