| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the FOSElasticaBundle package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 FOS\ElasticaBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use FOS\ElasticaBundle\Configuration\ConfigManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use FOS\ElasticaBundle\Index\AliasProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use FOS\ElasticaBundle\Index\IndexManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use FOS\ElasticaBundle\Index\MappingBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author Oleg Andreyev <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class CreateCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected static $defaultName = 'fos:elastica:create'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $indexManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $mappingBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $configManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $aliasProcessor; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 35 | 3 | View Code Duplication |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         IndexManager $indexManager, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         MappingBuilder $mappingBuilder, | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         ConfigManager $configManager, | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         AliasProcessor $aliasProcessor | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 3 |  |         parent::__construct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 3 |  |         $this->indexManager = $indexManager; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 3 |  |         $this->mappingBuilder = $mappingBuilder; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 3 |  |         $this->configManager = $configManager; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 3 |  |         $this->aliasProcessor = $aliasProcessor; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |             ->setName('fos:elastica:create') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |             ->addOption('index', null, InputOption::VALUE_OPTIONAL, 'Index that needs to be created') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |             ->setDescription('Creating empty index with mapping') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 3 |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |         $indexName = $input->getOption('index'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |         $indexes = null === $indexName ? array_keys($this->indexManager->getAllIndexes()) : [$indexName]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |         foreach ($indexes as $indexName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |             $output->writeln(sprintf('<info>Creating</info> <comment>%s</comment>', $indexName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  |             $indexConfig = $this->configManager->getIndexConfiguration($indexName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |             $index = $this->indexManager->getIndex($indexName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |             if ($indexConfig->isUseAlias()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |                 $this->aliasProcessor->setRootName($indexConfig, $index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 3 |  |             $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 3 |  |             $index->create($mapping, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |  |