|
| 1 | +<?php |
| 2 | +namespace LaunchDarkly; |
| 3 | + |
| 4 | +/** |
| 5 | + * This component allows you to use local files as a source of feature flag state. This would |
| 6 | + * typically be used in a test environment, to operate using a predetermined feature flag state |
| 7 | + * without an actual LaunchDarkly connection. |
| 8 | + * <p> |
| 9 | + * To use this component, create an instance of this class, passing the path(s) of your data |
| 10 | + * file(s). Then place the resulting object in your LaunchDarkly client configuration with the |
| 11 | + * key "feature_requester". |
| 12 | + * <pre> |
| 13 | + * $file_data = new FileDataFeatureRequester("./testData/flags.json"); |
| 14 | + * $config = array("feature_requester" => $file_data, "send_events" => false); |
| 15 | + * $client = new LDClient("sdk_key", $config); |
| 16 | + * </pre> |
| 17 | + * <p> |
| 18 | + * This will cause the client <i>not</i> to connect to LaunchDarkly to get feature flags. (Note |
| 19 | + * that in this example, <code>send_events</core> is also set to false so that it will not |
| 20 | + * connect to LaunchDarkly to send analytics events either.) |
| 21 | + * <p> |
| 22 | + */ |
| 23 | +class FileDataFeatureRequester implements FeatureRequester |
| 24 | +{ |
| 25 | + /** @var array */ |
| 26 | + private $_filePaths; |
| 27 | + /** @var array */ |
| 28 | + private $_flags; |
| 29 | + /** @var array */ |
| 30 | + private $_segments; |
| 31 | + |
| 32 | + public function __construct($filePaths) |
| 33 | + { |
| 34 | + $this->_filePaths = is_array($filePaths) ? $filePaths : array($filePaths); |
| 35 | + $this->_flags = array(); |
| 36 | + $this->_segments = array(); |
| 37 | + $this->readAllData(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Gets an individual feature flag |
| 42 | + * |
| 43 | + * @param $key string feature key |
| 44 | + * @return FeatureFlag|null The decoded FeatureFlag, or null if missing |
| 45 | + */ |
| 46 | + public function getFeature($key) |
| 47 | + { |
| 48 | + return isset($this->_flags[$key]) ? $this->_flags[$key] : null; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Gets an individual user segment |
| 53 | + * |
| 54 | + * @param $key string segment key |
| 55 | + * @return Segment|null The decoded Segment, or null if missing |
| 56 | + */ |
| 57 | + public function getSegment($key) |
| 58 | + { |
| 59 | + return isset($this->_segments[$key]) ? $this->_segments[$key] : null; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets all feature flags |
| 64 | + * |
| 65 | + * @return array()|null The decoded FeatureFlags, or null if missing |
| 66 | + */ |
| 67 | + public function getAllFeatures() |
| 68 | + { |
| 69 | + return $this->_flags; |
| 70 | + } |
| 71 | + |
| 72 | + private function readAllData() |
| 73 | + { |
| 74 | + $flags = array(); |
| 75 | + $segments = array(); |
| 76 | + foreach ($this->_filePaths as $filePath) { |
| 77 | + $this->loadFile($filePath, $flags, $segments); |
| 78 | + } |
| 79 | + $this->_flags = $flags; |
| 80 | + $this->_segments = $segments; |
| 81 | + } |
| 82 | + |
| 83 | + private function loadFile($filePath, &$flags, &$segments) |
| 84 | + { |
| 85 | + $content = file_get_contents($filePath); |
| 86 | + $data = json_decode($content, true); |
| 87 | + if ($data == null) { |
| 88 | + throw new \InvalidArgumentException("File is not valid JSON: " + $filePath); |
| 89 | + } |
| 90 | + if (isset($data['flags'])) { |
| 91 | + foreach ($data['flags'] as $key => $value) { |
| 92 | + $flag = FeatureFlag::decode($value); |
| 93 | + $this->tryToAdd($flags, $key, $flag, "feature flag"); |
| 94 | + } |
| 95 | + } |
| 96 | + if (isset($data['flagValues'])) { |
| 97 | + foreach ($data['flagValues'] as $key => $value) { |
| 98 | + $flag = FeatureFlag::decode(array( |
| 99 | + "key" => $key, |
| 100 | + "version" => 1, |
| 101 | + "on" => false, |
| 102 | + "prerequisites" => array(), |
| 103 | + "salt" => "", |
| 104 | + "targets" => array(), |
| 105 | + "rules" => array(), |
| 106 | + "fallthrough" => array(), |
| 107 | + "offVariation" => 0, |
| 108 | + "variations" => array($value), |
| 109 | + "deleted" => false, |
| 110 | + "trackEvents" => false, |
| 111 | + "clientSide" => false |
| 112 | + )); |
| 113 | + $this->tryToAdd($flags, $key, $flag, "feature flag"); |
| 114 | + } |
| 115 | + } |
| 116 | + if (isset($data['segments'])) { |
| 117 | + foreach ($data['segments'] as $key => $value) { |
| 118 | + $segment = Segment::decode($value); |
| 119 | + $this->tryToAdd($segments, $key, $segment, "user segment"); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private function tryToAdd(&$array, $key, $item, $kind) |
| 125 | + { |
| 126 | + if (isset($array[$key])) { |
| 127 | + throw new \InvalidArgumentException("File data contains more than one " . $kind . " with key: " . $key); |
| 128 | + } else { |
| 129 | + $array[$key] = $item; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments