Skip to content

Instantly share code, notes, and snippets.

@adrienlucas
Created July 24, 2023 08:59
Show Gist options
  • Save adrienlucas/a6c5bfd22efb4b1a89bba3e2b6e0c934 to your computer and use it in GitHub Desktop.
Save adrienlucas/a6c5bfd22efb4b1a89bba3e2b6e0c934 to your computer and use it in GitHub Desktop.
MakerBundle extension to add `make:standalone-controller`
<?= "<?php\n" ?>
namespace <?= $namespace; ?>;
<?= $use_statements; ?>
<?php if ($is_standalone) { ?>
#[AsController()]
<?php } ?>
class <?= $class_name; ?><?= !$is_standalone ? ' extends AbstractController': '' ?>
{
<?php if ($is_standalone && $with_template) { ?>
public function __construct(
private Environment $twig,
) {
}
<?php } ?>
<?= $generator->generateRouteForControllerMethod($route_path, $route_name); ?>
public function <?= $method_name ?>(): <?php if ($with_template) { ?>Response<?php } else { ?>JsonResponse<?php } ?>
{
<?php if ($with_template) { ?>
<?php if ($is_standalone) { ?>
return new Response($this->twig->render('<?= $template_name ?>', [
'controller_name' => '<?= $class_name ?>',
]));
<?php } else { ?>
return $this->render('<?= $template_name ?>', [
'controller_name' => '<?= $class_name ?>',
]);
<?php } ?>
<?php } else { ?>
<?php if ($is_standalone) { ?>
return new JsonResponse([
'message' => 'Welcome to your new controller!',
'path' => '<?= $relative_path; ?>',
]);
<?php } else { ?>
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => '<?= $relative_path; ?>',
]);
<?php } ?>
<?php } ?>
}
}
<?php
declare(strict_types=1);
namespace Adrien\MakerBundleExtensions;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\MakerBundle\Maker\MakeController;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
#[AsTaggedItem('maker.command')]
class MakeStandaloneController extends AbstractMaker
{
public function __construct(
#[Autowire('@maker.maker.make_controller')]
private MakeController $makeController,
private StandaloneControllerGenerator $standaloneControllerGenerator
)
{
}
public static function getCommandName(): string
{
return 'make:standalone-controller';
}
public static function getCommandDescription(): string
{
return 'Creates a new standalone controller class';
}
public function configureCommand(Command $command, InputConfiguration $inputConfig)
{
$this->makeController->configureCommand($command, $inputConfig);
}
public function configureDependencies(DependencyBuilder $dependencies)
{
$this->makeController->configureDependencies($dependencies);
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
{
$this->makeController->generate($input, $io, $this->standaloneControllerGenerator);
}
}
<?php
declare(strict_types=1);
namespace Adrien\MakerBundleExtensions;
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\Util\TemplateComponentGenerator;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
class StandaloneControllerGenerator extends Generator
{
public function __construct(
#[Autowire('@maker.file_manager')]
FileManager $fileManager,
#[Autowire('@maker.template_component_generator')]
TemplateComponentGenerator $templateComponentGenerator = null
) {
parent::__construct($fileManager, 'App', null, $templateComponentGenerator);
}
public function generateController(string $controllerClassName, string $controllerTemplatePath, array $parameters = []): string
{
$parameters['is_standalone'] = true;
$controllerTemplatePath = __DIR__ . '/Controller.tpl.php';
return parent::generateController($controllerClassName, $controllerTemplatePath, $parameters); // TODO: Change the autogenerated stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment