Enable PopulateCommand to target indexes and types
This makes the Populator service obsolete.
This commit is contained in:
parent
2046748516
commit
4a80e27283
|
@ -15,33 +15,115 @@ use Symfony\Component\Console\Output\Output;
|
||||||
class PopulateCommand extends ContainerAwareCommand
|
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()
|
protected function configure()
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
->setName('foq:elastica:populate')
|
->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)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$output->writeln('Resetting indexes');
|
$index = $input->getOption('index');
|
||||||
$this->getContainer()->get('foq_elastica.resetter')->reset();
|
$type = $input->getOption('type');
|
||||||
|
|
||||||
$output->writeln('Populating indexes');
|
if (null === $index && null !== $type) {
|
||||||
$this->getContainer()->get('foq_elastica.populator')->populate(function($text) use ($output) {
|
throw new \InvalidArgumentException('Cannot specify type option without an index.');
|
||||||
$output->writeLn($text);
|
}
|
||||||
});
|
|
||||||
|
|
||||||
$output->writeln('Refreshing indexes');
|
if (null !== $index) {
|
||||||
array_map(function($index) {
|
if (null !== $type) {
|
||||||
$index->refresh();
|
$this->populateIndexType($output, $index, $type);
|
||||||
}, $this->getContainer()->get('foq_elastica.index_manager')->getAllIndexes());
|
} 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace FOQ\ElasticaBundle;
|
|
||||||
|
|
||||||
use FOQ\ElasticaBundle\Provider\ProviderInterface;
|
|
||||||
use Closure;
|
|
||||||
|
|
||||||
class Populator
|
|
||||||
{
|
|
||||||
protected $providers;
|
|
||||||
|
|
||||||
public function __construct(array $providers)
|
|
||||||
{
|
|
||||||
foreach ($providers as $name => $provider) {
|
|
||||||
$this->addProvider($name, $provider);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addProvider($name, ProviderInterface $provider)
|
|
||||||
{
|
|
||||||
$this->providers[$name] = $provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function populate(Closure $loggerClosure)
|
|
||||||
{
|
|
||||||
foreach ($this->providers as $name => $provider) {
|
|
||||||
$provider->populate(function($text) use ($name, $loggerClosure) {
|
|
||||||
$loggerClosure(sprintf('Indexing %s, %s', $name, $text));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -38,10 +38,6 @@
|
||||||
<argument /> <!-- default index -->
|
<argument /> <!-- default index -->
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="foq_elastica.populator" class="FOQ\ElasticaBundle\Populator">
|
|
||||||
<argument /> <!-- providers -->
|
|
||||||
</service>
|
|
||||||
|
|
||||||
<service id="foq_elastica.resetter" class="FOQ\ElasticaBundle\Resetter">
|
<service id="foq_elastica.resetter" class="FOQ\ElasticaBundle\Resetter">
|
||||||
<argument /> <!-- index configs -->
|
<argument /> <!-- index configs -->
|
||||||
</service>
|
</service>
|
||||||
|
|
|
@ -1,60 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace FOQ\ElasticaBundle\Tests\Populator;
|
|
||||||
|
|
||||||
use FOQ\ElasticaBundle\Populator;
|
|
||||||
use FOQ\ElasticaBundle\Provider\ProviderInterface;
|
|
||||||
use Closure;
|
|
||||||
|
|
||||||
class PopulatorMock extends Populator
|
|
||||||
{
|
|
||||||
public $providers = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
class PopulatorTest extends \PHPUnit_Framework_TestCase
|
|
||||||
{
|
|
||||||
public function testThatWeCanAddProvider()
|
|
||||||
{
|
|
||||||
$provider = $this->getMock('FOQ\ElasticaBundle\Provider\ProviderInterface', array('populate'));
|
|
||||||
|
|
||||||
$populator = new PopulatorMock(array());
|
|
||||||
$populator->addProvider('l3l0Provider', $provider);
|
|
||||||
|
|
||||||
$this->assertEquals(count($populator->providers), 1);
|
|
||||||
$this->assertArrayHasKey('l3l0Provider', $populator->providers);
|
|
||||||
$this->assertInstanceOf('FOQ\ElasticaBundle\Provider\ProviderInterface', $populator->providers['l3l0Provider']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testThatPopulateThroughProviders()
|
|
||||||
{
|
|
||||||
$provider = $this->getMock('FOQ\ElasticaBundle\Provider\ProviderInterface', array('populate'));
|
|
||||||
$provider->expects($this->once())
|
|
||||||
->method('populate');
|
|
||||||
|
|
||||||
$provider2 = $this->getMock('FOQ\ElasticaBundle\Provider\ProviderInterface', array('populate'));
|
|
||||||
$provider2->expects($this->once())
|
|
||||||
->method('populate');
|
|
||||||
|
|
||||||
$populator = new Populator(array('l3l0Provider' => $provider, 'secondProvider' => $provider2));
|
|
||||||
$populator->populate(function ($text) { return $text; });
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException PHPUnit_Framework_Error
|
|
||||||
*/
|
|
||||||
public function testThatAddProviderHaveToImpelementProviderInterface()
|
|
||||||
{
|
|
||||||
$populator = new Populator(array());
|
|
||||||
$populator->addProvider('provider', new \stdClass());
|
|
||||||
$populator->populate(function ($text) { return $text; });
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException PHPUnit_Framework_Error
|
|
||||||
*/
|
|
||||||
public function testThatProvidersPassToTheContructorHaveToImplementProviderInterface()
|
|
||||||
{
|
|
||||||
$populator = new Populator(array('provider' => new \stdClass()));
|
|
||||||
$populator->populate(function ($text) { return $text; });
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue