config配置項(xiàng)正則增刪改插件

v0.0.2
版本
2022-05-20
版本更新時(shí)間
17
安裝
1
star
簡(jiǎn)介
插件化開(kāi)發(fā),安裝插件時(shí)很多情況需要修改配置文件,通過(guò)正則能完美解決修改配置影響備注信息的問(wèn)題。
taoser/setconfig
通過(guò)配置數(shù)組,即可非常簡(jiǎn)便的修改、添加、刪除配置文件中的數(shù)據(jù),使得插件化開(kāi)發(fā)更加統(tǒng)一和靈活的對(duì)配置文件的操作。
配置文件數(shù)組遵守的規(guī)則為:
- 一個(gè)數(shù)組中,無(wú)論它有多少個(gè)元素及是否包含子數(shù)組,它的索引數(shù)組(一維)在前,關(guān)聯(lián)(多維)數(shù)組排序在后。
- 數(shù)組每個(gè)元素后加
,
號(hào),數(shù)組結(jié)尾],
逗號(hào),以方便需要插入新元素時(shí)識(shí)別。
安裝
composer require taoser/setconfig
使用
類引用變量
app
即為config/app.php文件
$data = [
true,false,111,
'aaa'=>'bbb',
'ccc'=>[
true,
222,
"app()";
"http://備注信息",
......
],
......
];
$conf = new \Taoser\SetConfig\SetArr('app');
$conf->add($data);
添加數(shù)組節(jié)點(diǎn)
可以對(duì)配置文件數(shù)組中添加新的元素,添加規(guī)則:
- 支持嵌套4級(jí)數(shù)組,最后一級(jí)數(shù)組元素只能是一維數(shù)組,
- 只能給一維數(shù)組元素添加備注
“//備注”
,不能添加在關(guān)聯(lián)數(shù)組的前面,- 函數(shù),類,需要引號(hào)包裹
""
,- bool,數(shù)值,字符串非索引的元素的添加會(huì)重復(fù)添加,
- 結(jié)果返回布爾值
$add = [
1,true,false,
"http://支持備注的添加",
"base_path() . DIRECTORY_SEPARATOR . 'public',"
'a' => 1,
'b' => 'b',
'c' => true,
'd' => false,
'e' => "support\bootstrap\Session::class,",
"http:// 這里這一行添加備注是無(wú)效的,在子數(shù)組前寫(xiě)備注位置會(huì)不正確",
'f' => [
22,true,false,
'aa' => 11,
'bb' => 'bb',
'cc' => true,
'dd' => false,
"http://支持備注的添加",
'ee' => "base_path() . DIRECTORY_SEPARATOR . 'public',"
'ff' => [
333,true,false,
'aaa' => 11,
'bbb' => 'bb',
'ccc' => true,
'ddd' => false,
'eee' => "support\bootstrap\Session::class,",
'fff' => [
"http://支持備注的添加,最后一級(jí)數(shù)組只能是一維數(shù)組",
'aaaa' => 11,
'bbbb' => 'bb',
'cccc' => true,
'dddd' => false,
'eeee' => "support\bootstrap\Session::class,",
],
],
'gg' => [
1,true,false,
"base_path() . DIRECTORY_SEPARATOR . 'public',"
'aa' => 11,
'bb' => 'bb',
'cc' => true,
'dd' => false,
'ee' => "support\bootstrap\Session::class,",
],
],
'g' => [
1,true,false,
'ga' => 11,
'gb' => 'bb',
'gc' => true,
'gd' => false,
'ge' => "support\bootstrap\Session::class,",
'gf' => [
1,true,false,
"base_path() . DIRECTORY_SEPARATOR . 'public',"
'gaa' => 11,
'gbb' => 'bb',
'gcc' => true,
'gdd' => false,
'gee' => "support\bootstrap\Session::class,",
],
],
];
$conf = new \Taoser\SetConfig\SetArr('app');
$conf->add($add);
編輯數(shù)組節(jié)點(diǎn)
數(shù)組編輯,
- 不能直接對(duì)非索引(下標(biāo)0,1數(shù)值型索引)元素?cái)?shù)組進(jìn)行操作,
- 索引數(shù)組的編輯需要先
delete
刪除(如true,111,"app()","//備注"),再add
添加value(如,false,12345,"support\bootstrap\Session::class,","備注2")。
$edit = [
'a' => 1,
'b' => 'b',
'c' => true,
'd' => false,
'e' => "support\bootstrap\Session::class,",
'f' => [
'aa' => 11,
'bb' => 'bb',
'cc' => true,
'dd' => false,
'ee' => "base_path() . DIRECTORY_SEPARATOR . 'public',"
'ff' => [
'aaa' => 11,
'bbb' => 'bb',
'ccc' => true,
'ddd' => false,
'eee' => "support\bootstrap\Session::class,",
'fff' => [
'aaaa' => 11,
'bbbb' => 'bb',
'cccc' => true,
'dddd' => false,
'eeee' => "support\bootstrap\Session::class,",
],
],
'gg' => [
1,true,false,
"base_path() . DIRECTORY_SEPARATOR . 'public',"
'aa' => 11,
'bb' => 'bb',
'cc' => true,
'dd' => false,
'ee' => "support\bootstrap\Session::class,",
],
],
'g' => [
'ga' => 11,
'gb' => 'bb',
'ge' => "support\bootstrap\Session::class,",
'gf' => [
'gaa' => 11,
'gbb' => 'bb',
'gcc' => true,
'gdd' => false,
'gee' => "support\bootstrap\Session::class,",
],
],
];
$conf = new \Taoser\SetConfig\SetArr('app');
$conf->edit($edit);
刪除數(shù)值
刪除元素規(guī)則
- “//備注”、bool、111、'abcd'等,給定value即會(huì)刪除;
- 關(guān)聯(lián)數(shù)組需要給定key和value,value值為空,0,false,null均會(huì)刪除。
- 刪除數(shù)據(jù)是一條一條刪除的,不能只給一個(gè)數(shù)組的key
- 一個(gè)數(shù)組刪除后會(huì)空會(huì)把空數(shù)組刪除
$del = [
true,false,111,'US-A',
"http:// 刪除備注",
"base_path() . DIRECTORY_SEPARATOR . 'public',",
'a' => 0,
'b' => 0,
'c' => 0,
'd' => 0,
'e' => 0,
'f' => [
'aa' => '',
'bb' => '',
'cc' => '',
'dd' => '',
'ee' => ""
'ff' => [
'aaa' => false,
'bbb' => false,
'ccc' => false,
'ddd' => false,
'eee' => false,
'fff' => [
'aaaa' => null,
'bbbb' => null,
'cccc' => null,
'dddd' => null,
'eeee' => null,
],
],
];
$conf = new \Taoser\SetConfig\SetArr('app');
$conf->delete($del);