FOSElasticaBundle/DependencyInjection/Compiler/RegisterProvidersPass.php
Jeremy Mikola b360a36737 [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).
2012-03-12 12:07:51 -04:00

71 lines
2.4 KiB
PHP

<?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];
}
}