75 lines
1.8 KiB
PHP
Executable File
75 lines
1.8 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 MakeServiceCommand extends Command
|
|
{
|
|
protected static $defaultName = 'make:service';
|
|
protected static $defaultDescription = 'Cria uma nova classe de serviço';
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this
|
|
->addArgument('name', InputArgument::REQUIRED, 'O nome da classe de serviço (ex: PaymentService)');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$name = $input->getArgument('name');
|
|
|
|
if (!str_ends_with($name, 'Service')) {
|
|
$name .= 'Service';
|
|
}
|
|
|
|
$directory = dirname(__DIR__, 2) . '/Services';
|
|
$filePath = $directory . '/' . $name . '.php';
|
|
|
|
if (file_exists($filePath)) {
|
|
$output->writeln("<error>O serviço '{$name}' já existe!</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
if (!is_dir($directory)) {
|
|
mkdir($directory, 0755, true);
|
|
}
|
|
|
|
$year = date('Y');
|
|
$template = <<<PHP
|
|
<?php
|
|
|
|
/**
|
|
* SysCMS - Content Management System
|
|
*
|
|
* Copyright (c) {$year} Marco Costa
|
|
* All rights reserved.
|
|
*/
|
|
|
|
namespace App\Services;
|
|
|
|
class {$name}
|
|
{
|
|
/**
|
|
* Service logic here.
|
|
*/
|
|
public function execute()
|
|
{
|
|
// Implement logic
|
|
}
|
|
}
|
|
PHP;
|
|
|
|
if (file_put_contents($filePath, $template)) {
|
|
$output->writeln("<info>Serviço '{$name}' criado com sucesso em app/Services/</info>");
|
|
return Command::SUCCESS;
|
|
} else {
|
|
$output->writeln("<error>Falha ao criar o arquivo do serviço.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
}
|
|
}
|