Move IndexManager's resolution to tagged index services

This commit is contained in:
Tim Nagel 2014-06-18 16:49:38 +10:00
parent afbaf875b9
commit b155f304e4
5 changed files with 58 additions and 14 deletions

View file

@ -0,0 +1,38 @@
<?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 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);
}
}

View file

@ -133,6 +133,9 @@ class FOSElasticaExtension extends Extension
$indexDef = new DefinitionDecorator('fos_elastica.index_prototype'); $indexDef = new DefinitionDecorator('fos_elastica.index_prototype');
$indexDef->replaceArgument(0, $indexName); $indexDef->replaceArgument(0, $indexName);
$indexDef->addTag('fos_elastica.index', array(
'name' => $name,
));
if (isset($index['client'])) { if (isset($index['client'])) {
$client = $this->getClient($index['client']); $client = $this->getClient($index['client']);

View file

@ -3,6 +3,7 @@
namespace FOS\ElasticaBundle; namespace FOS\ElasticaBundle;
use FOS\ElasticaBundle\DependencyInjection\Compiler\ConfigSourcePass; use FOS\ElasticaBundle\DependencyInjection\Compiler\ConfigSourcePass;
use FOS\ElasticaBundle\DependencyInjection\Compiler\IndexPass;
use FOS\ElasticaBundle\DependencyInjection\Compiler\RegisterProvidersPass; use FOS\ElasticaBundle\DependencyInjection\Compiler\RegisterProvidersPass;
use FOS\ElasticaBundle\DependencyInjection\Compiler\TransformerPass; use FOS\ElasticaBundle\DependencyInjection\Compiler\TransformerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
@ -22,8 +23,9 @@ class FOSElasticaBundle extends Bundle
{ {
parent::build($container); parent::build($container);
$container->addCompilerPass(new ConfigSourcePass());
$container->addCompilerPass(new IndexPass());
$container->addCompilerPass(new RegisterProvidersPass(), PassConfig::TYPE_BEFORE_REMOVING); $container->addCompilerPass(new RegisterProvidersPass(), PassConfig::TYPE_BEFORE_REMOVING);
$container->addCompilerPass(new TransformerPass()); $container->addCompilerPass(new TransformerPass());
$container->addCompilerPass(new ConfigSourcePass());
} }
} }

View file

@ -6,19 +6,19 @@ use FOS\ElasticaBundle\Elastica\Index;
class IndexManager class IndexManager
{ {
private $indexesByName; /**
private $defaultIndexName; * @var array
*/
private $indexes;
/** /**
* Constructor. * @param array $indexes
*
* @param array $indexesByName
* @param Index $defaultIndex * @param Index $defaultIndex
*/ */
public function __construct(array $indexesByName, Index $defaultIndex) public function __construct(array $indexes, Index $defaultIndex)
{ {
$this->indexesByName = $indexesByName; $this->defaultIndex = $defaultIndex;
$this->defaultIndexName = $defaultIndex->getName(); $this->indexes = $indexes;
} }
/** /**
@ -28,7 +28,7 @@ class IndexManager
*/ */
public function getAllIndexes() public function getAllIndexes()
{ {
return $this->indexesByName; return $this->indexes;
} }
/** /**
@ -41,14 +41,14 @@ class IndexManager
public function getIndex($name = null) public function getIndex($name = null)
{ {
if (null === $name) { 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)); 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() public function getDefaultIndex()
{ {
return $this->getIndex($this->defaultIndexName); return $this->defaultIndex;
} }
} }

View file

@ -20,6 +20,7 @@
<service id="fos_elastica.index_prototype" class="%fos_elastica.index.class%" factory-service="fos_elastica.client" factory-method="getIndex" abstract="true"> <service id="fos_elastica.index_prototype" class="%fos_elastica.index.class%" factory-service="fos_elastica.client" factory-method="getIndex" abstract="true">
<argument /> <!-- index name --> <argument /> <!-- index name -->
<!-- tagged with fos_elastica.index in the Extension -->
</service> </service>
<service id="fos_elastica.type_prototype" class="%fos_elastica.type.class%" factory-method="getType" abstract="true"> <service id="fos_elastica.type_prototype" class="%fos_elastica.type.class%" factory-method="getType" abstract="true">