PHP code example of guidelytech / guidely-php

1. Go to this page and download the library: Download guidelytech/guidely-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

guidelytech / guidely-php example snippets


use GuidelyTech\Flags\GuidelyFlags;

$flags = new GuidelyFlags([
    'sdkKey' => 'pk_production_…',
]);

$user = ['id' => $user->id, 'email' => $user->email, 'plan' => $account->plan];

if ($flags->isEnabled('new-checkout', $user)) {
    // …
}

$limit = $flags->getNumber('upload-limit-mb', 10, $user);
$theme = $flags->getString('checkout-theme', 'classic', $user);

$flags = new GuidelyFlags([
    'sdkKey' => getenv('GUIDELY_SECRET_KEY'),   // gly_sk_…
    'environment' => 'production',              // development | staging | production
]);

$flags = $flags->forUser(['id' => 'u_123', 'country' => 'GB', 'account' => ['plan' => 'pro']]);

$flags->isEnabled('new-checkout');
$details = $flags->getDetails('new-checkout');
echo $details->reason;       // RULE_MATCH, DEFAULT, EXPERIMENT, DISABLED, OVERRIDE, ERROR, …

// app/Providers/AppServiceProvider.php
use GuidelyTech\Flags\GuidelyFlags;
use Illuminate\Support\Facades\Cache;

public function register(): void
{
    $this->app->singleton(GuidelyFlags::class, fn () => new GuidelyFlags([
        'sdkKey' => config('services.guidely.key'),          // pk_… or gly_sk_…
        'environment' => config('services.guidely.environment'),
        'cache' => Cache::store('redis'),                     // any PSR-16 store
        'logger' => logger(),
        'defaultAttributes' => ['app' => 'web'],
    ]));
}

// anywhere
public function show(Request $request, GuidelyFlags $flags)
{
    $features = $flags->forUser(['id' => (string) $request->user()->id, 'plan' => $request->user()->plan]);
    return view('checkout', ['newCheckout' => $features->isEnabled('new-checkout')]);
}

use GuidelyTech\Flags\Cache\ApcuCache;

$flags = new GuidelyFlags(['sdkKey' => 'pk_…', 'cache' => new ApcuCache()]);

$flags = new GuidelyFlags([
    'sdkKey' => 'pk_…',
    'trackingCallback' => function (array $experiment, EvaluationResult $result) use ($analytics): void {
        // $experiment: key, name?, variationId, variationName?, variationIndex, hashAttribute, hashValue, bucket, stickyBucketUsed
        $analytics->track('$experiment_viewed', ['experiment' => $experiment['key'], 'variation' => $experiment['variationId']]);
    },
]);

$variant = $flags->getString('checkout-button', 'control', ['id' => $userId]);

use GuidelyTech\Flags\Sticky\StickyBucketServiceInterface;

final class RedisStickyBuckets implements StickyBucketServiceInterface
{
    public function __construct(private \Redis $redis) {}

    public function getAssignments(string $docKey): ?array
    {
        $raw = $this->redis->get("sticky:$docKey");
        return $raw === false ? null : json_decode($raw, true);
    }

    public function saveAssignments(string $docKey, array $assignments): void
    {
        $this->redis->set("sticky:$docKey", json_encode($assignments));
    }
}

$flags = new GuidelyFlags([
    'overrides' => ['new-checkout' => true, 'upload-limit-mb' => 50],
    'overrideBehaviour' => 'localOnly',   // no network, no usage reports
]);

$flags->setOverrides(['new-checkout' => false]);   // change them mid-test

$flags = new GuidelyFlags(['sdkKey' => 'pk_…', 'remoteEval' => true]);
$flags->isEnabled('new-checkout', ['id' => 'u_123']);   // POST /api/v1/sdk/flags/evaluate

$flags = new GuidelyFlags([
    'sdkKey' => 'pk_…',
    'decryptionKey' => getenv('GUIDELY_DECRYPTION_KEY'),
]);

use GuidelyTech\Flags\Evaluator;
use GuidelyTech\Flags\Json;

$payload = Json::decode($json);
$result = Evaluator::evaluate($payload, 'new-checkout', ['id' => 'u_123'], now: new DateTimeImmutable());
bash
composer 
bash
cd sdks/php
composer install
composer test
bash
docker build -t guidely-php-test:8.1 --build-arg PHP_VERSION=8.1 sdks/php/docker
docker run --rm -v "$PWD/sdks":/sdks -w /sdks/php guidely-php-test:8.1 composer install
docker run --rm -v "$PWD/sdks":/sdks -w /sdks/php guidely-php-test:8.1 composer test
bash
   ssh-keygen -t ed25519 -C guidely-php-release -f guidely-php-key -N ""