| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the ONGR package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) NFQ Technologies UAB <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace ONGR\ElasticsearchBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use ONGR\ElasticsearchBundle\Service\IndexSuffixFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Style\SymfonyStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Command for creating elasticsearch index. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class IndexCreateCommand extends AbstractManagerAwareCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     protected function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         parent::configure(); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             ->setName('ongr:es:index:create') | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             ->setDescription('Creates elasticsearch index.') | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             ->addOption('time', 't', InputOption::VALUE_NONE, 'Adds date suffix to the new index name') | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                 'alias', | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |                 'a', | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |                 InputOption::VALUE_NONE, | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                 'If the time suffix is used, its nice to create an alias to the configured index name.' | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             ->addOption('no-mapping', null, InputOption::VALUE_NONE, 'Do not include mapping') | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                 'if-not-exists', | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                 null, | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                 InputOption::VALUE_NONE, | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                 'Don\'t trigger an error, when the index already exists' | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $io = new SymfonyStyle($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $manager = $this->getManager($input->getOption('manager')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $originalIndexName = $manager->getIndexName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if ($input->getOption('time')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             /** @var IndexSuffixFinder $finder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $finder = $this->getContainer()->get('es.client.index_suffix_finder'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $finder->setNextFreeIndex($manager); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if ($input->getOption('if-not-exists') && $manager->indexExists()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $io->note( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     'Index `%s` already exists in `%s` manager.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     $manager->getIndexName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     $input->getOption('manager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $manager->createIndex($input->getOption('no-mapping')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $io->text( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 'Created `<comment>%s</comment>` index for the `<comment>%s</comment>` manager. ', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $manager->getIndexName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $input->getOption('manager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if ($input->getOption('alias') && $originalIndexName != $manager->getIndexName()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $params['body'] = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 'actions' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                         'add' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                             'index' => $manager->getIndexName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                             'alias' => $originalIndexName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $message = 'Created an alias `<comment>'.$originalIndexName.'</comment>` for the `<comment>'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $manager->getIndexName().'</comment>` index. '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             if ($manager->getClient()->indices()->existsAlias(['name' => $originalIndexName])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $currentAlias = $manager->getClient()->indices()->getAlias( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                         'name' => $originalIndexName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $indexesToRemoveAliases = implode(',', array_keys($currentAlias)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 if (!empty($indexesToRemoveAliases)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                     $params['body']['actions'][]['remove'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                             'index' => $indexesToRemoveAliases, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                             'alias' => $originalIndexName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                     $message .= 'Removed `<comment>'.$originalIndexName.'</comment>` alias from `<comment>'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                         $indexesToRemoveAliases.'</comment>` index(es).'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $manager->getClient()->indices()->updateAliases($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $io->text($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                        
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.