3131use Symfony \Component \Console \Input \InputOption ;
3232use Symfony \Component \Console \Output \OutputInterface ;
3333use Symfony \Component \Console \Question \Question ;
34- use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
34+ use Symfony \Component \EventDispatcher \EventDispatcherInterface as LegacyEventDispatcherInterface ;
3535use Symfony \Component \EventDispatcher \LegacyEventDispatcherProxy ;
36+ use Symfony \Contracts \EventDispatcher \EventDispatcherInterface ;
3637
3738/**
3839 * Populate the search index.
@@ -42,12 +43,12 @@ class PopulateCommand extends Command
4243 protected static $ defaultName = 'fos:elastica:populate ' ;
4344
4445 /**
45- * @var EventDispatcherInterface
46+ * @var EventDispatcherInterface|LegacyEventDispatcherInterface
4647 */
4748 private $ dispatcher ;
4849
4950 /**
50- * @var IndexManager
51+ * @var IndexManager
5152 */
5253 private $ indexManager ;
5354
@@ -72,7 +73,7 @@ class PopulateCommand extends Command
7273 private $ resetter ;
7374
7475 public function __construct (
75- EventDispatcherInterface $ dispatcher ,
76+ $ dispatcher ,
7677 IndexManager $ indexManager ,
7778 PagerProviderRegistry $ pagerProviderRegistry ,
7879 PagerPersisterRegistry $ pagerPersisterRegistry ,
@@ -125,7 +126,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
125126 }
126127 }
127128
128- protected function execute (InputInterface $ input , OutputInterface $ output )
129+ protected function execute (InputInterface $ input , OutputInterface $ output ): int
129130 {
130131 $ index = $ input ->getOption ('index ' );
131132 $ type = $ input ->getOption ('type ' );
@@ -149,7 +150,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
149150 /** @var QuestionHelper $dialog */
150151 $ dialog = $ this ->getHelperSet ()->get ('question ' );
151152 if (!$ dialog ->ask ($ input , $ output , new Question ('<question>You chose to reset the index and start indexing with an offset. Do you really want to do that?</question> ' ))) {
152- return ;
153+ return 0 ;
153154 }
154155 }
155156
@@ -170,6 +171,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
170171 $ this ->populateIndex ($ output , $ index , $ reset , $ options );
171172 }
172173 }
174+
175+ return 0 ;
173176 }
174177
175178 /**
@@ -183,7 +186,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
183186 private function populateIndex (OutputInterface $ output , $ index , $ reset , $ options )
184187 {
185188 $ event = new IndexPopulateEvent ($ index , $ reset , $ options );
186- $ this ->dispatcher -> dispatch (IndexPopulateEvent::PRE_INDEX_POPULATE , $ event );
189+ $ this ->dispatch ($ event , IndexPopulateEvent::PRE_INDEX_POPULATE );
187190
188191 if ($ event ->isReset ()) {
189192 $ output ->writeln (sprintf ('<info>Resetting</info> <comment>%s</comment> ' , $ index ));
@@ -195,7 +198,7 @@ private function populateIndex(OutputInterface $output, $index, $reset, $options
195198 $ this ->populateIndexType ($ output , $ index , $ type , false , $ event ->getOptions ());
196199 }
197200
198- $ this ->dispatcher -> dispatch (IndexPopulateEvent::POST_INDEX_POPULATE , $ event );
201+ $ this ->dispatch ($ event , IndexPopulateEvent::POST_INDEX_POPULATE );
199202
200203 $ this ->refreshIndex ($ output , $ index );
201204 }
@@ -212,7 +215,7 @@ private function populateIndex(OutputInterface $output, $index, $reset, $options
212215 private function populateIndexType (OutputInterface $ output , $ index , $ type , $ reset , $ options )
213216 {
214217 $ event = new TypePopulateEvent ($ index , $ type , $ reset , $ options );
215- $ this ->dispatcher -> dispatch (TypePopulateEvent::PRE_TYPE_POPULATE , $ event );
218+ $ this ->dispatch ($ event , TypePopulateEvent::PRE_TYPE_POPULATE );
216219
217220 if ($ event ->isReset ()) {
218221 $ output ->writeln (sprintf ('<info>Resetting</info> <comment>%s/%s</comment> ' , $ index , $ type ));
@@ -264,7 +267,7 @@ function(PostAsyncInsertObjectsEvent $event) use ($loggerClosure) {
264267
265268 $ this ->pagerPersister ->insert ($ pager , $ options );
266269
267- $ this ->dispatcher -> dispatch (TypePopulateEvent::POST_TYPE_POPULATE , $ event );
270+ $ this ->dispatch ($ event , TypePopulateEvent::POST_TYPE_POPULATE );
268271
269272 $ this ->refreshIndex ($ output , $ index );
270273 }
@@ -281,4 +284,15 @@ private function refreshIndex(OutputInterface $output, $index)
281284 $ this ->indexManager ->getIndex ($ index )->refresh ();
282285 $ output ->writeln ("" );
283286 }
287+
288+ private function dispatch ($ event , $ eventName ): void
289+ {
290+ if ($ this ->dispatcher instanceof EventDispatcherInterface) {
291+ // Symfony >= 4.3
292+ $ this ->dispatcher ->dispatch ($ event , $ eventName );
293+ } else {
294+ // Symfony 3.4
295+ $ this ->dispatcher ->dispatch ($ eventName , $ event );
296+ }
297+ }
284298}
0 commit comments