| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | function MongofillAutoload($classname) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     $classname = str_replace('/', '\\', $classname); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     $classname = ltrim($classname, '\\'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     $namespace = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     if($lastNsPos = strrpos($classname, '\\')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         $namespace = substr($classname, 0, $lastNsPos); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         $classname = substr($classname, $lastNsPos + 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     if(strlen($namespace)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $namespace .= DIRECTORY_SEPARATOR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     $filename = __DIR__.'/../libs/mongofill/src/'.$namespace.$classname.'.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     if(is_readable($filename)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         require $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class MongoDataSet extends DataSet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $db_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     function __construct($params) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->client = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->mangaer = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->db = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->db_name = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if(class_exists('MongoClient')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $this->setupMongoClient($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         else if(class_exists('\MongoDB\Driver\Manager')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $this->setupMongoManager($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             require __DIR__.'/../libs/mongofill/src/functions.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             autoLoadHandler('\Data\MongofillAutoload'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $this->setupMongoClient($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     function tableExists($name) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $collections = $this->db->getCollectionNames(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         if(in_array($name, $collections)) | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     function getTable($name) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if($this->db !== null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return new MongoDataTable($this->db->selectCollection($name)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return new MongoDataTable($this, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     private function setupMongoClient($params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         if($params === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if(isset($params['user'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->client = new \MongoClient('mongodb://'.$params['host'].'/'.$params['db'], array('username'=>$params['user'], 'password'=>$params['pass'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $this->client = new \MongoClient('mongodb://'.$params['host'].'/'.$params['db']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->db = $this->client->selectDB($params['db']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     private function setupMongoManager($params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if($params === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         if(isset($params['user'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $this->manager = new \MongoDB\Driver\Manager('mongodb://'.$params['user'].':'.$params['pass'].'@'.$params['host'].'/'.$params['db']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $this->manager = new \MongoDB\Driver\Manager('mongodb://'.$params['host'].'/'.$params['db']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->db_name = $params['db']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function find($query = array(), $fields = array(), $collectionName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $namespace = $this->db_name.'.'.$collectionName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $dbQuery = new \MongoDB\Driver\Query($query, $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return $this->manager->executeQuery($namespace, $dbQuery); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function count($query = array(), $options = array(), $collectionName) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $cmd = new \MongoDB\Driver\Command(['count'=>$collectionName, 'query'=>$query]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return $this->manager->executeCommand($this->db_name, $cmd); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                        
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.