ceive/settings

该包最新版本(dev-master)没有可用的许可证信息。

访问设置的抽象层

dev-master 2018-07-10 11:15 UTC

This package is not auto-updated.

Last update: 2024-09-25 13:40:16 UTC


README

该包提供对设置的抽象访问,允许您在应用程序中依赖它来获取设置,旨在能够适应任何PHP工作环境栈。

Ceive\settings\Frontend 前端提供设置缓存的能力,并要求实现通过后端访问设置。 $settings->state 数组(\ArrayAccess):确保从后端懒加载设置(即已加载的内容不再加载) $settings->settings 数组(\ArrayAccess):存储从后端加载的设置的属性,可以将其用作缓存,通过实现 \ArrayAccess 接口来实现 $settings->backend:如果存在前端,则存在后端。后端提供设置访问的层实现,无论它们在哪里。

$schema = new settings\Schema();

$schema->section('Общее', 'general', [
	'siteName' => [
		'type'      => 'string',
		'default'   => 'Example site',
	],
], true);
$schema->section('Настройки смс ведомлений', 'sms', [
	'notification.update' => [
		'type'      => 'string',
		'default'   => 'Example site',
	],
]);

$a = [
	'notification.update' => true
];

$settings = new settings\Frontend();
$settings->schema = $schema;
$settings->backend = new settings\backend\Customizable(
	function($key, $value) use(&$a){
		$a[$key] = $value;
	},//set
	function($key) use(&$a){
		return isset($a[$key])?$a[$key]:null;
	},//get
	function($key) use(&$a){
		return isset($a[$key]);
	},//has
	function($key) use(&$a){
		unset($a[$key]);
	},//delete
	function($limit=null, $offset=null, $query=null) use(&$a){
		return $a;
	}//list
);

$settings['notification.update'] = false; // set to settings

$array = $settings->toArray();

echo $settings['notification.update']; // use settings

智能默认值(Yii集成示例)

$schema = new settings\Schema();
$schema->setting('mySettingKey', [
    'type' => 'string',
    'default' => function($key, settings\SettingsAbstract $settings){
        return Yii::$app->settings->has($key)
            ?Yii::$app->settings->get($key)
            :Yii::$app->params['config'][$key]
    }
]);

通过某些设置传输选项

$schema->setting('mySettingKey', [
    //...

    'myImportantOption' => 'value'

    //...
]);


////////////////////////////////////////////////////////////////////////
///// OTHER CODE


$setting = $schema->getSetting('mySettingKey');
$importantValue = $setting->myImportantOption;

传输单个选项的示例

$schema->setting('mySettingKey', [
    //...
    'type' => 'string'
    'autocomplete' => [
        'val1' => 'String1',
        'val1' => 'String2',
        'val1' => 'String3',
        'val1' => 'String4',
    ]

    //...
]);


////////////////////////////////////////////////////////////////////////
///// OTHER CODE


$setting = $schema->getSetting('mySettingKey');
if( $autocomplete = $setting->autocomplete ){
    ?> <select>
    <? foreach($autocomplete as $value=>$title){ ?><option value="<?= $value?>"><?= $title?></option><? }?>

    </select> <?
}

架构设置映射

$schema->map(, function($key, Setting $setting, $sectionKey, Section $section, Schema $schema){
    // onSetting
},function($doSettings, $sectionKey, Section $section, $schema){
    // onSection (optionaly: default null)
    // call $doSettings() here
}, function($key, Setting $setting, $sectionKey, Section $section, Schema $schema){
    // filter (optionaly: default null)
});

架构设置映射的示例

$schema->map(function($key, Setting $setting, $sectionKey, Section $section, Schema $schema){
    // onSetting
}, function($doSettings, $sectionKey, Section $section, Schema $schema){
    // onSection (abort if returned false)
    ?>
    <section>
        <div>
            <h1><?= $section->title?></h1>
        </div>
        <div><? $doSettings() ?></div>
    </section>
    <?
}, function($key, Setting $setting, $sectionKey, Section $section, Schema $schema){
    // filter optionaly
});

架构设置映射的示例,并尝试通过设置传输选项

$schema->map(function($key, Setting $setting, $sectionKey, Section $section, Schema $schema) use($settings){
    // onSetting
    if($setting->autocomplete){
        ?> <select name="<?=$key?>">
            <? foreach($autocomplete as $value=>$title){ ?><option value="<?= $value?>"><?= $title?></option><? }?>
        </select> <?
    }else{
        ?> <input type="<?=$setting->type?>" name="<?=$key?>" value="<?= $settings[$key]?>"/> <?
    }
}, null, null);

或使用映射对象


$mapper = new \stdClass();

//$mapper->onSection = null;
$mapper->onSetting = function($key, Setting $setting, $sectionKey, Section $section, Schema $schema) use($settings){
    // onSetting
    if($setting->autocomplete){
        ?> <select name="<?=$key?>">
            <? foreach($autocomplete as $value=>$title){ ?><option value="<?= $value?>"><?= $title?></option><? }?>
        </select> <?
    }else{
        ?> <input type="<?=$setting->type?>" name="<?=$key?>" value="<?= $settings[$key]?>"/> <?
    }
};
$mapper->filter = function(){};

$schema->mapper($mapper);