Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CronInstallCommand.php
Go to the documentation of this file.
1 <?php
7 
11 use Symfony\Component\Console\Command\Command;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Console\Input\InputOption;
16 
20 class CronInstallCommand extends Command
21 {
25  private $crontabManager;
26 
30  private $tasksProvider;
31 
36  public function __construct(
37  CrontabManagerInterface $crontabManager,
38  TasksProviderInterface $tasksProvider
39  ) {
40  $this->crontabManager = $crontabManager;
41  $this->tasksProvider = $tasksProvider;
42 
43  parent::__construct();
44  }
45 
49  protected function configure()
50  {
51  $this->setName('cron:install')
52  ->setDescription('Generates and installs crontab for current user')
53  ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force install tasks');
54 
55  parent::configure();
56  }
57 
61  protected function execute(InputInterface $input, OutputInterface $output)
62  {
63  if ($this->crontabManager->getTasks() && !$input->getOption('force')) {
64  $output->writeln('<error>Crontab has already been generated and saved</error>');
65  return Cli::RETURN_FAILURE;
66  }
67 
68  try {
69  $this->crontabManager->saveTasks($this->tasksProvider->getTasks());
70  } catch (LocalizedException $e) {
71  $output->writeln('<error>' . $e->getMessage() . '</error>');
72  return Cli::RETURN_FAILURE;
73  }
74 
75  $output->writeln('<info>Crontab has been generated and saved</info>');
76 
77  return Cli::RETURN_SUCCESS;
78  }
79 }
__construct(CrontabManagerInterface $crontabManager, TasksProviderInterface $tasksProvider)
execute(InputInterface $input, OutputInterface $output)