95 lines
2.7 KiB
PHP
Executable File
95 lines
2.7 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 MakeModelCommand extends Command
|
|
{
|
|
protected static $defaultName = 'make:model';
|
|
protected static $defaultDescription = 'Cria um novo modelo Eloquent em um módulo específico';
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this
|
|
->addArgument('module', InputArgument::REQUIRED, 'O nome do módulo (ex: blog)')
|
|
->addArgument('name', InputArgument::REQUIRED, 'O nome do modelo (ex: Post)');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$module = strtolower($input->getArgument('module'));
|
|
$name = $input->getArgument('name');
|
|
|
|
$modulePath = dirname(__DIR__, 3) . '/modules/' . $module;
|
|
|
|
if (!is_dir($modulePath)) {
|
|
$output->writeln("<error>O módulo '{$module}' não existe.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$modelsPath = $modulePath . '/classes/Models';
|
|
if (!is_dir($modelsPath)) {
|
|
mkdir($modelsPath, 0755, true);
|
|
}
|
|
|
|
$filePath = $modelsPath . '/' . $name . '.php';
|
|
|
|
if (file_exists($filePath)) {
|
|
$output->writeln("<error>O modelo '{$name}' já existe no módulo '{$module}'.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$namespace = "Modules\\" . ucfirst($module) . "\\Models";
|
|
$tableName = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name)) . 's';
|
|
|
|
$template = <<<PHP
|
|
<?php
|
|
|
|
namespace {$namespace};
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* Model {$name}
|
|
*/
|
|
class {$name} extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected \$table = '{$tableName}';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected \$fillable = [
|
|
// Adicione os campos aqui
|
|
];
|
|
|
|
/**
|
|
* Indicates if the model should be timestamped.
|
|
*
|
|
* @var bool
|
|
*/
|
|
public \$timestamps = true;
|
|
}
|
|
PHP;
|
|
|
|
if (file_put_contents($filePath, $template)) {
|
|
$output->writeln("<info>Modelo '{$name}' criado com sucesso em modules/{$module}/classes/Models/</info>");
|
|
return Command::SUCCESS;
|
|
} else {
|
|
$output->writeln("<error>Falha ao criar o arquivo do modelo.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
}
|
|
}
|