1818use Drift \CommandBus \Async \AsyncAdapter ;
1919use Drift \CommandBus \Bus \InlineCommandBus ;
2020use Drift \Console \OutputPrinter ;
21- use React \EventLoop \LoopInterface ;
21+ use Drift \EventBus \Bus \EventBus ;
22+ use Drift \EventBus \Subscriber \EventBusSubscriber ;
2223use Symfony \Component \Console \Command \Command ;
2324use Symfony \Component \Console \Input \InputInterface ;
2425use Symfony \Component \Console \Input \InputOption ;
@@ -31,22 +32,25 @@ class CommandConsumerCommand extends Command
3132{
3233 private AsyncAdapter $ asyncAdapter ;
3334 private InlineCommandBus $ commandBus ;
35+ private ?EventBusSubscriber $ eventBusSubscriber ;
3436
3537 /**
3638 * ConsumeCommand constructor.
3739 *
38- * @param AsyncAdapter $asyncAdapter
39- * @param InlineCommandBus $commandBus
40- * @param LoopInterface $loop
40+ * @param AsyncAdapter $asyncAdapter
41+ * @param InlineCommandBus $commandBus
42+ * @param EventBusSubscriber|null $eventBusSubscriber
4143 */
4244 public function __construct (
4345 AsyncAdapter $ asyncAdapter ,
44- InlineCommandBus $ commandBus
46+ InlineCommandBus $ commandBus ,
47+ ?EventBusSubscriber $ eventBusSubscriber
4548 ) {
4649 parent ::__construct ();
4750
4851 $ this ->asyncAdapter = $ asyncAdapter ;
4952 $ this ->commandBus = $ commandBus ;
53+ $ this ->eventBusSubscriber = $ eventBusSubscriber ;
5054 }
5155
5256 /**
@@ -62,6 +66,18 @@ protected function configure()
6266 'Number of jobs to handle before dying ' ,
6367 0
6468 );
69+
70+ /*
71+ * If we have the EventBus loaded, we can add listeners as well
72+ */
73+ if (class_exists (EventBus::class)) {
74+ $ this ->addOption (
75+ 'exchange ' ,
76+ null ,
77+ InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED ,
78+ 'Exchanges to listen '
79+ );
80+ }
6581 }
6682
6783 /**
@@ -82,6 +98,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
8298 (new CommandBusHeaderMessage ('' , 'Using adapter ' .$ adapterName ))->print ($ outputPrinter );
8399 (new CommandBusHeaderMessage ('' , 'Started listening... ' ))->print ($ outputPrinter );
84100
101+ $ exchanges = self ::buildQueueArray ($ input );
102+ if (
103+ class_exists (EventBusSubscriber::class) &&
104+ !empty ($ exchanges ) &&
105+ !is_null ($ this ->eventBusSubscriber )
106+ ) {
107+ (new CommandBusHeaderMessage ('' , 'Kernel connected to exchanges. ' ))->print ($ outputPrinter );
108+ $ this
109+ ->eventBusSubscriber
110+ ->subscribeToExchanges (
111+ $ exchanges ,
112+ $ outputPrinter
113+ );
114+ }
115+
85116 $ this
86117 ->asyncAdapter
87118 ->consume (
@@ -92,4 +123,26 @@ protected function execute(InputInterface $input, OutputInterface $output)
92123
93124 return 0 ;
94125 }
126+
127+ /**
128+ * Build queue architecture from array of strings.
129+ *
130+ * @param InputInterface $input
131+ *
132+ * @return array
133+ */
134+ private static function buildQueueArray (InputInterface $ input ): array
135+ {
136+ if (!$ input ->hasOption ('exchange ' )) {
137+ return [];
138+ }
139+
140+ $ exchanges = [];
141+ foreach ($ input ->getOption ('exchange ' ) as $ exchange ) {
142+ $ exchangeParts = explode (': ' , $ exchange , 2 );
143+ $ exchanges [$ exchangeParts [0 ]] = $ exchangeParts [1 ] ?? '' ;
144+ }
145+
146+ return $ exchanges ;
147+ }
95148}
0 commit comments