Guest User

Untitled

a guest
Apr 7th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class ConnectionManager {
  4.  
  5. static $configurations = array();
  6. static $connections = array();
  7.  
  8. static function &connection($name) {
  9. if (!isset(self::$connections[$name])) self::establish_connection($name);
  10. return self::$connections[$name];
  11. }
  12.  
  13. protected static function establish_connection($name) {
  14. if (isset(self::$configurations[$name])) {
  15. $configuration = self::$configurations[$name];
  16. if (!isset($configuration['dbname'])) $configuration['dbname'] = array_delete('database', $configuration);
  17. $adapter = array_delete('adapter', $configuration);
  18. $username = array_delete('username', $configuration);
  19. $password = array_delete('password', $configuration);
  20. $connection_string = $adapter.':';
  21. $connection_string .= ($adapter == 'sqlite') ? $configuration['dbname'] : array_join_assoc('=', ';', $configuration);
  22. self::$connections[$name] = new PDO($connection_string, $username, $password);
  23. } else {
  24. throw new InvalidArgumentException('Database configuration "'.$name.'" does not exist');
  25. }
  26. }
  27.  
  28. }
Add Comment
Please, Sign In to add comment