[DoctrineBridge] Fix arguments when registering event listeners on multiple connections
Fixes #4712
This commit is contained in:
parent
0bf3c068d7
commit
e97cd61a89
@ -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,18 +89,12 @@ 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);
|
|
||||||
foreach ($cons as $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))));
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($isListener) {
|
if ($isListener) {
|
||||||
if (!isset($instance['event'])) {
|
if (!isset($instance['event'])) {
|
||||||
throw new \InvalidArgumentException(sprintf('Doctrine event listener "%s" must specify the "event" attribute.', $id));
|
throw new \InvalidArgumentException(sprintf('Doctrine event listener "%s" must specify the "event" attribute.', $id));
|
||||||
@ -101,17 +104,22 @@ class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
|
|||||||
if (isset($instance['lazy']) && $instance['lazy']) {
|
if (isset($instance['lazy']) && $instance['lazy']) {
|
||||||
$this->container->getDefinition($id)->setPublic(true);
|
$this->container->getDefinition($id)->setPublic(true);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($grouped[$con][$id])) {
|
$cons = isset($instance['connection']) ? array($instance['connection']) : $allCons;
|
||||||
|
foreach ($cons as $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))));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($isListener && isset($grouped[$con][$id])) {
|
||||||
$grouped[$con][$id]['event'] = array_merge($grouped[$con][$id]['event'], $instance['event']);
|
$grouped[$con][$id]['event'] = array_merge($grouped[$con][$id]['event'], $instance['event']);
|
||||||
continue;
|
} else {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$grouped[$con][$id] = $instance;
|
$grouped[$con][$id] = $instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $grouped;
|
return $grouped;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user