![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Soon/DataSync/Console/ |
<?php /** * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @author Hervé Guétin <[email protected]> <@herveguetin> * @copyright Copyright (c) 2017 Agence Soon (http://www.agence-soon.fr) */ namespace Soon\DataSync\Console; use Magento\Framework\App\Area; use Magento\Framework\App\State; use Magento\Framework\Console\Cli; use Soon\DataSync\Model\JobStackInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class RunCommand extends Command { const INPUT_KEY_ALL = 'all'; const INPUT_KEY_JOB = 'job'; /** * @var JobStackInterface */ private $jobStack; /** * @var bool */ private $hasErrors; /** * @var InputInterface */ private $input; /** * @var OutputInterface */ private $output; /** * @var array */ private $jobs; /** * @var State */ private $state; /** * @var */ private $options = []; public function __construct( JobStackInterface $jobStack, State $state, $name = null ) { parent::__construct($name); $this->jobStack = $jobStack; $this->state = $state; } protected function configure() { $this->setName('soon_datasync:run')->setDescription('Run DataSync jobs'); $this->addArgument( self::INPUT_KEY_JOB, InputArgument::OPTIONAL, 'Code of the job' ); $this->addOption( self::INPUT_KEY_ALL, 'a', InputOption::VALUE_NONE, 'Run all registered jobs' ); $this->addOption( 'options', null, InputOption::VALUE_OPTIONAL, 'Options' ); } /** * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { try { $this->state->setAreaCode(Area::AREA_ADMINHTML); } catch (\Exception $e) { // Sometimes an exception saying "Area code already set may me thrown" // This is not critical so let's just output the exception for information. $output->writeln('<comment>An exception was thrown during area code setting:</comment>'); $output->writeln($e->getMessage()); } $this->input = $input; $this->output = $output; $this->prepareJobs(); $this->parseOptions(); if (is_null($this->jobs)) { $output->writeln('<error>No job specified. Specify a job or use the --all option</error>'); return Cli::RETURN_FAILURE; } $this->addSingleJob(); $this->processQueue(); return $this->finish(); } private function prepareJobs() { if ($this->input->getOption(self::INPUT_KEY_ALL)) { $this->jobs = $this->jobStack->addAll($this->options); } else { $this->jobs = $this->input->getArgument(self::INPUT_KEY_JOB); } } private function parseOptions() { if($this->input->getOption('options')) { array_map(function ($inputOption) use (&$options) { list($k, $v) = explode(':', $inputOption); $this->options[$k] = $v; }, explode(',', $this->input->getOption('options'))); } } private function addSingleJob() { if ($this->input->getArgument(self::INPUT_KEY_JOB) !== null) { try { $this->jobStack->add($this->jobs, $this->options); } catch (\Exception $e) { $this->output->writeln(sprintf('<error>%s failed: %s</error>', $this->jobs, $e->getMessage())); $this->hasErrors = true; } } } private function processQueue() { array_map(function ($job) { try { $this->jobStack->run($job['code']); $this->output->writeln(sprintf('%s successful', $job['code'])); } catch (\Exception $e) { $this->output->writeln(sprintf('<error>%s failed: %s</error>', $job['code'], $e->getMessage())); $this->hasErrors = true; } }, $this->jobStack->queue()); } /** * @return int */ private function finish() { if ($this->hasErrors) { $this->output->writeln('<fg=red>DataSync completed with errors.</>'); return Cli::RETURN_FAILURE; } $this->output->writeln('<info>DataSync completed with no error.</info>'); return Cli::RETURN_SUCCESS; } }