Skip to content

[Messenger] Lazy dependency to the Serializer #26908

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

<!-- Message encoding/decoding -->
<service id="messenger.transport.serialize_message_with_type_in_headers" class="Symfony\Component\Messenger\Transport\Serialization\Serializer">
<argument type="service" id="serializer" />
<argument type="service" id="serializer" on-invalid="null" />
</service>

<service id="messenger.transport.default_encoder" alias="messenger.transport.serialize_message_with_type_in_headers" public="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ public function process(ContainerBuilder $container)
$container->removeDefinition('messenger.middleware.debug.logging');
}

if (!$container->has('serializer')) {
$container->removeDefinition('messenger.transport.serialize_message_with_type_in_headers');
$container->removeAlias('messenger.transport.default_encoder');
$container->removeAlias('messenger.transport.default_decoder');
}

$this->registerReceivers($container);
$this->registerHandlers($container);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ class Serializer implements DecoderInterface, EncoderInterface
private $serializer;
private $format;

public function __construct(SerializerInterface $serializer, string $format = 'json')
public function __construct(SerializerInterface $serializer = null, string $format = 'json')
{
if (null === $serializer) {
throw new \InvalidArgumentException('A Serializer is required to use this Messenger transport. Try running "composer require 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.

IMO it's not the responsibility of this class to do that. It should be part of the DI pass, where it was handled before.

Copy link
Contributor

@ogizanagi ogizanagi Apr 12, 2018

Choose a reason for hiding this comment

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

I agree. The benefit is that it'd remain a compile-time check. Here it's only at runtime. Also, it's not necessarily the component that is not installed. It may just not be enabled.

}

$this->serializer = $serializer;
$this->format = $format;
}
Expand Down