|
10 | 10 |
|
11 | 11 | final class BuildCommandSubscriberRoutesPass implements CompilerPassInterface
|
12 | 12 | {
|
13 |
| - /** |
14 |
| - * @var string |
15 |
| - */ |
16 |
| - private $name; |
| 13 | + use FormatClientNameTrait; |
17 | 14 |
|
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; |
26 | 16 |
|
27 | 17 | public function process(ContainerBuilder $container): void
|
28 | 18 | {
|
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.'); |
32 | 21 | }
|
33 | 22 |
|
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'); |
41 | 24 |
|
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)); |
45 | 30 | }
|
46 | 31 |
|
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 | + } |
50 | 39 |
|
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 | + } |
53 | 44 |
|
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)); |
56 | 47 | }
|
57 | 48 |
|
58 |
| - /** @var CommandSubscriberInterface $processorClass */ |
59 |
| - $commands = $processorClass::getSubscribedCommand(); |
| 49 | + foreach ($tagAttributes as $tagAttribute) { |
| 50 | + $client = $tagAttribute['client'] ?? 'default'; |
60 | 51 |
|
61 |
| - if (empty($commands)) { |
62 |
| - throw new \LogicException('Command subscriber must return something.'); |
63 |
| - } |
| 52 | + if ($client !== $this->name && 'all' !== $client) { |
| 53 | + continue; |
| 54 | + } |
64 | 55 |
|
65 |
| - if (is_string($commands)) { |
66 |
| - $commands = [$commands]; |
67 |
| - } |
| 56 | + /** @var CommandSubscriberInterface $processorClass */ |
| 57 | + $commands = $processorClass::getSubscribedCommand(); |
68 | 58 |
|
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 | + } |
72 | 62 |
|
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 | + } |
76 | 74 |
|
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 | + } |
94 | 93 | }
|
95 | 94 | }
|
96 | 95 | }
|
97 |
| - } |
98 | 96 |
|
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 | + } |
100 | 106 |
|
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; |
106 | 110 | }
|
107 | 111 | }
|
0 commit comments