Skip to content

Instantly share code, notes, and snippets.

@jpalala
Last active January 25, 2025 03:21
Show Gist options
  • Save jpalala/2e2966de7ba94b548a7d18cf991bdc7f to your computer and use it in GitHub Desktop.
Save jpalala/2e2966de7ba94b548a7d18cf991bdc7f to your computer and use it in GitHub Desktop.
launch iterably wip
<?php
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Config;
// get data from an api that uses basic auth then decode it as a .env file
function get_features($url, $creds, $segment = []) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $creds['username'] . ":" . $creds['password']);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
// $segment is the customer segment you want this feature to be enabled for ie ['premium' => true]
if(!empty($segment)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $segment);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
// Split response into header and body
list($headers, $body) = explode("\r\n\r\n", $return, 2);
curl_close($ch);
return base64_decode($body); //returns you the feature flags enabled in the API for the customer segment or else its a global feature flag
}
$url = env('feature_flag_api_endpoint');
$creds = [
env('LI_AUTH_USERNAME'),
env('LI_AUTH_PASSWORD')
];
$featureCache = Cache::rememberForever('features', function () use ($url, $creds) {
return get_features($url, $creds); //base64_encoded string that decodes to a .env format ie KEY=PAIR
});
$features = explode(PHP_EOL, $featureCache); // ensures cross-platform \r\n or \n only compatibility
foreach ($features as $feat) {
// Extract key-value pair
list($key, $value) = explode('=', $feat, 2);
// Sanitize key (optional)
$key = Str::upper(str_replace(' ', '_', $key));
// Store in config
Config::set('li.' . $key, $value);
}
echo "Environment feature flag variables stored in config.\n";
// Note: Retrieving the value
// $value = Config::get('li.' . $key);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment