Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

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

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