diff --git a/Command/PopulateCommand.php b/Command/PopulateCommand.php index f5aa442..9f66e05 100644 --- a/Command/PopulateCommand.php +++ b/Command/PopulateCommand.php @@ -31,8 +31,7 @@ class PopulateCommand extends Command { $output->writeln('Populating indexes'); - $populator = $this->container->get('foq_elastica.populator'); - $populator->populate(); + $populator = $this->container->get('foq_elastica.populator')->populate(); $output->writeln('Done'); } diff --git a/DependencyInjection/FOQElasticaExtension.php b/DependencyInjection/FOQElasticaExtension.php index 7ef244e..69b655c 100644 --- a/DependencyInjection/FOQElasticaExtension.php +++ b/DependencyInjection/FOQElasticaExtension.php @@ -7,6 +7,7 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\Config\FileLocator; class FOQElasticaExtension extends Extension @@ -33,7 +34,7 @@ class FOQElasticaExtension extends Extension $clientsByName = $this->loadClients($config['clients'], $container); $indexesByName = $this->loadIndexes($config['indexes'], $container, $clientsByName, $config['default_client']); - $this->loadIndexManager($indexesByName, $container); + $this->loadIndexManager($indexesByName, $config['default_index'], $container); $container->setAlias('foq_elastica.client', sprintf('foq_elastica.client.%s', $config['default_client'])); $container->setAlias('foq_elastica.index', sprintf('foq_elastica.index.%s', $config['default_index'])); @@ -93,9 +94,10 @@ class FOQElasticaExtension extends Extension * * @return null **/ - public function loadIndexManager(array $indexDefs, ContainerBuilder $container) + public function loadIndexManager(array $indexDefs, $defaultIndexId, ContainerBuilder $container) { $managerDef = $container->getDefinition('foq_elastica.index_manager'); $managerDef->setArgument(0, $indexDefs); + $managerDef->setArgument(1, new Reference('foq_elastica.index')); } } diff --git a/IndexManager.php b/IndexManager.php index 62f2cbc..96a6e2c 100644 --- a/IndexManager.php +++ b/IndexManager.php @@ -2,13 +2,17 @@ namespace FOQ\ElasticaBundle; +use InvalidArgumentException; + class IndexManager { protected $indexes; + protected $defaultIndex; - public function __construct(array $indexes) + public function __construct(array $indexes, $defaultIndex) { - $this->indexes = $indexes; + $this->indexes = $indexes; + $this->defaultIndex = $defaultIndex; } /** @@ -22,14 +26,29 @@ class IndexManager } /** - * Destroys and creates all registered indexes + * Gets an index by its name * - * @return null - */ - public function createAllIndexes() + * @return Elastica_Index + **/ + public function getIndex($name) { - foreach ($this->getAllIndexes() as $index) { - $index->create(); + if (!$name) { + return $this->getDefaultIndex(); } + if (!isset($this->indexes[$name])) { + throw new InvalidArgumentException(sprintf('The index "%s" does not exist', $name)); + } + + return $this->indexes[$name]; + } + + /** + * Gets the default index + * + * @return Elastica_Index + **/ + public function getDefaultIndex() + { + return $this->defaultIndex; } }