ConnectionManager.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * @package ActiveRecord
  4. */
  5. namespace ActiveRecord;
  6. /**
  7. * Singleton to manage any and all database connections.
  8. *
  9. * @package ActiveRecord
  10. */
  11. class ConnectionManager extends Singleton
  12. {
  13. /**
  14. * Array of {@link Connection} objects.
  15. * @var array
  16. */
  17. static private $connections = array();
  18. /**
  19. * If $name is null then the default connection will be returned.
  20. *
  21. * @see Config
  22. * @param string $name Optional name of a connection
  23. * @return Connection
  24. */
  25. public static function get_connection($name=null)
  26. {
  27. $config = Config::instance();
  28. $name = $name ? $name : $config->get_default_connection();
  29. if (!isset(self::$connections[$name]) || !self::$connections[$name]->connection)
  30. self::$connections[$name] = Connection::instance($config->get_connection($name));
  31. return self::$connections[$name];
  32. }
  33. /**
  34. * Drops the connection from the connection manager. Does not actually close it since there
  35. * is no close method in PDO.
  36. *
  37. * @param string $name Name of the connection to forget about
  38. */
  39. public static function drop_connection($name=null)
  40. {
  41. if (isset(self::$connections[$name]))
  42. unset(self::$connections[$name]);
  43. }
  44. }
  45. ?>