Enable PopulateCommand to target indexes and types
This makes the Populator service obsolete.
This commit is contained in:
parent
2046748516
commit
4a80e27283
4 changed files with 96 additions and 110 deletions
|
|
@ -15,33 +15,115 @@ use Symfony\Component\Console\Output\Output;
|
|||
class PopulateCommand extends ContainerAwareCommand
|
||||
{
|
||||
/**
|
||||
* @see Command
|
||||
* @var FOQ\ElasticaBundle\IndexManager
|
||||
*/
|
||||
private $indexManager;
|
||||
|
||||
/**
|
||||
* @var FOQ\ElasticaBundle\Provider\ProviderRegistry
|
||||
*/
|
||||
private $providerRegistry;
|
||||
|
||||
/**
|
||||
* @var FOQ\ElasticaBundle\Resetter
|
||||
*/
|
||||
private $resetter;
|
||||
|
||||
/**
|
||||
* @see Symfony\Component\Console\Command\Command::configure()
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('foq:elastica:populate')
|
||||
->setDescription('Populates search indexes from providers');
|
||||
->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to repopulate')
|
||||
->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type to repopulate')
|
||||
->setDescription('Populates search indexes from providers')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @see Symfony\Component\Console\Command\Command::initialize()
|
||||
*/
|
||||
protected function initialize(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$this->indexManager = $this->getContainer()->get('foq_elastica.index_manager');
|
||||
$this->providerRegistry = $this->getContainer()->get('foq_elastica.provider_registry');
|
||||
$this->resetter = $this->getContainer()->get('foq_elastica.resetter');
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Symfony\Component\Console\Command\Command::execute()
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$output->writeln('Resetting indexes');
|
||||
$this->getContainer()->get('foq_elastica.resetter')->reset();
|
||||
$index = $input->getOption('index');
|
||||
$type = $input->getOption('type');
|
||||
|
||||
$output->writeln('Populating indexes');
|
||||
$this->getContainer()->get('foq_elastica.populator')->populate(function($text) use ($output) {
|
||||
$output->writeLn($text);
|
||||
});
|
||||
if (null === $index && null !== $type) {
|
||||
throw new \InvalidArgumentException('Cannot specify type option without an index.');
|
||||
}
|
||||
|
||||
$output->writeln('Refreshing indexes');
|
||||
array_map(function($index) {
|
||||
$index->refresh();
|
||||
}, $this->getContainer()->get('foq_elastica.index_manager')->getAllIndexes());
|
||||
if (null !== $index) {
|
||||
if (null !== $type) {
|
||||
$this->populateIndexType($output, $index, $type);
|
||||
} else {
|
||||
$this->populateIndex($output, $index);
|
||||
}
|
||||
} else {
|
||||
$indexes = array_keys($this->indexManager->getAllIndexes());
|
||||
|
||||
$output->writeln('Done');
|
||||
foreach ($indexes as $index) {
|
||||
$this->populateIndex($output, $index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recreates an index, populates its types, and refreshes the index.
|
||||
*
|
||||
* @param OutputInterface $output
|
||||
* @param string $index
|
||||
*/
|
||||
private function populateIndex(OutputInterface $output, $index)
|
||||
{
|
||||
$output->writeln(sprintf('Resetting: %s', $index));
|
||||
$this->resetter->resetIndex($index);
|
||||
|
||||
$providers = $this->providerRegistry->getIndexProviders($index);
|
||||
|
||||
foreach ($providers as $type => $provider) {
|
||||
$loggerClosure = function($message) use ($output, $index, $type) {
|
||||
$output->writeln(sprintf('Populating: %s/%s, %s', $index, $type, $message));
|
||||
};
|
||||
|
||||
$provider->populate($loggerClosure);
|
||||
}
|
||||
|
||||
$output->writeln(sprintf('Refreshing: %s', $index));
|
||||
$this->indexManager->getIndex($index)->refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes/remaps an index type, populates it, and refreshes the index.
|
||||
*
|
||||
* @param OutputInterface $output
|
||||
* @param string $index
|
||||
* @param string $type
|
||||
*/
|
||||
private function populateIndexType(OutputInterface $output, $index, $type)
|
||||
{
|
||||
$output->writeln(sprintf('Resetting: %s/%s', $index, $type));
|
||||
$this->resetter->resetIndexType($index, $type);
|
||||
|
||||
$loggerClosure = function($message) use ($output, $index, $type) {
|
||||
$output->writeln(sprintf('Populating: %s/%s, %s', $index, $type, $message));
|
||||
};
|
||||
|
||||
$provider = $this->providerRegistry->getProvider($index, $type);
|
||||
$provider->populate($loggerClosure);
|
||||
|
||||
$output->writeln(sprintf('Refreshing: %s', $index));
|
||||
$this->indexManager->getIndex($index)->refresh();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue