allow eg. unit-tests to set/change schema used by db class: Db::set_table_definition()

This commit is contained in:
Ralf Becker 2016-08-01 13:34:25 +02:00
parent c033435122
commit ba18f3f594

View File

@ -1575,6 +1575,28 @@ class Db
$this->app = $app; $this->app = $app;
} }
/**
* Data used by (get|set)_table_defintion and get_column_attribute
*
* @var array
*/
protected static $all_app_data = array();
/**
* Set/changes definition of one table
*
* If you set or change defition of a single table of an app, other tables
* are not loaded from $app/setup/tables_current.inc.php!
*
* @param string $app name of the app $table belongs too
* @param string $table table name
* @param array $definition table definition
*/
public static function set_table_definitions($app, $table, array $definition)
{
self::$all_app_data[$app][$table] = $definition;
}
/** /**
* reads the table-definitions from the app's setup/tables_current.inc.php file * reads the table-definitions from the app's setup/tables_current.inc.php file
* *
@ -1592,10 +1614,9 @@ class Db
// ease the transition to api // ease the transition to api
if ($app === 'phpgwapi') $app = 'api'; if ($app === 'phpgwapi') $app = 'api';
static $all_app_data = array();
if ($app === true && $table) if ($app === true && $table)
{ {
foreach($all_app_data as $app => &$app_data) foreach(self::$all_app_data as $app => &$app_data)
{ {
if (isset($app_data[$table])) if (isset($app_data[$table]))
{ {
@ -1605,25 +1626,25 @@ class Db
// $table not found in loaded apps, check not yet loaded ones // $table not found in loaded apps, check not yet loaded ones
foreach(scandir(EGW_INCLUDE_ROOT) as $app) foreach(scandir(EGW_INCLUDE_ROOT) as $app)
{ {
if ($app[0] == '.' || !is_dir(EGW_INCLUDE_ROOT.'/'.$app) || isset($all_app_data[$app])) if ($app[0] == '.' || !is_dir(EGW_INCLUDE_ROOT.'/'.$app) || isset(self::$all_app_data[$app]))
{ {
continue; continue;
} }
$tables_current = EGW_INCLUDE_ROOT . "/$app/setup/tables_current.inc.php"; $tables_current = EGW_INCLUDE_ROOT . "/$app/setup/tables_current.inc.php";
if (!@file_exists($tables_current)) if (!@file_exists($tables_current))
{ {
$all_app_data[$app] = False; self::$all_app_data[$app] = False;
} }
else else
{ {
$phpgw_baseline = null; $phpgw_baseline = null;
include($tables_current); include($tables_current);
$all_app_data[$app] =& $phpgw_baseline; self::$all_app_data[$app] =& $phpgw_baseline;
unset($phpgw_baseline); unset($phpgw_baseline);
if (isset($all_app_data[$app][$table])) if (isset(self::$all_app_data[$app][$table]))
{ {
return $all_app_data[$app][$table]; return self::$all_app_data[$app][$table];
} }
} }
} }
@ -1633,7 +1654,7 @@ class Db
{ {
$app = $this->app ? $this->app : $GLOBALS['egw_info']['flags']['currentapp']; $app = $this->app ? $this->app : $GLOBALS['egw_info']['flags']['currentapp'];
} }
$app_data =& $all_app_data[$app]; $app_data =& self::$all_app_data[$app];
if (!isset($app_data)) if (!isset($app_data))
{ {