vendor/easycorp/easyadmin-bundle/src/Command/MakeCrudControllerCommand.php line 27

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Command;
  3. use Doctrine\Persistence\ManagerRegistry;
  4. use EasyCorp\Bundle\EasyAdminBundle\Maker\ClassMaker;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Style\SymfonyStyle;
  9. use Symfony\Component\Filesystem\Filesystem;
  10. use function Symfony\Component\String\u;
  11. /**
  12. * Generates the PHP class needed to define a CRUD controller.
  13. *
  14. * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  15. */
  16. class MakeCrudControllerCommand extends Command
  17. {
  18. protected static $defaultName = 'make:admin:crud';
  19. protected static $defaultDescription = 'Creates a new EasyAdmin CRUD controller class';
  20. private $projectDir;
  21. private $classMaker;
  22. private $doctrine;
  23. public function __construct(string $projectDir, ClassMaker $classMaker, ManagerRegistry $doctrine, string $name = null)
  24. {
  25. parent::__construct($name);
  26. $this->projectDir = $projectDir;
  27. $this->classMaker = $classMaker;
  28. $this->doctrine = $doctrine;
  29. }
  30. protected function configure()
  31. {
  32. $this
  33. ->setDescription(self::$defaultDescription)
  34. ->setHelp($this->getCommandHelp())
  35. ;
  36. }
  37. /**
  38. * @return int
  39. */
  40. protected function execute(InputInterface $input, OutputInterface $output)
  41. {
  42. $io = new SymfonyStyle($input, $output);
  43. $fs = new Filesystem();
  44. $doctrineEntitiesFqcn = $this->getAllDoctrineEntitiesFqcn();
  45. if (0 === \count($doctrineEntitiesFqcn)) {
  46. $io->error('This command generates the CRUD controller of an existing Doctrine entity, but no entities were found in your application. Create some Doctrine entities first and then run this command again.');
  47. return 1;
  48. }
  49. $entityFqcn = $io->choice(
  50. 'Which Doctrine entity are you going to manage with this CRUD controller?',
  51. $doctrineEntitiesFqcn
  52. );
  53. $entityClassName = u($entityFqcn)->afterLast('\\')->toString();
  54. $controllerFileNamePattern = sprintf('%s{number}CrudController.php', $entityClassName);
  55. $projectDir = $this->projectDir;
  56. $controllerDir = $io->ask('Which directory do you want to generate the CRUD controller in?', 'src/Controller/Admin/', static function (string $selectedDir) use ($fs, $projectDir) {
  57. $absoluteDir = u($selectedDir)->ensureStart($projectDir.\DIRECTORY_SEPARATOR);
  58. if (!$fs->exists($absoluteDir)) {
  59. throw new \RuntimeException('The given directory does not exist. Type in the path of an existing directory relative to your project root (e.g. src/Controller/Admin/)');
  60. }
  61. return $absoluteDir->after($projectDir.\DIRECTORY_SEPARATOR)->trimEnd(\DIRECTORY_SEPARATOR)->toString();
  62. });
  63. $guessedNamespace = u($controllerDir)->equalsTo('src')
  64. ? 'App'
  65. : u($controllerDir)->replace('/', ' ')->replace('\\', ' ')->replace('src ', 'app ')->title(true)->replace(' ', '\\')->trimEnd(\DIRECTORY_SEPARATOR);
  66. $namespace = $io->ask('Namespace of the generated CRUD controller', $guessedNamespace, static function (string $namespace) {
  67. return u($namespace)->replace('/', '\\')->toString();
  68. });
  69. $generatedFilePath = $this->classMaker->make(
  70. sprintf('%s/%s', $controllerDir, $controllerFileNamePattern),
  71. 'crud_controller.tpl',
  72. ['entity_fqcn' => $entityFqcn, 'entity_class_name' => $entityClassName, 'namespace' => $namespace]
  73. );
  74. $io->success('Your CRUD controller class has been successfully generated.');
  75. $io->text('Next steps:');
  76. $io->listing([
  77. sprintf('Configure your controller at "%s"', $generatedFilePath),
  78. 'Read EasyAdmin docs: https://symfony.com/doc/master/bundles/EasyAdminBundle/index.html',
  79. ]);
  80. return 0;
  81. }
  82. private function getAllDoctrineEntitiesFqcn(): array
  83. {
  84. $entitiesFqcn = [];
  85. foreach ($this->doctrine->getManagers() as $entityManager) {
  86. $classesMetadata = $entityManager->getMetadataFactory()->getAllMetadata();
  87. foreach ($classesMetadata as $classMetadata) {
  88. $entitiesFqcn[] = $classMetadata->getName();
  89. }
  90. }
  91. sort($entitiesFqcn);
  92. return $entitiesFqcn;
  93. }
  94. private function getCommandHelp()
  95. {
  96. return <<<'HELP'
  97. The <info>%command.name%</info> command creates a new EasyAdmin CRUD controler
  98. class to manage some Doctrine entity in your application.
  99. Follow the steps shown by the command to select the Doctrine entity and the
  100. location and namespace of the generated class.
  101. This command never changes or overwrites an existing class, so you can run it
  102. safely as many times as needed to create multiple CRUD controllers.
  103. HELP
  104. ;
  105. }
  106. }