*/ class LSconfig { /** * Configuration Data * @see LSconfig::start() * @see $GLOBALS['LSconfig'] * @see $GLOBALS['LSaddons'] * @var array */ private static $data=array(); /** * Lancement de LSconfig * * Chargement initiale des données de configuration à partir des fichiers en * config.*.php du dossier LS_CONF_DIR * * @return boolean True si tout s'est bien passé, False sinon **/ public static function start() { $files=array('config.inc.php','config.LSaddons.php'); foreach($files as $file) { if (!LSsession::includeFile(LS_CONF_DIR.'/'.$file)) { return false; } } if (is_array($GLOBALS['LSconfig'])) { self :: $data = $GLOBALS['LSconfig']; self :: $data['LSaddons'] = $GLOBALS['LSaddons']; return true; } return false; } /** * Explode configuration variable keys * @param string $value The variable name to explode * @return array The exploded variable keys */ public static function explode_keys($value) { $keys = []; $key = ""; for($i=0; $i|string> $args Function arguments could be an array of key or a key * to implode * @return string The imploded configuration variable keys */ public static function implode_keys(...$args) { $keys = []; foreach ($args as $arg) if (is_array($arg)) $keys = array_merge($keys, $arg); else $keys[] = $arg; return implode('.', array_map(['LSconfig', 'escape_key'], $keys)); } /** * Get a specific configuration variable value * * @param string $var The configuration variable name * @param mixed $default The default value to return if configuration variable * is not set (Default : null) * @param string $cast The type of expected value. The configuration variable * value will be cast as this type. Could be : bool, int, * float or string. (Optional, default : raw value) * @param array $data The configuration data (optional) * * @return mixed The configuration variable value **/ public static function get($var, $default=null, $cast=null, $data=null) { $vars = self :: explode_keys($var); $value = $default; if (is_array($vars)) { $value = (is_array($data)?$data:self :: $data); foreach ($vars as $v) { if (!is_array($value) || !isset($value[$v])) { $value = $default; break; } $value = $value[$v]; } } switch($cast) { case 'bool': return boolval($value); case 'int': return intval($value); case 'float': return floatval($value); case 'string': return strval($value); case 'array': return ensureIsArray($value); default: return $value; } } /** * Get list of keys of a specific configuration variable * * @param string $var The configuration variable name * @param array $data The configuration data (optional) * * @return array An array of the keys of a specific configuration variable **/ public static function keys($var, $data=null) { $value = self :: get($var, null, null, $data); return (is_array($value)?array_keys($value):array()); } /** * Get list of configuration variables with their value that matching a specific pattern * * The character '*' could replace any part (expect the first one) of the configuration * variable name. In this case, the keys of the parent value will be iterated to compose * the result. * * @param string $pattern The configuration variable pattern * @param mixed $default The default value to return if configuration variable * is not set (optional, see self :: get()) * @param string $cast The type of expected value (optional, see self :: get()) * @param array $data The configuration data (optional, see self :: get()) * * @return array The list of matching configuration variables with their value **/ public static function getMatchingKeys($pattern, $default=null, $cast=null, $data=null, $prefix=null) { $return = array(); if ($pos = strpos($pattern, '*')) { // Handle subkey $root_key = (is_string($prefix)?"$prefix.":"").substr($pattern, 0, ($pos-1)); $suffix = substr($pattern, $pos+2, (strlen($pattern)-$pos)); $subkeys = self :: keys($root_key); if ($suffix) { foreach ($subkeys as $subkey) $return = array_merge($return, self :: getMatchingKeys($suffix, $default, $cast, $data, "$root_key.$subkey")); } else { foreach ($subkeys as $subkey) { $key = self :: implode_keys(self :: explode_keys($root_key), $subkey); $return[$key] = self :: get($key, $default, $cast, $data); } } } else { $key = (is_string($prefix)?"$prefix.":"").$pattern; $return[$key] = self :: get($key, $default, $cast, $data); } return $return; } /** * Définition d'une valeur * * @param string $var Le nom de valeur à définir * @param mixed $val La valeur de la variable * * @return boolean True si tout s'est bien passé, False sinon **/ public static function set($var,$val) { $vars = self :: explode_keys($var); if(is_array($vars)) { $code='self :: $data'; foreach ($vars as $v) { $code.='["'.$v.'"]'; } $code.='=$val;'; return (eval($code)===NULL); } return; } }