| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * small helper to read our VCAP_SERVICES in cf | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace Graviton\ImportExport\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Flow\JSONPath\JSONPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author   List of contributors <https://github.com/libgraviton/import-export/graphs/contributors> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @license  http://opensource.org/licenses/gpl-license.php GNU Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @link     http://swisscom.ch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class MongoCredentialsProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * gets connection params from ENV if available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return array connection params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public static function getConnection() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         // defaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $ret = ['uri' => null, 'db' => 'db']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         if (isset($_ENV['VCAP_SERVICES'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $path = new JSONPath(json_decode($_ENV['VCAP_SERVICES'], true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $ret['uri'] = $path->find('$..mongodb[0].*.uri')->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $ret['db'] = $path->find('$..mongodb[0].*.database')->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * gets connection params from input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param InputInterface $input input from user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 45 | 4 |  |     public static function fromInput(InputInterface $input) | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 47 | 4 |  |         $uri = $input->getOption('mongodb'); | 
            
                                                        
            
                                    
            
            
                | 48 | 4 |  |         $uriParts = parse_url($uri); | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 | 4 |  |         $db = 'db'; | 
            
                                                        
            
                                    
            
            
                | 51 | 4 |  |         if (array_key_exists('path', $uriParts) && $uriParts['path'] !== '/') { | 
            
                                                        
            
                                    
            
            
                | 52 | 2 |  |             $db = substr($uriParts['path'], 1); | 
            
                                                        
            
                                    
            
            
                | 53 | 2 |  |         } | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 55 | 4 |  |         if (array_key_exists('query', $uriParts)) { | 
            
                                                        
            
                                    
            
            
                | 56 | 1 |  |             $queryParts = []; | 
            
                                                        
            
                                    
            
            
                | 57 | 1 |  |             parse_str($uriParts['query'], $queryParts); | 
            
                                                        
            
                                    
            
            
                | 58 | 1 |  |             if (array_key_exists('db', $queryParts) && !empty($queryParts['db'])) { | 
            
                                                        
            
                                    
            
            
                | 59 | 1 |  |                 $db = $queryParts['db']; | 
            
                                                        
            
                                    
            
            
                | 60 | 1 |  |             } | 
            
                                                        
            
                                    
            
            
                | 61 | 1 |  |         } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         return [ | 
            
                                                        
            
                                    
            
            
                | 64 | 4 |  |             'uri' => $uri, | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |             'db' => $db | 
            
                                                        
            
                                    
            
            
                | 66 | 4 |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  |