sfDatabaseManager.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfDatabaseManager allows you to setup your database connectivity before the
  12. * request is handled. This eliminates the need for a filter to manage database
  13. * connections.
  14. *
  15. * @package symfony
  16. * @subpackage database
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @author Sean Kerr <sean@code-box.org>
  19. * @version SVN: $Id: sfDatabaseManager.class.php 11293 2008-09-02 18:56:30Z fabien $
  20. */
  21. class sfDatabaseManager
  22. {
  23. protected
  24. $configuration = null,
  25. $databases = array();
  26. /**
  27. * Class constructor.
  28. *
  29. * @see initialize()
  30. */
  31. public function __construct(sfProjectConfiguration $configuration, $options = array())
  32. {
  33. $this->initialize($configuration);
  34. if (!isset($options['auto_shutdown']) || $options['auto_shutdown'])
  35. {
  36. register_shutdown_function(array($this, 'shutdown'));
  37. }
  38. }
  39. /**
  40. * Initializes this sfDatabaseManager object
  41. *
  42. * @param sfProjectConfiguration $configuration A sfProjectConfiguration instance
  43. *
  44. * @return bool true, if initialization completes successfully, otherwise false
  45. *
  46. * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfDatabaseManager object
  47. */
  48. public function initialize(sfProjectConfiguration $configuration)
  49. {
  50. $this->configuration = $configuration;
  51. $this->loadConfiguration();
  52. }
  53. /**
  54. * Loads database configuration.
  55. */
  56. public function loadConfiguration()
  57. {
  58. if ($this->configuration instanceof sfApplicationConfiguration)
  59. {
  60. $databases = include($this->configuration->getConfigCache()->checkConfig('config/databases.yml'));
  61. }
  62. else
  63. {
  64. $configHandler = new sfDatabaseConfigHandler();
  65. $databases = $configHandler->evaluate(array($this->configuration->getRootDir().'/config/databases.yml'));
  66. }
  67. foreach ($databases as $name => $database)
  68. {
  69. $this->setDatabase($name, $database);
  70. }
  71. }
  72. /**
  73. * Sets a database connection.
  74. *
  75. * @param string $name The database name
  76. * @param sfDatabase $sfDatabase A sfDatabase instance
  77. */
  78. public function setDatabase($name, sfDatabase $database)
  79. {
  80. $this->databases[$name] = $database;
  81. }
  82. /**
  83. * Retrieves the database connection associated with this sfDatabase implementation.
  84. *
  85. * @param string $name A database name
  86. *
  87. * @return mixed A Database instance
  88. *
  89. * @throws <b>sfDatabaseException</b> If the requested database name does not exist
  90. */
  91. public function getDatabase($name = 'default')
  92. {
  93. if (isset($this->databases[$name]))
  94. {
  95. return $this->databases[$name];
  96. }
  97. // nonexistent database name
  98. throw new sfDatabaseException(sprintf('Database "%s" does not exist.', $name));
  99. }
  100. /**
  101. * Returns the names of all database connections.
  102. *
  103. * @return array An array containing all database connection names
  104. */
  105. public function getNames()
  106. {
  107. return array_keys($this->databases);
  108. }
  109. /**
  110. * Executes the shutdown procedure
  111. *
  112. * @return void
  113. *
  114. * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this DatabaseManager
  115. */
  116. public function shutdown()
  117. {
  118. // loop through databases and shutdown connections
  119. foreach ($this->databases as $database)
  120. {
  121. $database->shutdown();
  122. }
  123. }
  124. }