| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
class sfDatabaseManager |
|---|
| 24 |
{ |
|---|
| 25 |
protected |
|---|
| 26 |
$configuration = null, |
|---|
| 27 |
$databases = array(); |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
* Class constructor. |
|---|
| 31 |
* |
|---|
| 32 |
* @see initialize() |
|---|
| 33 |
*/ |
|---|
| 34 |
public function __construct(sfProjectConfiguration $configuration, $options = array()) |
|---|
| 35 |
{ |
|---|
| 36 |
$this->initialize($configuration); |
|---|
| 37 |
|
|---|
| 38 |
if (!isset($options['auto_shutdown']) || $options['auto_shutdown']) |
|---|
| 39 |
{ |
|---|
| 40 |
register_shutdown_function(array($this, 'shutdown')); |
|---|
| 41 |
} |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
* Initializes this sfDatabaseManager object |
|---|
| 46 |
* |
|---|
| 47 |
* @param sfProjectConfiguration $configuration A sfProjectConfiguration instance |
|---|
| 48 |
* |
|---|
| 49 |
* @return bool true, if initialization completes successfully, otherwise false |
|---|
| 50 |
* |
|---|
| 51 |
* @throws <b>sfInitializationException</b> If an error occurs while initializing this sfDatabaseManager object |
|---|
| 52 |
*/ |
|---|
| 53 |
public function initialize(sfProjectConfiguration $configuration) |
|---|
| 54 |
{ |
|---|
| 55 |
$this->configuration = $configuration; |
|---|
| 56 |
|
|---|
| 57 |
$this->loadConfiguration(); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
* Loads database configuration. |
|---|
| 62 |
*/ |
|---|
| 63 |
public function loadConfiguration() |
|---|
| 64 |
{ |
|---|
| 65 |
if ($this->configuration instanceof sfApplicationConfiguration) |
|---|
| 66 |
{ |
|---|
| 67 |
$databases = include($this->configuration->getConfigCache()->checkConfig('config/databases.yml')); |
|---|
| 68 |
} |
|---|
| 69 |
else |
|---|
| 70 |
{ |
|---|
| 71 |
$configHandler = new sfDatabaseConfigHandler(); |
|---|
| 72 |
$databases = $configHandler->evaluate(array($this->configuration->getRootDir().'/config/databases.yml')); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
foreach ($databases as $name => $database) |
|---|
| 76 |
{ |
|---|
| 77 |
$this->setDatabase($name, $database); |
|---|
| 78 |
} |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
* Sets a database connection. |
|---|
| 83 |
* |
|---|
| 84 |
* @param string $name The database name |
|---|
| 85 |
* @param sfDatabase $database A sfDatabase instance |
|---|
| 86 |
*/ |
|---|
| 87 |
public function setDatabase($name, sfDatabase $database) |
|---|
| 88 |
{ |
|---|
| 89 |
$this->databases[$name] = $database; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
* Retrieves the database connection associated with this sfDatabase implementation. |
|---|
| 94 |
* |
|---|
| 95 |
* @param string $name A database name |
|---|
| 96 |
* |
|---|
| 97 |
* @return mixed A Database instance |
|---|
| 98 |
* |
|---|
| 99 |
* @throws <b>sfDatabaseException</b> If the requested database name does not exist |
|---|
| 100 |
*/ |
|---|
| 101 |
public function getDatabase($name = 'default') |
|---|
| 102 |
{ |
|---|
| 103 |
if (isset($this->databases[$name])) |
|---|
| 104 |
{ |
|---|
| 105 |
return $this->databases[$name]; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
throw new sfDatabaseException(sprintf('Database "%s" does not exist.', $name)); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
* Returns the names of all database connections. |
|---|
| 114 |
* |
|---|
| 115 |
* @return array An array containing all database connection names |
|---|
| 116 |
*/ |
|---|
| 117 |
public function getNames() |
|---|
| 118 |
{ |
|---|
| 119 |
return array_keys($this->databases); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
* Executes the shutdown procedure |
|---|
| 124 |
* |
|---|
| 125 |
* @return void |
|---|
| 126 |
* |
|---|
| 127 |
* @throws <b>sfDatabaseException</b> If an error occurs while shutting down this DatabaseManager |
|---|
| 128 |
*/ |
|---|
| 129 |
public function shutdown() |
|---|
| 130 |
{ |
|---|
| 131 |
|
|---|
| 132 |
foreach ($this->databases as $database) |
|---|
| 133 |
{ |
|---|
| 134 |
$database->shutdown(); |
|---|
| 135 |
} |
|---|
| 136 |
} |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|