Skip to content

Instantly share code, notes, and snippets.

@wjzijderveld
Created December 30, 2014 08:14
Show Gist options
  • Save wjzijderveld/dcd9a6478346182b287a to your computer and use it in GitHub Desktop.
Save wjzijderveld/dcd9a6478346182b287a to your computer and use it in GitHub Desktop.
Broadway elasticsearch index generator
<?php
use Broadway\ReadModel\ElasticSearch\ElasticSearchRepository;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CreateElasticsearchIndicesCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('read_model:create_elasticsearch_indices')
->setDescription('Creates the elasticsearch indices')
->setHelp(<<<EOT
The <info>%command.name%</info>command creates the elasticsearch indices for the given environment .
EOT
)
->addArgument('index', InputArgument::OPTIONAL, 'Index to create');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$indexGenerator = $this->getContainer()->get('read_model.index_generator');
if ($input->getArgument('index')) {
$indexGenerator->generateElasticsearchIndex($input->getArgument('index'));
return 0;
}
$indexGenerator->generateElasticsearchIndices();
}
}
<?php
class ElasticSearchIndexGenerator
{
/**
* @param array $repositories
*/
public function __construct(array $repositories)
{
$this->repositories = $repositories;
}
public function generateElasticsearchIndices()
{
foreach ($this->repositories as $repository) {
$repository->createIndex();
}
}
/**
* @param string $index
*/
public function generateElasticsearchIndex($index)
{
foreach ($this->repositories as $repository) {
if ($index === get_class($repository)) {
$repository->createIndex();
return;
}
}
}
}
=
<service id="read_model.index_generator" class="ElasticSearchIndexGenerator">
<argument type="collection">
<argument type="service" id="read_model.repository1" />
<argument type="service" id="read_model.repository2" />
<argument type="service" id="read_model.repository3" />
<!-- .. -->
</argument>
</service>
@mbadolato
Copy link

Makes sense. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment