vendor/friendsofsymfony/ckeditor-bundle/src/Command/CKEditorInstallerCommand.php line 290

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSCKEditor Bundle.
  4.  *
  5.  * (c) 2018 - present  Friends of Symfony
  6.  * (c) 2009 - 2017     Eric GELOEN <geloen.eric@gmail.com>
  7.  *
  8.  * For the full copyright and license information, please read the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace FOS\CKEditorBundle\Command;
  12. use FOS\CKEditorBundle\Installer\CKEditorInstaller;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Helper\ProgressBar;
  15. use Symfony\Component\Console\Helper\QuestionHelper;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Input\InputOption;
  19. use Symfony\Component\Console\Output\NullOutput;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. use Symfony\Component\Console\Question\ChoiceQuestion;
  22. /**
  23.  * @author GeLo <geloen.eric@gmail.com>
  24.  */
  25. final class CKEditorInstallerCommand extends Command
  26. {
  27.     /**
  28.      * @var CKEditorInstaller
  29.      */
  30.     private $installer;
  31.     public function __construct(CKEditorInstaller $installer)
  32.     {
  33.         parent::__construct();
  34.         $this->installer $installer;
  35.     }
  36.     protected function configure(): void
  37.     {
  38.         $this
  39.             ->setName('ckeditor:install')
  40.             ->setDescription('Install CKEditor')
  41.             ->addArgument('path'InputArgument::OPTIONAL'Where to install CKEditor')
  42.             ->addOption(
  43.                 'release',
  44.                 null,
  45.                 InputOption::VALUE_OPTIONAL,
  46.                 'CKEditor release (basic, standard, full or custom)'
  47.             )
  48.             ->addOption(
  49.                 'custom-build-id',
  50.                 null,
  51.                 InputOption::VALUE_OPTIONAL,
  52.                 'CKEditor custom build ID'
  53.             )
  54.             ->addOption('tag'nullInputOption::VALUE_OPTIONAL'CKEditor tag (x.y.z or latest)')
  55.             ->addOption(
  56.                 'clear',
  57.                 null,
  58.                 InputOption::VALUE_OPTIONAL,
  59.                 'How to clear previous CKEditor installation (drop, keep or skip)'
  60.             )
  61.             ->addOption(
  62.                 'exclude',
  63.                 null,
  64.                 InputOption::VALUE_IS_ARRAY InputOption::VALUE_OPTIONAL,
  65.                 'Path to exclude when extracting CKEditor'
  66.             )
  67.             ->addOption(
  68.                 'no-progress-bar',
  69.                 'nobar',
  70.                 InputOption::VALUE_NONE,
  71.                 'Hide the progress bars?'
  72.             )
  73.             ->setHelp(
  74.                 <<<'EOF'
  75. The <info>%command.name%</info> command install CKEditor in your application:
  76.   <info>php %command.full_name%</info>
  77.   
  78. You can install it at a specific path (absolute):
  79.   <info>php %command.full_name% path</info>
  80.   
  81. You can install a specific release (basic, standard or full):
  82.   <info>php %command.full_name% --release=full</info>
  83.   
  84. You can install a specific version:
  85.   <info>php %command.full_name% --tag=4.7.0</info>
  86. You can install custom build generated on https://ckeditor.com/cke4/builder:
  87.   <info>php %command.full_name% --release=custom --custom-build-id=574a82a0d3e9226d94b0e91d10eaa372</info>
  88. If there is a previous CKEditor installation detected, 
  89. you can control how it should be handled in non-interactive mode:
  90.   <info>php %command.full_name% --clear=drop</info>
  91.   <info>php %command.full_name% --clear=keep</info>
  92.   <info>php %command.full_name% --clear=skip</info>
  93.   
  94. You can exclude path(s) when extracting CKEditor:
  95.   <info>php %command.full_name% --exclude=samples --exclude=adapters</info>
  96. EOF
  97.             );
  98.     }
  99.     protected function execute(InputInterface $inputOutputInterface $output): int
  100.     {
  101.         $this->title($output);
  102.         $success $this->installer->install($this->createOptions($input$output));
  103.         if ($success) {
  104.             $this->success('CKEditor has been successfully installed...'$output);
  105.         } else {
  106.             $this->info('CKEditor installation has been skipped...'$output);
  107.         }
  108.         return 0;
  109.     }
  110.     private function createOptions(InputInterface $inputOutputInterface $output): array
  111.     {
  112.         $options = ['notifier' => $this->createNotifier($input$output)];
  113.         if ($input->hasArgument('path')) {
  114.             $options['path'] = $input->getArgument('path');
  115.         }
  116.         if ($input->hasOption('release')) {
  117.             $options['release'] = $input->getOption('release');
  118.         }
  119.         if ($input->hasOption('custom-build-id')) {
  120.             $options['custom_build_id'] = $input->getOption('custom-build-id');
  121.         }
  122.         if ($input->hasOption('tag')) {
  123.             $options['version'] = $input->getOption('tag');
  124.         }
  125.         if ($input->hasOption('exclude')) {
  126.             $options['excludes'] = $input->getOption('exclude');
  127.         }
  128.         if ($input->hasOption('clear')) {
  129.             $options['clear'] = $input->getOption('clear');
  130.         }
  131.         return array_filter($options);
  132.     }
  133.     private function createNotifier(InputInterface $inputOutputInterface $output): \Closure
  134.     {
  135.         $barOutput $input->getOption('no-progress-bar') ? new NullOutput() : $output;
  136.         $clear = new ProgressBar($barOutput);
  137.         $download = new ProgressBar($barOutput);
  138.         $extract = new ProgressBar($barOutput);
  139.         return function ($type$data) use ($input$output$barOutput$clear$download$extract) {
  140.             switch ($type) {
  141.                 case CKEditorInstaller::NOTIFY_CLEAR:
  142.                     $result $this->choice(
  143.                         [
  144.                             sprintf('CKEditor is already installed in "%s"...'$data),
  145.                             '',
  146.                             'What do you want to do?',
  147.                         ],
  148.                         $choices = [
  149.                             CKEditorInstaller::CLEAR_DROP => 'Drop the directory & reinstall CKEditor',
  150.                             CKEditorInstaller::CLEAR_KEEP => 'Keep the directory & reinstall CKEditor by overriding files',
  151.                             CKEditorInstaller::CLEAR_SKIP => 'Skip installation',
  152.                         ],
  153.                         CKEditorInstaller::CLEAR_DROP,
  154.                         $input,
  155.                         $output
  156.                     );
  157.                     if (false !== ($key array_search($result$choicestrue))) {
  158.                         $result $key;
  159.                     }
  160.                     if (CKEditorInstaller::CLEAR_DROP === $result) {
  161.                         $this->comment(sprintf('Dropping CKEditor from "%s"'$data), $output);
  162.                     }
  163.                     return $result;
  164.                 case CKEditorInstaller::NOTIFY_CLEAR_ARCHIVE:
  165.                     $this->comment(sprintf('Dropping CKEditor ZIP archive "%s"'$data), $output);
  166.                     break;
  167.                 case CKEditorInstaller::NOTIFY_CLEAR_COMPLETE:
  168.                     $this->finishProgressBar($clear$barOutput);
  169.                     break;
  170.                 case CKEditorInstaller::NOTIFY_CLEAR_PROGRESS:
  171.                     $clear->advance();
  172.                     break;
  173.                 case CKEditorInstaller::NOTIFY_CLEAR_SIZE:
  174.                     $clear->start($data);
  175.                     break;
  176.                 case CKEditorInstaller::NOTIFY_DOWNLOAD:
  177.                     $this->comment(sprintf('Downloading CKEditor ZIP archive from "%s"'$data), $output);
  178.                     break;
  179.                 case CKEditorInstaller::NOTIFY_DOWNLOAD_COMPLETE:
  180.                     $this->finishProgressBar($download$barOutput);
  181.                     break;
  182.                 case CKEditorInstaller::NOTIFY_DOWNLOAD_PROGRESS:
  183.                     $download->setProgress($data);
  184.                     break;
  185.                 case CKEditorInstaller::NOTIFY_DOWNLOAD_SIZE:
  186.                     $download->start($data);
  187.                     break;
  188.                 case CKEditorInstaller::NOTIFY_EXTRACT:
  189.                     $this->comment(sprintf('Extracting CKEditor ZIP archive to "%s"'$data), $output);
  190.                     break;
  191.                 case CKEditorInstaller::NOTIFY_EXTRACT_COMPLETE:
  192.                     $this->finishProgressBar($extract$barOutput);
  193.                     break;
  194.                 case CKEditorInstaller::NOTIFY_EXTRACT_PROGRESS:
  195.                     $extract->advance();
  196.                     break;
  197.                 case CKEditorInstaller::NOTIFY_EXTRACT_SIZE:
  198.                     $extract->start($data);
  199.                     break;
  200.             }
  201.         };
  202.     }
  203.     private function title(OutputInterface $output): void
  204.     {
  205.         $output->writeln(
  206.             [
  207.                 '----------------------',
  208.                 '| CKEditor Installer |',
  209.                 '----------------------',
  210.                 '',
  211.             ]
  212.         );
  213.     }
  214.     private function comment(string $messageOutputInterface $output): void
  215.     {
  216.         $output->writeln(' // '.$message);
  217.         $output->writeln('');
  218.     }
  219.     private function success(string $messageOutputInterface $output): void
  220.     {
  221.         $this->block('[OK] - '.$message$output'green''black');
  222.     }
  223.     private function info(string $messageOutputInterface $output): void
  224.     {
  225.         $this->block('[INFO] - '.$message$output'yellow''black');
  226.     }
  227.     private function block(
  228.         string $message,
  229.         OutputInterface $output,
  230.         string $background null,
  231.         string $font null
  232.     ): void {
  233.         $options = [];
  234.         if (null !== $background) {
  235.             $options[] = 'bg='.$background;
  236.         }
  237.         if (null !== $font) {
  238.             $options[] = 'fg='.$font;
  239.         }
  240.         $pattern ' %s ';
  241.         if (!empty($options)) {
  242.             $pattern '<'.implode(';'$options).'>'.$pattern.'</>';
  243.         }
  244.         $output->writeln($block sprintf($patternstr_repeat(' 'strlen($message))));
  245.         $output->writeln(sprintf($pattern$message));
  246.         $output->writeln($block);
  247.     }
  248.     /**
  249.      * @param string[] $question
  250.      * @param string[] $choices
  251.      */
  252.     private function choice(
  253.         array $question,
  254.         array $choices,
  255.         string $default,
  256.         InputInterface $input,
  257.         OutputInterface $output
  258.     ): ?string {
  259.         $helper = new QuestionHelper();
  260.         if (is_array($question)) {
  261.             $question implode("\n"$question);
  262.         }
  263.         $result $helper->ask(
  264.             $input,
  265.             $output,
  266.             new ChoiceQuestion($question$choices$default)
  267.         );
  268.         $output->writeln('');
  269.         return $result;
  270.     }
  271.     private function finishProgressBar(ProgressBar $progressOutputInterface $output): void
  272.     {
  273.         $progress->finish();
  274.         $output->writeln(['''']);
  275.     }
  276. }