webman/app/command/CreateControllerCommand.php

45 lines
1.5 KiB
PHP

<?php
namespace app\command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CreateControllerCommand extends Command
{
protected static $defaultName = 'make:apicontroller';
protected function configure()
{
$this->setDescription('Create a new controller')
->setHelp('This command allows you to create a new controller...');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// 获取控制器名称
$controllerName = $input->getArgument('name');
if (!$controllerName) {
$output->writeln('<error>Controller name is required</error>');
return Command::FAILURE;
}
// 创建控制器文件
$controllerFile = __DIR__ . "/../../app/Controllers/api/{$controllerName}Controller.php";
if (file_exists($controllerFile)) {
$output->writeln("<error>Controller {$controllerName} already exists!</error>");
return Command::FAILURE;
}
// 控制器的基本代码模板
$controllerTemplate = "<?php\n\nnamespace App\Controllers;\n\nclass {$controllerName}Controller\n{\n // Your controller code here\n}\n";
// 将控制器内容写入文件
file_put_contents($controllerFile, $controllerTemplate);
$output->writeln("<info>Controller {$controllerName} created successfully!</info>");
return Command::SUCCESS;
}
}