[DoctrineBridge] Fix arguments when registering event listeners on multiple connections

Fixes #4712
This commit is contained in:
Alexander 2012-07-13 15:06:41 +02:00
parent 0bf3c068d7
commit e97cd61a89

View File

@ -15,6 +15,12 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
/**
* Registers event listeners and subscribers to the available doctrine connections.
*
* @author Jeremy Mikola <jmikola@gmail.com>
* @author Alexander <iam.asm89@gmail.com>
*/
class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
{ {
private $connections; private $connections;
@ -38,6 +44,9 @@ class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
$this->tagPrefix = $tagPrefix; $this->tagPrefix = $tagPrefix;
} }
/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container) public function process(ContainerBuilder $container)
{ {
if (!$container->hasParameter($this->connections)) { if (!$container->hasParameter($this->connections)) {
@ -80,35 +89,34 @@ class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
private function groupByConnection(array $services, $isListener = false) private function groupByConnection(array $services, $isListener = false)
{ {
$grouped = array(); $grouped = array();
foreach (array_keys($this->connections) as $con) { foreach ($allCons = array_keys($this->connections) as $con) {
$grouped[$con] = array(); $grouped[$con] = array();
} }
foreach ($services as $id => $instances) { foreach ($services as $id => $instances) {
foreach ($instances as $instance) { foreach ($instances as $instance) {
$cons = isset($instance['connection']) ? array($instance['connection']) : array_keys($this->connections); if ($isListener) {
if (!isset($instance['event'])) {
throw new \InvalidArgumentException(sprintf('Doctrine event listener "%s" must specify the "event" attribute.', $id));
}
$instance['event'] = array($instance['event']);
if (isset($instance['lazy']) && $instance['lazy']) {
$this->container->getDefinition($id)->setPublic(true);
}
}
$cons = isset($instance['connection']) ? array($instance['connection']) : $allCons;
foreach ($cons as $con) { foreach ($cons as $con) {
if (!isset($grouped[$con])) { if (!isset($grouped[$con])) {
throw new \RuntimeException(sprintf('The Doctrine connection "%s" referenced in service "%s" does not exist. Available connections names: %s', $con, $id, implode(', ', array_keys($this->connections)))); throw new \RuntimeException(sprintf('The Doctrine connection "%s" referenced in service "%s" does not exist. Available connections names: %s', $con, $id, implode(', ', array_keys($this->connections))));
} }
if ($isListener) { if ($isListener && isset($grouped[$con][$id])) {
if (!isset($instance['event'])) { $grouped[$con][$id]['event'] = array_merge($grouped[$con][$id]['event'], $instance['event']);
throw new \InvalidArgumentException(sprintf('Doctrine event listener "%s" must specify the "event" attribute.', $id)); } else {
} $grouped[$con][$id] = $instance;
$instance['event'] = array($instance['event']);
if (isset($instance['lazy']) && $instance['lazy']) {
$this->container->getDefinition($id)->setPublic(true);
}
if (isset($grouped[$con][$id])) {
$grouped[$con][$id]['event'] = array_merge($grouped[$con][$id]['event'], $instance['event']);
continue;
}
} }
$grouped[$con][$id] = $instance;
} }
} }
} }