merged branch asm89/issue-4712 (PR #4896)
Commits -------e97cd61
[DoctrineBridge] Fix arguments when registering event listeners on multiple connections0bf3c06
[DoctrineBridge] Failing testcase for event listeners and multiple connections Discussion ---------- [DoctrineBridge] Fix arguments when registering event listeners on multiple connections Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: [![Build Status](https://secure.travis-ci.org/asm89/symfony.png?branch=issue-4712)](http://travis-ci.org/asm89/symfony) Fixes the following tickets: #4712 License of the code: MIT
This commit is contained in:
commit
d67d66abf1
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,27 @@ class RegisterEventListenersAndSubscribersPassTest extends \PHPUnit_Framework_Te
|
|||||||
$this->assertEquals(array('foo', 'bar'), $calls[1][1][0]);
|
$this->assertEquals(array('foo', 'bar'), $calls[1][1][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testProcessEventListenersWithMultipleConnections()
|
||||||
|
{
|
||||||
|
$container = $this->createBuilder(true);
|
||||||
|
|
||||||
|
$container
|
||||||
|
->register('a', 'stdClass')
|
||||||
|
->addTag('doctrine.event_listener', array(
|
||||||
|
'event' => 'onFlush',
|
||||||
|
))
|
||||||
|
;
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$callsDefault = $container->getDefinition('doctrine.dbal.default_connection.event_manager')->getMethodCalls();
|
||||||
|
|
||||||
|
$this->assertEquals('addEventListener', $callsDefault[0][0]);
|
||||||
|
$this->assertEquals(array('onFlush'), $callsDefault[0][1][0]);
|
||||||
|
|
||||||
|
$callsSecond = $container->getDefinition('doctrine.dbal.second_connection.event_manager')->getMethodCalls();
|
||||||
|
$this->assertEquals($callsDefault, $callsSecond);
|
||||||
|
}
|
||||||
|
|
||||||
public function testProcessEventSubscribersWithPriorities()
|
public function testProcessEventSubscribersWithPriorities()
|
||||||
{
|
{
|
||||||
$container = $this->createBuilder();
|
$container = $this->createBuilder();
|
||||||
@ -114,12 +135,22 @@ class RegisterEventListenersAndSubscribersPassTest extends \PHPUnit_Framework_Te
|
|||||||
return $order;
|
return $order;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createBuilder()
|
private function createBuilder($multipleConnections = false)
|
||||||
{
|
{
|
||||||
$container = new ContainerBuilder();
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$connections = array('default' => 'doctrine.dbal.default_connection');
|
||||||
|
|
||||||
$container->register('doctrine.dbal.default_connection.event_manager', 'stdClass');
|
$container->register('doctrine.dbal.default_connection.event_manager', 'stdClass');
|
||||||
$container->register('doctrine.dbal.default_connection', 'stdClass');
|
$container->register('doctrine.dbal.default_connection', 'stdClass');
|
||||||
$container->setParameter('doctrine.connections', array('default' => 'doctrine.dbal.default_connection'));
|
|
||||||
|
if ($multipleConnections) {
|
||||||
|
$container->register('doctrine.dbal.second_connection.event_manager', 'stdClass');
|
||||||
|
$container->register('doctrine.dbal.second_connection', 'stdClass');
|
||||||
|
$connections['second'] = 'doctrine.dbal.second_connection';
|
||||||
|
}
|
||||||
|
|
||||||
|
$container->setParameter('doctrine.connections', $connections);
|
||||||
|
|
||||||
return $container;
|
return $container;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user