/** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return int|void */ protected function execute(InputInterface $input, OutputInterface $output) { $this->detectContao($output, true); if ($this->initContao()) { $this->writeSection($output, 'Reindex'); $this->disableObservers(); $indexCode = $input->getArgument('index_code'); $indexerList = $this->getIndexerList(); if ($indexCode === null) { $question = array(); foreach ($indexerList as $key => $indexer) { $question[] = '<comment>' . str_pad('[' . ($key + 1) . ']', 4, ' ', STR_PAD_RIGHT) . '</comment> ' . str_pad($indexer['code'], 40, ' ', STR_PAD_RIGHT) . ' <info>(last runtime: ' . $indexer['last_runtime'] . ')</info>' . "\n"; } $question[] = '<question>Please select a indexer:</question>'; $indexCodes = $this->getHelper('dialog')->askAndValidate($output, $question, function ($typeInput) use($indexerList) { if (strstr($typeInput, ',')) { $typeInputs = \IMI\Util\String::trimExplodeEmpty(',', $typeInput); } else { $typeInputs = array($typeInput); } $returnCodes = array(); foreach ($typeInputs as $typeInput) { if (!isset($indexerList[$typeInput - 1])) { throw new \InvalidArgumentException('Invalid indexer'); } $returnCodes[] = $indexerList[$typeInput - 1]['code']; } return $returnCodes; }); } else { // take cli argument $indexCodes = \IMI\Util\String::trimExplodeEmpty(',', $indexCode); } foreach ($indexCodes as $indexCode) { try { \Mage::dispatchEvent('shell_reindex_init_process'); $process = $this->_getIndexerModel()->getProcessByCode($indexCode); if (!$process) { throw new \InvalidArgumentException('Indexer was not found!'); } $output->writeln('<info>Started reindex of: <comment>' . $indexCode . '</comment></info>'); /** * Try to estimate runtime. If index was aborted or never created we have a timestamp < 0 */ $runtimeInSeconds = $this->getRuntimeInSeconds($process); if ($runtimeInSeconds > 0) { $estimatedEnd = new \DateTime('now', new \DateTimeZone('UTC')); $estimatedEnd->add(new \DateInterval('PT' . $runtimeInSeconds . 'S')); $output->writeln('<info>Estimated end: <comment>' . $estimatedEnd->format('Y-m-d H:i:s T') . '</comment></info>'); } $startTime = new \DateTime('now'); $dateTimeUtils = new \IMI\Util\DateTime(); $process->reindexEverything(); \Mage::dispatchEvent($process->getIndexerCode() . '_shell_reindex_after'); $endTime = new \DateTime('now'); $output->writeln('<info>Successfully reindexed <comment>' . $indexCode . '</comment> (Runtime: <comment>' . $dateTimeUtils->getDifferenceAsString($startTime, $endTime) . '</comment>)</info>'); \Mage::dispatchEvent('shell_reindex_finalize_process'); } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); \Mage::dispatchEvent('shell_reindex_finalize_process'); } } } }
/** * @param OutputInterface $output * @param string $commandString * @throws \RuntimeException * @return void */ protected function registerVariable(OutputInterface $output, $commandString) { if (preg_match('/^(\\$\\{[a-zA-Z0-9-_.]+\\})=(.+)/', $commandString, $matches)) { if (isset($matches[2]) && $matches[2][0] == '?') { // Variable is already defined if (isset($this->scriptVars[$matches[1]])) { return $this->scriptVars[$matches[1]]; } $dialog = $this->getHelperSet()->get('dialog'); /* @var $dialog DialogHelper */ /** * Check for select "?[" */ if (isset($matches[2][1]) && $matches[2][1] == '[') { if (preg_match('/\\[(.+)\\]/', $matches[2], $choiceMatches)) { $choices = String::trimExplodeEmpty(',', $choiceMatches[1]); $selectedIndex = $dialog->select($output, '<info>Please enter a value for <comment>' . $matches[1] . '</comment>:</info> ', $choices); $this->scriptVars[$matches[1]] = $choices[$selectedIndex]; } else { throw new \RuntimeException('Invalid choices'); } } else { // normal input $this->scriptVars[$matches[1]] = $dialog->askAndValidate($output, '<info>Please enter a value for <comment>' . $matches[1] . '</comment>:</info> ', function ($value) { if ($value == '') { throw new \Exception('Please enter a value'); } return $value; }); } } else { $this->scriptVars[$matches[1]] = $this->_replaceScriptVars($matches[2]); } } }
/** * @param \Symfony\Component\Console\Input\InputInterface $input * @return \Symfony\Component\Console\Input\ArgvInput|\Symfony\Component\Console\Input\InputInterface */ protected function checkConfigCommandAlias(InputInterface $input) { if ($this->hasConfigCommandAliases()) { foreach ($this->config['commands']['aliases'] as $alias) { if (is_array($alias)) { $aliasCommandName = key($alias); if ($input->getFirstArgument() == $aliasCommandName) { $aliasCommandParams = array_slice(String::trimExplodeEmpty(' ', $alias[$aliasCommandName]), 1); if (count($aliasCommandParams) > 0) { // replace with aliased data $mergedParams = array_merge(array_slice($_SERVER['argv'], 0, 2), $aliasCommandParams, array_slice($_SERVER['argv'], 2)); $input = new ArgvInput($mergedParams); } } } } return $input; } return $input; }