Add configuration processing for the mapping setter

This commit is contained in:
ornicar 2011-04-15 12:18:36 -07:00
parent 60e20bf59b
commit 7cb9d9ee71
3 changed files with 48 additions and 1 deletions

View file

@ -82,11 +82,34 @@ class Configuration
$builder = new TreeBuilder();
$node = $builder->root('types');
$node
->useAttributeAsKey('name')
->prototype('array')
->treatNullLike(array())
->append($this->getMappingsNode())
->end()
;
return $node;
}
/**
* Returns the array node used for "mappings".
*/
protected function getMappingsNode()
{
$builder = new TreeBuilder();
$node = $builder->root('mappings');
$node
->useAttributeAsKey('name')
->prototype('array')
->treatNullLike(array())
->children()
->scalarNode('type')->end()
->scalarNode('boost')->end()
->scalarNode('store')->end()
->scalarNode('index')->end()
->end()
->end()
;

View file

@ -13,6 +13,8 @@ use InvalidArgumentException;
class FOQElasticaExtension extends Extension
{
protected $typeMappings = array();
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
@ -40,6 +42,7 @@ class FOQElasticaExtension extends Extension
}, $indexIdsByName);
$this->loadIndexManager($indexDefsByName, $container->getDefinition($indexIdsByName[$config['default_index']]), $container);
$this->loadMappingSetter($this->typeMappings, $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']));
@ -110,11 +113,18 @@ class FOQElasticaExtension extends Extension
protected function loadTypes(array $types, ContainerBuilder $container, $indexId)
{
foreach ($types as $name => $type) {
$typeId = sprintf('%s.%s', $indexId, $name);
$typeDefArgs = array($name);
$typeDef = new Definition('%foq_elastica.type.class%', $typeDefArgs);
$typeDef->setFactoryService($indexId);
$typeDef->setFactoryMethod('getType');
$container->setDefinition(sprintf('%s.%s', $indexId, $name), $typeDef);
$container->setDefinition($typeId, $typeDef);
if (isset($type['mappings'])) {
$this->typeMappings[] = array(
new Reference($typeId),
$type['mappings']
);
}
}
}
@ -129,4 +139,15 @@ class FOQElasticaExtension extends Extension
$managerDef->setArgument(0, $indexDefs);
$managerDef->setArgument(1, new Reference('foq_elastica.index'));
}
/**
* Loads the mapping setter
*
* @return null
**/
public function loadMappingSetter(array $mappings, ContainerBuilder $container)
{
$managerDef = $container->getDefinition('foq_elastica.mapping_setter');
$managerDef->setArgument(0, $mappings);
}
}

View file

@ -22,6 +22,9 @@
<service id="foq_elastica.reseter" class="FOQ\ElasticaBundle\Reseter">
<argument type="service" id="foq_elastica.index_manager" />
</service>
<service id="foq_elastica.mapping_setter" class="FOQ\ElasticaBundle\MappingSetter">
<argument /> <!-- mappings -->
</service>
</services>
</container>