Files
dockerfluxodecaixapro/app/Console/Commands/MakeMiddlewareCommand.php
T
2026-09-01 10:46:29 -03:00

95 lines
2.4 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 MakeMiddlewareCommand extends Command
{
protected static $defaultName = 'make:middleware';
protected static $defaultDescription = 'Cria um novo middleware na aplicação';
protected function configure(): void
{
$this
->addArgument('name', InputArgument::REQUIRED, 'O nome da classe do middleware (ex: CheckAgeMiddleware)');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$name = $input->getArgument('name');
// Garante que o nome termine com Middleware para padronização
if (!str_ends_with($name, 'Middleware')) {
$name .= 'Middleware';
}
$directory = dirname(__DIR__, 2) . '/Middlewares';
$filePath = $directory . '/' . $name . '.php';
if (file_exists($filePath)) {
$output->writeln("<error>O middleware '{$name}' já existe!</error>");
return Command::FAILURE;
}
if (!is_dir($directory)) {
mkdir($directory, 0755, true);
}
$template = $this->getTemplate($name);
if (file_put_contents($filePath, $template)) {
$output->writeln("<info>Middleware '{$name}' criado com sucesso em app/Middlewares/</info>");
return Command::SUCCESS;
} else {
$output->writeln("<error>Falha ao criar o arquivo do middleware.</error>");
return Command::FAILURE;
}
}
protected function getTemplate(string $className): string
{
$year = date('Y');
return <<<PHP
<?php
/**
* SysCMS - Content Management System
*
* Copyright (c) {$year} Marco Costa
* All rights reserved.
*
* Author: Marco Costa
* Email: contato@syspanel.com.br
* Project: SysCMS
*/
namespace App\Middlewares;
use Closure;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class {$className}
{
/**
* Handle an incoming request.
*
* @param Request \$request
* @param Closure \$next
* @return mixed
*/
public function handle(Request \$request, Closure \$next)
{
// Lógica do middleware aqui
return \$next(\$request);
}
}
PHP;
}
}