Skip to content

aubes/openfeature-flagd-bundle

Repository files navigation

OpenFeature Flagd Bundle

CI Latest Version PHP Version Symfony Version

Symfony bundle that registers the OpenFeature Flagd provider as a Symfony service.

Flagd in Symfony, one composer require away.

Requirements

  • PHP 8.2+
  • Symfony 6.4, 7.4 or 8.x
  • A PSR-18 HTTP client (e.g. symfony/http-client, guzzlehttp/guzzle) and PSR-17 factories (e.g. nyholm/psr7)
  • A running flagd server

Installation

composer require aubes/openfeature-flagd-bundle symfony/http-client nyholm/psr7

Note: Without a Symfony Flex recipe, register the bundle manually in config/bundles.php:

Aubes\OpenFeatureFlagdBundle\OpenFeatureFlagdBundle::class => ['all' => true],

Configuration

# config/packages/open_feature_flagd.yaml
open_feature_flagd:
    host: '%env(FLAGD_HOST)%'    # default: localhost
    port: 8013                    # default: 8013
    protocol: http                # default: http (http or grpc)
    secure: false                 # default: false

    # PSR-18 client service ID (default: Psr\Http\Client\ClientInterface).
    # Use this to wire a scoped or custom HTTP client (base URI, timeouts, retry, logging...).
    http_client: ~

Custom HTTP client

By default, the bundle references the autowired Psr\Http\Client\ClientInterface. To use a scoped Symfony HTTP client:

# config/packages/framework.yaml
framework:
    http_client:
        scoped_clients:
            flagd.client:
                base_uri: '%env(FLAGD_URL)%'
                timeout: 2
                max_duration: 5
# config/packages/open_feature_flagd.yaml
open_feature_flagd:
    host: flagd
    http_client: flagd.client

Usage

Inject the provider directly

This bundle registers FlagdProvider as a Symfony service. You can inject it directly and use the OpenFeature API:

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

class MyService
{
    public function __construct(private FlagdProvider $provider)
    {
    }

    public function doSomething(): void
    {
        $client = OpenFeatureAPI::getInstance()
            ->setProvider($this->provider)
            ->getClient();

        if ($client->getBooleanValue('dark_mode', false)) {
            // ...
        }
    }
}

Pair with aubes/openfeature-bundle (optional)

If you want framework-level sugar on top of the OpenFeature SDK (#[FeatureFlag] / #[FeatureGate] attributes, Twig helpers, request-scoped evaluation context, profiler integration), install aubes/openfeature-bundle alongside and point it at this provider:

# config/packages/open_feature.yaml
open_feature:
    provider: OpenFeature\Providers\Flagd\FlagdProvider

It's entirely optional: this bundle works fine on its own with the plain OpenFeature SDK.

License

MIT. See LICENSE.

About

Symfony bundle for the OpenFeature Flagd provider

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages