Exemple #1
0
 /**
  * Constructor
  *
  * @param array $cfg Array of core configuration
  * @param array $common Array of common configuration
  * @param \Facula\Framework $facula The framework itself
  *
  * @return void
  */
 public function __construct(&$cfg)
 {
     if (class_exists('PDO')) {
         $this->configs = array('DefaultTimeout' => isset($cfg['DefaultTimeout']) ? (int) $cfg['DefaultTimeout'] : 1, 'WaitTimeout' => isset($cfg['WaitTimeout']) ? (int) $cfg['WaitTimeout'] : 0, 'SelectMethod' => isset($cfg['SelectMethod']) ? $cfg['SelectMethod'] : 'Normal', 'PriorMethod' => isset($cfg['PriorMethod']) ? $cfg['PriorMethod'] : 'Redundance');
         $supportedDrivers = \PDO::getAvailableDrivers();
         if (isset($cfg['DatabaseGroup']) && is_array($cfg['DatabaseGroup'])) {
             foreach ($cfg['DatabaseGroup'] as $index => $database) {
                 if (isset($database['Driver'][0])) {
                     if (in_array($database['Driver'], $supportedDrivers)) {
                         // Parse and save config to instance
                         $this->pool['DBs'][$index] = array('ID' => $index, 'Driver' => $database['Driver'], 'Connection' => isset($database['Connection'][0]) ? $database['Connection'] : 'host', 'Prefix' => isset($database['Prefix'][0]) ? $database['Prefix'] : null, 'Username' => isset($database['Username'][0]) ? $database['Username'] : null, 'Password' => isset($database['Password'][0]) ? $database['Password'] : null, 'Timeout' => isset($database['Timeout']) ? $database['Timeout'] : $this->configs['DefaultTimeout'], 'Wait' => isset($database['Wait']) ? $database['Wait'] : $this->configs['WaitTimeout'], 'LstConnected' => 0, 'Persistent' => isset($database['Persistent']) ? $database['Persistent'] ? true : false : false, 'Options' => isset($database['Options']) && is_array($database['Options']) ? $database['Options'] : array());
                         // If needed, add current item to Table mapping for search filter.
                         if ($this->configs['SelectMethod'] == 'Table' || $this->configs['SelectMethod'] == 'Table+Operation') {
                             if (isset($database['Tables']) && is_array($database['Tables'])) {
                                 foreach ($database['Tables'] as $table) {
                                     $this->pool['TTDBs'][$table][$index] =& $this->pool['DBs'][$index];
                                     // Add Tables to Database item
                                     $this->pool['DBs'][$index]['Tables'][] = $table;
                                 }
                             } else {
                                 new Error('TABLE_DECLARATION_NEEDED', array($index), 'ERROR');
                                 return;
                             }
                         }
                         // If needed, add current item to Permission mapping for search filter.
                         if ($this->configs['SelectMethod'] == 'Operation' || $this->configs['SelectMethod'] == 'Table+Operation') {
                             if (isset($database['Operates']) && is_array($database['Operates'])) {
                                 foreach ($database['Operates'] as $key => $operation) {
                                     $this->pool['OTDBs'][$operation][$index] =& $this->pool['DBs'][$index];
                                     // Add Operates to Database item
                                     $this->pool['DBs'][$index]['Operations'][] = $operation;
                                 }
                             } else {
                                 new Error('OPERATION_DECLARATION_NEEDED', array($index), 'ERROR');
                                 return;
                             }
                         }
                         // Mapping current database item to connection status store
                         $this->map['DBConn'][$index] = array('Connection' => null, 'Database' => &$this->pool['DBs'][$index]);
                         // Mapping current database item to Prioritize store for later use
                         // DBP for sort the database item so we can shuffle it without disturb database index
                         $this->map['DBP'][$index] =& $this->pool['DBs'][$index];
                     } else {
                         new Error('DRIVER_UNSUPPORTED', array($database['Driver'], $index, implode(', ', $supportedDrivers)), 'ERROR');
                         return;
                     }
                 } else {
                     new Error('DRIVER_DECLARATION_NEEDED', array($index, implode(', ', $supportedDrivers)), 'ERROR');
                     return;
                 }
             }
         } else {
             new Error('DBGROUP_DECLARATION_NEEDED', array(), 'ERROR');
             return;
         }
     } else {
         new Error('PDO_UNSUPPORTED', array(), 'ERROR');
         return;
     }
 }
Exemple #2
0
 /**
  * Constructor
  *
  * @param array $cfg Array of core configuration
  * @param array $common Array of common configuration
  * @param Framework $facula The framework itself
  */
 public function __construct(array &$cfg, array $common, Framework $facula)
 {
     if (!class_exists('PDO')) {
         new Error('PDO_UNSUPPORTED', array(), 'ERROR');
         return;
     }
     $cp = new ConfigParser($cfg, static::$defaultSetting);
     $this->configs = array('DefaultTimeout' => $cp->get('DefaultTimeout'), 'WaitTimeout' => $cp->get('WaitTimeout'), 'SelectMethod' => $cp->get('SelectMethod'), 'PriorMethod' => $cp->get('PriorMethod'));
     $supportedDrivers = PHPPDO::getAvailableDrivers();
     if ($cp->isEmpty('DatabaseGroup')) {
         new Error('DBGROUP_DECLARATION_NEEDED', array(), 'ERROR');
         return;
     }
     foreach ($cp->get('DatabaseGroup') as $index => $database) {
         $dbItem = new ConfigParser($database, static::$defaultSettingItem);
         if ($dbItem->isEmpty('Driver')) {
             new Error('DRIVER_DECLARATION_NEEDED', array($index, implode(', ', $supportedDrivers)), 'ERROR');
             return;
         }
         if (!in_array($dbItem->get('Driver'), $supportedDrivers)) {
             new Error('DRIVER_UNSUPPORTED', array($dbItem->get('Driver'), $index, implode(', ', $supportedDrivers)), 'ERROR');
             return;
         }
         // Parse and save config to instance
         $this->pool['DBs'][$index] = array('ID' => $index, 'Driver' => $dbItem->get('Driver'), 'Connection' => $dbItem->get('Connection'), 'Prefix' => $dbItem->get('Prefix'), 'Username' => $dbItem->get('Username'), 'Password' => $dbItem->get('Password'), 'Timeout' => $dbItem->get('Timeout') ? $dbItem->get('Timeout') : $this->configs['DefaultTimeout'], 'Wait' => $dbItem->get('Wait') ? $dbItem->get('Wait') : $this->configs['WaitTimeout'], 'LstConnected' => 0, 'Persistent' => $dbItem->get('Persistent'), 'Options' => $dbItem->get('Options'));
         // If needed, add current item to Table mapping for search filter.
         switch ($this->configs['SelectMethod']) {
             case 'Table':
             case 'Table+Operation':
                 if ($dbItem->has('Tables')) {
                     foreach ($dbItem->get('Tables') as $table) {
                         $this->pool['TTDBs'][$table][$index] =& $this->pool['DBs'][$index];
                         // Add Tables to Database item
                         $this->pool['DBs'][$index]['Tables'][] = $table;
                     }
                 } else {
                     new Error('TABLE_DECLARATION_NEEDED', array($index), 'ERROR');
                     return;
                 }
                 break;
             default:
                 break;
         }
         // If needed, add current item to Permission mapping for search filter.
         switch ($this->configs['SelectMethod']) {
             case 'Table':
             case 'Table+Operation':
                 if ($dbItem->has('Operates')) {
                     foreach ($dbItem->get('Operates') as $key => $operation) {
                         $this->pool['OTDBs'][$operation][$index] =& $this->pool['DBs'][$index];
                         // Add Operates to Database item
                         $this->pool['DBs'][$index]['Operations'][] = $operation;
                     }
                 } else {
                     new Error('OPERATION_DECLARATION_NEEDED', array($index), 'ERROR');
                     return;
                 }
                 break;
             default:
                 break;
         }
         // Mapping current database item to connection status store
         $this->map['DBConn'][$index] = array('Connection' => null, 'Database' => &$this->pool['DBs'][$index]);
         // Mapping current database item to Prioritize store for later use
         // DBP for sort the database item so we can shuffle it without disturb database index
         $this->map['DBP'][$index] =& $this->pool['DBs'][$index];
     }
 }