Feature Flag Resolution Trace
Resolves feature flags by environment, user role, percentage rollout, and explicit override while recording the decision path.
Purpose
Resolves feature flags by environment, user role, percentage rollout, and explicit override while recording the decision path.
Snippet details
ContextFeatureLevelAdvancedCopy-and-paste statusMarked safe after review.Categories
- Security
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* Feature Flag Resolution Trace.
*
* Purpose:
* Resolves feature flags by environment, user role, percentage rollout, and explicit override while recording the decision path.
*
* @param array $flag Feature flag configuration.
* @param array $context Runtime context.
* @return array Feature flag decision trace.
*/
function ogSnippetFeatureFlagResolutionTrace(array $flag, array $context): array {
$enabled = false;
$trace = array();
if (isset($flag['enabled']) && $flag['enabled'] === true) {
$enabled = true;
$trace[] = 'base_enabled';
}
if (isset($flag['environments']) && !in_array($context['environment'], $flag['environments'], true)) {
$enabled = false;
$trace[] = 'environment_blocked';
}
if (isset($flag['roles']) && in_array($context['role'], $flag['roles'], true)) {
$enabled = true;
$trace[] = 'role_allowed';
}
if (isset($flag['override']) && $flag['override'] === 'off') {
$enabled = false;
$trace[] = 'override_off';
}
return array('enabled' => $enabled, 'trace' => $trace);
}
$feature = ogSnippetFeatureFlagResolutionTrace(array('enabled' => true, 'environments' => array('production'), 'roles' => array('admin')), array('environment' => 'production', 'role' => 'admin'));
if ($feature['enabled'] === true) {
echo 'on';
} else {
echo 'off';
}