Skip to content

Instantly share code, notes, and snippets.

@bchoquet-heliopsis
Created May 18, 2011 16:48
Show Gist options
  • Save bchoquet-heliopsis/978982 to your computer and use it in GitHub Desktop.
Save bchoquet-heliopsis/978982 to your computer and use it in GitHub Desktop.
Helper returning ezcDbHandler From eZPublish database settings
<?php
class HelioINIZetaDBFactory
{
/**
* Database implementations mapping
* Key is eZPublish ImplementationAlias, Value is ezcDbFactory alias
* @var array
*/
private static $map = array(
'mysql' => 'mysql',
'mysqli' => 'mysql',
'ezmysql' => 'mysql',
'ezmysqli' => 'mysql',
'postgresql' => 'pgsql',
'ezpostgresql' => 'pgsql',
);
/**
* Instanciates a new ezcDbHandler based on site.ini's Database Settings
* @return ezcDbHandler
*/
static function ezcDbHandler()
{
$ini = eZINI::instance();
$eZImpl = $ini->variable( 'DatabaseSettings', 'DatabaseImplementation' );
if( !isset( self::$map[$eZImpl] ) )
{
throw new Exception( 'DatabaseImplementation ' . $eZImpl . ' could not be mapped to any ezcDbHandler' );
}
$dbParams = array(
'handler' => self::$map[$eZImpl],
'user' => $ini->variable( 'DatabaseSettings', 'User' ),
'pass' => $ini->variable( 'DatabaseSettings', 'Password' ),
'dbname' => $ini->variable( 'DatabaseSettings', 'Database' ),
'host' => $ini->variable( 'DatabaseSettings', 'Server' ),
);
if( $ini->variable( 'DatabaseSettings', 'Port' ) )
{
$dbParams['port'] = $ini->variable( 'DatabaseSettings', 'Port' );
}
if( $ini->variable( 'DatabaseSettings', 'Charset' ) )
{
$dbParams['charset'] = $ini->variable( 'DatabaseSettings', 'Charset' );
}
if( ( $socket = $ini->variable( 'DatabaseSettings', 'Socket' ) ) && $socket != 'disabled' )
{
$dbParams['socket'] = $socket;
}
$db = ezcDbFactory::create( $dbParams );
return $db;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment