sfDatabaseManager.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 9086 2008-05-20 01:56:29Z Carl.Vondrick $
  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(sfApplicationConfiguration $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 sfApplicationConfiguration $configuration A sfApplicationConfiguration 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(sfApplicationConfiguration $configuration)
  49. {
  50. $this->configuration = $configuration;
  51. $this->loadConfiguration();
  52. }
  53. /**
  54. * Loads database configuration.
  55. */
  56. public function loadConfiguration()
  57. {
  58. require($this->configuration->getConfigCache()->checkConfig('config/databases.yml'));
  59. }
  60. /**
  61. * Sets a database connection.
  62. *
  63. * @param string $name The database name
  64. * @param sfDatabase $sfDatabase A sfDatabase instance
  65. */
  66. public function setDatabase($name, sfDatabase $database)
  67. {
  68. $this->databases[$name] = $database;
  69. }
  70. /**
  71. * Retrieves the database connection associated with this sfDatabase implementation.
  72. *
  73. * @param string $name A database name
  74. *
  75. * @return mixed A Database instance
  76. *
  77. * @throws <b>sfDatabaseException</b> If the requested database name does not exist
  78. */
  79. public function getDatabase($name = 'default')
  80. {
  81. if (isset($this->databases[$name]))
  82. {
  83. return $this->databases[$name];
  84. }
  85. // nonexistent database name
  86. throw new sfDatabaseException(sprintf('Database "%s" does not exist.', $name));
  87. }
  88. /**
  89. * Returns the names of all database connections.
  90. *
  91. * @return array An array containing all database connection names
  92. */
  93. public function getNames()
  94. {
  95. return array_keys($this->databases);
  96. }
  97. /**
  98. * Executes the shutdown procedure
  99. *
  100. * @return void
  101. *
  102. * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this DatabaseManager
  103. */
  104. public function shutdown()
  105. {
  106. // loop through databases and shutdown connections
  107. foreach ($this->databases as $database)
  108. {
  109. $database->shutdown();
  110. }
  111. }
  112. }