-
Notifications
You must be signed in to change notification settings - Fork 0
Config v2 branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
005fce8
0b98993
cb17173
08c9280
b6aa75d
d018352
4ca2344
196440f
d547071
14d1380
c8625e6
61d5864
e142a24
5cfdf6c
022e175
421fc2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2019-2021, Optimizely | ||
* | ||
|
@@ -93,6 +94,18 @@ class DatafileProjectConfig implements ProjectConfigInterface | |
*/ | ||
private $datafile; | ||
|
||
/** | ||
* @var string environmentKey of the config. | ||
*/ | ||
private $environmentKey; | ||
|
||
/** | ||
* @var string sdkKey of the config. | ||
*/ | ||
private $sdkKey; | ||
|
||
|
||
|
||
/** | ||
* @var string Revision of the datafile. | ||
*/ | ||
|
@@ -132,7 +145,7 @@ class DatafileProjectConfig implements ProjectConfigInterface | |
* @var array Associative array of experiment id to associative array of variation key to variations. | ||
*/ | ||
private $_variationKeyMapByExperimentId; | ||
|
||
/** | ||
* @var array Associative array of event key to Event(s) in the datafile. | ||
*/ | ||
|
@@ -172,6 +185,34 @@ class DatafileProjectConfig implements ProjectConfigInterface | |
*/ | ||
private $_rollouts; | ||
|
||
/** | ||
* list of Attributes that will be parsed from the datafile | ||
* | ||
* @var [Attribute] | ||
*/ | ||
private $attributes; | ||
|
||
/** | ||
* list of Audiences that will be parsed from the datafile | ||
* | ||
* @var [Audience] | ||
*/ | ||
private $audiences; | ||
|
||
/** | ||
* list of Events that will be parsed from the datafile | ||
* | ||
* @var [Event] | ||
*/ | ||
private $events; | ||
|
||
/** | ||
* list of Typed Audiences that will be parsed from the datafile | ||
* | ||
* @var [typed_audience] | ||
*/ | ||
private $typedAudiences; | ||
|
||
/** | ||
* internal mapping of feature keys to feature flag models. | ||
* | ||
|
@@ -210,10 +251,11 @@ class DatafileProjectConfig implements ProjectConfigInterface | |
/** | ||
* DatafileProjectConfig constructor to load and set project configuration data. | ||
* | ||
* @param $datafile string JSON string representing the project. | ||
* @param $logger LoggerInterface | ||
* @param $datafile string JSON string representing the project. | ||
* @param $logger LoggerInterface | ||
* @param $errorHandler ErrorHandlerInterface | ||
*/ | ||
|
||
public function __construct($datafile, $logger, $errorHandler) | ||
{ | ||
$supportedVersions = array(self::V2, self::V3, self::V4); | ||
|
@@ -222,6 +264,8 @@ public function __construct($datafile, $logger, $errorHandler) | |
$this->_logger = $logger; | ||
$this->_errorHandler = $errorHandler; | ||
$this->_version = $config['version']; | ||
$this->environmentKey = isset($config['environmentKey']) ? $config['environmentKey'] : ''; | ||
$this->sdkKey = isset($config['sdkKey']) ? $config['sdkKey'] : ''; | ||
if (!in_array($this->_version, $supportedVersions)) { | ||
throw new InvalidDatafileVersionException( | ||
"This version of the PHP SDK does not support the given datafile version: {$this->_version}." | ||
|
@@ -230,19 +274,19 @@ public function __construct($datafile, $logger, $errorHandler) | |
|
||
$this->_accountId = $config['accountId']; | ||
$this->_projectId = $config['projectId']; | ||
$this->_anonymizeIP = isset($config['anonymizeIP'])? $config['anonymizeIP'] : false; | ||
$this->_botFiltering = isset($config['botFiltering'])? $config['botFiltering'] : null; | ||
$this->attributes = isset($config['attributes']) ? $config['attributes'] : []; | ||
$this->audiences = isset($config['audiences']) ? $config['audiences']: []; | ||
$this->events = $config['events'] ?: []; | ||
$this->typedAudiences = isset($config['typedAudiences']) ? $config['typedAudiences'] : []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this can't be $config['typedAudiences'] ?: [] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i just followed the existing way to get typedAudiences ealier it was being used as a variable but i made it a class attribute. |
||
$this->_anonymizeIP = isset($config['anonymizeIP']) ? $config['anonymizeIP'] : false; | ||
$this->_botFiltering = isset($config['botFiltering']) ? $config['botFiltering'] : null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the default value, why specifying explicit null. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default value for botFiltering was not mentioned anywhere i did not change this line it is as it was. |
||
$this->_revision = $config['revision']; | ||
$this->_sendFlagDecisions = isset($config['sendFlagDecisions']) ? $config['sendFlagDecisions'] : false; | ||
|
||
$groups = $config['groups'] ?: []; | ||
$experiments = $config['experiments'] ?: []; | ||
$events = $config['events'] ?: []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason to remove it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they were being used as variables but i need to get them as attributes so delete the variables and using attributes in further functions |
||
$attributes = $config['attributes'] ?: []; | ||
$audiences = $config['audiences'] ?: []; | ||
$typedAudiences = isset($config['typedAudiences']) ? $config['typedAudiences']: []; | ||
$rollouts = isset($config['rollouts']) ? $config['rollouts'] : []; | ||
$featureFlags = isset($config['featureFlags']) ? $config['featureFlags']: []; | ||
$rollouts = isset($config['rollouts']) ? $config['rollouts']: []; | ||
$featureFlags = isset($config['featureFlags']) ? $config['featureFlags'] : []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid nit changes in parts of code which are not changed. Change it back to what it was. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually i started my work before the pr of duplicate exp key issue was merged with master so i just update my branch with the latest master branch to use that instead of doing that task again |
||
|
||
// JSON type is represented in datafile as a subtype of string for the sake of backwards compatibility. | ||
// Converting it to a first-class json type while creating Project Config | ||
|
@@ -258,10 +302,10 @@ public function __construct($datafile, $logger, $errorHandler) | |
|
||
$this->_groupIdMap = ConfigParser::generateMap($groups, 'id', Group::class); | ||
$this->_experimentIdMap = ConfigParser::generateMap($experiments, 'id', Experiment::class); | ||
$this->_eventKeyMap = ConfigParser::generateMap($events, 'key', Event::class); | ||
$this->_attributeKeyMap = ConfigParser::generateMap($attributes, 'key', Attribute::class); | ||
$typedAudienceIdMap = ConfigParser::generateMap($typedAudiences, 'id', Audience::class); | ||
$this->_audienceIdMap = ConfigParser::generateMap($audiences, 'id', Audience::class); | ||
$this->_eventKeyMap = ConfigParser::generateMap($this->events, 'key', Event::class); | ||
$this->_attributeKeyMap = ConfigParser::generateMap($this->attributes, 'key', Attribute::class); | ||
$typedAudienceIdMap = ConfigParser::generateMap($this->typedAudiences, 'id', Audience::class); | ||
$this->_audienceIdMap = ConfigParser::generateMap($this->audiences, 'id', Audience::class); | ||
$this->_rollouts = ConfigParser::generateMap($rollouts, null, Rollout::class); | ||
$this->_featureFlags = ConfigParser::generateMap($featureFlags, null, FeatureFlag::class); | ||
|
||
|
@@ -367,10 +411,10 @@ public function __construct($datafile, $logger, $errorHandler) | |
/** | ||
* Create ProjectConfig based on datafile string. | ||
* | ||
* @param string $datafile JSON string representing the Optimizely project. | ||
* @param bool $skipJsonValidation boolean representing whether JSON schema validation needs to be performed. | ||
* @param LoggerInterface $logger Logger instance | ||
* @param ErrorHandlerInterface $errorHandler ErrorHandler instance. | ||
* @param string $datafile JSON string representing the Optimizely project. | ||
* @param bool $skipJsonValidation boolean representing whether JSON schema validation needs to be performed. | ||
* @param LoggerInterface $logger Logger instance | ||
* @param ErrorHandlerInterface $errorHandler ErrorHandler instance. | ||
* @return ProjectConfig ProjectConfig instance or null; | ||
*/ | ||
public static function createProjectConfigFromDatafile($datafile, $skipJsonValidation, $logger, $errorHandler) | ||
|
@@ -449,6 +493,22 @@ public function getRevision() | |
return $this->_revision; | ||
} | ||
|
||
/** | ||
* @return string Config environmentKey. | ||
*/ | ||
public function getEnvironmentKey() | ||
{ | ||
return $this->environmentKey; | ||
} | ||
|
||
/** | ||
* @return string Config sdkKey. | ||
*/ | ||
public function getSdkKey() | ||
{ | ||
return $this->sdkKey; | ||
} | ||
|
||
/** | ||
* @return array List of feature flags parsed from the datafile | ||
*/ | ||
|
@@ -457,6 +517,38 @@ public function getFeatureFlags() | |
return $this->_featureFlags; | ||
} | ||
|
||
/** | ||
* @return array List of attributes parsed from the datafile | ||
*/ | ||
public function getAttributes() | ||
{ | ||
return $this->attributes; | ||
} | ||
|
||
/** | ||
* @return array List of audiences parsed from the datafile | ||
*/ | ||
public function getAudiences() | ||
{ | ||
return $this->audiences; | ||
} | ||
|
||
/** | ||
* @return array List of events parsed from the datafile | ||
*/ | ||
public function getEvents() | ||
{ | ||
return $this->events; | ||
} | ||
|
||
/** | ||
* @return array List of typed audiences parsed from the datafile | ||
*/ | ||
public function getTypedAudiences() | ||
{ | ||
return $this->typedAudiences; | ||
} | ||
|
||
/** | ||
* @return array List of all experiments (including group experiments) | ||
* parsed from the datafile | ||
|
@@ -470,9 +562,12 @@ public function getAllExperiments() | |
$rolloutExperimentIds[] = $experiment->getId(); | ||
} | ||
} | ||
return array_filter(array_values($this->_experimentKeyMap), function ($experiment) use ($rolloutExperimentIds) { | ||
return !in_array($experiment->getId(), $rolloutExperimentIds); | ||
}); | ||
return array_filter( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't change just for the sake of change. please. |
||
array_values($this->_experimentIdMap), | ||
function ($experiment) use ($rolloutExperimentIds) { | ||
return !in_array($experiment->getId(), $rolloutExperimentIds); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -634,7 +729,8 @@ public function getAttribute($attributeKey) | |
*/ | ||
public function getVariationFromKey($experimentKey, $variationKey) | ||
{ | ||
if (isset($this->_variationKeyMap[$experimentKey]) | ||
if ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change all the extra nit changes you have done back to as it was. make another PR if you want to fix these nits. |
||
isset($this->_variationKeyMap[$experimentKey]) | ||
&& isset($this->_variationKeyMap[$experimentKey][$variationKey]) | ||
) { | ||
return $this->_variationKeyMap[$experimentKey][$variationKey]; | ||
|
@@ -654,14 +750,15 @@ public function getVariationFromKey($experimentKey, $variationKey) | |
|
||
/** | ||
* @param $experimentKey string Key for experiment. | ||
* @param $variationId string ID for variation. | ||
* @param $variationId string ID for variation. | ||
* | ||
* @return Variation Entity corresponding to the provided experiment key and variation ID. | ||
* Dummy entity is returned if key or ID is invalid. | ||
*/ | ||
public function getVariationFromId($experimentKey, $variationId) | ||
{ | ||
if (isset($this->_variationIdMap[$experimentKey]) | ||
if ( | ||
isset($this->_variationIdMap[$experimentKey]) | ||
&& isset($this->_variationIdMap[$experimentKey][$variationId]) | ||
) { | ||
return $this->_variationIdMap[$experimentKey][$variationId]; | ||
|
@@ -688,7 +785,8 @@ public function getVariationFromId($experimentKey, $variationId) | |
*/ | ||
public function getVariationFromIdByExperimentId($experimentId, $variationId) | ||
{ | ||
if (isset($this->_variationIdMapByExperimentId[$experimentId]) | ||
if ( | ||
isset($this->_variationIdMapByExperimentId[$experimentId]) | ||
&& isset($this->_variationIdMapByExperimentId[$experimentId][$variationId]) | ||
) { | ||
return $this->_variationIdMapByExperimentId[$experimentId][$variationId]; | ||
|
@@ -715,7 +813,8 @@ public function getVariationFromIdByExperimentId($experimentId, $variationId) | |
*/ | ||
public function getVariationFromKeyByExperimentId($experimentId, $variationKey) | ||
{ | ||
if (isset($this->_variationKeyMapByExperimentId[$experimentId]) | ||
if ( | ||
isset($this->_variationKeyMapByExperimentId[$experimentId]) | ||
&& isset($this->_variationKeyMapByExperimentId[$experimentId][$variationKey]) | ||
) { | ||
return $this->_variationKeyMapByExperimentId[$experimentId][$variationKey]; | ||
|
@@ -748,7 +847,8 @@ public function getFeatureVariableFromKey($featureFlagKey, $variableKey) | |
return null; | ||
} | ||
|
||
if (isset($this->_featureFlagVariableMap[$featureFlagKey]) | ||
if ( | ||
isset($this->_featureFlagVariableMap[$featureFlagKey]) | ||
&& isset($this->_featureFlagVariableMap[$featureFlagKey][$variableKey]) | ||
) { | ||
return $this->_featureFlagVariableMap[$featureFlagKey][$variableKey]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,37 @@ public function getBotFiltering(); | |
*/ | ||
public function getRevision(); | ||
|
||
/** | ||
* @return string String represnting environment key of the datafile. | ||
*/ | ||
public function getEnvironmentKey(); | ||
|
||
/** | ||
* @return string String representing sdkkey of the datafile. | ||
*/ | ||
public function getSdkKey(); | ||
|
||
/** | ||
* @return array List of attributes parsed from the datafile | ||
*/ | ||
public function getAttributes(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. following are not a part of ProjectConfigInterface There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. attributes, audiences, events, typedAudiences. |
||
|
||
/** | ||
* @return array List of audiences parsed from the datafile | ||
*/ | ||
public function getAudiences(); | ||
|
||
/** | ||
* @return array List of events parsed from the datafile | ||
*/ | ||
public function getEvents(); | ||
|
||
/** | ||
* @return array List of typed audiences parsed from the datafile | ||
*/ | ||
public function getTypedAudiences(); | ||
|
||
|
||
/** | ||
* @return array List of feature flags parsed from the datafile | ||
*/ | ||
|
@@ -125,7 +156,7 @@ public function getAttribute($attributeKey); | |
|
||
/** | ||
* @param $experimentKey string Key for experiment. | ||
* @param $variationKey string Key for variation. | ||
* @param $variationKey string Key for variation. | ||
* | ||
* @return Variation Entity corresponding to the provided experiment key and variation key. | ||
* Dummy entity is returned if key or ID is invalid. | ||
|
@@ -134,7 +165,7 @@ public function getVariationFromKey($experimentKey, $variationKey); | |
|
||
/** | ||
* @param $experimentKey string Key for experiment. | ||
* @param $variationId string ID for variation. | ||
* @param $variationId string ID for variation. | ||
* | ||
* @return Variation Entity corresponding to the provided experiment key and variation ID. | ||
* Dummy entity is returned if key or ID is invalid. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update header of this file and all others. 2021