2011-04-10 23:08:51 +02:00
|
|
|
<?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;
|
2011-04-13 00:15:48 +02:00
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
2011-04-10 23:08:51 +02:00
|
|
|
use Symfony\Component\Config\FileLocator;
|
2011-04-14 00:04:49 +02:00
|
|
|
use InvalidArgumentException;
|
2011-04-10 23:08:51 +02:00
|
|
|
|
|
|
|
class FOQElasticaExtension extends Extension
|
|
|
|
{
|
2011-04-15 21:18:36 +02:00
|
|
|
protected $typeMappings = array();
|
|
|
|
|
2011-04-10 23:08:51 +02:00
|
|
|
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);
|
|
|
|
|
2011-04-17 19:55:46 +02:00
|
|
|
if (empty($config['clients']) || empty($config['indexes'])) {
|
|
|
|
throw new InvalidArgumentException('You must define at least one client and one index');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($config['default_client'])) {
|
2011-04-10 23:08:51 +02:00
|
|
|
$keys = array_keys($config['clients']);
|
|
|
|
$config['default_client'] = reset($keys);
|
|
|
|
}
|
|
|
|
|
2011-04-17 19:55:46 +02:00
|
|
|
if (empty($config['default_index'])) {
|
2011-04-12 04:23:29 +02:00
|
|
|
$keys = array_keys($config['indexes']);
|
|
|
|
$config['default_index'] = reset($keys);
|
|
|
|
}
|
|
|
|
|
2011-04-13 01:43:45 +02:00
|
|
|
$clientIdsByName = $this->loadClients($config['clients'], $container);
|
2011-04-14 00:04:49 +02:00
|
|
|
$indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client']);
|
|
|
|
$indexDefsByName = array_map(function($id) use ($container) {
|
|
|
|
return $container->getDefinition($id);
|
|
|
|
}, $indexIdsByName);
|
|
|
|
|
|
|
|
$this->loadIndexManager($indexDefsByName, $container->getDefinition($indexIdsByName[$config['default_index']]), $container);
|
2011-04-15 21:18:36 +02:00
|
|
|
$this->loadMappingSetter($this->typeMappings, $container);
|
2011-04-10 23:08:51 +02:00
|
|
|
|
|
|
|
$container->setAlias('foq_elastica.client', sprintf('foq_elastica.client.%s', $config['default_client']));
|
2011-04-12 04:23:29 +02:00
|
|
|
$container->setAlias('foq_elastica.index', sprintf('foq_elastica.index.%s', $config['default_index']));
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2011-04-13 01:43:45 +02:00
|
|
|
$clientIds = array();
|
2011-04-10 23:08:51 +02:00
|
|
|
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);
|
2011-04-13 01:43:45 +02:00
|
|
|
$clientId = sprintf('foq_elastica.client.%s', $name);
|
|
|
|
$container->setDefinition($clientId, $clientDef);
|
2011-04-14 00:04:49 +02:00
|
|
|
$clientIds[$name] = $clientId;
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|
|
|
|
|
2011-04-14 00:04:49 +02:00
|
|
|
return $clientIds;
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the configured indexes.
|
|
|
|
*
|
|
|
|
* @param array $config An array of indexes configurations
|
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance
|
|
|
|
*/
|
2011-04-13 01:43:45 +02:00
|
|
|
protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientIdsByName, $defaultClientName)
|
2011-04-10 23:08:51 +02:00
|
|
|
{
|
2011-04-13 01:43:45 +02:00
|
|
|
$indexIds = array();
|
2011-04-10 23:08:51 +02:00
|
|
|
foreach ($indexes as $name => $index) {
|
|
|
|
if (isset($index['client'])) {
|
|
|
|
$clientName = $index['client'];
|
2011-04-13 01:43:45 +02:00
|
|
|
if (!isset($clientIdsByName[$clientName])) {
|
2011-04-10 23:08:51 +02:00
|
|
|
throw new InvalidArgumentException(sprintf('The elastica client with name "%s" is not defined', $clientName));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$clientName = $defaultClientName;
|
|
|
|
}
|
2011-04-14 00:04:49 +02:00
|
|
|
$clientId = $clientIdsByName[$clientName];
|
|
|
|
$clientDef = $container->getDefinition($clientId);
|
2011-04-13 01:43:45 +02:00
|
|
|
$indexId = sprintf('foq_elastica.index.%s', $name);
|
2011-04-14 00:04:49 +02:00
|
|
|
$indexDefArgs = array($name);
|
2011-04-10 23:08:51 +02:00
|
|
|
$indexDef = new Definition('%foq_elastica.index.class%', $indexDefArgs);
|
2011-04-14 00:04:49 +02:00
|
|
|
$indexDef->setFactoryService($clientId);
|
|
|
|
$indexDef->setFactoryMethod('getIndex');
|
|
|
|
$container->setDefinition($indexId, $indexDef);
|
2011-04-13 01:43:45 +02:00
|
|
|
$this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $indexId);
|
|
|
|
$indexIds[$name] = $indexId;
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|
|
|
|
|
2011-04-13 01:43:45 +02:00
|
|
|
return $indexIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the configured types.
|
|
|
|
*
|
|
|
|
* @param array $config An array of types configurations
|
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance
|
|
|
|
*/
|
2011-04-14 00:04:49 +02:00
|
|
|
protected function loadTypes(array $types, ContainerBuilder $container, $indexId)
|
2011-04-13 01:43:45 +02:00
|
|
|
{
|
|
|
|
foreach ($types as $name => $type) {
|
2011-04-15 21:18:36 +02:00
|
|
|
$typeId = sprintf('%s.%s', $indexId, $name);
|
2011-04-14 00:04:49 +02:00
|
|
|
$typeDefArgs = array($name);
|
2011-04-13 01:43:45 +02:00
|
|
|
$typeDef = new Definition('%foq_elastica.type.class%', $typeDefArgs);
|
2011-04-14 00:04:49 +02:00
|
|
|
$typeDef->setFactoryService($indexId);
|
|
|
|
$typeDef->setFactoryMethod('getType');
|
2011-04-15 21:18:36 +02:00
|
|
|
$container->setDefinition($typeId, $typeDef);
|
|
|
|
if (isset($type['mappings'])) {
|
|
|
|
$this->typeMappings[] = array(
|
|
|
|
new Reference($typeId),
|
|
|
|
$type['mappings']
|
|
|
|
);
|
|
|
|
}
|
2011-04-13 01:43:45 +02:00
|
|
|
}
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the index manager
|
|
|
|
*
|
|
|
|
* @return null
|
|
|
|
**/
|
2011-04-13 00:15:48 +02:00
|
|
|
public function loadIndexManager(array $indexDefs, $defaultIndexId, ContainerBuilder $container)
|
2011-04-10 23:08:51 +02:00
|
|
|
{
|
2011-04-12 04:23:29 +02:00
|
|
|
$managerDef = $container->getDefinition('foq_elastica.index_manager');
|
|
|
|
$managerDef->setArgument(0, $indexDefs);
|
2011-04-13 00:15:48 +02:00
|
|
|
$managerDef->setArgument(1, new Reference('foq_elastica.index'));
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|
2011-04-15 21:18:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the mapping setter
|
|
|
|
*
|
|
|
|
* @return null
|
|
|
|
**/
|
|
|
|
public function loadMappingSetter(array $mappings, ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$managerDef = $container->getDefinition('foq_elastica.mapping_setter');
|
|
|
|
$managerDef->setArgument(0, $mappings);
|
|
|
|
}
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|