forked from FriendsOfSymfony/FOSElasticaBundle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Provider] Create ProviderRegistry service (BC break)
This introduces a registry service for persistence providers. Previously, tagging one or more provider services would cause AddProviderPass to clobber the default providers created by the bundle's extension class. Now, the extension class tags its created providers and allows them to be registered via RegisterProvidersPass just like custom providers. BC break: Custom providers tagged "foq_elastica.provider" must now specify a "type" attribute on their tag. An "index" attribute is optional (the default ES index will be used by default).
- Loading branch information
Showing
8 changed files
with
342 additions
and
32 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,70 @@ | |||
<?php | |||
|
|||
namespace FOQ\ElasticaBundle\DependencyInjection\Compiler; | |||
|
|||
use Symfony\Component\DependencyInjection\ContainerBuilder; | |||
use Symfony\Component\DependencyInjection\Reference; | |||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | |||
|
|||
class RegisterProvidersPass implements CompilerPassInterface | |||
{ | |||
/** | |||
* Mapping of class names to booleans indicating whether the class | |||
* implements ProviderInterface. | |||
* | |||
* @var array | |||
*/ | |||
private $implementations = array(); | |||
|
|||
/** | |||
* @see Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface::process() | |||
*/ | |||
public function process(ContainerBuilder $container) | |||
{ | |||
if (!$container->hasDefinition('foq_elastica.provider_registry')) { | |||
return; | |||
} | |||
|
|||
// Infer the default index name from the service alias | |||
$defaultIndex = substr($container->getAlias('foq_elastica.index'), 19); | |||
|
|||
$registry = $container->getDefinition('foq_elastica.provider_registry'); | |||
$providers = $container->findTaggedServiceIds('foq_elastica.provider'); | |||
|
|||
foreach ($providers as $providerId => $tags) { | |||
$index = $type = null; | |||
$class = $container->getDefinition($providerId)->getClass(); | |||
|
|||
if (!$class || !$this->isProviderImplementation($class)) { | |||
throw new \InvalidArgumentException(sprintf('Elastica provider "%s" with class "%s" must implement ProviderInterface.', $providerId, $class)); | |||
} | |||
|
|||
foreach ($tags as $tag) { | |||
if (!isset($tag['type'])) { | |||
throw new \InvalidArgumentException(sprintf('Elastica provider "%s" must specify the "type" attribute.', $providerId)); | |||
} | |||
|
|||
$index = isset($tag['index']) ? $tag['index'] : $defaultIndex; | |||
$type = $tag['type']; | |||
} | |||
|
|||
$registry->addMethodCall('addProvider', array($index, $type, $providerId)); | |||
} | |||
} | |||
|
|||
/** | |||
* Returns whether the class implements ProviderInterface. | |||
* | |||
* @param string $class | |||
* @return boolean | |||
*/ | |||
private function isProviderImplementation($class) | |||
{ | |||
if (!isset($this->implementations[$class])) { | |||
$refl = new \ReflectionClass($class); | |||
$this->implementations[$class] = $refl->implementsInterface('FOQ\ElasticaBundle\Provider\ProviderInterface'); | |||
} | |||
|
|||
return $this->implementations[$class]; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,98 @@ | |||
<?php | |||
|
|||
namespace FOQ\ElasticaBundle\Provider; | |||
|
|||
use Symfony\Component\DependencyInjection\ContainerBuilder; | |||
|
|||
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | |||
use Symfony\Component\DependencyInjection\ContainerInterface; | |||
|
|||
/** | |||
* References persistence providers for each index and type. | |||
*/ | |||
class ProviderRegistry implements ContainerAwareInterface | |||
{ | |||
private $container; | |||
private $providers = array(); | |||
|
|||
/** | |||
* Registers a provider for the specified index and type. | |||
* | |||
* @param string $index | |||
* @param string $type | |||
* @param string $providerId | |||
*/ | |||
public function addProvider($index, $type, $providerId) | |||
{ | |||
if (!isset($this->providers[$index])) { | |||
$this->providers[$index] = array(); | |||
} | |||
|
|||
$this->providers[$index][$type] = $providerId; | |||
} | |||
|
|||
/** | |||
* Gets all registered providers. | |||
* | |||
* @return array of ProviderInterface instances | |||
*/ | |||
public function getAllProviders() | |||
{ | |||
$providers = array(); | |||
|
|||
foreach ($this->providers as $indexProviders) { | |||
foreach ($indexProviders as $providerId) { | |||
$providers[] = $this->container->get($providerId); | |||
} | |||
} | |||
|
|||
return $providers; | |||
} | |||
|
|||
/** | |||
* Gets all providers for an index. | |||
* | |||
* @param string $index | |||
* @return array of ProviderInterface instances | |||
* @throws InvalidArgumentException if no providers were registered for the index | |||
*/ | |||
public function getIndexProviders($index) | |||
{ | |||
if (!isset($this->providers[$index])) { | |||
throw new \InvalidArgumentException(sprintf('No providers were registered for index "%s".', $index)); | |||
} | |||
|
|||
$providers = array(); | |||
|
|||
foreach ($this->providers[$index] as $providerId) { | |||
$providers[] = $this->container->get($providerId); | |||
} | |||
|
|||
return $providers; | |||
} | |||
|
|||
/** | |||
* Gets the provider for an index and type. | |||
* | |||
* @param string $index | |||
* @param string $type | |||
* @return ProviderInterface | |||
* @throws InvalidArgumentException if no provider was registered for the index and type | |||
*/ | |||
public function getProvider($index, $type) | |||
{ | |||
if (!isset($this->providers[$index][$type])) { | |||
throw new \InvalidArgumentException(sprintf('No provider was registered for index "%s" and type "%s".', $index, $type)); | |||
} | |||
|
|||
return $this->container->get($this->providers[$index][$type]); | |||
} | |||
|
|||
/** | |||
* @see Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer() | |||
*/ | |||
public function setContainer(ContainerInterface $container = null) | |||
{ | |||
$this->container = $container; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
Tests/DependencyInjection/Compiler/RegisterProvidersPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,78 @@ | |||
<?php | |||
|
|||
namespace FOQ\ElasticaBundle\Tests\DependencyInjection\Compiler; | |||
|
|||
use FOQ\ElasticaBundle\DependencyInjection\Compiler\RegisterProvidersPass; | |||
use Symfony\Component\DependencyInjection\Definition; | |||
use Symfony\Component\DependencyInjection\ContainerBuilder; | |||
|
|||
class RegisterProvidersPassTest extends \PHPUnit_Framework_TestCase | |||
{ | |||
public function testProcessShouldRegisterTaggedProviders() | |||
{ | |||
$container = new ContainerBuilder(); | |||
$pass = new RegisterProvidersPass(); | |||
|
|||
$registryDefinition = new Definition(); | |||
|
|||
$container->setDefinition('foq_elastica.provider_registry', $registryDefinition); | |||
$container->setAlias('foq_elastica.index', 'foq_elastica.index.foo'); | |||
|
|||
$container->setDefinition('provider.foo.a', $this->createProviderDefinition(array('type' => 'a'))); | |||
$container->setDefinition('provider.foo.b', $this->createProviderDefinition(array('index' => 'foo', 'type' => 'b'))); | |||
$container->setDefinition('provider.bar.a', $this->createProviderDefinition(array('index' => 'bar', 'type' => 'a'))); | |||
|
|||
$pass->process($container); | |||
|
|||
$calls = $registryDefinition->getMethodCalls(); | |||
|
|||
$this->assertEquals(array('addProvider', array('foo', 'a', 'provider.foo.a')), $calls[0]); | |||
$this->assertEquals(array('addProvider', array('foo', 'b', 'provider.foo.b')), $calls[1]); | |||
$this->assertEquals(array('addProvider', array('bar', 'a', 'provider.bar.a')), $calls[2]); | |||
} | |||
|
|||
/** | |||
* @expectedException InvalidArgumentException | |||
*/ | |||
public function testProcessShouldRequireProviderImplementation() | |||
{ | |||
$container = new ContainerBuilder(); | |||
$pass = new RegisterProvidersPass(); | |||
|
|||
$container->setDefinition('foq_elastica.provider_registry', new Definition()); | |||
$container->setAlias('foq_elastica.index', 'foq_elastica.index.foo'); | |||
|
|||
$providerDef = $this->createProviderDefinition(); | |||
$providerDef->setClass('stdClass'); | |||
|
|||
$container->setDefinition('provider.foo.a', $providerDef); | |||
|
|||
$pass->process($container); | |||
} | |||
|
|||
/** | |||
* @expectedException InvalidArgumentException | |||
*/ | |||
public function testProcessShouldRequireTypeAttribute() | |||
{ | |||
$container = new ContainerBuilder(); | |||
$pass = new RegisterProvidersPass(); | |||
|
|||
$container->setDefinition('foq_elastica.provider_registry', new Definition()); | |||
$container->setAlias('foq_elastica.index', 'foq_elastica.index.foo'); | |||
|
|||
$container->setDefinition('provider.foo.a', $this->createProviderDefinition()); | |||
|
|||
$pass->process($container); | |||
} | |||
|
|||
private function createProviderDefinition(array $attributes = array()) | |||
{ | |||
$provider = $this->getMock('FOQ\ElasticaBundle\Provider\ProviderInterface'); | |||
|
|||
$definition = new Definition(get_class($provider)); | |||
$definition->addTag('foq_elastica.provider', $attributes); | |||
|
|||
return $definition; | |||
} | |||
} |
Oops, something went wrong.