From ec5c05e8becd6342b20dc0499759e290a6a6f0df Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Tue, 17 Jun 2014 23:22:58 +1000 Subject: [PATCH] dev --- Configuration/IndexConfig.php | 2 +- Configuration/Manager.php | 23 ++++---- Configuration/ManagerInterface.php | 9 +++- DependencyInjection/FOSElasticaExtension.php | 19 ++----- Index/IndexManager.php | 6 +-- Index/Resetter.php | 54 +++++++------------ Resources/config/index.xml | 5 +- Tests/Functional/ConfigurationManagerTest.php | 6 ++- 8 files changed, 59 insertions(+), 65 deletions(-) diff --git a/Configuration/IndexConfig.php b/Configuration/IndexConfig.php index 684713e..9add5e3 100644 --- a/Configuration/IndexConfig.php +++ b/Configuration/IndexConfig.php @@ -115,7 +115,7 @@ class IndexConfig /** * @return boolean */ - public function getUseAlias() + public function isUseAlias() { return $this->useAlias; } diff --git a/Configuration/Manager.php b/Configuration/Manager.php index 85afb99..4436986 100644 --- a/Configuration/Manager.php +++ b/Configuration/Manager.php @@ -31,6 +31,20 @@ class Manager implements ManagerInterface } } + public function getIndexConfiguration($indexName) + { + if (!$this->hasIndexConfiguration($indexName)) { + throw new \InvalidArgumentException(sprintf('Index with name "%s" is not configured.', $indexName)); + } + + return $this->indexes[$indexName]; + } + + public function getIndexNames() + { + return array_keys($this->indexes); + } + public function getTypeConfiguration($indexName, $typeName) { $index = $this->getIndexConfiguration($indexName); @@ -43,15 +57,6 @@ class Manager implements ManagerInterface return $type; } - public function getIndexConfiguration($indexName) - { - if (!$this->hasIndexConfiguration($indexName)) { - throw new \InvalidArgumentException(sprintf('Index with name "%s" is not configured.', $indexName)); - } - - return $this->indexes[$indexName]; - } - public function hasIndexConfiguration($indexName) { return isset($this->indexes[$indexName]); diff --git a/Configuration/ManagerInterface.php b/Configuration/ManagerInterface.php index 7852828..96d510f 100644 --- a/Configuration/ManagerInterface.php +++ b/Configuration/ManagerInterface.php @@ -24,6 +24,13 @@ interface ManagerInterface */ public function getIndexConfiguration($index); + /** + * Returns an array of known index names. + * + * @return array + */ + public function getIndexNames(); + /** * Returns a type configuration. * @@ -32,4 +39,4 @@ interface ManagerInterface * @return TypeConfig */ public function getTypeConfiguration($index, $type); -} \ No newline at end of file +} diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index d122218..4d76baf 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -74,7 +74,6 @@ class FOSElasticaExtension extends Extension $container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs); $this->loadIndexManager($container); - $this->loadResetter($container); $this->createDefaultManagerAlias($config['default_manager'], $container); } @@ -549,9 +548,12 @@ class FOSElasticaExtension extends Extension **/ private function loadIndexManager(ContainerBuilder $container) { + $indexRefs = array_map(function ($index) { + return $index['reference']; + }, $this->indexConfigs); + $managerDef = $container->getDefinition('fos_elastica.index_manager'); - $managerDef->replaceArgument(0, array_keys($this->clients)); - $managerDef->replaceArgument(1, new Reference('fos_elastica.index')); + $managerDef->replaceArgument(0, $indexRefs); } /** @@ -609,15 +611,4 @@ class FOSElasticaExtension extends Extension return $this->clients[$clientName]['reference']; } - - /** - * Loads the resetter - * - * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container - */ - private function loadResetter(ContainerBuilder $container) - { - $resetterDef = $container->getDefinition('fos_elastica.resetter'); - $resetterDef->replaceArgument(0, $this->indexConfigs); - } } diff --git a/Index/IndexManager.php b/Index/IndexManager.php index 543ccae..9dd7bfe 100644 --- a/Index/IndexManager.php +++ b/Index/IndexManager.php @@ -2,12 +2,12 @@ namespace FOS\ElasticaBundle\Index; -use Elastica\Index; +use FOS\ElasticaBundle\Elastica\Index; class IndexManager { - protected $indexesByName; - protected $defaultIndexName; + private $indexesByName; + private $defaultIndexName; /** * Constructor. diff --git a/Index/Resetter.php b/Index/Resetter.php index 3356c27..aabad87 100644 --- a/Index/Resetter.php +++ b/Index/Resetter.php @@ -6,22 +6,27 @@ use Elastica\Exception\ExceptionInterface; use Elastica\Index; use Elastica\Exception\ResponseException; use Elastica\Type\Mapping; +use FOS\ElasticaBundle\Configuration\Manager; /** * Deletes and recreates indexes */ class Resetter { - protected $indexConfigsByName; + /*** + * @var \FOS\ElasticaBundle\Configuration\Manager + */ + private $configManager; /** - * Constructor. - * - * @param array $indexConfigsByName + * @var IndexManager */ - public function __construct(array $indexConfigsByName) + private $indexManager; + + public function __construct(Manager $configManager, IndexManager $indexManager) { - $this->indexConfigsByName = $indexConfigsByName; + $this->configManager = $configManager; + $this->indexManager = $indexManager; } /** @@ -29,7 +34,7 @@ class Resetter */ public function resetAllIndexes() { - foreach (array_keys($this->indexConfigsByName) as $name) { + foreach ($this->configManager->getIndexNames() as $name) { $this->resetIndex($name); } } @@ -42,18 +47,17 @@ class Resetter */ public function resetIndex($indexName) { - $indexConfig = $this->getIndexConfig($indexName); - $esIndex = $indexConfig['index']; - if (isset($indexConfig['use_alias']) && $indexConfig['use_alias']) { - $name = $indexConfig['name_or_alias']; - $name .= uniqid(); - $esIndex->overrideName($name); - $esIndex->create($indexConfig['config']); + $indexConfig = $this->configManager->getIndexConfiguration($indexName); + $index = $this->indexManager->getIndex($indexName); - return; + if ($indexConfig->isUseAlias()) { + $name = sprintf('%s_%s', $indexConfig->getElasticSearchName(), uniqid()); + + $index->overrideName($name); } - $esIndex->create($indexConfig['config'], true); + + $index->create($indexConfig['config'], true); } /** @@ -108,24 +112,6 @@ class Resetter return $mapping; } - /** - * Gets an index config by its name - * - * @param string $indexName Index name - * - * @param $indexName - * @return array - * @throws \InvalidArgumentException if no index config exists for the given name - */ - protected function getIndexConfig($indexName) - { - if (!isset($this->indexConfigsByName[$indexName])) { - throw new \InvalidArgumentException(sprintf('The configuration for index "%s" does not exist.', $indexName)); - } - - return $this->indexConfigsByName[$indexName]; - } - public function postPopulate($indexName) { $indexConfig = $this->getIndexConfig($indexName); diff --git a/Resources/config/index.xml b/Resources/config/index.xml index de1a52e..331bfdd 100644 --- a/Resources/config/index.xml +++ b/Resources/config/index.xml @@ -28,11 +28,12 @@ - + - + + diff --git a/Tests/Functional/ConfigurationManagerTest.php b/Tests/Functional/ConfigurationManagerTest.php index 0574481..88bfa8b 100644 --- a/Tests/Functional/ConfigurationManagerTest.php +++ b/Tests/Functional/ConfigurationManagerTest.php @@ -24,7 +24,11 @@ class ConfigurationManagerTest extends WebTestCase $manager = $this->getManager($client); $index = $manager->getIndexConfiguration('index'); - var_dump($index); die; + + $this->assertEquals('index', $index->getName()); + $this->assertCount(2, $index->getTypes()); + $this->assertInstanceOf('FOS\\ElasticaBundle\\Configuration\\TypeConfig', $index->getType('type')); + $this->assertInstanceOf('FOS\\ElasticaBundle\\Configuration\\TypeConfig', $index->getType('parent')); } protected function setUp()