Skip to content
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] Different decorator class for php serializer #257

Merged
merged 1 commit into from
Apr 27, 2024
Merged
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 @@ -18,6 +18,7 @@
use Draw\Component\Messenger\Searchable\EnvelopeFinder;
use Draw\Component\Messenger\Searchable\TransportRepository;
use Draw\Component\Messenger\SerializerEventDispatcher\EventDispatcherSerializerDecorator;
use Draw\Component\Messenger\SerializerEventDispatcher\PhpEventDispatcherSerializerDecorator;
use Draw\Component\Messenger\Transport\DrawTransportFactory;
use Draw\Component\Messenger\Transport\Entity\DrawMessageInterface;
use Draw\Component\Messenger\Transport\Entity\DrawMessageTagInterface;
Expand Down Expand Up @@ -224,13 +225,24 @@ private function loadSerializerEventDispatcher(array $config, PhpFileLoader $loa
\dirname((new \ReflectionClass(EventDispatcherSerializerDecorator::class))->getFileName()),
);

$container
->getDefinition(EventDispatcherSerializerDecorator::class)
->setDecoratedService(
$config['transport'],
$config['transport'].'.inner'
)
->setArgument(0, new Reference($config['transport'].'.inner'));
foreach ($config['decorate_serializers'] as $key => $serializer) {
$decoratorClass = EventDispatcherSerializerDecorator::class;
if ('messenger.transport.native_php_serializer' === $serializer) {
$decoratorClass = PhpEventDispatcherSerializerDecorator::class;
}

$container
->setDefinition(
'draw.messenger.serializer_event_dispatcher'.$key,
(new Definition($decoratorClass))
->setAutowired(true)
->setDecoratedService(
$serializer,
$serializer.'.inner'
)
->setArgument(0, new Reference($serializer.'.inner'))
);
}
}

private function loadVersioning(array $config, PhpFileLoader $loader, ContainerBuilder $container): void
Expand Down Expand Up @@ -394,7 +406,13 @@ private function createSerializerEventDispatcherNode(): ArrayNodeDefinition
return (new ArrayNodeDefinition('serializer_event_dispatcher'))
->canBeEnabled()
->children()
->scalarNode('transport')->defaultValue('messenger.transport.native_php_serializer')->end()
->arrayNode('decorate_serializers')
->defaultValue([
'messenger.transport.native_php_serializer',
'messenger.transport.symfony_serializer',
])
->scalarPrototype()->end()
->end()
->end();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Draw\Component\Messenger\Retry\EventDrivenRetryStrategy;
use Draw\Component\Messenger\Searchable\EnvelopeFinder;
use Draw\Component\Messenger\Searchable\TransportRepository;
use Draw\Component\Messenger\SerializerEventDispatcher\EventDispatcherSerializerDecorator;
use Draw\Component\Messenger\Transport\DrawTransportFactory;
use Draw\Component\Messenger\Transport\Entity\DrawMessageInterface;
use Draw\Component\Messenger\Transport\Entity\DrawMessageTagInterface;
Expand Down Expand Up @@ -91,7 +90,10 @@ public function getDefaultConfiguration(): array
],
'serializer_event_dispatcher' => [
'enabled' => false,
'transport' => 'messenger.transport.native_php_serializer',
'decorate_serializers' => [
'messenger.transport.native_php_serializer',
'messenger.transport.symfony_serializer',
],
],
'versioning' => [
'enabled' => false,
Expand Down Expand Up @@ -207,18 +209,16 @@ public static function provideTestLoad(): iterable
[
[
'serializer_event_dispatcher' => [
'transport' => 'messenger.transport.serializer',
'decorate_serializers' => ['messenger.transport.serializer'],
],
],
],
array_merge(
$defaultServices,
[
new ServiceConfiguration(
'draw.messenger.serializer_event_dispatcher.event_dispatcher_serializer_decorator',
[
EventDispatcherSerializerDecorator::class,
],
'draw.messenger.serializer_event_dispatcher0',
[],
function (Definition $definition): void {
static::assertSame(
['messenger.transport.serializer', 'messenger.transport.serializer.inner', 0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use Draw\Component\Messenger\SerializerEventDispatcher\Event\PostDecodeEvent;
use Draw\Component\Messenger\SerializerEventDispatcher\Event\PostEncodeEvent;
use Draw\Component\Messenger\SerializerEventDispatcher\Event\PreEncodeEvent;
use Symfony\Component\DependencyInjection\Attribute\Exclude;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

#[Exclude]
class EventDispatcherSerializerDecorator implements SerializerInterface
{
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Draw\Component\Messenger\SerializerEventDispatcher;

use Draw\Component\Messenger\SerializerEventDispatcher\Event\PostDecodeEvent;
use Draw\Component\Messenger\SerializerEventDispatcher\Event\PostEncodeEvent;
use Draw\Component\Messenger\SerializerEventDispatcher\Event\PreEncodeEvent;
use Symfony\Component\DependencyInjection\Attribute\Exclude;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

#[Exclude]
class PhpEventDispatcherSerializerDecorator extends PhpSerializer
{
public function __construct(
private PhpSerializer $serializer,
private EventDispatcherInterface $eventDispatcher
) {
}

public function decode(array $encodedEnvelope): Envelope
{
$envelope = $this->serializer->decode($encodedEnvelope);

return $this->eventDispatcher->dispatch(new PostDecodeEvent($envelope))->getEnvelope();
}

public function encode(Envelope $envelope): array
{
$envelope = $this->eventDispatcher->dispatch(new PreEncodeEvent($envelope))->getEnvelope();

$result = $this->serializer->encode($envelope);

$this->eventDispatcher->dispatch(new PostEncodeEvent($envelope));

return $result;
}
}
Loading