featurit/featurit-sdk-php

PHP SDK for FeaturIT

v0.8.0 2024-09-26 11:50 UTC

This package is auto-updated.

Last update: 2024-11-26 12:05:38 UTC


README

Build Status Total Downloads Latest Stable Version License

PHP client for the FeaturIT Feature Flag management platform.

Description

This package aims to simplify the integration of the FeaturIT API in a PHP project.

Getting started

Dependencies

  • PHP >= 8.0
  • psr/http-client-implementation
  • psr/simple-cache-implementation

Installing

composer require featurit/featurit-sdk-php

If there's no package providing psr/http-client-implementation, visit https://packagist.org/providers/psr/http-client-implementation and choose the package that better suits your project.

If there's no package providing psr/simple-cache-implementation, visit https://packagist.org/providers/psr/simple-cache-implementation and choose the package that better suits your project.

Basic Usage

$featurit = new \Featurit\Client\Featurit(
    'my-tenant-subdomain', 
    'my-environment-key'
);

$isFeatureFlagActive = $featurit->isActive('MY_FEATURE_NAME');

if ($isFeatureFlagActive) {
    my_feature_code();
}

Segmentation Usage

This is useful when you want to show different versions of your features to your users depending on certain attributes.

$featurit = new \Featurit\Client\Featurit(
    'my-tenant-subdomain', 
    'my-environment-key'
);

$userContext = new \Featurit\Client\Modules\Segmentation\DefaultFeaturitUserContext(
    "1234",
    "123af503",
    "192.168.1.1",
    [
        "role" => "ADMIN",
        "email" => "featurit.tech@gmail.com",
    ] 
);

$featurit->setUserContext($userContext);

$isFeatureFlagActive = $featurit->isActive('MY_FEATURE_NAME');

if ($isFeatureFlagActive) {
    $featureVersion = $featurit->version('MY_FEATURE_NAME');
    
    if ($featureVersion == 'v1') {
        my_feature_v1_code();
    } else if ($featureVersion == 'v2') {
        my_feature_v2_code();
    }
}

Creating a UserContextProvider

In some cases you want to fill the UserContext data once and forget about it when checking for feature flags.

If that is your case you can implement your own UserContextProvider and pass it to the Featurit client constructor (we recommend using our builder in order to create a new Featurit client).

class MyFeaturitUserContextProvider implements FeaturitUserContextProvider

    public function getContext(): FeaturitUserContext
    {
        $contextData = get_my_context_data();
        
        return new DefaultFeaturitUserContext(
            $contextData['userId'],
            $contextData['sessionId'],
            $contextData['ipAddress'],
            [
                "role" => $contextData['role'],
                ...
            ]
        );
    }
}

Authors

FeaturIT

https://www.featurit.com

featurit.tech@gmail.com