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

Debug toolbar