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/MakeAdminDashboardCommand.php line 29

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Command;
  3. use EasyCorp\Bundle\EasyAdminBundle\Maker\ClassMaker;
  4. use Symfony\Component\Console\Attribute\AsCommand;
  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 Symfony\Component\String\Slugger\AsciiSlugger;
  11. use function Symfony\Component\String\u;
  12. /**
  13.  * Generates the PHP class needed to define a Dashboard controller.
  14.  *
  15.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  16.  */
  17. #[AsCommand(
  18.     name'make:admin:dashboard',
  19.     description'Creates a new EasyAdmin Dashboard class',
  20. )]
  21. class MakeAdminDashboardCommand extends Command
  22. {
  23.     private ClassMaker $classMaker;
  24.     private string $projectDir;
  25.     public function __construct(ClassMaker $classMakerstring $projectDirstring $name null)
  26.     {
  27.         parent::__construct($name);
  28.         $this->classMaker $classMaker;
  29.         $this->projectDir $projectDir;
  30.     }
  31.     protected function configure()
  32.     {
  33.         $this
  34.             ->setHelp($this->getCommandHelp())
  35.         ;
  36.     }
  37.     protected function execute(InputInterface $inputOutputInterface $output): int
  38.     {
  39.         $io = new SymfonyStyle($input$output);
  40.         $fs = new Filesystem();
  41.         $controllerClassName $io->ask(
  42.             'Which class name do you prefer for your Dashboard controller?',
  43.             'DashboardController',
  44.             fn (string $className): string => u($className)->ensureEnd('Controller')->toString()
  45.         );
  46.         $projectDir $this->projectDir;
  47.         $controllerDir $io->ask(
  48.             sprintf('In which directory of your project do you want to generate "%s"?'$controllerClassName),
  49.             'src/Controller/Admin/',
  50.             static function (string $selectedDir) use ($fs$projectDir) {
  51.                 $absoluteDir u($selectedDir)->ensureStart($projectDir.\DIRECTORY_SEPARATOR);
  52.                 if (null !== $absoluteDir->indexOf('..')) {
  53.                     throw new \RuntimeException(sprintf('The given directory path can\'t contain ".." and must be relative to the project directory (which is "%s")'$projectDir));
  54.                 }
  55.                 $fs->mkdir($absoluteDir);
  56.                 if (!$fs->exists($absoluteDir)) {
  57.                     throw new \RuntimeException('The given directory does not exist and couldn\'t be created. Type in the path of an existing directory relative to your project root (e.g. src/Controller/Admin/)');
  58.                 }
  59.                 return $absoluteDir->after($projectDir.\DIRECTORY_SEPARATOR)->trimEnd(\DIRECTORY_SEPARATOR)->toString();
  60.             }
  61.         );
  62.         $controllerFilePath sprintf('%s/%s.php'u($controllerDir)->ensureStart($projectDir.\DIRECTORY_SEPARATOR), $controllerClassName);
  63.         if ($fs->exists($controllerFilePath)) {
  64.             throw new \RuntimeException(sprintf('The "%s.php" file already exists in the given "%s" directory. Use a different controller name or generate it in a different directory.'$controllerClassName$controllerDir));
  65.         }
  66.         $guessedNamespace u($controllerDir)->equalsTo('src')
  67.             ? 'App'
  68.             u($controllerDir)->replace('/'' ')->replace('\\'' ')->replace('src ''app ')->title(true)->replace(' ''\\')->trimEnd('\\');
  69.         $generatedFilePath $this->classMaker->make(sprintf('%s/%s.php'$controllerDir$controllerClassName), 'dashboard.tpl', [
  70.             'namespace' => $guessedNamespace,
  71.             'site_title' => $this->getSiteTitle($this->projectDir),
  72.         ]);
  73.         $io = new SymfonyStyle($input$output);
  74.         $io->success('Your dashboard class has been successfully generated.');
  75.         $io->text('Next steps:');
  76.         $io->listing([
  77.             sprintf('Configure your Dashboard at "%s"'$generatedFilePath),
  78.             'Run "make:admin:crud" to generate CRUD controllers and link them from the Dashboard.',
  79.         ]);
  80.         return Command::SUCCESS;
  81.     }
  82.     private function getSiteTitle(string $projectDir): string
  83.     {
  84.         $guessedTitle = (new AsciiSlugger())
  85.             ->slug(basename($projectDir), ' ')
  86.             ->title(true)
  87.             ->trim()
  88.             ->toString();
  89.         return '' === $guessedTitle 'EasyAdmin' $guessedTitle;
  90.     }
  91.     private function getCommandHelp(): string
  92.     {
  93.         return <<<'HELP'
  94.             The <info>%command.name%</info> command creates a new EasyAdmin Dashboard class
  95.             in your application. Follow the steps shown by the command to configure the
  96.             name and location of the new class.
  97.             This command never changes or overwrites an existing class, so you can run it
  98.             safely as many times as needed to create multiple dashboards.
  99.             HELP;
  100.     }
  101. }