Skip to content

Commit 08a20a5

Browse files
committed
[client] migrate passes.
1 parent 07af0c5 commit 08a20a5

File tree

6 files changed

+213
-168
lines changed

6 files changed

+213
-168
lines changed

pkg/enqueue-bundle/EnqueueBundle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function build(ContainerBuilder $container): void
3030
$container->addCompilerPass(new BuildClientConsumptionExtensionsPass());
3131
$container->addCompilerPass(new BuildClientExtensionsPass());
3232
$container->addCompilerPass(new BuildClientTopicSubscriberRoutesPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
33-
$container->addCompilerPass(new BuildClientCommandSubscriberRoutesPass('default'), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
34-
$container->addCompilerPass(new BuildClientProcessorRoutesPass('default'), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
33+
$container->addCompilerPass(new BuildClientCommandSubscriberRoutesPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
34+
$container->addCompilerPass(new BuildClientProcessorRoutesPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
3535
$container->addCompilerPass(new AnalyzeRouteCollectionPass('default'), PassConfig::TYPE_BEFORE_OPTIMIZATION, 30);
3636
$container->addCompilerPass(new BuildClientProcessorRegistryPass('default'));
3737

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

Lines changed: 74 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,98 +10,102 @@
1010

1111
final class BuildCommandSubscriberRoutesPass implements CompilerPassInterface
1212
{
13-
/**
14-
* @var string
15-
*/
16-
private $name;
13+
use FormatClientNameTrait;
1714

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

2717
public function process(ContainerBuilder $container): void
2818
{
29-
$routeCollectionId = sprintf('enqueue.client.%s.route_collection', $this->name);
30-
if (false == $container->hasDefinition($routeCollectionId)) {
31-
return;
19+
if (false == $container->hasParameter('enqueue.clients')) {
20+
throw new \LogicException('The "enqueue.clients" parameter must be set.');
3221
}
3322

34-
$tag = 'enqueue.command_subscriber';
35-
$routeCollection = new RouteCollection([]);
36-
foreach ($container->findTaggedServiceIds($tag) as $serviceId => $tagAttributes) {
37-
$processorDefinition = $container->getDefinition($serviceId);
38-
if ($processorDefinition->getFactory()) {
39-
throw new \LogicException('The command subscriber tag could not be applied to a service created by factory.');
40-
}
23+
$names = $container->getParameter('enqueue.clients');
4124

42-
$processorClass = $processorDefinition->getClass();
43-
if (false == class_exists($processorClass)) {
44-
throw new \LogicException(sprintf('The processor class "%s" could not be found.', $processorClass));
25+
foreach ($names as $name) {
26+
$this->name = $name;
27+
$routeCollectionId = sprintf('enqueue.client.%s.route_collection', $this->name);
28+
if (false == $container->hasDefinition($routeCollectionId)) {
29+
throw new \LogicException(sprintf('Service "%s" not found', $routeCollectionId));
4530
}
4631

47-
if (false == is_subclass_of($processorClass, CommandSubscriberInterface::class)) {
48-
throw new \LogicException(sprintf('The processor must implement "%s" interface to be used with the tag "%s"', CommandSubscriberInterface::class, $tag));
49-
}
32+
$tag = 'enqueue.command_subscriber';
33+
$routeCollection = new RouteCollection([]);
34+
foreach ($container->findTaggedServiceIds($tag) as $serviceId => $tagAttributes) {
35+
$processorDefinition = $container->getDefinition($serviceId);
36+
if ($processorDefinition->getFactory()) {
37+
throw new \LogicException('The command subscriber tag could not be applied to a service created by factory.');
38+
}
5039

51-
foreach ($tagAttributes as $tagAttribute) {
52-
$client = $tagAttribute['client'] ?? 'default';
40+
$processorClass = $processorDefinition->getClass();
41+
if (false == class_exists($processorClass)) {
42+
throw new \LogicException(sprintf('The processor class "%s" could not be found.', $processorClass));
43+
}
5344

54-
if ($client !== $this->name && 'all' !== $client) {
55-
continue;
45+
if (false == is_subclass_of($processorClass, CommandSubscriberInterface::class)) {
46+
throw new \LogicException(sprintf('The processor must implement "%s" interface to be used with the tag "%s"', CommandSubscriberInterface::class, $tag));
5647
}
5748

58-
/** @var CommandSubscriberInterface $processorClass */
59-
$commands = $processorClass::getSubscribedCommand();
49+
foreach ($tagAttributes as $tagAttribute) {
50+
$client = $tagAttribute['client'] ?? 'default';
6051

61-
if (empty($commands)) {
62-
throw new \LogicException('Command subscriber must return something.');
63-
}
52+
if ($client !== $this->name && 'all' !== $client) {
53+
continue;
54+
}
6455

65-
if (is_string($commands)) {
66-
$commands = [$commands];
67-
}
56+
/** @var CommandSubscriberInterface $processorClass */
57+
$commands = $processorClass::getSubscribedCommand();
6858

69-
if (!is_array($commands)) {
70-
throw new \LogicException('Command subscriber configuration is invalid. Should be an array or string.');
71-
}
59+
if (empty($commands)) {
60+
throw new \LogicException('Command subscriber must return something.');
61+
}
7262

73-
if (isset($commands['command'])) {
74-
$commands = [$commands];
75-
}
63+
if (is_string($commands)) {
64+
$commands = [$commands];
65+
}
66+
67+
if (!is_array($commands)) {
68+
throw new \LogicException('Command subscriber configuration is invalid. Should be an array or string.');
69+
}
70+
71+
if (isset($commands['command'])) {
72+
$commands = [$commands];
73+
}
7674

77-
foreach ($commands as $key => $params) {
78-
if (is_string($params)) {
79-
$routeCollection->add(new Route($params, Route::COMMAND, $serviceId, ['processor_service_id' => $serviceId]));
80-
} elseif (is_array($params)) {
81-
$source = $params['command'] ?? null;
82-
$processor = $params['processor'] ?? $serviceId;
83-
unset($params['command'], $params['source'], $params['source_type'], $params['processor'], $params['options']);
84-
$options = $params;
85-
$options['processor_service_id'] = $serviceId;
86-
87-
$routeCollection->add(new Route($source, Route::COMMAND, $processor, $options));
88-
} else {
89-
throw new \LogicException(sprintf(
90-
'Command subscriber configuration is invalid for "%s::getSubscribedCommand()". "%s"',
91-
$processorClass,
92-
json_encode($processorClass::getSubscribedCommand())
93-
));
75+
foreach ($commands as $key => $params) {
76+
if (is_string($params)) {
77+
$routeCollection->add(new Route($params, Route::COMMAND, $serviceId, ['processor_service_id' => $serviceId]));
78+
} elseif (is_array($params)) {
79+
$source = $params['command'] ?? null;
80+
$processor = $params['processor'] ?? $serviceId;
81+
unset($params['command'], $params['source'], $params['source_type'], $params['processor'], $params['options']);
82+
$options = $params;
83+
$options['processor_service_id'] = $serviceId;
84+
85+
$routeCollection->add(new Route($source, Route::COMMAND, $processor, $options));
86+
} else {
87+
throw new \LogicException(sprintf(
88+
'Command subscriber configuration is invalid for "%s::getSubscribedCommand()". "%s"',
89+
$processorClass,
90+
json_encode($processorClass::getSubscribedCommand())
91+
));
92+
}
9493
}
9594
}
9695
}
97-
}
9896

99-
$rawRoutes = $routeCollection->toArray();
97+
$rawRoutes = $routeCollection->toArray();
98+
99+
$routeCollectionService = $container->getDefinition($routeCollectionId);
100+
$routeCollectionService->replaceArgument(0, array_merge(
101+
$routeCollectionService->getArgument(0),
102+
$rawRoutes
103+
));
104+
}
105+
}
100106

101-
$routeCollectionService = $container->getDefinition($routeCollectionId);
102-
$routeCollectionService->replaceArgument(0, array_merge(
103-
$routeCollectionService->getArgument(0),
104-
$rawRoutes
105-
));
107+
protected function getName(): string
108+
{
109+
return $this->name;
106110
}
107111
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ public function process(ContainerBuilder $container): void
5757
$registry = $container->getDefinition($processorRegistryId);
5858
$registry->setArgument(0, ServiceLocatorTagPass::register($container, $map, $processorRegistryId));
5959
}
60+
61+
private function getName(): string
62+
{
63+
return $this->name;
64+
}
6065
}

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

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,72 +9,76 @@
99

1010
final class BuildProcessorRoutesPass implements CompilerPassInterface
1111
{
12-
/**
13-
* @var string
14-
*/
15-
private $name;
12+
use FormatClientNameTrait;
1613

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

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

33-
$tag = 'enqueue.processor';
34-
$routeCollection = new RouteCollection([]);
35-
foreach ($container->findTaggedServiceIds($tag) as $serviceId => $tagAttributes) {
36-
foreach ($tagAttributes as $tagAttribute) {
37-
$client = $tagAttribute['client'] ?? 'default';
22+
$names = $container->getParameter('enqueue.clients');
3823

39-
if ($client !== $this->name && 'all' !== $client) {
40-
continue;
41-
}
24+
foreach ($names as $name) {
25+
$this->name = $name;
26+
$routeCollectionId = $this->format('route_collection');
27+
if (false == $container->hasDefinition($routeCollectionId)) {
28+
throw new \LogicException(sprintf('Service "%s" not found', $routeCollectionId));
29+
}
4230

43-
$topic = $tagAttribute['topic'] ?? null;
44-
$command = $tagAttribute['command'] ?? null;
31+
$tag = 'enqueue.processor';
32+
$routeCollection = new RouteCollection([]);
33+
foreach ($container->findTaggedServiceIds($tag) as $serviceId => $tagAttributes) {
34+
foreach ($tagAttributes as $tagAttribute) {
35+
$client = $tagAttribute['client'] ?? 'default';
4536

46-
if (false == $topic && false == $command) {
47-
throw new \LogicException(sprintf('Either "topic" or "command" tag attribute must be set on service "%s". None is set.', $serviceId));
48-
}
49-
if ($topic && $command) {
50-
throw new \LogicException(sprintf('Either "topic" or "command" tag attribute must be set on service "%s". Both are set.', $serviceId));
51-
}
37+
if ($client !== $this->name && 'all' !== $client) {
38+
continue;
39+
}
40+
41+
$topic = $tagAttribute['topic'] ?? null;
42+
$command = $tagAttribute['command'] ?? null;
5243

53-
$source = $command ?: $topic;
54-
$sourceType = $command ? Route::COMMAND : Route::TOPIC;
55-
$processor = $tagAttribute['processor'] ?? $serviceId;
56-
57-
unset(
58-
$tagAttribute['topic'],
59-
$tagAttribute['command'],
60-
$tagAttribute['source'],
61-
$tagAttribute['source_type'],
62-
$tagAttribute['processor'],
63-
$tagAttribute['options']
64-
);
65-
$options = $tagAttribute;
66-
$options['processor_service_id'] = $serviceId;
67-
68-
$routeCollection->add(new Route($source, $sourceType, $processor, $options));
44+
if (false == $topic && false == $command) {
45+
throw new \LogicException(sprintf('Either "topic" or "command" tag attribute must be set on service "%s". None is set.', $serviceId));
46+
}
47+
if ($topic && $command) {
48+
throw new \LogicException(sprintf('Either "topic" or "command" tag attribute must be set on service "%s". Both are set.', $serviceId));
49+
}
50+
51+
$source = $command ?: $topic;
52+
$sourceType = $command ? Route::COMMAND : Route::TOPIC;
53+
$processor = $tagAttribute['processor'] ?? $serviceId;
54+
55+
unset(
56+
$tagAttribute['topic'],
57+
$tagAttribute['command'],
58+
$tagAttribute['source'],
59+
$tagAttribute['source_type'],
60+
$tagAttribute['processor'],
61+
$tagAttribute['options']
62+
);
63+
$options = $tagAttribute;
64+
$options['processor_service_id'] = $serviceId;
65+
66+
$routeCollection->add(new Route($source, $sourceType, $processor, $options));
67+
}
6968
}
70-
}
7169

72-
$rawRoutes = $routeCollection->toArray();
70+
$rawRoutes = $routeCollection->toArray();
7371

74-
$routeCollectionService = $container->getDefinition($routeCollectionId);
75-
$routeCollectionService->replaceArgument(0, array_merge(
76-
$routeCollectionService->getArgument(0),
77-
$rawRoutes
78-
));
72+
$routeCollectionService = $container->getDefinition($routeCollectionId);
73+
$routeCollectionService->replaceArgument(0, array_merge(
74+
$routeCollectionService->getArgument(0),
75+
$rawRoutes
76+
));
77+
}
78+
}
79+
80+
protected function getName(): string
81+
{
82+
return $this->name;
7983
}
8084
}

0 commit comments

Comments
 (0)