From 0b4839e718fae23b7056d67745aca3af69a6a9c0 Mon Sep 17 00:00:00 2001 From: ornicar Date: Sun, 10 Apr 2011 14:08:51 -0700 Subject: [PATCH] Introduce the Index manager and setup dependency injection --- Command/CreateIndexesCommand.php | 33 +++++++ DependencyInjection/Configuration.php | 62 ++++++++++-- DependencyInjection/FOQElasticaExtension.php | 95 +++++++++++++++++++ .../FOQElasticsearchExtension.php | 23 ----- FOQElasticaBundle.php | 9 ++ FOQElasticsearchBundle.php | 9 -- IndexManager.php | 35 +++++++ README.md | 2 +- Resources/config/config.xml | 14 +++ 9 files changed, 241 insertions(+), 41 deletions(-) create mode 100644 Command/CreateIndexesCommand.php create mode 100644 DependencyInjection/FOQElasticaExtension.php delete mode 100644 DependencyInjection/FOQElasticsearchExtension.php create mode 100644 FOQElasticaBundle.php delete mode 100644 FOQElasticsearchBundle.php create mode 100644 IndexManager.php create mode 100644 Resources/config/config.xml diff --git a/Command/CreateIndexesCommand.php b/Command/CreateIndexesCommand.php new file mode 100644 index 0000000..6614266 --- /dev/null +++ b/Command/CreateIndexesCommand.php @@ -0,0 +1,33 @@ +setName('foq:elastica:reset') + ->setDescription('Recreates all indexes'); + } + + /** + * @see Command + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + foreach ($this->container->get('foq_elastica.index_manager')->getAllIndexes() as $index) { + + } + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 645890e..ce6919a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -1,16 +1,10 @@ root('foq_elasticsearch', 'array'); + $rootNode = $treeBuilder->root('foq_elastica', 'array'); + + $this->addClientsSection($rootNode); + $this->addIndexesSection($rootNode); + + $rootNode + ->children() + ->scalarNode('default_client')->end() + ->end() + ; return $treeBuilder->buildTree(); } + + /** + * Adds the configuration for the "clients" key + */ + private function addClientsSection(ArrayNodeDefinition $rootNode) + { + $rootNode + ->fixXmlConfig('client') + ->children() + ->arrayNode('clients') + ->useAttributeAsKey('id') + ->prototype('array') + ->performNoDeepMerging() + ->children() + ->scalarNode('host')->defaultValue('localhost')->end() + ->scalarNode('port')->defaultValue('9000')->end() + ->end() + ->end() + ->end() + ->end() + ; + } + + /** + * Adds the configuration for the "indexes" key + */ + private function addIndexesSection(ArrayNodeDefinition $rootNode) + { + $rootNode + ->fixXmlConfig('index') + ->children() + ->arrayNode('indexes') + ->useAttributeAsKey('id') + ->prototype('array') + ->performNoDeepMerging() + ->children() + ->scalarNode('client')->end() + ->end() + ->end() + ->end() + ->end() + ; + } } diff --git a/DependencyInjection/FOQElasticaExtension.php b/DependencyInjection/FOQElasticaExtension.php new file mode 100644 index 0000000..95f4bdf --- /dev/null +++ b/DependencyInjection/FOQElasticaExtension.php @@ -0,0 +1,95 @@ +load('config.xml'); + + $configuration = new Configuration(); + $processor = new Processor(); + + $config = $processor->process($configuration->getConfigTree(), $configs); + + if (empty ($config['default_client'])) { + $keys = array_keys($config['clients']); + $config['default_client'] = reset($keys); + } + + $clientsByName = $this->loadClients($config['clients'], $container); + $indexesByName = $this->loadIndexes($config['indexes'], $container, $clientsByName, $config['default_client']); + $this->loadIndexManager($indexesByName, $container); + + $container->setAlias('foq_elastica.client', sprintf('foq_elastica.client.%s', $config['default_client'])); + } + + /** + * Loads the configured clients. + * + * @param array $config An array of clients configurations + * @param ContainerBuilder $container A ContainerBuilder instance + */ + protected function loadClients(array $clients, ContainerBuilder $container) + { + $clientDefs = array(); + foreach ($clients as $name => $client) { + $clientDefArgs = array( + isset($client['host']) ? $client['host'] : null, + isset($client['port']) ? $client['port'] : array(), + ); + $clientDef = new Definition('%foq_elastica.client.class%', $clientDefArgs); + $container->setDefinition(sprintf('foq_elastica.client.%s', $name), $clientDef); + $clientDefs[$name] = $clientDef; + } + + return $clientDefs; + } + + /** + * Loads the configured indexes. + * + * @param array $config An array of indexes configurations + * @param ContainerBuilder $container A ContainerBuilder instance + */ + protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientsByName, $defaultClientName) + { + $indexDefs = array(); + foreach ($indexes as $name => $index) { + if (isset($index['client'])) { + $clientName = $index['client']; + if (!isset($clientsByName[$clientName])) { + throw new InvalidArgumentException(sprintf('The elastica client with name "%s" is not defined', $clientName)); + } + } else { + $clientName = $defaultClientName; + } + $indexDefArgs = array($clientsByName[$clientName], $name); + $indexDef = new Definition('%foq_elastica.index.class%', $indexDefArgs); + $container->setDefinition(sprintf('foq_elastica.index.%s', $name), $indexDef); + $indexDefs[$name] = $indexDef; + } + + return $indexDefs; + } + + /** + * Loads the index manager + * + * @return null + **/ + public function loadIndexManager(array $indexDefs, ContainerBuilder $container) + { + $managerDef = new Definition('%foq_elastica.index_manager.class%', $indexDefs); + $container->setDefinition('fos_elastica.index_manager', $managerDef); + } +} diff --git a/DependencyInjection/FOQElasticsearchExtension.php b/DependencyInjection/FOQElasticsearchExtension.php deleted file mode 100644 index 2e1ad73..0000000 --- a/DependencyInjection/FOQElasticsearchExtension.php +++ /dev/null @@ -1,23 +0,0 @@ -load('config.xml'); - - $configuration = new Configuration(); - $processor = new Processor(); - - $config = $processor->process($configuration->getConfigTree(), $configs); - } -} diff --git a/FOQElasticaBundle.php b/FOQElasticaBundle.php new file mode 100644 index 0000000..db9e926 --- /dev/null +++ b/FOQElasticaBundle.php @@ -0,0 +1,9 @@ +indexes = $indexes; + } + + /** + * Gets all registered indexes + * + * @return array + */ + public function getAllIndexes() + { + return $this->indexes; + } + + /** + * Destroys and creates all registered indexes + * + * @return null + */ + public function createAllIndexes() + { + foreach ($this->getAllIndexes() as $index) { + $index->create(); + } + } +} diff --git a/README.md b/README.md index 99916ca..c1b2c55 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ With clone: $loader->registerPrefixes(array( ... - 'elastica' => __DIR__.'/../vendor/elastica/lib', + 'Elastica' => __DIR__.'/../vendor/elastica/lib', )); ### Install ElasticaBundle diff --git a/Resources/config/config.xml b/Resources/config/config.xml new file mode 100644 index 0000000..db18f5c --- /dev/null +++ b/Resources/config/config.xml @@ -0,0 +1,14 @@ + + + + + + Elastica_Client + Elastica_Index + + FOQ\ElasticaBundle\IndexManager + + +