From 56fdcb8f9f21c340a6aa90bcf1d0a69591942273 Mon Sep 17 00:00:00 2001 From: Alan Garcia Date: Fri, 28 Mar 2014 14:28:25 +0000 Subject: [PATCH] Quelques modifs sur la classe variables --- bureau/class/m_export.php | 4 ++-- bureau/class/m_variables.php | 23 ++++++++----------- phpunit/bootstrap.php | 3 +++ .../tests/bureau/class/m_variablesTest.php | 16 +++++++++++++ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/bureau/class/m_export.php b/bureau/class/m_export.php index 187fef91..9289ea38 100644 --- a/bureau/class/m_export.php +++ b/bureau/class/m_export.php @@ -11,8 +11,8 @@ Class m_export { function export_conf(){ global $hooks; - $conf=$hooks->invoke('alternc_export_conf'); - return $conf; + $config=$hooks->invoke('alternc_export_conf'); + return $config; } /** le repertoire de base est passé en paramettre puis en construit une arborescence de la forme diff --git a/bureau/class/m_variables.php b/bureau/class/m_variables.php index 2662be1b..8aecf835 100644 --- a/bureau/class/m_variables.php +++ b/bureau/class/m_variables.php @@ -37,6 +37,7 @@ class m_variables { var $strata_order = array('DEFAULT','GLOBAL','FQDN_CREATOR','FQDN','CREATOR','MEMBER','DOMAIN'); var $cache_variable_list = false; var $replace_array = array(); + var $cache_conf = array(); /** * @@ -169,7 +170,7 @@ class m_variables { // Replace needed vars foreach ($variables as $vv => $hh) { - if (!isset($hh['value']) || !empty($hh['value'])) { + if (!isset($hh['value']) || empty($hh['value'])) { continue; } $variables[$vv]['value'] = strtr($hh['value'], $this->replace_array ); @@ -178,22 +179,20 @@ class m_variables { if ($var && isset($variables[$var])) { return $variables[$var]; } else { - return $variables; + return $variables; } } /** - * Initialize the global $conf array if necessary + * Initialize the global conf * - * @global $conf the global conf array * @uses variable_init() * @param boolean $force */ function variable_init_maybe($force=false) { - global $conf; - if ($force || !isset($conf)) { + if ($force || empty($this->cache_conf) ) { $this->cache_variable_list = false; - $conf = $this->variable_init(); + $this->cache_conf = $this->variable_init(); } } @@ -209,19 +208,17 @@ class m_variables { * and createit_comment value as comment * @return mixed * The value of the variable. - * @global array $conf - * A cache of the configuration. */ function variable_get($name, $default = null, $createit_comment = null, $type=null) { - global $conf; $this->variable_init_maybe(); - if (isset($conf[$name])) { - return $conf[$name]['value']; + if (isset($this->cache_conf[$name])) { + return $this->cache_conf[$name]['value']; } elseif (!is_null($createit_comment)) { $this->variable_update_or_create($name, $default, 'DEFAULT', 'null', 'null', $createit_comment, $type); } + return $default; } @@ -256,6 +253,7 @@ class m_variables { $sql="UPDATE variable SET value='".mysql_real_escape_string($var_value)."' WHERE id = ".intval($var_id); } else { if ( empty($strata) ) { + $this->variable_init_maybe(true); $err->raise('variables', _("Err: Missing strata when creating var")); return false; } @@ -412,7 +410,6 @@ class m_variables { function variables_list() { global $db; if ( ! $this->cache_variable_list ) { - $result = $db->query('SELECT * FROM `variable`'); $arr_var=array(); diff --git a/phpunit/bootstrap.php b/phpunit/bootstrap.php index f9d459c9..cce42a6e 100644 --- a/phpunit/bootstrap.php +++ b/phpunit/bootstrap.php @@ -137,6 +137,7 @@ class DB_system extends DB_Sql { // Creates database from schema // ********************************************* +echo "*** In progress: importing mysql.sql\n"; $queryList = array( "mysql -u $user --password='$password' -e 'DROP DATABASE IF EXISTS $database '", "mysql -u $user --password='$password' -e 'CREATE DATABASE $database'", @@ -148,6 +149,8 @@ foreach ($queryList as $exec_command) { throw new \Exception("[!] Mysql exec error : $exec_command \n Error : \n ".print_r($output,true)); } } +echo "*** In progress: mysql.sql imported\n"; + $db = new \DB_system($user,$database,$password); $db->connect(); $cuid = 0; diff --git a/phpunit/tests/bureau/class/m_variablesTest.php b/phpunit/tests/bureau/class/m_variablesTest.php index 4f26d294..006f1983 100644 --- a/phpunit/tests/bureau/class/m_variablesTest.php +++ b/phpunit/tests/bureau/class/m_variablesTest.php @@ -73,6 +73,22 @@ class m_variablesTest extends AlterncTest { $result = $this->object->variable_get("phpunit"); $this->assertStringMatchesFormat("phpunit",$result); + +/* + // Check old way + $this->object->variable_get('phpunit1', 'toto','plop'); + $result = $this->object->variable_get('phpunit1'); + $this->assertSame("toto",$result); + + // New way + $this->object->variable_get('phpunit2', 'here','comment', array('desc'=>'Want a string','type'=>'string')); + $result = $this->object->variable_get('phpunit2'); + $this->assertSame("here",$result); + + $this->object->variable_get('phpunit3', array("ns1"=>'ns1.tld',"ip"=>"1.2.3.4"),'comment', array("ns1"=>array('desc'=>'ns name','type'=>'string'),"ip"=>array("desc"=>"here an ip", "type"=>"ip"))); + $result = $this->object->variable_get('phpunit2'); + $this->assertSame(array('ns1'=>"ns1.tld", "ip"=>"1.2.3.4"),$result); +*/ } /**