Introduce the Index manager and setup dependency injection

This commit is contained in:
ornicar 2011-04-10 14:08:51 -07:00
parent f20ee555ea
commit 0b4839e718
9 changed files with 241 additions and 41 deletions

View file

@ -0,0 +1,33 @@
<?php
namespace FOQ\ElasticaBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\Command as BaseCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
class CreateIndexesCommand extends BaseCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->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) {
}
}
}

View file

@ -1,16 +1,10 @@
<?php
namespace FOQ\ElasticsearchBundle\DependencyInjection;
namespace FOQ\ElasticaBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
/**
* This class contains the configuration information for the bundle
*
* This information is solely responsible for how the different configuration
* sections are normalized, and merged.
*/
class Configuration
{
/**
@ -21,8 +15,60 @@ class Configuration
public function getConfigTree()
{
$treeBuilder = new TreeBuilder();
$treeBuilder->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()
;
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace FOQ\ElasticaBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Processor;
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\Config\FileLocator;
class FOQElasticaExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->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);
}
}

View file

@ -1,23 +0,0 @@
<?php
namespace FOQ\ElasticsearchBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
class FOQElasticsearchExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('config.xml');
$configuration = new Configuration();
$processor = new Processor();
$config = $processor->process($configuration->getConfigTree(), $configs);
}
}

9
FOQElasticaBundle.php Normal file
View file

@ -0,0 +1,9 @@
<?php
namespace FOQ\ElasticaBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class FOQElasticaBundle extends Bundle
{
}

View file

@ -1,9 +0,0 @@
<?php
namespace FOQ\ElasticsearchBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class FOQElasticsearchBundle extends Bundle
{
}

35
IndexManager.php Normal file
View file

@ -0,0 +1,35 @@
<?php
namespace FOQ\ElasticaBundle;
class IndexManager
{
protected $indexes;
public function __construct(array $indexes)
{
$this->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();
}
}
}

View file

@ -22,7 +22,7 @@ With clone:
$loader->registerPrefixes(array(
...
'elastica' => __DIR__.'/../vendor/elastica/lib',
'Elastica' => __DIR__.'/../vendor/elastica/lib',
));
### Install ElasticaBundle

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="foq_elastica.client.class">Elastica_Client</parameter>
<parameter key="foq_elastica.index.class">Elastica_Index</parameter>
<parameter key="foq_elastica.index_manager.class">FOQ\ElasticaBundle\IndexManager</parameter>
</parameters>
</container>