Config

Package engine.lib
Inheritance class Config
Since 1.0
Source Code /engine/lib/internal/ConfigSimple/Config.class.php
Управление простым конфигом в виде массива

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
aConfig array Store for configuration entries for current instance Config
aInstance array Массив сущностей класса Config
aMapper array Mapper rules for Config Path <-> Constant Name relations Config

Public Methods

Hide inherited methods

MethodDescriptionDefined By
DefineConstant() Define constants using config-constant mapping Config
Get() Retrive information from configuration array Config
GetConfig() Возвращает текущий полный конфиг Config
GetKeys() Find all keys recursivly in config array Config
GetValue() Получает значение из конфигурации по переданному ключу Config
KeyReplace() Заменяет плейсхолдеры ключей в значениях конфига Config
Load() Load configuration array from given array Config
LoadFromFile() Load configuration array from file Config
Set() Add information in config array by handle path Config
SetConfig() Устанавливает значения конфига Config
getInstance() Ограничиваем объект только одним экземпляром Config
isExist() Try to find element by given key Config

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
ArrayEmerge() Сливает ассоциативные массивы Config
__construct() Disabled constract process Config
func_array_keys_recursive() Рекурсивный вариант array_keys Config
func_array_merge_assoc() Сливает два ассоциативных массива Config

Property Details

aConfig property
protected array $aConfig;

Store for configuration entries for current instance

aInstance property
protected static array $aInstance;

Массив сущностей класса

aMapper property
protected static array $aMapper;

Mapper rules for Config Path <-> Constant Name relations

Method Details

ArrayEmerge() method
protected array ArrayEmerge(array $aArr1, array $aArr2)
$aArr1 array Массив
$aArr2 array Массив
{return} array
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#312 (show)
protected function ArrayEmerge($aArr1,$aArr2) {
    return 
$this->func_array_merge_assoc($aArr1,$aArr2);
}

Сливает ассоциативные массивы

DefineConstant() method
public static bool DefineConstant(string $sKey='', string $sInstance='general')
$sKey string Ключ
$sInstance string Название инстанции конфига
{return} bool
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#285 (show)
static public function DefineConstant($sKey='',$sInstance=self::DEFAULT_CONFIG_INSTANCE) {
    if(
$aKeys=self::getInstance($sInstance)->GetKeys()) {
        foreach(
$aKeys as $key) {
            
// If there is key-mapping rool, replace it
            
$sName = isset(self::$aMapper[$key])
                ? 
self::$aMapper[$key]
                : 
strtoupper(str_replace('.','_',$key));
            if( (
substr($key,0,strlen($sKey))==strtoupper($sKey))
                && !
defined($sName)
                && (
self::isExist($key,$sInstance)) )
            {
                
$cfg=self::Get($key,$sInstance);
                
// Define constant, if founded value is scalar or NULL
                
if(is_scalar($cfg)||$cfg===NULL)define(strtoupper($sName),$cfg);
            }
        }
        return 
true;
    }
    return 
false;
}

Define constants using config-constant mapping

Get() method
public static mixed Get(string $sKey='', string $sInstance='general')
$sKey string Ключ
$sInstance string Название инстанции конфига
{return} mixed
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#143 (show)
static public function Get($sKey=''$sInstance=self::DEFAULT_CONFIG_INSTANCE) {
    
// Return all config array
    
if($sKey=='') {
        return 
self::getInstance($sInstance)->GetConfig();
    }

    return 
self::getInstance($sInstance)->GetValue($sKey,$sInstance);
}

Retrive information from configuration array

GetConfig() method
public array GetConfig()
{return} array
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#114 (show)
public function GetConfig() {
    return 
$this->aConfig;
}

Возвращает текущий полный конфиг

GetKeys() method
public array GetKeys()
{return} array
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#269 (show)
public function GetKeys() {
    
$cfg=$this->GetConfig();
    
// If it`s not array, return key
    
if(!is_array($cfg)) {
        return 
false;
    }
    
// If it`s array, get array_keys recursive
    
return $this->func_array_keys_recursive($cfg);
}

Find all keys recursivly in config array

GetValue() method
public mixed GetValue(string $sKey, string $sInstance='general')
$sKey string Ключ
$sInstance string Название инстанции конфига
{return} mixed
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#158 (show)
public function GetValue($sKey$sInstance=self::DEFAULT_CONFIG_INSTANCE) {
    
// Return config by path (separator=".")
    
$aKeys=explode('.',$sKey);

    
$cfg=$this->GetConfig();
    foreach ((array)
$aKeys as $sK) {
        if(isset(
$cfg[$sK])) {
            
$cfg=$cfg[$sK];
        } else {
            return 
null;
        }
    }

    
$cfg self::KeyReplace($cfg,$sInstance);
    return 
$cfg;
}

Получает значение из конфигурации по переданному ключу

KeyReplace() method
public static array|mixed KeyReplace(string|array $cfg, string $sInstance='general')
$cfg string|array Значения конфига
$sInstance string Название инстанции конфига
{return} array|mixed
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#182 (show)
static public function KeyReplace($cfg,$sInstance=self::DEFAULT_CONFIG_INSTANCE) {
    if(
is_array($cfg)) {
        foreach(
$cfg as $k=>$v) {
            
$k_replaced self::KeyReplace($k$sInstance);
            if(
$k==$k_replaced) {
                
$cfg[$k] = self::KeyReplace($v,$sInstance);
            } else {
                
$cfg[$k_replaced] = self::KeyReplace($v,$sInstance);
                unset(
$cfg[$k]);
            }
        }
    } else {
        if(
preg_match('~___([\S|\.|]+)___~Ui',$cfg))
            
$cfg preg_replace_callback(
                
'~___([\S|\.]+)___~Ui',
                
create_function('$value','return Config::Get($value[1],"'.$sInstance.'");'),
                
$cfg
            
);
    }
    return 
$cfg;
}

Заменяет плейсхолдеры ключей в значениях конфига

Load() method
public static bool|Config Load(array $aConfig, bool $bRewrite=true, string $sInstance='general')
$aConfig array Массив конфига
$bRewrite bool Перезаписывать значения
$sInstance string Название инстанции конфига
{return} bool|Config
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#100 (show)
static public function Load($aConfig,$bRewrite=true,$sInstance=self::DEFAULT_CONFIG_INSTANCE) {
    
// Check if it`s array
    
if(!is_array($aConfig)) {
        return 
false;
    }
    
// Set config to current or handle instance
    
self::getInstance($sInstance)->SetConfig($aConfig,$bRewrite);
    return 
self::getInstance($sInstance);
}

Load configuration array from given array

LoadFromFile() method
public static bool|Config LoadFromFile(string $sFile, bool $bRewrite=true, string $sInstance='general')
$sFile string Путь до файла конфига
$bRewrite bool Перезаписывать значения
$sInstance string Название инстанции конфига
{return} bool|Config
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#82 (show)
static public function LoadFromFile($sFile,$bRewrite=true,$sInstance=self::DEFAULT_CONFIG_INSTANCE) {
    
// Check if file exists
    
if (!file_exists($sFile)) {
        return 
false;
    }
    
// Get config from file
    
$aConfig=include($sFile);
    return 
self::Load($aConfig,$bRewrite,$sInstance);
}

Load configuration array from file

Set() method
public static bool Set(string $sKey, mixed $value, string $sInstance='general')
$sKey string Ключ
$value mixed Значение
$sInstance string Название инстанции конфига
{return} bool
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#238 (show)
static public function Set($sKey,$value,$sInstance=self::DEFAULT_CONFIG_INSTANCE) {
    
$aKeys=explode('.',$sKey);

    if(isset(
$value['$root$']) && is_array($value['$root$'])){
        
$aRoot $value['$root$'];
        unset(
$value['$root$']);
        foreach(
$aRoot as $sRk => $mRv){
            
self::Set(
                
$sRk,
                
self::isExist($sRk)
                    ? 
func_array_merge_assoc(Config::Get($sRk$sInstance), $mRv)
                    : 
$mRv
                
,
                
$sInstance
            
);
        }
    }

    
$sEval='self::getInstance($sInstance)->aConfig';
    foreach (
$aKeys as $sK) {
        
$sEval.='['.var_export((string)$sK,true).']';
    }
    
$sEval.='=$value;';
    eval(
$sEval);
    return 
true;
}

Add information in config array by handle path

SetConfig() method
public bool SetConfig(array $aConfig=array ( ), bool $bRewrite=true)
$aConfig array Массив конфига
$bRewrite bool Перезаписывать значения
{return} bool
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#124 (show)
public function SetConfig($aConfig=array(),$bRewrite=true) {
    if (
is_array($aConfig)) {
        if (
$bRewrite) {
            
$this->aConfig=$aConfig;
        } else {
            
$this->aConfig=$this->ArrayEmerge($this->aConfig,$aConfig);
        }
        return 
true;
    }
    
$this->aConfig=array();
    return 
false;
}

Устанавливает значения конфига

__construct() method
protected void __construct()
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#54 (show)
protected function __construct() {

}

Disabled constract process

func_array_keys_recursive() method
protected array func_array_keys_recursive(array $array)
$array array Массив
{return} array
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#321 (show)
protected function func_array_keys_recursive($array) {
    if(!
is_array($array)) {
        return 
false;
    } else {
        
$keys array_keys($array);
        foreach (
$keys as $k=>$v) {
            if(
$append $this->func_array_keys_recursive($array[$v])){
                unset(
$keys[$k]);
                foreach (
$append as $new_key){
                    
$keys[] = $v.".".$new_key;
                }
            }
        }
        return 
$keys;
    }
}

Рекурсивный вариант array_keys

func_array_merge_assoc() method
protected array func_array_merge_assoc(array $aArr1, array $aArr2)
$aArr1 array Массив
$aArr2 array Массив
{return} array
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#344 (show)
protected function func_array_merge_assoc($aArr1,$aArr2) {
    
$aRes=$aArr1;
    foreach (
$aArr2 as $k2 => $v2) {
        
$bIsKeyInt=false;
        if (
is_array($v2)) {
            foreach (
$v2 as $k => $v) {
                if (
is_int($k)) {
                    
$bIsKeyInt=true;
                    break;
                }
            }
        }
        if (
is_array($v2) and !$bIsKeyInt and isset($aArr1[$k2])) {
            
$aRes[$k2]=$this->func_array_merge_assoc($aArr1[$k2],$v2);
        } else {
            
$aRes[$k2]=$v2;
        }
    }
    return 
$aRes;
}

Сливает два ассоциативных массива

getInstance() method
public static Config getInstance(string $sName='general')
$sName string Название инстанции конфига
{return} Config
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#65 (show)
static public function getInstance($sName=self::DEFAULT_CONFIG_INSTANCE) {
    if (isset(
self::$aInstance[$sName])) {
        return 
self::$aInstance[$sName];
    } else {
        
self::$aInstance[$sName]= new self();
        return 
self::$aInstance[$sName];
    }
}

Ограничиваем объект только одним экземпляром

isExist() method
public static bool isExist(string $sKey, string $sInstance='general')
$sKey string Path to needed value
$sInstance string Name of needed instance
{return} bool
Source Code: /engine/lib/internal/ConfigSimple/Config.class.php#213 (show)
static public function isExist($sKey$sInstance=self::DEFAULT_CONFIG_INSTANCE) {
    
// Return all config array
    
if($sKey=='') {
        return (
count((array)self::getInstance($sInstance)->GetConfig())>0);
    }
    
// Analyze config by path (separator=".")
    
$aKeys=explode('.',$sKey);
    
$cfg=self::getInstance($sInstance)->GetConfig();
    foreach ((array)
$aKeys as $sK) {
        if (
array_key_exists($sK$cfg)) {
            
$cfg=$cfg[$sK];
        } else {
            return 
false;
        }
    }
    return 
true;
}

Try to find element by given key Using function ARRAY_KEY_EXISTS (like in SPL)

Workaround for http://bugs.php.net/bug.php?id=40442