DatabaseManager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. use Yaf\Exception;
  3. /**
  4. * Multi-connection database manager
  5. *
  6. * @author Ilya Sabelnikov <[email protected]>
  7. */
  8. class DatabaseManager
  9. {
  10. /**
  11. * @var DatabaseManager
  12. */
  13. protected static $instance = null;
  14. /**
  15. * Initialized database connections
  16. *
  17. * @var array
  18. */
  19. protected $connections = array();
  20. /**
  21. * All database connection parameters
  22. *
  23. * @var array
  24. */
  25. protected $definedConnections = array();
  26. private function __construct (array $definedConnections)
  27. {
  28. $this->definedConnections = $definedConnections;
  29. }
  30. /**
  31. * @param array $definedConnections
  32. * @return DatabaseManager
  33. */
  34. public static function getInstance ()
  35. {
  36. if ( ! isset(self::$instance)) {
  37. $definedConnections = Yaf\Application::app()->getConfig()->get('db.connections')->toArray();
  38. self::$instance = new self($definedConnections);
  39. register_shutdown_function(array(self::$instance, 'resetInstance'));
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * Initialises database connection by given connection name
  45. *
  46. * @param string $connectionName
  47. * @return \PDO
  48. */
  49. public function getConnection ($connectionName = 'default')
  50. {
  51. if ( ! isset($this->connections[$connectionName])) {
  52. $this->connections[$connectionName] = $this->createPdoInstance($connectionName);
  53. }
  54. return $this->connections[$connectionName];
  55. }
  56. /**
  57. * Initialize connection defined in application.ini under db.connection area
  58. *
  59. * @param string $connectionName
  60. * @return \PDO
  61. */
  62. protected function createPdoInstance ($connectionName)
  63. {
  64. $options = $this->getConnectionOptions($connectionName);
  65. $params = array();
  66. if (isset($options['params']) && is_array($options['params'])) {
  67. foreach ($options['params'] as $key => $val) {
  68. $attributeName = '\\PDO::' . $key;
  69. if ( ! defined($attributeName)) {
  70. throw new Exception(sprintf('Unknown PDO attribute "%s"', $attributeName));
  71. }
  72. $params[constant($attributeName)] = $val;
  73. }
  74. }
  75. return new PDO($options['dsn'], $options['user'], $options['pass'], $params);
  76. }
  77. /**
  78. * @param string $connectionName
  79. * @throws Exception
  80. * @return array
  81. */
  82. protected function getConnectionOptions ($connectionName)
  83. {
  84. if ( ! $this->hasConnectionOptions($connectionName)) {
  85. throw new Exception(sprintf('Unknown connection alias "%s"', $connectionName));
  86. }
  87. return $this->definedConnections[$connectionName];
  88. }
  89. /**
  90. * Check whether connection is defined within configuration file
  91. *
  92. * @param string $connectionName
  93. * @return boolean
  94. */
  95. protected function hasConnectionOptions ($connectionName)
  96. {
  97. return isset($this->definedConnections[$connectionName]);
  98. }
  99. public function closeConnections ()
  100. {
  101. foreach (array_keys($this->connections) as $connectionName) {
  102. $this->closeConnection($connectionName);
  103. }
  104. }
  105. public function closeConnection ($connectionName = 'default')
  106. {
  107. if ( ! isset($this->connections[$connectionName])) {
  108. return false;
  109. }
  110. unset($this->connections[$connectionName]);
  111. return true;
  112. }
  113. /**
  114. * Reset the internal static instance
  115. *
  116. * @return void
  117. */
  118. public static function resetInstance ()
  119. {
  120. if ( ! self::$instance) {
  121. return;
  122. }
  123. self::$instance->reset();
  124. self::$instance = null;
  125. }
  126. /**
  127. * Reset this instance of the manager
  128. *
  129. * @return void
  130. */
  131. public function reset ()
  132. {
  133. $this->closeConnections();
  134. $this->definedConnections = array();
  135. }
  136. }