Skip to content

Commit a1db8b0

Browse files
committed
NEXT-39755 - Refactor message bus decorator to middleware
1 parent bec00f9 commit a1db8b0

33 files changed

+391
-260
lines changed

.danger.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
const BaseTestClasses = [
3232
'RuleTestCase',
3333
'TestCase',
34+
'MiddlewareTestCase',
3435
];
3536

3637
return (new Config())
@@ -394,7 +395,10 @@ function checkMigrationForBundle(string $bundle, Context $context): void
394395
}
395396
})
396397
->useRule(function (Context $context): void {
397-
$addedUnitTests = $context->platform->pullRequest->getFiles()->filterStatus(File::STATUS_ADDED)->matches('tests/unit/**/*Test.php');
398+
$addedUnitTests = $context->platform->pullRequest->getFiles()
399+
->filter(fn (File $file) => in_array($file->status, [File::STATUS_ADDED, File::STATUS_MODIFIED], true))
400+
->matches('tests/unit/**/*Test.php');
401+
398402
$addedSrcFiles = $context->platform->pullRequest->getFiles()->filterStatus(File::STATUS_ADDED)->matches('src/**/*.php');
399403
$missingUnitTests = [];
400404
$unitTestsName = [];
@@ -425,6 +429,7 @@ function checkMigrationForBundle(string $bundle, Context $context): void
425429
$context->warning(sprintf('Was not able to load phpunit config file %s. Please check configuration.', $phpUnitConfig));
426430
}
427431

432+
428433
foreach ($addedUnitTests as $file) {
429434
$content = $file->getContent();
430435

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Deprecate messenger.bus.shopware service
3+
issue: NEXT-39755
4+
---
5+
# Core
6+
* Deprecated the `messenger.bus.shopware` service. The functionality provided by our decorator has been moved to middleware so you can safely use `messenger.default_bus` instead.
7+
___
8+
# Upgrade Information
9+
## Deprecated `messenger.bus.shopware` service
10+
Change your usages of `messenger.bus.shopware` to `messenger.default_bus`. As long as you typed the interface `\Symfony\Component\Messenger\MessageBusInterface`, your code will work as expected.
11+
12+
___
13+
# Next Major Version Changes
14+
## Removed `messenger.bus.shopware` service
15+
Use `messenger.default_bus` instead.

phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -2926,11 +2926,6 @@ parameters:
29262926
count: 1
29272927
path: src/Core/Framework/Demodata/PersonalData/CleanPersonalDataCommand.php
29282928

2929-
-
2930-
message: "#^Throwing new exceptions within classes are not allowed\\. Please use domain exception pattern\\. See https\\://github\\.com/shopware/platform/blob/v6\\.4\\.20\\.0/adr/2022\\-02\\-24\\-domain\\-exceptions\\.md$#"
2931-
count: 1
2932-
path: src/Core/Framework/DependencyInjection/CompilerPass/DefaultTransportCompilerPass.php
2933-
29342929
-
29352930
message: "#^Throwing new exceptions within classes are not allowed\\. Please use domain exception pattern\\. See https\\://github\\.com/shopware/platform/blob/v6\\.4\\.20\\.0/adr/2022\\-02\\-24\\-domain\\-exceptions\\.md$#"
29362931
count: 2

src/Core/Content/DependencyInjection/category.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Indexing\TreeUpdater"/>
7474
<argument type="service" id="Shopware\Core\Content\Category\DataAbstractionLayer\CategoryBreadcrumbUpdater"/>
7575
<argument type="service" id="event_dispatcher"/>
76-
<argument type="service" id="messenger.bus.shopware"/>
76+
<argument type="service" id="messenger.default_bus"/>
7777
<tag name="shopware.entity_indexer"/>
7878
</service>
7979

src/Core/Content/DependencyInjection/flow.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
</service>
171171

172172
<service id="Shopware\Core\Content\Flow\Indexing\FlowIndexerSubscriber">
173-
<argument type="service" id="messenger.bus.shopware"/>
173+
<argument type="service" id="messenger.default_bus"/>
174174
<tag name="kernel.event_subscriber"/>
175175
</service>
176176

src/Core/Content/DependencyInjection/import_export.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<argument type="service" id="Shopware\Core\Framework\Validation\DataValidator" />
6666
<argument type="service" id="Shopware\Core\Content\ImportExport\ImportExportFactory"/>
6767
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry"/>
68-
<argument type="service" id="messenger.bus.shopware"/>
68+
<argument type="service" id="messenger.default_bus"/>
6969
<argument type="service" id="Shopware\Core\Content\ImportExport\Service\MappingService"/>
7070

7171
<call method="setContainer">
@@ -233,7 +233,7 @@
233233
</service>
234234

235235
<service id="Shopware\Core\Content\ImportExport\Message\ImportExportHandler" public="true">
236-
<argument type="service" id="messenger.bus.shopware"/>
236+
<argument type="service" id="messenger.default_bus"/>
237237
<argument type="service" id="Shopware\Core\Content\ImportExport\ImportExportFactory"/>
238238
<argument type="service" id="event_dispatcher"/>
239239
<tag name="messenger.message_handler" />
@@ -258,7 +258,7 @@
258258

259259
<!-- Subscriber -->
260260
<service id="Shopware\Core\Content\ImportExport\Event\Subscriber\FileDeletedSubscriber">
261-
<argument type="service" id="messenger.bus.shopware"/>
261+
<argument type="service" id="messenger.default_bus"/>
262262

263263
<tag name="kernel.event_subscriber"/>
264264
</service>

src/Core/Content/DependencyInjection/media.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
<argument type="service" id="Shopware\Core\Content\Media\Thumbnail\ThumbnailService"/>
9898
<argument type="service" id="Shopware\Core\Content\Media\Metadata\MetadataLoader"/>
9999
<argument type="service" id="Shopware\Core\Content\Media\TypeDetector\TypeDetector"/>
100-
<argument type="service" id="messenger.bus.shopware" />
100+
<argument type="service" id="messenger.default_bus" />
101101
<argument type="service" id="event_dispatcher" />
102102
<argument type="service" id="Shopware\Core\Content\Media\Core\Application\MediaLocationBuilder" />
103103
<argument type="service" id="Shopware\Core\Content\Media\Core\Application\AbstractMediaPathStrategy" />
@@ -130,7 +130,7 @@
130130
<argument type="service" id="Shopware\Core\Content\Media\Thumbnail\ThumbnailService"/>
131131
<argument type="service" id="media.repository"/>
132132
<argument type="service" id="media_folder.repository"/>
133-
<argument type="service" id="messenger.bus.shopware"/>
133+
<argument type="service" id="messenger.default_bus"/>
134134
<argument>%shopware.media.remote_thumbnails.enable%</argument>
135135

136136
<tag name="console.command"/>
@@ -316,7 +316,7 @@
316316
<service id="Shopware\Core\Content\Media\Subscriber\MediaDeletionSubscriber">
317317
<argument type="service" id="event_dispatcher"/>
318318
<argument type="service" id="media_thumbnail.repository"/>
319-
<argument type="service" id="messenger.bus.shopware"/>
319+
<argument type="service" id="messenger.default_bus"/>
320320
<argument type="service" id="Shopware\Core\Content\Media\Message\DeleteFileHandler"/>
321321
<argument type="service" id="Doctrine\DBAL\Connection"/>
322322
<argument type="service" id="media.repository"/>

src/Core/Content/DependencyInjection/newsletter_recipient.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
</service>
6868

6969
<service id="Shopware\Core\Content\Newsletter\Event\Subscriber\NewsletterRecipientDeletedSubscriber">
70-
<argument type="service" id="messenger.bus.shopware"/>
70+
<argument type="service" id="messenger.default_bus"/>
7171
<tag name="kernel.event_subscriber"/>
7272
</service>
7373

src/Core/Content/DependencyInjection/product.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,15 @@
406406
<argument type="service" id="Shopware\Core\Content\Product\DataAbstractionLayer\CheapestPriceUpdater"/>
407407
<argument type="service" id="Shopware\Core\Content\Product\DataAbstractionLayer\ProductStreamUpdater"/>
408408
<argument type="service" id="Shopware\Core\Content\Product\DataAbstractionLayer\StatesUpdater"/>
409-
<argument type="service" id="messenger.bus.shopware"/>
409+
<argument type="service" id="messenger.default_bus"/>
410410
<tag name="shopware.entity_indexer" priority="100"/>
411411
</service>
412412

413413
<service id="Shopware\Core\Content\Product\DataAbstractionLayer\ProductStreamUpdater">
414414
<argument type="service" id="Doctrine\DBAL\Connection"/>
415415
<argument type="service" id="Shopware\Core\Content\Product\ProductDefinition"/>
416416
<argument type="service" id="product.repository"/>
417-
<argument type="service" id="messenger.bus.shopware"/>
417+
<argument type="service" id="messenger.default_bus"/>
418418
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Indexing\ManyToManyIdFieldUpdater"/>
419419
<tag name="shopware.entity_indexer"/>
420420
</service>

src/Core/Content/DependencyInjection/product_export.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<argument type="service" id="Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory"/>
6565
<argument type="service" id="sales_channel.repository"/>
6666
<argument type="service" id="product_export.repository"/>
67-
<argument type="service" id="messenger.bus.shopware"/>
67+
<argument type="service" id="messenger.default_bus"/>
6868
<tag name="messenger.message_handler"/>
6969
</service>
7070

@@ -73,7 +73,7 @@
7373
<argument type="service" id="Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory"/>
7474
<argument type="service" id="product_export.repository"/>
7575
<argument type="service" id="Shopware\Core\Content\ProductExport\Service\ProductExportFileHandler"/>
76-
<argument type="service" id="messenger.bus.shopware"/>
76+
<argument type="service" id="messenger.default_bus"/>
7777
<argument type="service" id="Shopware\Core\Content\ProductExport\Service\ProductExportRenderer"/>
7878
<argument type="service" id="Shopware\Core\Framework\Adapter\Translation\Translator"/>
7979
<argument type="service" id="Shopware\Core\System\SalesChannel\Context\SalesChannelContextService"/>

src/Core/Content/DependencyInjection/sitemap.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
<argument type="service" id="logger"/>
115115
<argument type="service" id="sales_channel.repository"/>
116116
<argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/>
117-
<argument type="service" id="messenger.bus.shopware"/>
117+
<argument type="service" id="messenger.default_bus"/>
118118
<argument type="service" id="event_dispatcher"/>
119119

120120
<tag name="messenger.message_handler"/>

src/Core/Framework/Adapter/Messenger/MessageBus.php

-97
This file was deleted.

src/Core/Framework/DependencyInjection/CompilerPass/DefaultTransportCompilerPass.php

+2-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Shopware\Core\Framework\DependencyInjection\CompilerPass;
44

5-
use Shopware\Core\Framework\Feature;
5+
use Shopware\Core\Framework\DependencyInjection\DependencyInjectionException;
66
use Shopware\Core\Framework\Log\Package;
77
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -17,32 +17,9 @@ public function process(ContainerBuilder $container): void
1717
// the default transport is defined by the parameter `messenger.default_transport_name`
1818
$defaultName = $container->getParameter('messenger.default_transport_name');
1919
if (!\is_string($defaultName)) {
20-
throw new \TypeError('Parameter `messenger.default_transport_name` should be a string.');
20+
throw DependencyInjectionException::parameterHasWrongType('messenger.default_transport_name', 'string', get_debug_type($defaultName));
2121
}
2222
$id = 'messenger.transport.' . $defaultName;
2323
$container->addAliases(['messenger.default_transport' => $id]);
24-
25-
if (Feature::isActive('v6.7.0.0')) {
26-
return;
27-
}
28-
29-
// @deprecated tag:v6.7.0 - remove all code below, overwrites are now handled via shopware.messenger.routing_overwrites
30-
$config = $this->getConfig($container, 'framework');
31-
32-
if (!\array_key_exists('messenger', $config)) {
33-
return;
34-
}
35-
36-
$mapped = [];
37-
foreach ($config['messenger']['routing'] as $message => $transports) {
38-
if (!\array_key_exists('senders', $transports)) {
39-
continue;
40-
}
41-
$mapped[$message] = array_shift($transports['senders']);
42-
}
43-
44-
$container
45-
->getDefinition('messenger.bus.shopware')
46-
->replaceArgument(1, $mapped);
4724
}
4825
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Shopware\Core\Framework\DependencyInjection\CompilerPass;
4+
5+
use Shopware\Core\Framework\Log\Package;
6+
use Shopware\Core\Framework\MessageQueue\Middleware\RoutingOverwriteMiddleware;
7+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
8+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
use Symfony\Component\DependencyInjection\Reference;
11+
12+
#[Package('core')]
13+
class MessengerMiddlewareCompilerPass implements CompilerPassInterface
14+
{
15+
use CompilerPassConfigTrait;
16+
17+
public function process(ContainerBuilder $container): void
18+
{
19+
$messageBus = $container->getDefinition('messenger.bus.default');
20+
21+
$middlewares = $messageBus->getArgument(0);
22+
23+
\assert($middlewares instanceof IteratorArgument);
24+
25+
$messageBus->replaceArgument(
26+
0,
27+
new IteratorArgument([
28+
new Reference(RoutingOverwriteMiddleware::class),
29+
...$middlewares->getValues(),
30+
])
31+
);
32+
33+
// @deprecated tag:v6.7.0 - remove all code below, overwrites are now handled via shopware.messenger.routing_overwrites
34+
$config = $this->getConfig($container, 'framework');
35+
36+
if (!\array_key_exists('messenger', $config)) {
37+
return;
38+
}
39+
40+
$mapped = [];
41+
foreach ($config['messenger']['routing'] as $message => $transports) {
42+
if (!\array_key_exists('senders', $transports)) {
43+
continue;
44+
}
45+
$mapped[$message] = array_shift($transports['senders']);
46+
}
47+
48+
$container
49+
->getDefinition(RoutingOverwriteMiddleware::class)
50+
->replaceArgument(1, $mapped);
51+
}
52+
}

src/Core/Framework/DependencyInjection/DependencyInjectionException.php

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class DependencyInjectionException extends HttpException
1212
public const PROJECT_DIR_IS_NOT_A_STRING = 'FRAMEWORK__PROJECT_DIR_IS_NOT_A_STRING';
1313
public const BUNDLES_METADATA_IS_NOT_AN_ARRAY = 'FRAMEWORK__BUNDLES_METADATA_IS_NOT_AN_ARRAY';
1414
public const TAGGED_SERVICE_HAS_WRONG_TYPE = 'FRAMEWORK__TAGGED_SERVICE_HAS_WRONG_TYPE';
15+
public const PARAMETER_HAS_WRONG_TYPE = 'FRAMEWORK__PARAMETER_HAS_WRONG_TYPE';
1516

1617
public static function projectDirNotInContainer(): self
1718
{
@@ -39,4 +40,13 @@ public static function taggedServiceHasWrongType(string $service, string $tag, s
3940
\sprintf('Service "%s" is tagged as "%s" and must therefore be of type "%s".', $service, $tag, $type)
4041
);
4142
}
43+
44+
public static function parameterHasWrongType(string $parameter, string $expectedType, string $actualType): self
45+
{
46+
return new self(
47+
Response::HTTP_INTERNAL_SERVER_ERROR,
48+
self::PARAMETER_HAS_WRONG_TYPE,
49+
\sprintf('Parameter "%s" should be: "%s". Got: "%s"', $parameter, $expectedType, $actualType)
50+
);
51+
}
4252
}

0 commit comments

Comments
 (0)