Config Source providers
This commit is contained in:
parent
813a4a5d26
commit
ada3942576
122
Configuration/IndexConfig.php
Normal file
122
Configuration/IndexConfig.php
Normal file
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the FOSElasticaBundle project.
|
||||
*
|
||||
* (c) Tim Nagel <tim@nagel.com.au>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FOS\ElasticaBundle\Configuration;
|
||||
|
||||
class IndexConfig
|
||||
{
|
||||
/**
|
||||
* The name of the index for ElasticSearch.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $elasticSearchName;
|
||||
|
||||
/**
|
||||
* The internal name of the index. May not be the same as the name used in ElasticSearch,
|
||||
* especially if aliases are enabled.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* An array of settings sent to ElasticSearch when creating the index.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* All types that belong to this index.
|
||||
*
|
||||
* @var TypeConfig[]
|
||||
*/
|
||||
private $types;
|
||||
|
||||
/**
|
||||
* Indicates if the index should use an alias, allowing an index repopulation to occur
|
||||
* without overwriting the current index.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $useAlias = false;
|
||||
|
||||
/**
|
||||
* Constructor expects an array as generated by the Container Configuration builder.
|
||||
*
|
||||
* @param string $name
|
||||
* @param TypeConfig[] $types
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct($name, array $types, array $config)
|
||||
{
|
||||
$this->elasticSearchName = $config['elasticSearchName'];
|
||||
$this->name = $name;
|
||||
$this->settings = $config['settings'];
|
||||
$this->types = $types;
|
||||
$this->useAlias = $config['useAlias'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getElasticSearchName()
|
||||
{
|
||||
return $this->elasticSearchName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSettings()
|
||||
{
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $typeName
|
||||
* @return TypeConfig
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function getType($typeName)
|
||||
{
|
||||
if (!array_key_exists($typeName, $this->types)) {
|
||||
throw new \InvalidArgumentException(sprintf('Type "%s" does not exist on index "%s"', $typeName, $this->name));
|
||||
}
|
||||
|
||||
return $this->types[$typeName];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \FOS\ElasticaBundle\Configuration\TypeConfig[]
|
||||
*/
|
||||
public function getTypes()
|
||||
{
|
||||
return $this->types;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function getUseAlias()
|
||||
{
|
||||
return $this->useAlias;
|
||||
}
|
||||
}
|
59
Configuration/Manager.php
Normal file
59
Configuration/Manager.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the FOSElasticaBundle project.
|
||||
*
|
||||
* (c) Tim Nagel <tim@nagel.com.au>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FOS\ElasticaBundle\Configuration;
|
||||
|
||||
/**
|
||||
* Central manager for index and type configuration.
|
||||
*/
|
||||
class Manager implements ManagerInterface
|
||||
{
|
||||
/**
|
||||
* @var IndexConfig[]
|
||||
*/
|
||||
private $indexes = array();
|
||||
|
||||
/**
|
||||
* @param Source\SourceInterface[] $sources
|
||||
*/
|
||||
public function __construct(array $sources)
|
||||
{
|
||||
foreach ($sources as $source) {
|
||||
$this->indexes = array_merge($source->getConfiguration(), $this->indexes);
|
||||
}
|
||||
}
|
||||
|
||||
public function getTypeConfiguration($indexName, $typeName)
|
||||
{
|
||||
$index = $this->getIndexConfiguration($indexName);
|
||||
$type = $index->getType($typeName);
|
||||
|
||||
if (!$type) {
|
||||
throw new \InvalidArgumentException(sprintf('Type with name "%s" on index "%s" is not configured', $typeName, $indexName));
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
35
Configuration/ManagerInterface.php
Normal file
35
Configuration/ManagerInterface.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the FOSElasticaBundle project.
|
||||
*
|
||||
* (c) Tim Nagel <tim@nagel.com.au>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FOS\ElasticaBundle\Configuration;
|
||||
|
||||
/**
|
||||
* Central manager for index and type configuration.
|
||||
*/
|
||||
interface ManagerInterface
|
||||
{
|
||||
/**
|
||||
* Returns configuration for an index.
|
||||
*
|
||||
* @param $index
|
||||
* @return IndexConfig
|
||||
*/
|
||||
public function getIndexConfiguration($index);
|
||||
|
||||
/**
|
||||
* Returns a type configuration.
|
||||
*
|
||||
* @param string $index
|
||||
* @param string $type
|
||||
* @return TypeConfig
|
||||
*/
|
||||
public function getTypeConfiguration($index, $type);
|
||||
}
|
62
Configuration/Source/ContainerSource.php
Normal file
62
Configuration/Source/ContainerSource.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the FOSElasticaBundle project.
|
||||
*
|
||||
* (c) Tim Nagel <tim@nagel.com.au>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FOS\ElasticaBundle\Configuration\Source;
|
||||
|
||||
use FOS\ElasticaBundle\Configuration\IndexConfig;
|
||||
use FOS\ElasticaBundle\Configuration\TypeConfig;
|
||||
|
||||
/**
|
||||
* Returns index and type configuration from the container.
|
||||
*/
|
||||
class ContainerSource implements SourceInterface
|
||||
{
|
||||
/**
|
||||
* The internal container representation of information.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $configArray;
|
||||
|
||||
public function __construct(array $configArray)
|
||||
{
|
||||
$this->configArray = $configArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should return all configuration available from the data source.
|
||||
*
|
||||
* @return IndexConfig[]
|
||||
*/
|
||||
public function getConfiguration()
|
||||
{
|
||||
$indexes = array();
|
||||
foreach ($this->configArray as $config) {
|
||||
$types = array();
|
||||
foreach ($config['types'] as $typeConfig) {
|
||||
$types[$typeConfig['name']] = new TypeConfig($typeConfig['name'], array(
|
||||
'dynamicTemplates' => $typeConfig['dynamic_templates'],
|
||||
'properties' => $typeConfig['properties'],
|
||||
), $config['type_prototype']);
|
||||
}
|
||||
|
||||
$index = new IndexConfig($config['name'], $types, array(
|
||||
'elasticSearchName' => $config['elasticsearch_name'],
|
||||
'settings' => $config['settings'],
|
||||
'useAlias' => $config['use_alias'],
|
||||
));
|
||||
|
||||
$indexes[$config['name']] = $index;
|
||||
}
|
||||
|
||||
return $indexes;
|
||||
}
|
||||
}
|
26
Configuration/Source/SourceInterface.php
Normal file
26
Configuration/Source/SourceInterface.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the FOSElasticaBundle project.
|
||||
*
|
||||
* (c) Tim Nagel <tim@nagel.com.au>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FOS\ElasticaBundle\Configuration\Source;
|
||||
|
||||
/**
|
||||
* Represents a source of index and type information (ie, the Container configuration or
|
||||
* annotations).
|
||||
*/
|
||||
interface SourceInterface
|
||||
{
|
||||
/**
|
||||
* Should return all configuration available from the data source.
|
||||
*
|
||||
* @return \FOS\ElasticaBundle\Configuration\IndexConfig[]
|
||||
*/
|
||||
public function getConfiguration();
|
||||
}
|
31
Configuration/TypeConfig.php
Normal file
31
Configuration/TypeConfig.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the FOSElasticaBundle project.
|
||||
*
|
||||
* (c) Tim Nagel <tim@nagel.com.au>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FOS\ElasticaBundle\Configuration;
|
||||
|
||||
class TypeConfig
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
public function __construct($name, array $config, array $prototype)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->name = $name;
|
||||
}
|
||||
}
|
36
DependencyInjection/Compiler/ConfigSourcePass.php
Normal file
36
DependencyInjection/Compiler/ConfigSourcePass.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the FOSElasticaBundle project.
|
||||
*
|
||||
* (c) Tim Nagel <tim@nagel.com.au>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FOS\ElasticaBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
class ConfigSourcePass implements CompilerPassInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->hasDefinition('fos_elastica.config_manager')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sources = array();
|
||||
foreach (array_keys($container->findTaggedServiceIds('fos_elastica.config_source')) as $id) {
|
||||
$sources[] = new Reference($id);
|
||||
}
|
||||
|
||||
$container->getDefinition('fos_elastica.config_manager')->replaceArgument(0, $sources);
|
||||
}
|
||||
}
|
|
@ -49,7 +49,7 @@ class FOSElasticaExtension extends Extension
|
|||
return;
|
||||
}
|
||||
|
||||
foreach (array('config', 'index', 'persister', 'provider', 'transformer') as $basename) {
|
||||
foreach (array('config', 'index', 'persister', 'provider', 'source', 'transformer') as $basename) {
|
||||
$loader->load(sprintf('%s.xml', $basename));
|
||||
}
|
||||
|
||||
|
@ -71,6 +71,8 @@ class FOSElasticaExtension extends Extension
|
|||
$this->loadIndexes($config['indexes'], $container);
|
||||
$container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index']));
|
||||
|
||||
$container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs);
|
||||
|
||||
$this->loadIndexManager($container);
|
||||
$this->loadResetter($container);
|
||||
|
||||
|
@ -142,13 +144,10 @@ class FOSElasticaExtension extends Extension
|
|||
$reference = new Reference($indexId);
|
||||
|
||||
$this->indexConfigs[$name] = array(
|
||||
'config' => array(
|
||||
'properties' => array(),
|
||||
'settings' => $index['settings']
|
||||
),
|
||||
'elasticsearch_name' => $indexName,
|
||||
'reference' => $reference,
|
||||
'name' => $name,
|
||||
'settings' => $index['settings'],
|
||||
'type_prototype' => isset($index['type_prototype']) ? $index['type_prototype'] : array(),
|
||||
'use_alias' => $index['use_alias'],
|
||||
);
|
||||
|
@ -197,7 +196,6 @@ class FOSElasticaExtension extends Extension
|
|||
{
|
||||
foreach ($types as $name => $type) {
|
||||
$indexName = $indexConfig['name'];
|
||||
$type = self::deepArrayUnion($indexConfig['type_prototype'], $type);
|
||||
|
||||
$typeId = sprintf('%s.%s', $indexName, $name);
|
||||
$typeDef = new DefinitionDecorator('fos_elastica.type_prototype');
|
||||
|
@ -208,7 +206,14 @@ class FOSElasticaExtension extends Extension
|
|||
$this->loadTypePersistenceIntegration($type['persistence'], $container, $typeDef, $indexName, $name);
|
||||
}
|
||||
|
||||
$this->indexConfigs[$indexName]['types'][$name] = array(
|
||||
'dynamic_templates' => array(),
|
||||
'name' => $name,
|
||||
'properties' => array()
|
||||
);
|
||||
|
||||
foreach (array(
|
||||
'dynamic_templates',
|
||||
'index_analyzer',
|
||||
'properties',
|
||||
'search_analyzer',
|
||||
|
@ -222,14 +227,7 @@ class FOSElasticaExtension extends Extension
|
|||
'_ttl',
|
||||
) as $field) {
|
||||
if (array_key_exists($field, $type)) {
|
||||
$this->indexConfigs[$indexName]['config']['properties'][$name][$field] = $type[$field];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($type['dynamic_templates'])) {
|
||||
$this->indexConfigs[$indexName]['config']['properties'][$name]['dynamic_templates'] = array();
|
||||
foreach ($type['dynamic_templates'] as $templateName => $templateData) {
|
||||
$this->indexConfigs[$indexName]['config']['properties'][$name]['dynamic_templates'][] = array($templateName => $templateData);
|
||||
$this->indexConfigs[$indexName]['types'][$name]['properties'][$field] = $type[$field];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -259,27 +257,6 @@ class FOSElasticaExtension extends Extension
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges two arrays without reindexing numeric keys.
|
||||
*
|
||||
* @param array $array1 An array to merge
|
||||
* @param array $array2 An array to merge
|
||||
*
|
||||
* @return array The merged array
|
||||
*/
|
||||
private static function deepArrayUnion($array1, $array2)
|
||||
{
|
||||
foreach ($array2 as $key => $value) {
|
||||
if (is_array($value) && isset($array1[$key]) && is_array($array1[$key])) {
|
||||
$array1[$key] = self::deepArrayUnion($array1[$key], $value);
|
||||
} else {
|
||||
$array1[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $array1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the optional provider and finder for a type
|
||||
*
|
||||
|
@ -397,7 +374,7 @@ class FOSElasticaExtension extends Extension
|
|||
$arguments[] = array(new Reference($callbackId), 'serialize');
|
||||
} else {
|
||||
$abstractId = 'fos_elastica.object_persister';
|
||||
$arguments[] = $this->indexConfigs[$indexName]['config']['properties'][$typeName]['properties'];
|
||||
$arguments[] = $this->indexConfigs[$indexName]['types'][$typeName]['properties'];
|
||||
}
|
||||
|
||||
$serviceId = sprintf('fos_elastica.object_persister.%s.%s', $indexName, $typeName);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace FOS\ElasticaBundle;
|
||||
|
||||
use FOS\ElasticaBundle\DependencyInjection\Compiler\ConfigSourcePass;
|
||||
use FOS\ElasticaBundle\DependencyInjection\Compiler\RegisterProvidersPass;
|
||||
use FOS\ElasticaBundle\DependencyInjection\Compiler\TransformerPass;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
|
@ -23,5 +24,6 @@ class FOSElasticaBundle extends Bundle
|
|||
|
||||
$container->addCompilerPass(new RegisterProvidersPass(), PassConfig::TYPE_BEFORE_REMOVING);
|
||||
$container->addCompilerPass(new TransformerPass());
|
||||
$container->addCompilerPass(new ConfigSourcePass());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,10 @@
|
|||
<argument /> <!-- callback -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.config_manager" class="FOS\ElasticaBundle\Configuration\Manager">
|
||||
<argument type="collection" /> <!-- collection of SourceInterface services -->
|
||||
</service>
|
||||
|
||||
<service id="fos_elastica.data_collector" class="%fos_elastica.data_collector.class%">
|
||||
<tag name="data_collector" template="FOSElasticaBundle:Collector:elastica" id="elastica" />
|
||||
<argument type="service" id="fos_elastica.logger" />
|
||||
|
|
13
Resources/config/source.xml
Normal file
13
Resources/config/source.xml
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?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">
|
||||
|
||||
<services>
|
||||
<service id="fos_elastica.config_source.container" class="FOS\ElasticaBundle\Configuration\Source\ContainerSource" public="false">
|
||||
<argument type="collection" /> <!-- index configs -->
|
||||
<tag name="fos_elastica.config_source" />
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
54
Tests/Functional/ConfigurationManagerTest.php
Normal file
54
Tests/Functional/ConfigurationManagerTest.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the FOSElasticaBundle project.
|
||||
*
|
||||
* (c) Tim Nagel <tim@nagel.com.au>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace FOS\ElasticaBundle\Tests\Functional;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Client;
|
||||
|
||||
/**
|
||||
* @group functional
|
||||
*/
|
||||
class ConfigurationManagerTest extends WebTestCase
|
||||
{
|
||||
public function testContainerSource()
|
||||
{
|
||||
$client = $this->createClient(array('test_case' => 'Basic'));
|
||||
$manager = $this->getManager($client);
|
||||
|
||||
$index = $manager->getIndexConfiguration('index');
|
||||
var_dump($index); die;
|
||||
}
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->deleteTmpDir('Basic');
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
$this->deleteTmpDir('Basic');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Client $client
|
||||
* @return \FOS\ElasticaBundle\Configuration\Manager
|
||||
*/
|
||||
private function getManager(Client $client)
|
||||
{
|
||||
$manager = $client->getContainer()->get('fos_elastica.config_manager');
|
||||
|
||||
return $manager;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue