| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PhpBotFramework. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * PhpBotFramework is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * it under the terms of the GNU Lesser General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * published by the Free Software Foundation, version 3. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * PhpBotFramework is distributed in the hope that it will be useful, but | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Lesser General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * You should have received a copy of the GNU Lesser General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * along with this program. If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | namespace PhpBotFramework\Database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use PhpBotFramework\Exceptions\BotException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | define('PDO_DEFAULT_ADAPTER', 'mysql'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * \addtogroup Modules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | /** \class Handler Handler Database connection handler | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | trait Handler | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     /** @} */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     /** PDO connection to the database. */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public $pdo; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * \addtogroup Bot Bot | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @{ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * \brief Open a database connection using PDO. | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * \details Provides a simple way to initialize a database connection | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * and create a PDO instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param array $params Parameters for initialize connection. | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * Index required: | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      *     - <code>username</code> | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      *     - <code>password</code> (can be a null string) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * Optional index: | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      *     - <code>adapter</code> <b>Default</b>: <code>mysql</code> | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      *     - <code>host</code> <b>Default</b>: <code>localhost</code> | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      *     - <code>options</code> (<i>Array of options passed when creating PDO object</i>) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @return bool True when the connection is succefully created. | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |     public function connect(array $params) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         $params = $this->addDefaultValue($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         $config = $this->getDns($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |             $this->pdo = new \PDO($config, $params['username'], $params['password'], $params['options']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |             echo 'Unable to connect to database, an error occured:' . $e->getMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |         return false; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     /** \brief (<i>Internal</i>) Add default connection value to parameter passed to PDO. | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * @param array $params Parameter for PDO connection. | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @return array Parameter with defaults value. | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |     protected function addDefaultValue(array $params) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |         static $defaults = [ 'adapter' => PDO_DEFAULT_ADAPTER, 'host' => 'localhost', 'options' => [] ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |         return array_merge($defaults, $params); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** \brief (<i>Internal</i>) Returns a string that can passed to PDO as DNS parameter in order to open connection. | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @param array $params Array containing parameter of the connection | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @return string Parameters contained in array $params sanitized in a string that can be passed as DNS param of PDO object creation. | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 3 |  |     protected function getDns($params) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |         $response = $params['adapter'] . ':'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  |         unset($params['adapter']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  |         $fields = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 3 |  |         foreach ($params as $field => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |              *Check if the current field matches one of the fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |              * that are passed to PDO in another way and so don't need | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |              * to be included in the string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 3 |  |             if ($field === 'username' || $field === 'password' || $field === 'options') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |                 unset($params[$field]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 3 |  |             $fields[] = $field . '=' . $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 3 |  |         return $response . join(';', $fields); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |     /** @} */ | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  |