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

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