Skip to content

Commit b6387e9

Browse files
committed
[client] migrate passes.
1 parent 057aeda commit b6387e9

File tree

11 files changed

+270
-126
lines changed

11 files changed

+270
-126
lines changed

pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function load(array $configs, ContainerBuilder $container): void
3636
$container->setParameter('enqueue.transports', array_keys($config['transport']));
3737

3838
if (isset($config['client'])) {
39+
$container->setParameter('enqueue.clients', ['default']);
40+
3941
$this->setupAutowiringForProcessors($container);
4042

4143
$loader->load('client.yml');

pkg/enqueue-bundle/EnqueueBundle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function build(ContainerBuilder $container): void
2727
$container->addCompilerPass(new BuildProcessorRegistryPass());
2828

2929
//client passes
30-
$container->addCompilerPass(new BuildClientConsumptionExtensionsPass('default'));
31-
$container->addCompilerPass(new BuildClientExtensionsPass('default'));
30+
$container->addCompilerPass(new BuildClientConsumptionExtensionsPass());
31+
$container->addCompilerPass(new BuildClientExtensionsPass());
3232
$container->addCompilerPass(new BuildClientTopicSubscriberRoutesPass('default'), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
3333
$container->addCompilerPass(new BuildClientCommandSubscriberRoutesPass('default'), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
3434
$container->addCompilerPass(new BuildClientProcessorRoutesPass('default'), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);

pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,24 @@ public function testShouldSetPropertyWithAllConfiguredTransports()
461461
$this->assertEquals(['default', 'foo', 'bar'], $container->getParameter('enqueue.transports'));
462462
}
463463

464+
public function testShouldSetPropertyWithAllConfiguredClients()
465+
{
466+
$container = $this->getContainerBuilder(true);
467+
468+
$extension = new EnqueueExtension();
469+
$extension->load([[
470+
'client' => [],
471+
'transport' => [
472+
'default' => ['dsn' => 'default:'],
473+
'foo' => ['dsn' => 'foo:'],
474+
'bar' => ['dsn' => 'foo:'],
475+
],
476+
]], $container);
477+
478+
$this->assertTrue($container->hasParameter('enqueue.clients'));
479+
$this->assertEquals(['default'], $container->getParameter('enqueue.clients'));
480+
}
481+
464482
public function testShouldLoadProcessAutoconfigureChildDefinition()
465483
{
466484
$container = $this->getContainerBuilder(true);

pkg/enqueue/Symfony/Client/DependencyInjection/BuildClientExtensionsPass.php

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,62 @@
88

99
final class BuildClientExtensionsPass implements CompilerPassInterface
1010
{
11-
/**
12-
* @var string
13-
*/
14-
private $name;
11+
use FormatClientNameTrait;
1512

16-
public function __construct(string $clientName)
17-
{
18-
if (empty($clientName)) {
19-
throw new \InvalidArgumentException('The name could not be empty.');
20-
}
21-
22-
$this->name = $clientName;
23-
}
13+
protected $name;
2414

2515
public function process(ContainerBuilder $container): void
2616
{
27-
$extensionsId = sprintf('enqueue.client.%s.client_extensions', $this->name);
28-
if (false == $container->hasDefinition($extensionsId)) {
29-
return;
17+
if (false == $container->hasParameter('enqueue.clients')) {
18+
throw new \LogicException('The "enqueue.clients" parameter must be set.');
3019
}
3120

32-
$tags = array_merge(
33-
$container->findTaggedServiceIds('enqueue.client_extension'),
34-
$container->findTaggedServiceIds('enqueue.client.extension') // TODO BC
35-
);
21+
$names = $container->getParameter('enqueue.clients');
3622

37-
$groupByPriority = [];
38-
foreach ($tags as $serviceId => $tagAttributes) {
39-
foreach ($tagAttributes as $tagAttribute) {
40-
$client = $tagAttribute['client'] ?? 'default';
23+
foreach ($names as $name) {
24+
$this->name = $name;
25+
$extensionsId = $this->format('client_extensions');
26+
if (false == $container->hasDefinition($extensionsId)) {
27+
throw new \LogicException(sprintf('Service "%s" not found', $extensionsId));
28+
}
4129

42-
if ($client !== $this->name && 'all' !== $client) {
43-
continue;
44-
}
30+
$tags = array_merge(
31+
$container->findTaggedServiceIds('enqueue.client_extension'),
32+
$container->findTaggedServiceIds('enqueue.client.extension') // TODO BC
33+
);
34+
35+
$groupByPriority = [];
36+
foreach ($tags as $serviceId => $tagAttributes) {
37+
foreach ($tagAttributes as $tagAttribute) {
38+
$client = $tagAttribute['client'] ?? 'default';
4539

46-
$priority = (int) ($tagAttribute['priority'] ?? 0);
40+
if ($client !== $this->name && 'all' !== $client) {
41+
continue;
42+
}
4743

48-
$groupByPriority[$priority][] = new Reference($serviceId);
44+
$priority = (int) ($tagAttribute['priority'] ?? 0);
45+
46+
$groupByPriority[$priority][] = new Reference($serviceId);
47+
}
4948
}
50-
}
5149

52-
krsort($groupByPriority, SORT_NUMERIC);
50+
krsort($groupByPriority, SORT_NUMERIC);
5351

54-
$flatExtensions = [];
55-
foreach ($groupByPriority as $extension) {
56-
$flatExtensions = array_merge($flatExtensions, $extension);
52+
$flatExtensions = [];
53+
foreach ($groupByPriority as $extension) {
54+
$flatExtensions = array_merge($flatExtensions, $extension);
55+
}
56+
57+
$extensionsService = $container->getDefinition($extensionsId);
58+
$extensionsService->replaceArgument(0, array_merge(
59+
$extensionsService->getArgument(0),
60+
$flatExtensions
61+
));
5762
}
63+
}
5864

59-
$extensionsService = $container->getDefinition($extensionsId);
60-
$extensionsService->replaceArgument(0, array_merge(
61-
$extensionsService->getArgument(0),
62-
$flatExtensions
63-
));
65+
protected function getName(): string
66+
{
67+
return $this->name;
6468
}
6569
}

pkg/enqueue/Symfony/Client/DependencyInjection/BuildConsumptionExtensionsPass.php

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,63 @@
88

99
final class BuildConsumptionExtensionsPass implements CompilerPassInterface
1010
{
11-
/**
12-
* @var string
13-
*/
14-
private $name;
11+
use FormatClientNameTrait;
1512

16-
public function __construct(string $clientName)
17-
{
18-
if (empty($clientName)) {
19-
throw new \InvalidArgumentException('The name could not be empty.');
20-
}
21-
22-
$this->name = $clientName;
23-
}
13+
protected $name;
2414

2515
public function process(ContainerBuilder $container): void
2616
{
27-
$extensionsId = sprintf('enqueue.client.%s.consumption_extensions', $this->name);
28-
if (false == $container->hasDefinition($extensionsId)) {
29-
return;
17+
if (false == $container->hasParameter('enqueue.clients')) {
18+
throw new \LogicException('The "enqueue.clients" parameter must be set.');
3019
}
3120

32-
$tags = array_merge(
33-
$container->findTaggedServiceIds('enqueue.consumption_extension'),
34-
$container->findTaggedServiceIds('enqueue.consumption.extension') // TODO BC
35-
);
21+
$names = $container->getParameter('enqueue.clients');
3622

37-
$groupByPriority = [];
38-
foreach ($tags as $serviceId => $tagAttributes) {
39-
foreach ($tagAttributes as $tagAttribute) {
40-
$client = $tagAttribute['client'] ?? 'default';
23+
foreach ($names as $name) {
24+
$this->name = $name;
4125

42-
if ($client !== $this->name && 'all' !== $client) {
43-
continue;
44-
}
26+
$extensionsId = $this->format('consumption_extensions');
27+
if (false == $container->hasDefinition($extensionsId)) {
28+
throw new \LogicException(sprintf('Service "%s" not found', $extensionsId));
29+
}
30+
31+
$tags = array_merge(
32+
$container->findTaggedServiceIds('enqueue.consumption_extension'),
33+
$container->findTaggedServiceIds('enqueue.consumption.extension') // TODO BC
34+
);
35+
36+
$groupByPriority = [];
37+
foreach ($tags as $serviceId => $tagAttributes) {
38+
foreach ($tagAttributes as $tagAttribute) {
39+
$client = $tagAttribute['client'] ?? 'default';
4540

46-
$priority = (int) ($tagAttribute['priority'] ?? 0);
41+
if ($client !== $this->name && 'all' !== $client) {
42+
continue;
43+
}
4744

48-
$groupByPriority[$priority][] = new Reference($serviceId);
45+
$priority = (int) ($tagAttribute['priority'] ?? 0);
46+
47+
$groupByPriority[$priority][] = new Reference($serviceId);
48+
}
4949
}
50-
}
5150

52-
krsort($groupByPriority, SORT_NUMERIC);
51+
krsort($groupByPriority, SORT_NUMERIC);
5352

54-
$flatExtensions = [];
55-
foreach ($groupByPriority as $extension) {
56-
$flatExtensions = array_merge($flatExtensions, $extension);
53+
$flatExtensions = [];
54+
foreach ($groupByPriority as $extension) {
55+
$flatExtensions = array_merge($flatExtensions, $extension);
56+
}
57+
58+
$extensionsService = $container->getDefinition($extensionsId);
59+
$extensionsService->replaceArgument(0, array_merge(
60+
$extensionsService->getArgument(0),
61+
$flatExtensions
62+
));
5763
}
64+
}
5865

59-
$extensionsService = $container->getDefinition($extensionsId);
60-
$extensionsService->replaceArgument(0, array_merge(
61-
$extensionsService->getArgument(0),
62-
$flatExtensions
63-
));
66+
protected function getName(): string
67+
{
68+
return $this->name;
6469
}
6570
}

pkg/enqueue/Symfony/Client/DependencyInjection/FormatClientNameTrait.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@ private function reference(string $serviceName, $invalidBehavior = ContainerInte
1616

1717
private function parameter(string $serviceName): string
1818
{
19-
$fullName = $this->format($serviceName, false);
19+
$fullName = $this->format($serviceName);
2020

2121
return "%$fullName%";
2222
}
2323

24-
private function format(string $serviceName, $parameter = false): string
24+
private function format(string $serviceName): string
2525
{
2626
$pattern = 'enqueue.client.%s.'.$serviceName;
2727

28-
$fullName = sprintf($pattern, $this->getName());
29-
30-
return $parameter ? "%$fullName%" : $fullName;
28+
return sprintf($pattern, $this->getName());
3129
}
3230
}

pkg/enqueue/Symfony/DependencyInjection/FormatTransportNameTrait.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@ private function reference(string $serviceName, $invalidBehavior = ContainerInte
1616

1717
private function parameter(string $serviceName): string
1818
{
19-
$fullName = $this->format($serviceName, false);
19+
$fullName = $this->format($serviceName);
2020

2121
return "%$fullName%";
2222
}
2323

24-
private function format(string $serviceName, $parameter = false): string
24+
private function format(string $serviceName): string
2525
{
2626
$pattern = 'enqueue.transport.%s.'.$serviceName;
2727

28-
$fullName = sprintf($pattern, $this->getName());
29-
30-
return $parameter ? "%$fullName%" : $fullName;
28+
return sprintf($pattern, $this->getName());
3129
}
3230
}

0 commit comments

Comments
 (0)