First Commit
This commit is contained in:
Executable
+129
@@ -0,0 +1,129 @@
|
||||
<?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 MakeModuleCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'make:module';
|
||||
protected static $defaultDescription = 'Cria a estrutura básica de um novo módulo';
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->addArgument('name', InputArgument::REQUIRED, 'O nome do módulo (ex: blog)');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$name = strtolower($input->getArgument('name'));
|
||||
$modulePath = dirname(__DIR__, 3) . '/modules/' . $name;
|
||||
|
||||
if (is_dir($modulePath)) {
|
||||
$output->writeln("<error>O módulo '{$name}' já existe.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Criar estrutura de diretórios
|
||||
$directories = [
|
||||
$modulePath,
|
||||
$modulePath . '/routes',
|
||||
$modulePath . '/views',
|
||||
$modulePath . '/classes',
|
||||
$modulePath . '/classes/Controllers',
|
||||
$modulePath . '/classes/Models',
|
||||
];
|
||||
|
||||
foreach ($directories as $dir) {
|
||||
if (!mkdir($dir, 0755, true)) {
|
||||
$output->writeln("<error>Falha ao criar diretório: {$dir}</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
// Criar module.json
|
||||
$moduleJson = [
|
||||
'name' => ucfirst($name),
|
||||
'version' => '1.0.0',
|
||||
'description' => 'Descrição do módulo ' . ucfirst($name),
|
||||
'author' => 'SysCMS',
|
||||
'enabled' => true
|
||||
];
|
||||
file_put_contents($modulePath . '/module.json', json_encode($moduleJson, JSON_PRETTY_PRINT));
|
||||
|
||||
// Criar rotas web.php
|
||||
$webRoutes = <<<PHP
|
||||
<?php
|
||||
|
||||
/** @var \Slim\App \$app */
|
||||
|
||||
\$app->group('/{$name}', function (\$group) {
|
||||
\$group->get('', [\Modules\\ucfirst($name)\Controllers\\ucfirst($name)Controller::class, 'index']);
|
||||
});
|
||||
PHP;
|
||||
file_put_contents($modulePath . '/routes/web.php', $webRoutes);
|
||||
|
||||
// Criar rotas api.php
|
||||
$apiRoutes = "<?php\n\n/** @var \Slim\App \$app */\n";
|
||||
file_put_contents($modulePath . '/routes/api.php', $apiRoutes);
|
||||
|
||||
// Criar Controlador inicial
|
||||
$controllerName = ucfirst($name) . 'Controller';
|
||||
$namespace = "Modules\\" . ucfirst($name) . "\\Controllers";
|
||||
$controllerTemplate = <<<PHP
|
||||
<?php
|
||||
|
||||
namespace {$namespace};
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Kernel\View;
|
||||
|
||||
/**
|
||||
* Controller for {$name} module
|
||||
*/
|
||||
class {$controllerName} extends BaseController
|
||||
{
|
||||
/**
|
||||
* Index action
|
||||
*
|
||||
* @param Request \$request
|
||||
* @param Response \$response
|
||||
* @return Response
|
||||
*/
|
||||
public function index(Request \$request, Response \$response): Response
|
||||
{
|
||||
return View::render(\$response, '@{$name}/index.twig', [
|
||||
'title' => 'Módulo ' . ucfirst('{$name}')
|
||||
]);
|
||||
}
|
||||
}
|
||||
PHP;
|
||||
file_put_contents($modulePath . '/classes/Controllers/' . $controllerName . '.php', $controllerTemplate);
|
||||
|
||||
// Criar view inicial
|
||||
$viewTemplate = <<<HTML
|
||||
{% extends "layouts/dashboard.twig" %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container-fluid">
|
||||
<h1 class="mt-4">{{ title }}</h1>
|
||||
<div class="card mb-4">
|
||||
<div class="card-body">
|
||||
Este é o módulo {{ title }} gerado automaticamente.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
HTML;
|
||||
file_put_contents($modulePath . '/views/index.twig', $viewTemplate);
|
||||
|
||||
$output->writeln("<info>Módulo '{$name}' criado com sucesso em modules/{$name}</info>");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user