From b155f304e434b0cc0e0b6ee9310bd9b1e40e3c20 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Wed, 18 Jun 2014 16:49:38 +1000 Subject: [PATCH] Move IndexManager's resolution to tagged index services --- DependencyInjection/Compiler/IndexPass.php | 38 ++++++++++++++++++++ DependencyInjection/FOSElasticaExtension.php | 3 ++ FOSElasticaBundle.php | 4 ++- Index/IndexManager.php | 26 +++++++------- Resources/config/index.xml | 1 + 5 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 DependencyInjection/Compiler/IndexPass.php diff --git a/DependencyInjection/Compiler/IndexPass.php b/DependencyInjection/Compiler/IndexPass.php new file mode 100644 index 0000000..e131214 --- /dev/null +++ b/DependencyInjection/Compiler/IndexPass.php @@ -0,0 +1,38 @@ + + * + * 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 IndexPass implements CompilerPassInterface +{ + /** + * {@inheritDoc} + */ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition('fos_elastica.index_manager')) { + return; + } + + $indexes = array(); + foreach ($container->findTaggedServiceIds('fos_elastica.index') as $id => $tags) { + foreach ($tags as $tag) { + $indexes[$tag['name']] = new Reference($id); + } + } + + $container->getDefinition('fos_elastica.index_manager')->replaceArgument(0, $indexes); + } +} diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 4d76baf..4730c0b 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -133,6 +133,9 @@ class FOSElasticaExtension extends Extension $indexDef = new DefinitionDecorator('fos_elastica.index_prototype'); $indexDef->replaceArgument(0, $indexName); + $indexDef->addTag('fos_elastica.index', array( + 'name' => $name, + )); if (isset($index['client'])) { $client = $this->getClient($index['client']); diff --git a/FOSElasticaBundle.php b/FOSElasticaBundle.php index ac4cc3d..3dec2a0 100644 --- a/FOSElasticaBundle.php +++ b/FOSElasticaBundle.php @@ -3,6 +3,7 @@ namespace FOS\ElasticaBundle; use FOS\ElasticaBundle\DependencyInjection\Compiler\ConfigSourcePass; +use FOS\ElasticaBundle\DependencyInjection\Compiler\IndexPass; use FOS\ElasticaBundle\DependencyInjection\Compiler\RegisterProvidersPass; use FOS\ElasticaBundle\DependencyInjection\Compiler\TransformerPass; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -22,8 +23,9 @@ class FOSElasticaBundle extends Bundle { parent::build($container); + $container->addCompilerPass(new ConfigSourcePass()); + $container->addCompilerPass(new IndexPass()); $container->addCompilerPass(new RegisterProvidersPass(), PassConfig::TYPE_BEFORE_REMOVING); $container->addCompilerPass(new TransformerPass()); - $container->addCompilerPass(new ConfigSourcePass()); } } diff --git a/Index/IndexManager.php b/Index/IndexManager.php index 9dd7bfe..38249a7 100644 --- a/Index/IndexManager.php +++ b/Index/IndexManager.php @@ -6,19 +6,19 @@ use FOS\ElasticaBundle\Elastica\Index; class IndexManager { - private $indexesByName; - private $defaultIndexName; + /** + * @var array + */ + private $indexes; /** - * Constructor. - * - * @param array $indexesByName + * @param array $indexes * @param Index $defaultIndex */ - public function __construct(array $indexesByName, Index $defaultIndex) + public function __construct(array $indexes, Index $defaultIndex) { - $this->indexesByName = $indexesByName; - $this->defaultIndexName = $defaultIndex->getName(); + $this->defaultIndex = $defaultIndex; + $this->indexes = $indexes; } /** @@ -28,7 +28,7 @@ class IndexManager */ public function getAllIndexes() { - return $this->indexesByName; + return $this->indexes; } /** @@ -41,14 +41,14 @@ class IndexManager public function getIndex($name = null) { if (null === $name) { - $name = $this->defaultIndexName; + return $this->defaultIndex; } - if (!isset($this->indexesByName[$name])) { + if (!isset($this->indexes[$name])) { throw new \InvalidArgumentException(sprintf('The index "%s" does not exist', $name)); } - return $this->indexesByName[$name]; + return $this->indexes[$name]; } /** @@ -58,6 +58,6 @@ class IndexManager */ public function getDefaultIndex() { - return $this->getIndex($this->defaultIndexName); + return $this->defaultIndex; } } diff --git a/Resources/config/index.xml b/Resources/config/index.xml index ef9f4e7..60b75a9 100644 --- a/Resources/config/index.xml +++ b/Resources/config/index.xml @@ -20,6 +20,7 @@ +