First Commit
This commit is contained in:
+91
@@ -0,0 +1,91 @@
|
||||
<?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 MakeControllerCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'make:controller';
|
||||
protected static $defaultDescription = 'Cria um novo controlador 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 controlador (ex: PostController)');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$module = strtolower($input->getArgument('module'));
|
||||
$name = $input->getArgument('name');
|
||||
|
||||
// Garantir que termina com Controller
|
||||
if (!str_ends_with($name, 'Controller')) {
|
||||
$name .= 'Controller';
|
||||
}
|
||||
|
||||
$modulePath = dirname(__DIR__, 3) . '/modules/' . $module;
|
||||
|
||||
if (!is_dir($modulePath)) {
|
||||
$output->writeln("<error>O módulo '{$module}' não existe.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$controllersPath = $modulePath . '/classes/Controllers';
|
||||
if (!is_dir($controllersPath)) {
|
||||
mkdir($controllersPath, 0755, true);
|
||||
}
|
||||
|
||||
$filePath = $controllersPath . '/' . $name . '.php';
|
||||
|
||||
if (file_exists($filePath)) {
|
||||
$output->writeln("<error>O controlador '{$name}' já existe no módulo '{$module}'.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$namespace = "Modules\\" . ucfirst($module) . "\\Controllers";
|
||||
|
||||
$template = <<<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 {$name}
|
||||
*/
|
||||
class {$name} extends BaseController
|
||||
{
|
||||
/**
|
||||
* Default action
|
||||
*
|
||||
* @param Request \$request
|
||||
* @param Response \$response
|
||||
* @return Response
|
||||
*/
|
||||
public function index(Request \$request, Response \$response): Response
|
||||
{
|
||||
// Sua lógica aqui
|
||||
return \$response;
|
||||
}
|
||||
}
|
||||
PHP;
|
||||
|
||||
if (file_put_contents($filePath, $template)) {
|
||||
$output->writeln("<info>Controlador '{$name}' criado com sucesso em modules/{$module}/classes/Controllers/</info>");
|
||||
return Command::SUCCESS;
|
||||
} else {
|
||||
$output->writeln("<error>Falha ao criar o arquivo do controlador.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user