Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
jderusse left a comment
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 theXxxTransport 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 OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsTransport.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
| $this->connection =$connection; | ||
| $this->serializer =$serializer ??newPhpSerializer(); | ||
| $serializer =$serializer ??newPhpSerializer(); | ||
| $this->receiver =$receiver ??newAmazonSqsReceiver($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.
chalasr left a comment
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
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/AmazonSqsTransportTest.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsTransport.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
chalasr commentedOct 27, 2020
I think so (let's make sure we are all good on the first one, we have time for 5.3.) |
jacekwilczynski commentedOct 28, 2020
chalasr commentedOct 28, 2020
@jacekwilczynski You can amend. If you don't, we will squash your commits when merging the PR |
15a9024 tof48e7c7Comparesrc/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsTransport.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/AmazonSqsTransport.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
0291fd5 to828a0f4Compareefd1863 to913d581Comparejacekwilczynski commentedNov 3, 2020
@chalasr , anything I should still change in this PR? |
derrabus left a comment
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 aAmazonSqsTransport? 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.
jderusse commentedNov 3, 2020
Hmm a |
derrabus commentedNov 3, 2020
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 |
jderusse commentedNov 4, 2020
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). |
derrabus commentedNov 4, 2020
Let's keep it that way, thanks for the explanation! |
derrabus left a comment
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 to281af26Comparederrabus commentedNov 15, 2020
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
createstatic 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
createmethod.