| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author    jan huang <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright 2016 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @see      https://www.github.com/janhuang | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @see      http://www.fast-d.cn/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace FastD\Pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use FastD\Model\Database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class DatabasePool. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class DatabasePool implements PoolInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var Database[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $connections = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Database constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param array $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 53 |  |     public function __construct(array $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 53 |  |         $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 53 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return Database | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 53 |  |     public function getConnection($key) | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 53 |  |         if (!isset($this->connections[$key])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 53 |  |             if (!isset($this->config[$key])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 10 |  |                 throw new \LogicException(sprintf('No set %s database', $key)); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 43 |  |             $config = $this->config[$key]; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 43 |  |             $this->connections[$key] = new Database( | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 [ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 43 |  |                     'database_type' => isset($config['adapter']) ? $config['adapter'] : 'mysql', | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 43 |  |                     'database_name' => $config['name'], | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 43 |  |                     'server' => $config['host'], | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 43 |  |                     'username' => $config['user'], | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 43 |  |                     'password' => $config['pass'], | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 43 |  |                     'charset' => isset($config['charset']) ? $config['charset'] : 'utf8', | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 43 |  |                     'port' => isset($config['port']) ? $config['port'] : 3306, | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 43 |  |                     'prefix' => isset($config['prefix']) ? $config['prefix'] : '', | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 43 |  |                     'option' => isset($config['option']) ? $config['option'] : [], | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 43 |  |                     'command' => isset($config['command']) ? $config['command'] : [], | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 ] | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 43 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 43 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 43 |  |         return $this->connections[$key]; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |     public function initPool() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         foreach ($this->config as $name => $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             $this->getConnection($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  |