Skip to content

Commit

Permalink
Merge branch 'next-36479/messenger-fix' into 'trunk'
Browse files Browse the repository at this point in the history
NEXT-36479 - Move routing overwrites config to shopware domain

See merge request shopware/6/product/platform!14057
  • Loading branch information
OliverSkroblin committed Jun 13, 2024
2 parents b727572 + b43b3ed commit e70b892
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 8 deletions.
35 changes: 35 additions & 0 deletions changelog/_unreleased/2024-06-05-move-routing-overwrite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: Move routing overwrite
issue: NEXT-36479
author: Oliver Skroblin
author_github: OliverSkroblin
---
# Core
* Deprecated `framework.messenger.routing` overwrite logic and moved the config to `shopware.messenger.routing_overwrite`

___
# Upgrade Information
## Messenger routing overwrite

The overwriting logic for the messenger routing has been moved from `framework.messenger.routing` to `shopware.messenger.routing_overwrite`. The old config key is still supported but deprecated and will be removed in the next major release.
The new config key considers also the `instanceof` logic of the default symfony behavior.

We have made these changes for various reasons:
1) In the old logic, the `instanceof` logic of Symfony was not taken into account. This means that only exact matches of the class were overwritten.
2) It is not possible to simply backport this in the same config, as not only the project overwrites are in the old config, but also the standard Shopware routing configs.

```yaml

#before
framework:
messenger:
routing:
Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage: entity_indexing

#after
shopware:
messenger:
routing_overwrite:
Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage: entity_indexing

```
17 changes: 17 additions & 0 deletions config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
"stock": {
"$ref": "#/definitions/stock"
},
"messenger": {
"$ref": "#/definitions/messenger"
},
"usage_data": {
"$ref": "#/definitions/usage_data"
},
Expand Down Expand Up @@ -260,6 +263,20 @@
}
}
},
"messenger": {
"type": "object",
"additionalProperties": false,
"properties": {
"routing_overwrite": {
"type": "array",
"title": "Allows to overwrite the routing configuration of the messenger component",
"uniqueItems": true,
"items": {
"type": "string"
}
}
}
},
"mail": {
"type": "object",
"additionalProperties": false,
Expand Down
42 changes: 35 additions & 7 deletions src/Core/Framework/Adapter/Messenger/MessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Shopware\Core\Framework\Adapter\Messenger;

use Shopware\Core\Framework\Feature;
use Shopware\Core\Framework\Log\Package;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -16,10 +17,15 @@ class MessageBus implements MessageBusInterface
{
/**
* @param array<string, string|string[]> $routing
* @param array<string, string|string[]> $overwrite
*/
public function __construct(
private readonly MessageBusInterface $decorated,
private readonly array $routing
/**
* @deprecated tag:v6.7.0 - Will be removed in v6.7.0.0. Use $overwrite instead
*/
private readonly array $routing,
private readonly array $overwrite
) {
}

Expand All @@ -32,13 +38,23 @@ public function dispatch(object $message, array $stamps = []): Envelope
return $this->decorated->dispatch($message, $stamps);
}

$transports = $this->getTransports($message);
$transports = $this->getTransports($message, $this->overwrite, true);
if (!empty($transports)) {
$stamps[] = new TransportNamesStamp($transports);

if (empty($transports)) {
return $this->decorated->dispatch($message, $stamps);
}

$stamps[] = new TransportNamesStamp($transports);
if (Feature::isActive('v6.7.0.0')) {
return $this->decorated->dispatch($message, $stamps);
}

$transports = $this->getTransports($message, $this->routing, false);
if (!empty($transports)) {
$stamps[] = new TransportNamesStamp($transports);

return $this->decorated->dispatch($message, $stamps);
}

return $this->decorated->dispatch($message, $stamps);
}
Expand All @@ -52,16 +68,28 @@ private function hasTransportStamp(array $stamps): bool
}

/**
* @param array<string, string|string[]> $overwrites
*
* @return array<string>|string|null
*/
private function getTransports(object $message): array|string|null
private function getTransports(object $message, array $overwrites, bool $inherited): array|string|null
{
$class = $message::class;

if (!\array_key_exists($class, $this->routing)) {
if (\array_key_exists($class, $overwrites)) {
return $overwrites[$class];
}

if (!$inherited) {
return null;
}

return $this->routing[$class];
foreach ($overwrites as $class => $transports) {
if ($message instanceof $class) {
return $transports;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Shopware\Core\Framework\DependencyInjection\CompilerPass;

use Shopware\Core\Framework\Feature;
use Shopware\Core\Framework\Log\Package;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -21,6 +22,11 @@ public function process(ContainerBuilder $container): void
$id = 'messenger.transport.' . $defaultName;
$container->addAliases(['messenger.default_transport' => $id]);

if (Feature::isActive('v6.7.0.0')) {
return;
}

// @deprecated tag:v6.7.0 - remove all code below, overwrites are now handled via shopware.messenger.routing_overwrites
$config = $this->getConfig($container, 'framework');

if (!\array_key_exists('messenger', $config)) {
Expand Down
17 changes: 17 additions & 0 deletions src/Core/Framework/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->append($this->createFeatureToggleNode())
->append($this->createStagingNode())
->append($this->createSystemConfigNode())
->append($this->createMessengerSection())
->end();

return $treeBuilder;
Expand Down Expand Up @@ -902,4 +903,20 @@ private function createHttpCacheSection(): ArrayNodeDefinition

return $rootNode;
}

private function createMessengerSection(): ArrayNodeDefinition
{
$treeBuilder = new TreeBuilder('messenger');

$rootNode = $treeBuilder->getRootNode();
$rootNode
->children()
->arrayNode('routing_overwrite')
->useAttributeAsKey('name')
->scalarPrototype()->end()
->end()
->end();

return $rootNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@
<argument type="service" id="event_dispatcher"/>
<tag name="kernel.event_subscriber"/>
<tag name="console.command"/>
<argument type="service" id="messenger.bus.shopware"/>
<argument type="service" id="Doctrine\DBAL\Connection"/>
</service>

<service id="Shopware\Core\Framework\DataAbstractionLayer\Indexing\Subscriber\RegisteredIndexerSubscriber">
Expand Down
1 change: 1 addition & 0 deletions src/Core/Framework/DependencyInjection/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ base-uri 'self';
<service id="messenger.bus.shopware" class="Shopware\Core\Framework\Adapter\Messenger\MessageBus" decorates="messenger.default_bus">
<argument type="service" id=".inner"/>
<argument type="collection"></argument>
<argument>%shopware.messenger.routing_overwrite%</argument>
</service>

<service id="http_kernel" class="Shopware\Core\Framework\Adapter\Kernel\HttpKernel" public="true">
Expand Down
4 changes: 4 additions & 0 deletions src/Core/Framework/Resources/config/packages/shopware.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,7 @@ shopware:

system_config:
default: []

messenger:
routing_overwrite:

24 changes: 23 additions & 1 deletion tests/unit/Core/Framework/Adapter/Messenger/MessageBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\Product\DataAbstractionLayer\ProductIndexingMessage;
use Shopware\Core\Framework\Adapter\Messenger\MessageBus;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage;
use Shopware\Core\Framework\Feature;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\MessageQueue\AsyncMessageInterface;
use Shopware\Core\Framework\MessageQueue\LowPriorityMessageInterface;
Expand All @@ -30,13 +33,32 @@ class MessageBusTest extends TestCase
#[DataProvider('dispatchProvider')]
public function testDispatch(object $message, array $config, array $providedStamps, array $expectedStamps): void
{
$bus = new MessageBus(new Collector(), $config);
if (Feature::isActive('v6.7.0.0')) {
$bus = new MessageBus(new Collector(), [], $config);
} else {
$bus = new MessageBus(new Collector(), $config, []);
}

$envelope = $bus->dispatch($message, $providedStamps);

static::assertEquals(new Envelope($message, $expectedStamps), $envelope);
}

public function testOverwrite(): void
{
Feature::skipTestIfInActive('v6.7.0.0', $this);

$bus = new MessageBus(new Collector(), [], [
EntityIndexingMessage::class => 'low_priority',
]);

$message = new ProductIndexingMessage([]);

$envelope = $bus->dispatch($message);

static::assertEquals(new Envelope($message, [new TransportNamesStamp('low_priority')]), $envelope);
}

public static function dispatchProvider(): \Generator
{
yield 'No config, no stamps' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Shopware\Core\Content\Product\DataAbstractionLayer\ProductIndexingMessage;
use Shopware\Core\Framework\Adapter\Messenger\MessageBus;
use Shopware\Core\Framework\DependencyInjection\CompilerPass\DefaultTransportCompilerPass;
use Shopware\Core\Framework\Feature;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

Expand All @@ -18,6 +19,8 @@ class DefaultTransportCompilerPassTest extends TestCase
{
public function testCompiler(): void
{
Feature::skipTestIfActive('v6.7.0.0', $this);

$config = [
'routing' => [
ProductIndexingMessage::class => 'foo',
Expand Down

0 comments on commit e70b892

Please sign in to comment.