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

Does that generateElasticsearchIndex($index) actually work for you?

Given

<service id="broadway_demo.read_model.repository.people_that_bought_this_product"
         factory-service="broadway.read_model.repository_factory"
         factory-method="create"
         class="Broadway\ReadModel\ReadModel">
    <argument>broadway_demo.people_that_bought_this_product</argument>
    <argument>BroadwayDemo\ReadModel\PeopleThatBoughtThisProductAlsoBought</argument>
</service>

<service id="broadway_demo.read_model.projector.people_that_bought_this_product"
         class="BroadwayDemo\ReadModel\PeopleThatBoughtThisProductAlsoBoughtProjector">
    <argument type="service" id="broadway_demo.read_model.repository.people_that_bought_this_product" />
    <tag name="broadway.domain.event_listener" />
</service>

<service id="read_model.index_generator" class="ElasticSearchIndexGenerator">
    <argument type="collection">
        <argument type="service" id="broadway_demo.read_model.repository.people_that_bought_this_product" />
    </argument>
</service>

the get_class($repository) returns Broadway\ReadModel\ElasticSearch\ElasticSearchRepository not BroadwayDemo\ReadModel\PeopleThatBoughtThisProductAlsoBought so how would that if ($index === get_class($repository)) work?

@wjzijderveld
Copy link
Author

At Qandidate we create specific repository classes that extend the ElasticSearchRepository.
So yes, for us this works 😏
(Sorry, never saw your comment until Luis opened the issue on Broadway).

@mbadolato
Copy link

Makes sense. Thanks!

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