云眼配置
About 3 min
云眼配置
本主题介绍如何使用 PHP SDK 的云眼配置访问数据文件中的项目配置数据。
概述
全栈 SDK 打开一组定义明确的公共 API,隐藏所有实现详细信息。但是,某些客户端可能需要访问数据文件中的项目配置数据。
在本文档中,我们扩展了公共 API 以定义数据模型和访问方法,客户端可以使用这些模型和方法访问项目配置数据。
EyeofcloudConfig API
公共配置数据模型(EyeofcloudConfig)在下面定义为静态Eyeofcloud项目数据的结构化格式。
获取云眼配置
EyeofcloudConfig可以通过以下公共API调用从EyeofcloudClient(顶级)访问:
PHP
$config = $eyeofcloudClient->getEyeofcloudConfig();
getEyeofcloudConfig
返回一个EyeofcloudConfig
实例,其中包括:
- 环境键
- 开发工具包密钥
- 数据文件修订号
- 按其键值映射的所有实验
- 所有属性
- 所有受众
- 所有活动
- 按其键值映射的灰度发布(特性标帜)
- 检索项目配置(数据文件)的函数
📘 注意
当 SDK 数据文件更新时(客户端可以为
EYEOFCLOUD_CONFIG_UPDATE
添加通知侦听器以获取通知),客户端应调用该方法以获取更新的 EyeofcloudConfig 数据。请参阅以下示例。
获取数据文件
若要确保多个 SDK 实例都从同一配置实例化(例如,在客户端/服务器方案中),可以在实例之间传递配置(数据文件)的 JSON 字符串表示形式。若要获取数据文件,请使用EyeofcloudConfig
对象的getDatafile
方法。有关更多信息,请参阅与多个 SDK 实现共享数据文件。
对象模型
下面显示了 EyeofcloudConfig 的对象模型。
PHP - 对象模型
/**
* EyeofcloudConfig is an object describing the current project configuration data being used by this SDK instance.
* @typedef {string} environmentKey
* @typedef {string} sdkKey
* @typedef {string} revision
* This experimentsMap is for experiments of legacy projects only.
* For flag projects, experiment keys are not guaranteed to be unique
* across multiple flags, so this map may not include all experiments
* when keys conflict.
* @property {Object.<string, EyeofcloudExperiment>}} experimentsMap
* @property {Object.<string, EyeofcloudFeature>}} featuresMap
* @property [Object.<string, EyeofcloudAttribute>}] attributes
* @property [Object.<string, EyeofcloudAudience>}] audiences
* @property [Object.<string, EyeofcloudEvent>}] events
*/
/**
* EyeofcloudFeature is an object describing a feature flag
* @typedef {Object} EyeofcloudFeature
* @property {string} id
* @property {string} key
* @property [Object.<string, EyeofcloudExperiment>}] experimentRules
* @property [Object.<string, EyeofcloudExperiment>}] deliveryRules
* Use experimentRules and deliverRules
* @property {Object.<string, EyeofcloudExperiment>}} experimentsMap
* @property {Object.<string, EyeofcloudVariable>}} variablesMap
*/
/**
* EyeofcloudExperiment is an object describing an experiment
* @typedef {Object} EyeofcloudExperiment
* @property {string} id
* @property {string} key
* @property {string} audiences
* @property {Object.<string, EyeofcloudVariation>}} variationMap
*/
/**
* EyeofcloudVariation is an object describing a variation in an experiment
* @typedef {Object} EyeofcloudVariation
* @property {string} id
* @property {string} key
* @property {boolean=} featureEnabled
* @property {Object.<string, EyeofcloudVariable>}} variablesMap
*/
/**
* EyeofcloudVariable is an object describing a feature flag variable
* @typedef {Object} EyeofcloudVariable
* @property {string} id
* @property {string} key
* @property {string} type
* @property {string} value
*/
/**
* EyeofcloudAttribute is an object describing an attribute
* @typedef {Object} EyeofcloudAttribute
* @property {string} id
* @property {string} key
*/
/**
* EyeofcloudAudience is an object describing an audience
* @typedef {Object} EyeofcloudAudience
* @property {string} id
* @property {string} name
* @property {string} conditions
*/
/**
* EyeofcloudEvent is an object describing an event
* @typedef {Object} EyeofcloudEvent
* @property {string} id
* @property {string} key
* @property {array} experimentIds
*/
Examples
可以从 EyeofcloudClient(顶级)访问 EyeofcloudConfig,如下所示:
PHP
$config = $eyeofcloudClient->getEyeofcloudConfig();
// get the revision
echo "[EyeofcloudConfig] revision:" . $config->getRevision();
// get the SDK key
echo "[EyeofcloudConfig] SDKKey:" . $config->getSdkKey();
// get the environment key
echo "[EyeofcloudConfig] environmentKey:" . $config->getEnvironmentKey();
// all attributes
echo "[EyeofcloudConfig] attributes:";
$attributes = $config->getAttributes();
foreach($attributes as $attribute)
{
echo "[EyeofcloudAttribute] -- (id, key) = ((" . $attribute->getId(). "), (". $attribute->getKey() . "))";
}
// all audiences
echo "[EyeofcloudConfig] audiences:";
$audiences = $config->getAudiences();
foreach($audiences as $audience)
{
echo "[EyeofcloudAudience] -- (id, key, conditions) = ((" . $audience->getId(). "), (". $audience->getName() . "), (". $audience->getConditions() . "))";
}
// all events
echo "[EyeofcloudConfig] events:";
$events = $config->getEvents();
foreach($events as $event) {
echo "[EyeofcloudEvent] -- (id, key, experimentIds) = ((" . $event->getId(). "), (". $event->getKey() . "), (". $event->getExperimentIds() . "))";
}
// all flags
$flags = array_values((array)$config->getFeaturesMap());
foreach ($flags as $flag)
{
// Use experimentRules and deliverRules
$experimentRules = $flag->getExperimentRules();
foreach ($experimentRules as $experimentRule)
{
echo "[EyeofcloudExperiment] - experiment rule-key = " . $experimentRule->getKey();
echo "[EyeofcloudExperiment] - experiment audiences = " . $experimentRule->getExperimentAudiences();
// all variations
$variations = array_values((array)$experimentRule->getVariationsMap());
foreach ($variations as $variation)
{
echo "[EyeofcloudVariation] -- variation = { key: " . $variation->getKey() . ", id: " . $variation->getId() . ", featureEnabled: " . $variation->getFeatureEnabled() . " }";
$variables = $variation->getVariablesMap();
foreach ($variables as $variable)
{
echo "[EyeofcloudVariable] --- variable: " . $variable->getKey() . ", " . $variable->getId();
// use variable data here.
}
// use experimentRule data here.
}
}
$deliveryRules = $flag->getDeliveryRules();
foreach ($deliveryRules as $deliveryRule)
{
echo "[EyeofcloudExperiment] - delivery rule-key = " . $deliveryRule->getKey();
echo "[EyeofcloudExperiment] - delivery audiences = " . $deliveryRule->getExperimentAudiences();
// use delivery rule data here...
}
}
$eyeofcloudClient->notificationCenter->addNotificationListener(
NotificationType::EYEOFCLOUD_CONFIG_UPDATE,
function () {
$newConfig = $eyeofcloudClient->getEyeofcloudConfig();
echo "[EyeofcloudConfig] revision = " . $newConfig ? $newConfig->getRevision() : "";
}
);