Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Allow to configure the transport #26941

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

Merged
merged 1 commit into from
Apr 17, 2018

Conversation

sroze
Copy link
Contributor

@sroze sroze commented Apr 16, 2018

Q A
Branch? master
Bug fix? no
New feature? ish
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #26900, #26908, #26935
License MIT
Doc PR ø

We allow users to configure the encoder/decoder used by the built-in adapter(s). This also adds the support of configuring the default's encoder/decoder format and context.

@@ -1446,6 +1446,20 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder

$loader->load('messenger.xml');

if ($config['transport']['serializer']['enabled']) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Detail, but could use Extension::isConfigEnabled

$container->removeAlias('messenger.transport.default_decoder');
if (!$container->has('serializer') && $container->hasAlias('messenger.transport.encoder')) {
if ('messenger.transport.symfony_serializer_encoder' === (string) $container->getAlias('messenger.transport.encoder')) {
throw new RuntimeException('Using the default encoder/decoder, Symfony Messenger requires the Serializer. Try running "composer require symfony/serializer-pack".');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\LogicException

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could also happen when the serializer component is simply disabled (but sure, using flex the component is enabled by default when installed).

However, what about throwing directly from the FrameworkExtension? When $this->isConfigEnabled($container, $config['transport']['serializer']) is true and the $this->isConfigEnabled($container, $config['serializer']) is false, we should throw an exception about enabling the component. If trying to enabling it without the component installed, the Fwb ext will already hint for installing the component.

The 'messenger.transport.symfony_serializer_encoder' === (string) $container->getAlias('messenger.transport.encoder') check doesn't look really relevant to me. If the messenger.transport.serializer config is enabled without the Serializer component installed/enabled, it should throw anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, what about throwing directly from the FrameworkExtension? When $this->isConfigEnabled($container, $config['transport']['serializer']) is true and the $this->isConfigEnabled($container, $config['serializer']) is false, we should throw an exception about enabling the component. If trying to enabling it without the component installed, the Fwb ext will already hint for installing the component.

Very good idea!

</service>

<service id="messenger.transport.default_encoder" alias="messenger.transport.serialize_message_with_type_in_headers" public="true" />
<service id="messenger.transport.default_decoder" alias="messenger.transport.serialize_message_with_type_in_headers" public="true" />
<service id="messenger.transport.symfony_serializer_encoder" alias="messenger.transport.symfony_serializer" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those aliases really needed? I think messenger.transport.symfony_serializer is fine as default encoder/decoder config value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, definitely now that we have the aliases generated by FWB.

@sroze sroze added this to the 4.1 milestone Apr 16, 2018
@sroze sroze force-pushed the configure-messenger-transport branch from f8145c4 to 2fd43ab Compare April 16, 2018 12:50
@sroze
Copy link
Contributor Author

sroze commented Apr 16, 2018

@ogizanagi changed the PR based on your feedbacks. Looks better 👌

@sroze sroze force-pushed the configure-messenger-transport branch 2 times, most recently from 6de67e1 to 97ffc7b Compare April 16, 2018 12:52
{
if (!interface_exists(MessageBusInterface::class)) {
throw new LogicException('Messenger support cannot be enabled as the Messenger component is not installed.');
}

$loader->load('messenger.xml');

if ($this->isConfigEnabled($container, $config['transport']['serializer'])) {
if (count($config['adapters']) > 0 && !$this->isConfigEnabled($container, $serializerConfig)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the count($config['adapters']) > 0 required?
To me, as soon as the $config['transport']['serializer'] is enabled without serializer support enabled we should throw anyway. We don't really care if it's used by an adapter or not at this point:
in any case, messenger.transport.serializer will never be automatically enabled, unless using flex and both Serializer and Messenger component are installed (so everything is fine).
If enabled without without the Serializer installed/enabled, it means the developer explicitly enabled messenger.transport.serializer or disabled serializer. 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually changing this (see the last commit). Now this makes sense (by have the Symfony Serialized based Transport enabled no matter what). It's important to have the count($config['adapters']) so that it doesn't throw anything if you don't use any adapter (i.e. don't require an encoder/decoder).

@sroze sroze force-pushed the configure-messenger-transport branch 2 times, most recently from 3c41fb9 to 45cdf4e Compare April 16, 2018 17:42
->end()
->end()
->end()
->scalarNode('encoder')->defaultValue('messenger.transport.symfony_serializer')->end()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about removing the symdony_ here (and in the other places in this PR also)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see either a benefit nor a downside 🙃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except that it's about Symfony's internal here so it might make sense to remove symfony_

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed 👍

</service>

<service id="messenger.transport.default_encoder" alias="messenger.transport.serialize_message_with_type_in_headers" public="true" />
<service id="messenger.transport.default_decoder" alias="messenger.transport.serialize_message_with_type_in_headers" public="true" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to move to PHP? If not, better keep here, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a reason: it's now configurable via the framework.messenger.transport configuration.

@sroze
Copy link
Contributor Author

sroze commented Apr 17, 2018

Though, if we rename "adapters" to "transports" this would clash 🤔. WDYT about removing this .transport configuration layer and just having framework.messenger.encoder, framework.messenger.decoder & framework.messenger.serializer ?

@nicolas-grekas
Copy link
Member

Yes for less nesting levels

@sroze sroze force-pushed the configure-messenger-transport branch from b8e7347 to 1a3f0bb Compare April 17, 2018 16:48
@sroze sroze merged commit 1a3f0bb into symfony:master Apr 17, 2018
sroze added a commit that referenced this pull request Apr 17, 2018
This PR was squashed before being merged into the 4.1-dev branch (closes #26941).

Discussion
----------

[Messenger] Allow to configure the transport

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | ish
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26900, #26908, #26935
| License       | MIT
| Doc PR        | ø

We allow users to configure the encoder/decoder used by the built-in adapter(s). This also adds the support of configuring the default's encoder/decoder format and context.

Commits
-------

1a3f0bb [Messenger] Allow to configure the transport
@sroze sroze deleted the configure-messenger-transport branch April 17, 2018 16:49
@fabpot fabpot mentioned this pull request May 7, 2018
@r3m4k3
Copy link

r3m4k3 commented Oct 7, 2018

Why framework.messenger.serializer is expecting an array instead of a string?

@sroze
Copy link
Contributor Author

sroze commented Oct 7, 2018

@r3m4k3 it does now supports a string directly on framework.messenger.serializer (it will configure framework.messenger.serializer.id)

@r3m4k3
Copy link

r3m4k3 commented Oct 7, 2018

@sroze Thank you for the answer. But hmm, I got this error: Invalid type for path "framework.messenger.serializer". Expected array, but got string when I set serializer: App\Serializer\CustomSerializer under framework.messenger.

@sroze
Copy link
Contributor Author

sroze commented Oct 7, 2018

"now" is in 4.2, the development branch. So make sure you use it :)

@r3m4k3
Copy link

r3m4k3 commented Oct 7, 2018

@sroze I have managed to do this, now I use dev branch.
I have installed symfony/* component with all dependencies on version 4.2@dev.

But is there any example of this serializer implementing Encoder and DecoderInterface, which is handling circular references properly?

I'm asking because it's working like it still loads default symfony serializer instead of my custom, setting setCircularReferenceLimit or setCircularReferenceHandler doesn't change anything indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants