First Commit
This commit is contained in:
+68
@@ -0,0 +1,68 @@
|
||||
<?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\Database\Capsule\Manager as Capsule;
|
||||
|
||||
class ModuleRegisterCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'module:register';
|
||||
protected static $defaultDescription = 'Registra um módulo existente na tabela modules do banco de dados';
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->addArgument('module', InputArgument::REQUIRED, 'O nome do diretório do módulo');
|
||||
}
|
||||
|
||||
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 processar o arquivo module.json do módulo '{$moduleName}'.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Verifica se a tabela modules existe
|
||||
if (!Capsule::schema()->hasTable('modules')) {
|
||||
$output->writeln("<error>A tabela 'modules' não existe no banco de dados. Execute as migrations primeiro.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Verifica se o módulo já está registrado usando a coluna correta
|
||||
$existing = Capsule::table('modules')->where('modules_name', $moduleName)->first();
|
||||
|
||||
$data = [
|
||||
'modules_name' => $moduleName,
|
||||
'modules_display_name' => $config['display_name'] ?? $moduleName,
|
||||
'modules_description' => $config['description'] ?? '',
|
||||
'modules_version' => $config['version'] ?? '1.0.0',
|
||||
'modules_ative' => $config['module_active'] ?? $config['module_ative'] ?? true,
|
||||
'updated_at' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
if ($existing) {
|
||||
Capsule::table('modules')->where('modules_name', $moduleName)->update($data);
|
||||
$output->writeln("<info>Registro do módulo '{$moduleName}' atualizado com sucesso!</info>");
|
||||
} else {
|
||||
$data['created_at'] = date('Y-m-d H:i:s');
|
||||
Capsule::table('modules')->insert($data);
|
||||
$output->writeln("<info>Módulo '{$moduleName}' registrado com sucesso na tabela modules!</info>");
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user