Giter Site home page Giter Site logo

php-sdk's Introduction

OpenFeature SDK for PHP

a codecov Specification Latest Stable Version Total Downloads PHP 8.0+ License OpenSSF Best Practices

Overview

This package provides a functional SDK for an OpenFeature API and client. It also builds on various PSRs (PHP Standards Recommendations) such as the Logger interfaces (PSR-3) and the Basic and Extended Coding Standards (PSR-1 and PSR-12).

Future development goals for the package include:

  • Integrate container standards (PSR-11)
  • Build on PHP HTTP Promises to support async clients

Installation

composer require open-feature/sdk

Usage

While Boolean provides the simplest introduction, we offer a variety of flag types.

use OpenFeature\OpenFeatureClient;

class MyClass 
{
  private OpenFeatureClient $client;

  public function __construct() 
  {
    $this->client = OpenFeatureAPI::getInstance()->getClient('MyClass');
  }

  public function booleanExample(): UI
  {
      // Should we render the redesign? Or the default webpage? 
      if ($this->client->getBooleanValue('redesign_enabled', false)) {
          return render_redesign();
      }
      return render_normal();
  }

  public function stringExample(): Template
  {
      // Get the template to load for the custom new homepage
      $template = $this->client->getStringValue('homepage_template', 'default-homepage.html');

      return render_template($template);
  }

  public function numberExample(): array
  {
      // How many modules should we be fetching?
      $count = $this->client->getIntegerValue('module-fetch-count', 4);

      return fetch_modules($count);
  }

  public function structureExample(): HomepageModule
  {
      $obj = $this->client->getObjectValue('hero-module', $previouslyDefinedDefaultStructure);

      return HomepageModuleBuilder::new()
              ->title($obj->getValue('title'))
              ->body($obj->getValue('description'))
              ->build();
  }
}

Configuration

To configure OpenFeature, you'll need to add a provider to the global singleton OpenFeatureAPI. From there, you can generate a Client which is usable by your code. If you do not set a provider, then the NoOpProvider, which simply returns the default passed in, will be used.

use OpenFeature\OpenFeatureAPI;
use OpenFeature\Providers\Flagd\FlagdProvider;

class MyApp 
{
    public function bootstrap() 
    {
        $api = OpenFeatureAPI::getInstance();
        $api->setProvider(new FlagdProvider());
        $client = $api->getClient();

        // Now use your `$client` instance to evaluate some feature flags!
    }
}

Providers & Hooks

The OpenFeature project maintains the open-feature/php-sdk-contrib repository, which includes contributed providers and hooks. Vendors can also implement and maintain their own providers as well. We update the PHP docs page with these vendors as often as possible.

Development

PHP Versioning

This library targets PHP version 8.0 and newer. As long as you have any compatible version of PHP on your system you should be able to utilize the OpenFeature SDK.

This package also has a .tool-versions file for use with PHP version managers like asdf.

Installation and Dependencies

Install dependencies with composer install. composer install will update the composer.lock with the most recent compatible versions.

We value having as few runtime dependencies as possible. The addition of any dependencies requires careful consideration and review.

Testing

Run tests with composer run test.

php-sdk's People

Contributors

tcarrio avatar github-actions[bot] avatar renovate[bot] avatar beeme1mr avatar xepozz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.