gathern/growthbook-openfeature-provider

this package provide growthbook (a feature flag serice) provider to openfeature

v1.0.0 2025-04-10 15:40 UTC

This package is auto-updated.

Last update: 2025-04-14 01:17:10 UTC


README

Latest Version on Packagist Tests Total Downloads

This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.

Installation

You can install the package via composer:

composer require gathern/growthbook-openfeature-provider

Usage

<?php
    use Cache\Adapter\Apcu\ApcuCachePool;
    use Growthbook\Growthbook;
    use OpenFeature\OpenFeatureAPI;

    $growthbook Growthbook::create();
    //By default, there is no caching enabled.
    // You can enable it by passing any PSR16-compatible
    // instance into the withCache method.
    
    // Any psr-16 library will work
    $cache = new ApcuCachePool();

    $growthbook ->withCache($cache);

    $api = OpenFeatureAPI::getInstance();
    $api->setProvider(new GrowthbookOpenfeatureProvider(
            growthbook: Growthbook,
            clientKey: '<Growthbook_CLIENT_KEY>',
            apiHost: '<Growthbook_API_HOST>',
        ));

    $client = $api->getClient(
        GrowthbookOpenfeatureProvider::class,
        'v1.17',
         );

    $client->getStringValue
    (
        flagKey: 'enable-success-button',
        defaultValue: 'wrong',
    );

if you need to get the flag value of specific user attributes you can openfeature evaluation-context and set the user data as attributes .

<?php
    use OpenFeature\implementation\flags\Attributes;
    use OpenFeature\implementation\flags\MutableEvaluationContext;

        $user_data = [
            'name' => 'john Doe',
            'age' => '20',
            'gender' => 'male',
            'nationality' => 'martian'
            ];

       echo $client->getBooleanValue(
                flagKey: 'test-boolean',
                defaultValue: false,
                context: new MutableEvaluationContext(
                    targetingKey: "targeting-key-value",
                    attributes: new Attributes(attributesMap: $user_data)
                )
            );

you can follow the usage of openfeature-php-package and docs of growthbook-php-sdk for instance of growthbook

if you need to use all growthbook sdk features or any feature service throw the openfeature api to reach feature using spatie/invade package just install it and follow this example

<?php

$openfeatureAPI= invade($client)->api;
$provider=invade($openfeatureAPI)->getProvider();
$growthbook=invade($provider)->growthbook;
print_r($growthbook->getFeatures());
print_r($growthbook->getAttributes());
print_r( $growthbook->getViewedExperiments());

Testing

composer test:unit

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.