| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Facile\MongoDbBundle\DependencyInjection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Facile\MongoDbBundle\Services\ClientRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use MongoDB\Database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Config\FileLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\DependencyInjection\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\DependencyInjection\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpKernel\DependencyInjection\Extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Class MongoDbBundleExtension. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class MongoDbBundleExtension extends Extension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var ContainerBuilder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $containerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $env; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 2 |  |     public function load(array $configs, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 2 |  |         $this->containerBuilder = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  |         $configuration = new Configuration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  |         $config = $this->processConfiguration($configuration, $configs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 2 |  |         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |         $loader->load('factory.xml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |         $this->env = $container->getParameter("kernel.environment"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |         if ($this->env === 'dev' && class_exists(WebProfilerBundle::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 2 |  |             $loader->load('web_profiler.xml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |         $this->defineLoggers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |         $this->defineClientRegistry($config['clients']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |         $this->defineConnections($config['connections']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |         return $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param array $clientsConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return Definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |     private function defineClientRegistry(array $clientsConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |         $clientRegistryDefinition = new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |             ClientRegistry::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |                 new Reference('facile_mongo_db.logger') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         foreach ($clientsConfig as $name => $conf) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $clientRegistryDefinition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |                 ->addMethodCall( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |                     'addClientConfiguration', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |                         $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |                         $conf, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  |         $this->containerBuilder->setDefinition('mongo.client_registry', $clientRegistryDefinition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param array $connections | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 2 |  |     private function defineConnections(array $connections) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 2 |  |         foreach ($connections as $name => $conf) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 2 |  |             $connectionDefinition = new Definition( | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 2 |  |                 Database::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                 [ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 2 |  |                     $conf['client_name'], | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 2 |  |                     $conf['database_name'], | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                 ] | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 2 |  |             $connectionDefinition->setFactory([new Reference('mongo.connection_factory'), 'createConnection']); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 2 |  |             $this->containerBuilder->setDefinition('mongo.connection.'.$name, $connectionDefinition); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 2 |  |         $this->containerBuilder->setAlias('mongo.connection', 'mongo.connection.'.array_keys($connections)[0]); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |     private function defineLoggers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |         $loggerDefinition = new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 2 |  |             DataCollectorLoggerInterface::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |         $loggerDefinition->setFactory([new Reference('mongo.logger_factory'), 'createLogger']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |         $this->containerBuilder->setDefinition('facile_mongo_db.logger', $loggerDefinition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  |