New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Messenger] Wire the transaction middleware factory when component is enabled #817
Merged
kimhemsoe
merged 2 commits into
doctrine:master
from
ogizanagi:messenger/transaction_middleware_factory
Sep 12, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,27 @@ | ||
<?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) | ||
{ | ||
if ($container->hasAlias('message_bus') && is_subclass_of($container->findDefinition('message_bus')->getClass(), MessageBusInterface::class)) { | ||
return; | ||
} | ||
|
||
// Remove wired services if the Messenger component actually isn't enabled: | ||
$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,68 @@ | ||
<?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; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
class MessengerPassTest extends TestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
if (interface_exists(MessageBusInterface::class)) { | ||
return; | ||
} | ||
|
||
$this->markTestSkipped('Symfony Messenger component is not installed'); | ||
} | ||
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd argue that it should be a dev-dependency, otherwise it's untested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I though
symfony/symfony
was used on CI for specific versions. So I expected it to be available in the new build.The issue with adding it into the
dev-dependencies
(see first commit) is that it'll conflict with lowest dependencies tested for this packages due to the component requirements. So it must be explicitly added on a specific build. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can override it for now using
^4.1@beta
, the@beta
part would be dropped once 4.1 is stable. But yes, it wouldn't allow SF pre-3.4. Since there is only one (or two) CI job to test SF 2.x, I think it'd be better to drop this dependency only in such job.(Another argument for dropping 2.x support soon.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would actually require dropping
symfony/messenger
for every build without PHP 7.1, aminimum-stability: dev
or with a specificSYMFONY_VERSION
except for the new 4.1-beta2 one, as the component is only available since 4.1 and the required classes would be part of the beta2 (so it'll be quite a mess).For now, I've just added the
symfony/messenger
component on the new build, so this build will pass once the related PR onsymfony/symfony
is merged.But if there is a better strategy to you, please feel free to push the required changes to my branch :) Thank you!