62 lines
2.2 KiB
PHP
Executable File
62 lines
2.2 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;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
class MakeViewCommand extends Command
|
|
{
|
|
protected static $defaultName = 'make:view';
|
|
protected static $defaultDescription = 'Cria um novo template Twig';
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this
|
|
->addArgument('name', InputArgument::REQUIRED, 'O nome do template (ex: users/index)')
|
|
->addOption('module', 'm', InputOption::VALUE_OPTIONAL, 'O módulo onde o template será criado');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$name = $input->getArgument('name');
|
|
$module = $input->getOption('module');
|
|
|
|
// Garante a extensão .twig
|
|
if (!str_ends_with($name, '.twig')) {
|
|
$name .= '.twig';
|
|
}
|
|
|
|
if ($module) {
|
|
$directory = dirname(__DIR__, 3) . "/modules/{$module}/views";
|
|
} else {
|
|
$directory = dirname(__DIR__, 3) . "/templates";
|
|
}
|
|
|
|
$filePath = $directory . '/' . $name;
|
|
$fileDir = dirname($filePath);
|
|
|
|
if (file_exists($filePath)) {
|
|
$output->writeln("<error>O template '{$name}' já existe!</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
if (!is_dir($fileDir)) {
|
|
mkdir($fileDir, 0755, true);
|
|
}
|
|
|
|
$template = "{% extends \"layouts/base.twig\" %}\n\n{% block content %}\n<div class=\"container mt-4\">\n <h1>" . ucfirst(basename($name, '.twig')) . "</h1>\n <p>Novo template criado via CLI.</p>\n</div>\n{% endblock %}\n";
|
|
|
|
if (file_put_contents($filePath, $template)) {
|
|
$output->writeln("<info>Template '{$name}' criado com sucesso em " . ($module ? "modules/{$module}/views/" : "templates/") . "</info>");
|
|
return Command::SUCCESS;
|
|
} else {
|
|
$output->writeln("<error>Falha ao criar o arquivo do template.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
}
|
|
}
|