|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LaunchDarkly\Impl\Evaluation; |
| 4 | + |
| 5 | +use LaunchDarkly\EvaluationReason; |
| 6 | +use LaunchDarkly\FeatureRequester; |
| 7 | +use LaunchDarkly\Impl\Model\Clause; |
| 8 | +use LaunchDarkly\Impl\Model\FeatureFlag; |
| 9 | +use LaunchDarkly\Impl\Model\Rule; |
| 10 | +use LaunchDarkly\Impl\Model\Segment; |
| 11 | +use LaunchDarkly\Impl\Model\SegmentRule; |
| 12 | +use LaunchDarkly\LDContext; |
| 13 | + |
| 14 | +/** |
| 15 | + * Encapsulates the feature flag evaluation logic. The Evaluator has no direct access to the |
| 16 | + * rest of the SDK environment; if it needs to retrieve flags or segments that are referenced |
| 17 | + * by a flag, it does so through a FeatureRequester that is provided in the constructor. It also |
| 18 | + * produces evaluation events as appropriate for any referenced prerequisite flags. |
| 19 | + * @ignore |
| 20 | + * @internal |
| 21 | + */ |
| 22 | +class Evaluator |
| 23 | +{ |
| 24 | + private FeatureRequester $_featureRequester; |
| 25 | + |
| 26 | + public function __construct(FeatureRequester $featureRequester) |
| 27 | + { |
| 28 | + $this->_featureRequester = $featureRequester; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * The client's entry point for evaluating a flag. No other Evaluator methods should be exposed. |
| 33 | + * |
| 34 | + * @param FeatureFlag $flag an existing feature flag; any other referenced flags or segments will be |
| 35 | + * queried via the FeatureRequester |
| 36 | + * @param LDContext $context the evaluation context |
| 37 | + * @param ?callable $prereqEvalSink a function that may be called with a |
| 38 | + * PrerequisiteEvaluationRecord parameter for any prerequisite flags that are evaluated as a side |
| 39 | + * effect of evaluating this flag |
| 40 | + * @return EvalResult the outputs of evaluation |
| 41 | + */ |
| 42 | + public function evaluate(FeatureFlag $flag, LDContext $context, ?callable $prereqEvalSink): EvalResult |
| 43 | + { |
| 44 | + return $this->evaluateInternal($flag, $context, $prereqEvalSink); |
| 45 | + } |
| 46 | + |
| 47 | + private function evaluateInternal( |
| 48 | + FeatureFlag $flag, |
| 49 | + LDContext $context, |
| 50 | + ?callable $prereqEvalSink |
| 51 | + ): EvalResult { |
| 52 | + if (!$flag->isOn()) { |
| 53 | + return EvaluatorHelpers::getOffResult($flag, EvaluationReason::off()); |
| 54 | + } |
| 55 | + |
| 56 | + $prereqFailureReason = $this->checkPrerequisites($flag, $context, $prereqEvalSink); |
| 57 | + if ($prereqFailureReason !== null) { |
| 58 | + return EvaluatorHelpers::getOffResult($flag, $prereqFailureReason); |
| 59 | + } |
| 60 | + |
| 61 | + // Check to see if targets match |
| 62 | + foreach ($flag->getTargets() as $target) { |
| 63 | + foreach ($target->getValues() as $value) { |
| 64 | + if ($value === $context->getKey()) { |
| 65 | + $detail = EvaluatorHelpers::evaluationDetailForVariation( |
| 66 | + $flag, |
| 67 | + $target->getVariation(), |
| 68 | + EvaluationReason::targetMatch() |
| 69 | + ); |
| 70 | + return new EvalResult($detail, false); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Now walk through the rules and see if any match |
| 76 | + foreach ($flag->getRules() as $i => $rule) { |
| 77 | + if ($this->ruleMatchesContext($rule, $context)) { |
| 78 | + return EvaluatorHelpers::getResultForVariationOrRollout( |
| 79 | + $flag, |
| 80 | + $rule, |
| 81 | + $rule->isTrackEvents(), |
| 82 | + $context, |
| 83 | + EvaluationReason::ruleMatch($i, $rule->getId()) |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + return EvaluatorHelpers::getResultForVariationOrRollout( |
| 88 | + $flag, |
| 89 | + $flag->getFallthrough(), |
| 90 | + $flag->isTrackEventsFallthrough(), |
| 91 | + $context, |
| 92 | + EvaluationReason::fallthrough() |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + private function checkPrerequisites( |
| 97 | + FeatureFlag $flag, |
| 98 | + LDContext $context, |
| 99 | + ?callable $prereqEvalSink |
| 100 | + ): ?EvaluationReason { |
| 101 | + foreach ($flag->getPrerequisites() as $prereq) { |
| 102 | + $prereqOk = true; |
| 103 | + try { |
| 104 | + $prereqFeatureFlag = $this->_featureRequester->getFeature($prereq->getKey()); |
| 105 | + if ($prereqFeatureFlag == null) { |
| 106 | + $prereqOk = false; |
| 107 | + } else { |
| 108 | + $prereqEvalResult = $this->evaluateInternal($prereqFeatureFlag, $context, $prereqEvalSink); |
| 109 | + $variation = $prereq->getVariation(); |
| 110 | + if (!$prereqFeatureFlag->isOn() || $prereqEvalResult->getDetail()->getVariationIndex() !== $variation) { |
| 111 | + $prereqOk = false; |
| 112 | + } |
| 113 | + if ($prereqEvalSink !== null) { |
| 114 | + $prereqEvalSink(new PrerequisiteEvaluationRecord($prereqFeatureFlag, $flag, $prereqEvalResult)); |
| 115 | + } |
| 116 | + } |
| 117 | + } catch (\Exception $e) { |
| 118 | + $prereqOk = false; |
| 119 | + } |
| 120 | + if (!$prereqOk) { |
| 121 | + return EvaluationReason::prerequisiteFailed($prereq->getKey()); |
| 122 | + } |
| 123 | + } |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + private function ruleMatchesContext(Rule $rule, LDContext $context): bool |
| 128 | + { |
| 129 | + foreach ($rule->getClauses() as $clause) { |
| 130 | + if (!$this->clauseMatchesContext($clause, $context)) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + private function clauseMatchesContext(Clause $clause, LDContext $context): bool |
| 138 | + { |
| 139 | + if ($clause->getOp() === 'segmentMatch') { |
| 140 | + foreach ($clause->getValues() as $value) { |
| 141 | + $segment = $this->_featureRequester->getSegment($value); |
| 142 | + if ($segment) { |
| 143 | + if ($this->segmentMatchesContext($segment, $context)) { |
| 144 | + return EvaluatorHelpers::maybeNegate($clause, true); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + return EvaluatorHelpers::maybeNegate($clause, false); |
| 149 | + } |
| 150 | + return EvaluatorHelpers::matchClauseWithoutSegments($clause, $context); |
| 151 | + } |
| 152 | + |
| 153 | + private function segmentMatchesContext(Segment $segment, LDContext $context): bool |
| 154 | + { |
| 155 | + $key = $context->getKey(); |
| 156 | + if (!$key) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + if (in_array($key, $segment->getIncluded(), true)) { |
| 160 | + return true; |
| 161 | + } |
| 162 | + if (in_array($key, $segment->getExcluded(), true)) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + foreach ($segment->getRules() as $rule) { |
| 166 | + if ($this->segmentRuleMatchesContext($rule, $context, $segment->getKey(), $segment->getSalt())) { |
| 167 | + return true; |
| 168 | + } |
| 169 | + } |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + private function segmentRuleMatchesContext( |
| 174 | + SegmentRule $rule, |
| 175 | + LDContext $context, |
| 176 | + string $segmentKey, |
| 177 | + string $segmentSalt |
| 178 | + ): bool { |
| 179 | + foreach ($rule->getClauses() as $clause) { |
| 180 | + if (!EvaluatorHelpers::matchClauseWithoutSegments($clause, $context)) { |
| 181 | + return false; |
| 182 | + } |
| 183 | + } |
| 184 | + // If the weight is absent, this rule matches |
| 185 | + if ($rule->getWeight() === null) { |
| 186 | + return true; |
| 187 | + } |
| 188 | + // All of the clauses are met. See if the user buckets in |
| 189 | + $bucketBy = $rule->getBucketBy() ?: 'key'; |
| 190 | + $bucket = EvaluatorBucketing::getBucketValueForContext($context, $segmentKey, $bucketBy, $segmentSalt, null); |
| 191 | + $weight = $rule->getWeight() / 100000.0; |
| 192 | + return $bucket < $weight; |
| 193 | + } |
| 194 | +} |
0 commit comments