Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/sdk-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ permissions:
jobs:
sdk-spec:
# Keep the ref in `uses:` and `spec-ref` in sync.
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.7.1
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.9.0
with:
spec-ref: v0.7.1
tags: core or events or local-evaluation
spec-ref: v0.9.0
tags: core events local-evaluation
language: php
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"guzzlehttp/psr7": "^2.1.0",
"phpunit/phpunit": "^9.5",
"symfony/cache": "^5.4.6",
"symfony/http-client": "^6.2 || ^7.0 || ^8.0",
"friendsofphp/php-cs-fixer": "^3.6",
"doppiogancio/mocked-client": "^3.0",
"colinodell/json5": "^3.0"
Expand Down
29 changes: 29 additions & 0 deletions sdk-spec/steps.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ function createClientWithEvents(string $environmentKey, string $apiUrl, string $
return new Flagsmith(apiKey: $environmentKey, host: $apiUrl, enableEvents: true, eventsApiUrl: $eventsApiUrl);
}

// Create the SDK client with local evaluation and events enabled
function createClientWithLocalEvaluationAndEvents(string $environmentKey, string $apiUrl, string $eventsApiUrl): Flagsmith
{
$flagsmith = new Flagsmith(apiKey: $environmentKey, host: $apiUrl, environmentTtl: 60, enableEvents: true, eventsApiUrl: $eventsApiUrl);
$flagsmith->updateEnvironment();
return $flagsmith;
}

// When the application gets the environment flags
function getEnvironmentFlags(Flagsmith $flagsmith): array
{
Expand Down Expand Up @@ -76,3 +84,24 @@ function trackExposureEvent(Flagsmith $flagsmith, string $feature, string $ident
$flagsmith->trackExposureEvent($feature, identifier: $identifier, value: $value, metadata: $metadata);
$flagsmith->flushEvents(); // Events are sent when a PHP request ends, which the harness's long-lived process never does.
}

// When the application tracks events "event_{first}" to "event_{last}" for identity "{identifier}"
function trackEvents(Flagsmith $flagsmith, int $first, int $last, string $identifier): void
{
// Tracked in one go, then sent when the application flushes events.
for ($number = $first; $number <= $last; $number++) {
$flagsmith->trackEvent("event_{$number}", identifier: $identifier);
}
}

// When the application flushes events
function flushEvents(Flagsmith $flagsmith): void
{
$flagsmith->flushEvents();
}

// When the application closes the client with a timeout of {timeout} seconds
function closeClient(Flagsmith $flagsmith, int|float $timeout): void
{
$flagsmith->close($timeout);
}
14 changes: 14 additions & 0 deletions src/Flagsmith.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,19 @@ public function flushEvents(): void
$this->eventProcessor?->flush();
}

/**
* Send all buffered events, giving up after `$timeoutSeconds`, and stop tracking events. Call it
* before the application stops so that its events are not lost. Never fails the application.
* With an HTTP client other than Guzzle or Symfony's, a request already sent when the timeout
* elapses can outlast it, until that client's own timeout: see `EventProcessor`.
* @param float $timeoutSeconds
* @return void
*/
public function close(float $timeoutSeconds = 5): void
{
$this->eventProcessor?->close($timeoutSeconds);
}

/**
* Get a list of segments that the given identity is in.
* @param string $identifier a unique identifier for the identity in the current
Expand Down Expand Up @@ -597,6 +610,7 @@ traits: $traits,
$evaluationResult,
analyticsProcessor: $this->analyticsProcessor,
defaultFlagHandler: $this->defaultFlagHandler,
identityEvaluation: true,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Models/Experiment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Flagsmith\Concerns\HasWith;

/**
* The running experiment a flag was evaluated under. Only remote identity evaluations carry it.
* The running experiment a flag was evaluated under. Only identity evaluations carry it.
*/
class Experiment
{
Expand Down
7 changes: 7 additions & 0 deletions src/Models/Flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ public function withAnalyticsProcessor(?AnalyticsProcessor $analyticsProcessor):
* @param EvaluationResult $evaluationResult
* @param ?AnalyticsProcessor $analyticsProcessor
* @param ?\Closure $defaultFlagHandler
* @param bool $identityEvaluation whether the result is an identity's, which carries its experiments
* @return void
*/
public static function fromEvaluationResult(
$evaluationResult,
?AnalyticsProcessor $analyticsProcessor,
?\Closure $defaultFlagHandler,
bool $identityEvaluation = false,
): Flags {
$flags = [];
foreach ($evaluationResult->flags as $flagResult) {
Expand All @@ -98,6 +100,11 @@ public static function fromEvaluationResult(
$flag->feature_id = $flagResult->metadata['id'];
$flag->enabled = $flagResult->enabled;
$flag->value = $flagResult->value;
$flag->variant = $flagResult->variant;
if ($identityEvaluation && isset($flagResult->metadata['experiment'])) {
// Only an identity is enrolled in an experiment
$flag->experiment = Experiment::fromApiMetadata((object) $flagResult->metadata);
}
$flags[$flagResult->name] = $flag;
}

Expand Down
Loading
Loading