Sympony - Propel - Get Connection Details



/**
* Returns an associative array with the details of the database connection
*
* @param string $application the name of a valid application (i.e.: frontend) - used to get a Propel connection
* @param string $environment the environment we want to apply the deltas to
* @return array(of strings) the keys are: name, host, user, password
*/
public static function getDatabaseConnectionDetails($application, $environment)

{
$databaseConnectionDetails = array();
$configuration = ProjectConfiguration::getApplicationConfiguration($application, $environment, true);
sfContext::createInstance($configuration);
$databaseManager = new sfDatabaseManager($configuration);
$databaseConfig = sfPropelDatabase::getConfiguration();
$databaseDsn = $databaseConfig[’propel’][’datasources’][’propel’][’connection’][’dsn’];

if (preg_match(’/dbname=([^;]+);/’, $databaseDsn, $matches))
{
$databaseConnectionDetails[’name’] = $matches[1];
}
else
{
throw new sfException(sprintf(’Unable to get the database name.’));
}

if (preg_match(’/;host=(.+)/’, $databaseDsn, $matches))
{
$databaseConnectionDetails[’host’] = $matches[1];
}
else
{
throw new sfException(sprintf(’Unable to get the host for the database.’));
}

$user = $databaseConfig[’propel’][’datasources’][’propel’][’connection’][’user’];
if ($user)
{
$databaseConnectionDetails[’user’] = $user;
}
else
{
throw new sfException(sprintf(’Unable to get the username for the database.’));
}

$password = $databaseConfig[’propel’][’datasources’][’propel’][’connection’][’password’];
if ($password)
{
$databaseConnectionDetails[’password’] = $password;
}
else
{
throw new sfException(sprintf(’Unable to get the username for the database.’));
}
return $databaseConnectionDetails;
}

Leave a Reply

You must be logged in to post a comment.