Il peut être intéressant de créer des profils à la volée dans jDb. Voici un jDb patché permettant de le faire grâce à la méthode createVirtualProfile. Ce qui a changé :
- Rajout d'un attribut protégé statique $_profils
- Modification de la méthode getProfils en conséquence.
- Création de la méthode statique createVirtualProfile
La mise en place de cette version de jDb (qui finalement change bien peu de choses) nécessite la création de deux locales supplémentaires :
- jelix~db.error.virtual.profil.no.name
- jelix~db.error.virtual.profil.invalid.params
Code final de la classe jDb :
class jDb{
static private $_profils = null;
public static function getConnection($name = null){
static $cnxPool = array();
$profil = self::getProfil($name);
if(!isset($cnxPool[$name])){
$cnxPool[$name] = self::_createConnector($profil);
}
return $cnxPool[$name];
}
public static function getDbWidget($name=null){
$dbw = new jDbWidget(self::getConnection($name));
return $dbw;
}
public static function getTools($name=null){
$profil = self::getProfil($name);
$driver = $profil['driver'];
if($driver == 'pdo'){
preg_match('/^(\w+)\:.*$/',$profil['dsn'], $m);
$driver = $m[1];
}
global $gJConfig;
require_once($gJConfig->_pluginsPathList_db[$profil['driver']].$profil['driver'].'.dbtools.php');
$class = $profil['driver'].'DbTools';
$cnx = self::getConnection($name);
$tools = new $class($cnx);
return $tools;
}
public static function getProfil($name='', $nameIsProfilType=false){
global $gJConfig;
if(self::$_profils === null){
self::$_profils = parse_ini_file(JELIX_APP_CONFIG_PATH.$gJConfig->dbProfils , true);
}
if($name == ''){
if(isset(self::$_profils['default']))
$name=self::$_profils['default'];
else
throw new jException('jelix~db.error.default.profil.unknow');
}elseif($nameIsProfilType){
if(isset(self::$_profils[$name]) && is_string(self::$_profils[$name])){
$name = self::$_profils[$name];
}else{
throw new jException('jelix~db.error.profil.type.unknow',$name);
}
}
if(isset(self::$_profils[$name]) && is_array(self::$_profils[$name])){
self::$_profils[$name]['name'] = $name;
return self::$_profils[$name];
}else{
throw new jException('jelix~db.error.profil.unknow',$name);
}
}
public function testProfil($profil){
try{
self::_createConnector($profil);
$ok = true;
}catch(Exception $e){
$ok = false;
}
return $ok;
}
private static function _createConnector($profil){
if($profil['driver'] == 'pdo'){
$dbh = new jDbPDOConnection($profil);
return $dbh;
}else{
global $gJConfig;
$p = $gJConfig->_pluginsPathList_db[$profil['driver']].$profil['driver'];
require_once($p.'.dbconnection.php');
require_once($p.'.dbresultset.php');
$class = $profil['driver'].'DbConnection';
$dbh = new $class($profil);
return $dbh;
}
}
public static function createVirtualProfile ($name = null, $params)
{
if (is_null ($name))
{
throw new jException('jelix~db.error.virtual.profil.no.name');
}
if (! is_array ($params))
{
throw new jException('jelix~db.error.virtual.profil.invalid.params');
}
if (self::$_profils === null)
{
self::$_profils =
parse_ini_file (
JELIX_APP_CONFIG_PATH . $gJConfig->dbProfils,
true);
}
self::$_profils[$name] = $params;
print_r (self::$_profils);
}
}