-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Make all the dependencies of AmazonSqsTransport injectable #38846
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] Make all the dependencies of AmazonSqsTransport injectable #38846
Conversation
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.
Thank for this great work.
All transports (amqp
, doctrine
, redis
, sqs
...) are currently using the same pattern. And the XxxTransport
are identical.
I wonder if it wouldn't make sens to apply this PR to all transports for consistency?
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/AmazonSqsTransportTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsTransport.php
Outdated
Show resolved
Hide resolved
{ | ||
$this->connection = $connection; | ||
$this->serializer = $serializer ?? new PhpSerializer(); | ||
$serializer = $serializer ?? new PhpSerializer(); | ||
$this->receiver = $receiver ?? new AmazonSqsReceiver($connection, $serializer); |
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.
Creation of receiver and sender were lay-loaded on purpose. Let these properties null and revert code in methods below.
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.
No problem, I can do it, but may I ask why? The default receiver and sender have constructors with just basic assignments so they're extremely cheap to create, I thought it wasn't worth the additional code complexity.
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.
Thanks for the PR
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/AmazonSqsTransportTest.php
Show resolved
Hide resolved
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsTransport.php
Outdated
Show resolved
Hide resolved
I think so (let's make sure we are all good on the first one, we have time for 5.3.) |
@jacekwilczynski You can amend. If you don't, we will squash your commits when merging the PR |
15a9024
to
f48e7c7
Compare
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsTransport.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsTransport.php
Outdated
Show resolved
Hide resolved
0291fd5
to
828a0f4
Compare
efd1863
to
913d581
Compare
@chalasr , anything I should still change in this PR? |
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.
What I was wondering: Do we really need two ways to construct a AmazonSqsTransport
? If not, can we change the constructor to this?
public function __construct(Connection $connection, ReceiverInterface $receiver = null, SenderInterface $sender = null)
Other than that, the change looks reasonable.
Hmm a |
Is it? Right now, the constructor is able to build a And if you want to change the serializer implementation, you could still do that with the signature I proposed by constructing |
I mean, today, all bridges lazy build the sender/receiver (I don't know why. As already pointed in this PR, this looks like a micro-optimization). |
Let's keep it that way, thanks for the explanation! |
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.
Let's wait until 5.2 has been branched out and merge to 5.x then.
913d581
to
281af26
Compare
Thank you @jacekwilczynski. |
This is a pure refactoring PR that enables more flexibility with service injection without actually changing any behaviour or breaking backwards compatibility. It satisfies only 1 of 2 acceptance criteria of #38640 but since they're independent, I'm not marking the PR as WIP.
Receiver & sender injection into AmazonSqsTransport
It is now possible to inject your own receiver and sender into
Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransport
.Recommended way - AmazonSqsTransport::create
For clean dependency injection, I recommed using the
create
static method, which obliges you to pass all dependencies:For example, this code from
Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory
:could be replaced with this:
I didn't replace that code in the factory because I didn't find it essential but I will certainly do it in my custom factory in my project, passing my own receiver implementation.
Using the main constructor
You can still use the main constructor but it's most suited for backwards compatibility, i.e. when you don't want to inject a receiver or a sender. With the full list of arguments it gets a bit messy due to their optionality.
Minimal call
As before this PR, a receiver and a sender will be created using the default serializer, i.e.
Symfony\Component\Messenger\Transport\Serialization\PhpSerializer
.With a custom serializer
As before this PR, a receiver and a sender will be created using the passed serializer.
With a custom receiver and sender
The injected services will be used. The second parameter (serializer) is unnecessary because it was only ever used while creating a receiver and a sender inside the transport. Because of this, I recommend using the new static
create
method.