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

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 Doctrine\Common\Annotations\Annotation;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Bundle\MakerBundle\ConsoleStyle;
  14. use Symfony\Bundle\MakerBundle\DependencyBuilder;
  15. use Symfony\Bundle\MakerBundle\Generator;
  16. use Symfony\Bundle\MakerBundle\InputConfiguration;
  17. use Symfony\Bundle\MakerBundle\Str;
  18. use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
  19. use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
  20. use Symfony\Bundle\TwigBundle\TwigBundle;
  21. use Symfony\Component\Console\Command\Command;
  22. use Symfony\Component\Console\Input\InputArgument;
  23. use Symfony\Component\Console\Input\InputInterface;
  24. use Symfony\Component\Console\Input\InputOption;
  25. use Symfony\Component\HttpFoundation\JsonResponse;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\HttpKernel\Kernel;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. /**
  30. * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  31. * @author Ryan Weaver <weaverryan@gmail.com>
  32. */
  33. final class MakeController extends AbstractMaker
  34. {
  35. private $phpCompatUtil;
  36. public function __construct(PhpCompatUtil $phpCompatUtil = null)
  37. {
  38. if (null === $phpCompatUtil) {
  39. @trigger_error(sprintf('Passing a "%s" instance is mandatory since version 1.42.0', PhpCompatUtil::class), \E_USER_DEPRECATED);
  40. }
  41. $this->phpCompatUtil = $phpCompatUtil;
  42. }
  43. public static function getCommandName(): string
  44. {
  45. return 'make:controller';
  46. }
  47. public static function getCommandDescription(): string
  48. {
  49. return 'Creates a new controller class';
  50. }
  51. public function configureCommand(Command $command, InputConfiguration $inputConfig): void
  52. {
  53. $command
  54. ->addArgument('controller-class', InputArgument::OPTIONAL, sprintf('Choose a name for your controller class (e.g. <fg=yellow>%sController</>)', Str::asClassName(Str::getRandomTerm())))
  55. ->addOption('no-template', null, InputOption::VALUE_NONE, 'Use this option to disable template generation')
  56. ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))
  57. ;
  58. }
  59. public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
  60. {
  61. $controllerClassNameDetails = $generator->createClassNameDetails(
  62. $input->getArgument('controller-class'),
  63. 'Controller\\',
  64. 'Controller'
  65. );
  66. $withTemplate = $this->isTwigInstalled() && !$input->getOption('no-template');
  67. $useStatements = new UseStatementGenerator([
  68. AbstractController::class,
  69. $withTemplate ? Response::class : JsonResponse::class,
  70. Route::class,
  71. ]);
  72. $templateName = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()).'/index.html.twig';
  73. $controllerPath = $generator->generateController(
  74. $controllerClassNameDetails->getFullName(),
  75. 'controller/Controller.tpl.php',
  76. [
  77. 'use_statements' => $useStatements,
  78. 'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
  79. 'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
  80. 'with_template' => $withTemplate,
  81. 'template_name' => $templateName,
  82. ]
  83. );
  84. if ($withTemplate) {
  85. $generator->generateTemplate(
  86. $templateName,
  87. 'controller/twig_template.tpl.php',
  88. [
  89. 'controller_path' => $controllerPath,
  90. 'root_directory' => $generator->getRootDirectory(),
  91. 'class_name' => $controllerClassNameDetails->getShortName(),
  92. ]
  93. );
  94. }
  95. $generator->writeChanges();
  96. $this->writeSuccessMessage($io);
  97. $io->text('Next: Open your new controller class and add some pages!');
  98. }
  99. public function configureDependencies(DependencyBuilder $dependencies): void
  100. {
  101. // @legacy - Remove method when support for Symfony 5.4 is dropped
  102. if (null !== $this->phpCompatUtil && 60000 <= Kernel::VERSION_ID && $this->phpCompatUtil->canUseAttributes()) {
  103. return;
  104. }
  105. $dependencies->addClassDependency(
  106. Annotation::class,
  107. 'doctrine/annotations'
  108. );
  109. }
  110. private function isTwigInstalled(): bool
  111. {
  112. return class_exists(TwigBundle::class);
  113. }
  114. }