Created
December 30, 2014 08:14
-
-
Save wjzijderveld/dcd9a6478346182b287a to your computer and use it in GitHub Desktop.
Broadway elasticsearch index generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
} | |
} | |
= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).