Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Messenger] Wire the transaction middleware factory when component is…
… enabled
- Loading branch information
Showing
7 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
/** | ||
* Class for Symfony Messenger component integrations | ||
*/ | ||
class MessengerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
// Remove wired services if the Messenger component actually isn't enabled: | ||
if (!$container->hasAlias('message_bus') || !is_subclass_of($container->findDefinition('message_bus')->getClass(), MessageBusInterface::class, true)) { | ||
$container->removeDefinition('doctrine.orm.messenger.middleware_factory.transaction'); | ||
$container->removeDefinition('messenger.middleware.doctrine_transaction_middleware'); | ||
} | ||
} | ||
} |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="doctrine.orm.messenger.middleware_factory.transaction" class="Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory" public="false"> | ||
<argument type="service" id="doctrine" /> | ||
</service> | ||
|
||
<!-- | ||
The following service isn't prefixed by the "doctrine.orm" namespace in order for end-users to just use | ||
the "doctrine_transaction_middleware" shortcut in message buses middleware config | ||
--> | ||
<service id="messenger.middleware.doctrine_transaction_middleware" class="Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware" abstract="true" public="false"> | ||
<factory service="doctrine.orm.messenger.middleware_factory.transaction" method="createMiddleware" /> | ||
<argument /> <!-- default entity manager --> | ||
</service> | ||
</services> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace DependencyInjection\Compiler; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\MessengerPass; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\Messenger\MessageBus; | ||
|
||
class MessengerPassTest extends TestCase | ||
{ | ||
public function testRemovesDefinitionsWhenMessengerComponentIsDisabled() | ||
{ | ||
$pass = new MessengerPass(); | ||
$container = new ContainerBuilder(); | ||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../../../Resources/config')); | ||
$loader->load('messenger.xml'); | ||
|
||
$pass->process($container); | ||
|
||
$this->assertFalse($container->hasDefinition('doctrine.orm.messenger.middleware_factory.transaction')); | ||
$this->assertFalse($container->hasDefinition('messenger.middleware.doctrine_transaction_middleware')); | ||
} | ||
|
||
public function testRemoveDefinitionsWhenHasAliasButNotMessengerComponent() | ||
{ | ||
$pass = new MessengerPass(); | ||
$container = new ContainerBuilder(); | ||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../../../Resources/config')); | ||
$loader->load('messenger.xml'); | ||
|
||
$container->register('some_other_bus', \stdClass::class); | ||
$container->setAlias('message_bus', 'some_other_bus'); | ||
|
||
$pass->process($container); | ||
|
||
$this->assertFalse($container->hasDefinition('doctrine.orm.messenger.middleware_factory.transaction')); | ||
$this->assertFalse($container->hasDefinition('messenger.middleware.doctrine_transaction_middleware')); | ||
} | ||
|
||
public function testDoesNotRemoveDefinitionsWhenMessengerComponentIsEnabled() | ||
{ | ||
$pass = new MessengerPass(); | ||
$container = new ContainerBuilder(); | ||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../../../Resources/config')); | ||
$loader->load('messenger.xml'); | ||
|
||
$container->register('messenger.bus.default', MessageBus::class); | ||
$container->setAlias('message_bus', 'messenger.bus.default'); | ||
|
||
$pass->process($container); | ||
|
||
$this->assertTrue($container->hasDefinition('doctrine.orm.messenger.middleware_factory.transaction')); | ||
$this->assertTrue($container->hasDefinition('messenger.middleware.doctrine_transaction_middleware')); | ||
} | ||
} |
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