vendor/symfony/maker-bundle/src/Maker/MakeController.php line 39

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony MakerBundle package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\MakerBundle\Maker;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Bundle\MakerBundle\ConsoleStyle;
  13. use Symfony\Bundle\MakerBundle\DependencyBuilder;
  14. use Symfony\Bundle\MakerBundle\Generator;
  15. use Symfony\Bundle\MakerBundle\InputConfiguration;
  16. use Symfony\Bundle\MakerBundle\Str;
  17. use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
  18. use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
  19. use Symfony\Bundle\TwigBundle\TwigBundle;
  20. use Symfony\Component\Console\Command\Command;
  21. use Symfony\Component\Console\Input\InputArgument;
  22. use Symfony\Component\Console\Input\InputInterface;
  23. use Symfony\Component\Console\Input\InputOption;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. /**
  28. * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  29. * @author Ryan Weaver <weaverryan@gmail.com>
  30. */
  31. final class MakeController extends AbstractMaker
  32. {
  33. private PhpCompatUtil $phpCompatUtil;
  34. public function __construct(PhpCompatUtil $phpCompatUtil = null)
  35. {
  36. if (null === $phpCompatUtil) {
  37. @trigger_error(sprintf('Passing a "%s" instance is mandatory since version 1.42.0', PhpCompatUtil::class), \E_USER_DEPRECATED);
  38. }
  39. $this->phpCompatUtil = $phpCompatUtil;
  40. }
  41. public static function getCommandName(): string
  42. {
  43. return 'make:controller';
  44. }
  45. public static function getCommandDescription(): string
  46. {
  47. return 'Creates a new controller class';
  48. }
  49. public function configureCommand(Command $command, InputConfiguration $inputConfig): void
  50. {
  51. $command
  52. ->addArgument('controller-class', InputArgument::OPTIONAL, sprintf('Choose a name for your controller class (e.g. <fg=yellow>%sController</>)', Str::asClassName(Str::getRandomTerm())))
  53. ->addOption('no-template', null, InputOption::VALUE_NONE, 'Use this option to disable template generation')
  54. ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))
  55. ;
  56. }
  57. public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
  58. {
  59. $controllerClassNameDetails = $generator->createClassNameDetails(
  60. $input->getArgument('controller-class'),
  61. 'Controller\\',
  62. 'Controller'
  63. );
  64. $withTemplate = $this->isTwigInstalled() && !$input->getOption('no-template');
  65. $useStatements = new UseStatementGenerator([
  66. AbstractController::class,
  67. $withTemplate ? Response::class : JsonResponse::class,
  68. Route::class,
  69. ]);
  70. $templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()).'/index.html.twig';
  71. $controllerPath = $generator->generateController(
  72. $controllerClassNameDetails->getFullName(),
  73. 'controller/Controller.tpl.php',
  74. [
  75. 'use_statements' => $useStatements,
  76. 'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
  77. 'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
  78. 'with_template' => $withTemplate,
  79. 'template_name' => $templateName,
  80. ]
  81. );
  82. if ($withTemplate) {
  83. $generator->generateTemplate(
  84. $templateName,
  85. 'controller/twig_template.tpl.php',
  86. [
  87. 'controller_path' => $controllerPath,
  88. 'root_directory' => $generator->getRootDirectory(),
  89. 'class_name' => $controllerClassNameDetails->getShortName(),
  90. ]
  91. );
  92. }
  93. $generator->writeChanges();
  94. $this->writeSuccessMessage($io);
  95. $io->text('Next: Open your new controller class and add some pages!');
  96. }
  97. public function configureDependencies(DependencyBuilder $dependencies): void
  98. {
  99. }
  100. private function isTwigInstalled(): bool
  101. {
  102. return class_exists(TwigBundle::class);
  103. }
  104. }