PHP

This PHP SDK is designed to help you integrate with Unleash and evaluate feature flags inside your application.

You can use this client with Unleash Enterprise or Unleash Open Source.

Unleash Client SDK

A PHP implementation of the Unleash protocol aka Feature Flags in GitLab.

You may also be interested in the Symfony Bundle for this package.

Unleash allows you to gradually release your app’s feature before doing a full release based on multiple strategies like releasing to only specific users or releasing to a percentage of your user base. Read more in the above linked documentations.

Migrating

If you’re migrating from 1.x to 2.x, you can read the migration guide.

Installation

composer require unleash/client

Requires PHP 7.2 or newer.

You will also need some implementation of PSR-18 and PSR-17, for example Guzzle and PSR-16, for example Symfony Cache. Example:

composer require unleash/client guzzlehttp/guzzle symfony/cache

or

composer require unleash/client symfony/http-client nyholm/psr7 symfony/cache

If you want to make use of events you also need to install symfony/event-dispatcher. See event documentation here.

Usage

The basic usage is getting the Unleash object and checking for a feature:

1<?php
2
3use Unleash\Client\UnleashBuilder;
4
5$unleash = UnleashBuilder::create()
6 ->withAppName('Some app name')
7 ->withAppUrl('https://some-app-url.com')
8 ->withInstanceId('Some instance id')
9 ->build();
10
11if ($unleash->isEnabled('some-feature-name')) {
12 // do something
13}

You can (and in some cases you must) also provide a context object. If the feature doesn’t exist on the server you will get false from isEnabled(), but you can change the default value to true.

1<?php
2
3use Unleash\Client\UnleashBuilder;
4use Unleash\Client\Configuration\UnleashContext;
5
6$unleash = UnleashBuilder::create()
7 ->withAppName('Some app name')
8 ->withAppUrl('https://some-app-url.com')
9 ->withInstanceId('Some instance id')
10 ->build();
11
12$context = new UnleashContext(
13 currentUserId: 'some-user-id-from-app',
14 ipAddress: '127.0.0.1', // will be populated automatically from $_SERVER if needed
15 sessionId: 'sess-123456', // will be populated automatically via session_id() if needed
16);
17
18// or using pre php 8 style:
19
20$context = (new UnleashContext())
21 ->setCurrentUserId('some-user-id-from-app')
22 ->setIpAddress('127.0.0.1')
23 ->setSessionId('sess-123456');
24
25if ($unleash->isEnabled('some-feature', $context)) {
26 // do something
27}
28
29// changing the default value for non-existent features
30if ($unleash->isEnabled('nonexistent-feature', $context, true)) {
31 // do something
32}

Builder

The builder contains many configuration options, and it’s advised to always use the builder to construct an Unleash instance. The builder is immutable.

The builder object can be created using the create() static method or by using its constructor:

1<?php
2
3use Unleash\Client\UnleashBuilder;
4
5// both are the same
6$builder = new UnleashBuilder();
7$builder = UnleashBuilder::create();

You can replace various parts of the Unleash SDK with custom implementation using the builder, like custom registration service, custom metrics handler and so on.

Replaceable parts (some of them have further documentation below):

  • registration service (withRegistrationService())
  • context provider (withContextProvider())
  • bootstrap handler (withBootstrapHandler())
  • event dispatcher (withEventDispatcher())
  • metrics handler (withMetricsHandler())
  • variant handler (withVariantHandler())

Dependencies can be injected by implementing one of the following interfaces from the Unleash\Client\Helper\Builder namespace:

  • CacheAware - injects standard cache
  • ConfigurationAware - injects the global configuration object
  • HttpClientAware - injects the http client
  • MetricsSenderAware - injects the metrics sender service
  • RequestFactoryAware - injects the request factory
  • StaleCacheAware - injects the stale cache handler
  • StickinessCalculatorAware - injects the stickiness calculator used for calculating stickiness in gradual rollout strategy

In addition to the parts above these interfaces can also be implemented by these kinds of classes:

  • bootstrap providers
  • event subscribers
  • strategy handlers

Some classes cannot depend on certain objects, namely any object that is present in the configuration cannot implement ConfigurationAware (to avoid circular dependency). The same classes also cannot implement MetricsSenderAware because metrics sender depends on the configuration object. You will get a \Unleash\Client\Exception\CyclicDependencyException if that happens.

Example:

1<?php
2
3use Unleash\Client\Helper\Builder\ConfigurationAware;
4use Unleash\Client\Metrics\MetricsHandler;
5use Unleash\Client\Configuration\UnleashConfiguration;
6use Unleash\Client\DTO\Feature;
7use Unleash\Client\DTO\Variant;
8use Unleash\Client\UnleashBuilder;
9
10final class CustomMetricsHandler implements MetricsHandler, ConfigurationAware
11{
12 private UnleashConfiguration $configuration;
13
14 public function setConfiguration(UnleashConfiguration $configuration): void
15 {
16 // this method gets called automatically by the builder
17 $this->configuration = $configuration;
18 }
19
20 public function handleMetrics(Feature $feature, bool $successful, Variant $variant = null): void
21 {
22 // the configuration object is available here
23 if ($this->configuration->getInstanceId() === '...') {
24 // do something
25 }
26 }
27}
28
29$instance = UnleashBuilder::create()
30 ->withMetricsHandler(new CustomMetricsHandler())
31 ->build();

Required Parameters

The app name, instance id and app url are required as per the specification.

1<?php
2
3use Unleash\Client\UnleashBuilder;
4
5$builder = UnleashBuilder::create()
6 ->withAppName('Some app name')
7 ->withAppUrl('https://some-app-url.com')
8 ->withInstanceId('Some instance id');

If you’re using Unleash v4 you also need to specify authorization key (API key), you can do so with custom header.

1<?php
2
3use Unleash\Client\UnleashBuilder;
4
5$builder = UnleashBuilder::create()
6 ->withAppName('Some app name')
7 ->withAppUrl('https://some-app-url.com')
8 ->withInstanceId('Some instance id')
9 ->withHeader('Authorization', 'my-api-key');

To filter feature toggles by tag or name prefix you can use the Url helper:

1<?php
2
3use Unleash\Client\UnleashBuilder;
4use Unleash\Client\Helper\Url;
5
6$builder = UnleashBuilder::create()
7 ->withAppName('Some app name')
8 ->withAppUrl(new Url('https://some-app-url.com', namePrefix: 'somePrefix.', tags: [
9 'myTag' => 'myValue',
10 ]))
11 ->withInstanceId('Some instance id');

Optional Parameters

Some optional parameters can be set, these include:

  • http client implementation (PSR-18)
  • request factory implementation (PSR-17)
  • cache implementation (PSR-16)
  • cache ttl
  • available strategies
  • http headers

The builder will attempt to load http client and request factory implementations automatically. Most implementations, such as guzzlehttp/guzzle or symfony/http-client (in combination with nyholm/psr7), will be loaded automatically. If the builder is unable to locate a http client or request factory implementation, you will need to provide some implementation on your own.

If you use symfony/cache or cache/filesystem-adapter as your cache implementation, the cache handler will be created automatically, otherwise you need to provide some implementation on your own.

1<?php
2
3use Cache\Adapter\Filesystem\FilesystemCachePool;
4use League\Flysystem\Adapter\Local;
5use League\Flysystem\Filesystem;
6use GuzzleHttp\Client;
7use GuzzleHttp\Psr7\HttpFactory;
8use Unleash\Client\Stickiness\MurmurHashCalculator;
9use Unleash\Client\Strategy\DefaultStrategyHandler;
10use Unleash\Client\Strategy\GradualRolloutStrategyHandler;
11use Unleash\Client\Strategy\IpAddressStrategyHandler;
12use Unleash\Client\Strategy\UserIdStrategyHandler;
13use Unleash\Client\UnleashBuilder;
14use Unleash\Client\Helper\Url;
15
16$builder = UnleashBuilder::create()
17 ->withAppName('Some app name')
18 ->withAppUrl('https://some-app-url.com') // as a string
19 ->withAppUrl(new Url('https://some-app-url.com', tags: ['myTag' => 'myValue'])) // or as Url instance
20 ->withInstanceId('Some instance id')
21 // now the optional ones
22 ->withHttpClient(new Client())
23 ->withRequestFactory(new HttpFactory())
24 ->withCacheHandler(new FilesystemCachePool( // example with using cache/filesystem-adapter
25 new Filesystem(
26 new Local(sys_get_temp_dir()),
27 ),
28 ), 30) // the second parameter is time to live in seconds
29 ->withCacheTimeToLive(60) // you can also set the cache time to live separately
30 // if you don't add any strategies, by default all strategies are added
31 ->withStrategies( // this example includes all available strategies
32 new DefaultStrategyHandler(),
33 new GradualRolloutStrategyHandler(new MurmurHashCalculator()),
34 new IpAddressStrategyHandler(),
35 new UserIdStrategyHandler(),
36 )
37 // add headers one by one, if you specify a header with the same name multiple times it will be replaced by the
38 // latest value
39 ->withHeader('My-Custom-Header', 'some-value')
40 ->withHeader('Some-Other-Header', 'some-other-value')
41 // you can specify multiple headers at the same time, be aware that this REPLACES all the headers
42 ->withHeaders([
43 'Yet-Another-Header' => 'and-another-value',
44 ]);

Returning Intermediate Objects

For some use cases the builder can return intermediate objects, for example the UnleashRepository object. This can be useful if you need to directly interact with the repository, to refresh the cache manually for example.

1<?php
2
3use Unleash\Client\UnleashBuilder;
4use Unleash\Client\Helper\Url;
5
6$repository = UnleashBuilder::create()
7 ->withAppName('Some app name')
8 ->withAppUrl(new Url('https://some-app-url.com', namePrefix: 'somePrefix.', tags: [
9 'myTag' => 'myValue',
10 ]))
11 ->withInstanceId('Some instance id')
12 ->buildRepository();
13$repository->refreshCache();

Proxy SDK

By default the SDK uses the Backend endpoints on the Unleash API. You can also use the Proxy SDK, which is a lightweight SDK that uses the Frontend endpoints on the Unleash API. The Proxy SDK give a substantial performance improvement when using a large set of feature toggles (10K+).

To use the Proxy SDK, you need to call withProxy($apiKey) on the builder. The $apiKey needs to be a frontend token. Note that withProxy($apiKey) is in lieu of setting the API key header.

Example of using the builder to create a Proxy SDK instance:

1<?php
2$builder = UnleashBuilder::create()
3 ->withAppName('Some app name')
4 ->withAppUrl('https://some-app-url.com/api/frontend')
5 ->withInstanceId('Some instance id')
6 ->withProxy("some-proxy-key"); // <-- This is the only difference
7
8$unleash = $builder->build();
9
10$unleash.isEnabled("some-feature");

As of version 1.12, the Proxy SDK requires Edge, so the appUrl needs to point to the Edge server.

Not supported in the Proxy SDK:

  • Custom strategies
  • Registration (this is handled by Edge)

Caching

It would be slow to perform a http request every time you check if a feature is enabled, especially in popular apps. That’s why this library has built-in support for PSR-16 cache implementations.

If you don’t provide any implementation and default implementation exists, it’s used, otherwise you’ll get an exception. You can also provide a TTL which defaults to 15 seconds for standard cache and 30 minutes for stale data cache.

Stale data cache is used when http communication fails while fetching feature list from the server. In that case the latest valid version is used until the TTL expires or server starts responding again. An event gets emitted when this happens, for more information see events documentation.

Cache implementations supported out of the box (meaning you don’t need to configure anything):

1<?php
2
3use Cache\Adapter\Filesystem\FilesystemCachePool;
4use League\Flysystem\Adapter\Local;
5use League\Flysystem\Filesystem;
6use Unleash\Client\UnleashBuilder;
7
8$builder = UnleashBuilder::create()
9 ->withCacheHandler(new FilesystemCachePool( // example with using cache/filesystem-adapter
10 new Filesystem(
11 new Local(sys_get_temp_dir()),
12 ),
13 ))
14 ->withCacheTimeToLive(120)
15 ->withStaleTtl(300)
16;
17
18// you can set the cache handler explicitly to null to revert back to autodetection
19
20$builder = $builder
21 ->withCacheHandler(null);

You can use a different cache implementation for standard item cache and for stale cache. If you don’t provide any implementation for stale cache, the same instance as for standard cache is used.

1<?php
2
3use Cache\Adapter\Filesystem\FilesystemCachePool;
4use League\Flysystem\Adapter\Local;
5use League\Flysystem\Filesystem;
6use Unleash\Client\UnleashBuilder;
7use Symfony\Component\Cache\Psr16Cache;
8use Symfony\Component\Cache\Adapter\ArrayAdapter;
9
10$builder = UnleashBuilder::create()
11 ->withCacheHandler(new FilesystemCachePool( // example with using cache/filesystem-adapter
12 new Filesystem(
13 new Local(sys_get_temp_dir()),
14 ),
15 ))
16 ->withStaleCacheHandler(new Psr16Cache(new ArrayAdapter()))
17 ->withCacheTimeToLive(120)
18 ->withStaleTtl(300)
19;

Bootstrapping

You can set a default response from the SDK in cases when for some reason contacting Unleash server fails.

By default, you can bootstrap using:

  • json string
  • file (via path or instance of SplFileInfo)
  • URL address
  • custom stream wrapper path
  • array
  • instances of Traversable
  • instances of JsonSerializable

These correspond to bootstrap providers:

  • JsonBootstrapProvider (json string)
  • FileBootstrapProvider (file, URL address, custom stream wrapper path)
  • JsonSerializableBootstrapProvider (array, Traversable, JsonSerializable)
  • EmptyBootstrapProvider (default provider that doesn’t provide any bootstrap)
  • CompoundBootstrapProvider (can contain multiple bootstrap providers and tries them one by one)

Examples of bootstraps:

1<?php
2
3use Unleash\Client\UnleashBuilder;
4
5$bootstrapJson = '{"features": []}';
6$bootstrapFile = 'path/to/my/file.json';
7$bootstrapSplFile = new SplFileInfo('path/to/my/file.json');
8$bootstrapUrl = 'https://example.com/unleash-bootstrap.json';
9$bootstrapStreamWrapper = 's3://my-bucket/bootstrap.json'; // assuming you have a custom stream wrapper called 's3'
10$bootstrapArray = [
11 'features' => [
12 [
13 'enabled' => true,
14 'name' => 'BootstrapDemo',
15 'description' => '',
16 'project' => 'default',
17 'stale' => false,
18 'type' => 'release',
19 'variants' => [],
20 'strategies' => [[ 'name' => 'default' ]],
21 ],
22 ],
23];
24$bootstrapTraversable = new class implements Iterator {
25 public function current(): mixed
26 {
27 // todo implement method
28 }
29
30 public function next(): void
31 {
32 // todo implement method
33 }
34
35 public function key(): mixed
36 {
37 // todo implement method
38 }
39
40 public function valid(): bool
41 {
42 // todo implement method
43 }
44
45 public function rewind(): void
46 {
47 // todo implement method
48 }
49};
50$bootstrapJsonSerializable = new class implements JsonSerializable {
51 public function jsonSerialize(): array {
52 // TODO: Implement jsonSerialize() method.
53 }
54}
55
56// now assign them to the builder, note that each withBootstrap* method call overrides the bootstrap
57
58$builder = UnleashBuilder::create()
59 ->withBootstrap($bootstrapJson)
60 ->withBootstrapFile($bootstrapFile)
61 ->withBootstrapFile($bootstrapSplFile)
62 ->withBootstrapUrl($bootstrapUrl)
63 ->withBootstrapFile($bootstrapStreamWrapper)
64 ->withBootstrap($bootstrapArray)
65 ->withBootstrap($bootstrapTraversable)
66 ->withBootstrap($bootstrapJsonSerializable)
67 ->withBootstrap(null) // empty bootstrap
68;

Using bootstrap providers directly:

1<?php
2
3use Unleash\Client\Bootstrap\EmptyBootstrapProvider;
4use Unleash\Client\Bootstrap\FileBootstrapProvider;
5use Unleash\Client\Bootstrap\JsonBootstrapProvider;
6use Unleash\Client\Bootstrap\JsonSerializableBootstrapProvider;
7use Unleash\Client\UnleashBuilder;
8
9// using variables defined in previous example, again each call overrides the last bootstrap provider
10
11$builder = UnleashBuilder::create()
12 ->withBootstrapProvider(new JsonBootstrapProvider($bootstrapJson))
13 ->withBootstrapProvider(new FileBootstrapProvider($bootstrapFile))
14 ->withBootstrapProvider(new FileBootstrapProvider($bootstrapSplFile))
15 ->withBootstrapProvider(new FileBootstrapProvider($bootstrapUrl))
16 ->withBootstrapProvider(new FileBootstrapProvider($bootstrapStreamWrapper))
17 ->withBootstrapProvider(new JsonSerializableBootstrapProvider($bootstrapArray))
18 ->withBootstrapProvider(new JsonSerializableBootstrapProvider($bootstrapTraversable))
19 ->withBootstrapProvider(new JsonSerializableBootstrapProvider($bootstrapJsonSerializable))
20 ->withBootstrapProvider(new EmptyBootstrapProvider()) // equivalent to ->withBootstrap(null)
21;

Using multiple bootstrap providers:

1<?php
2
3use Unleash\Client\Bootstrap\CompoundBootstrapProvider;
4use Unleash\Client\Bootstrap\FileBootstrapProvider;
5use Unleash\Client\Bootstrap\JsonSerializableBootstrapProvider;
6use Unleash\Client\UnleashBuilder;
7
8// using variables defined in first example
9
10$provider = new CompoundBootstrapProvider(
11 new FileBootstrapProvider($bootstrapUrl),
12 new FileBootstrapProvider($bootstrapFile),
13 new JsonSerializableBootstrapProvider($bootstrapArray),
14);
15
16// All providers in compound bootstrap provider will be tried one by one in the order they were assigned until
17// at least one returns something.
18
19// If no provider returns non-null value, the compound provider itself returns null.
20
21// If an exception is thrown in any of the inner providers it's ignored and next provider is tried.
22
23// If an exception was thrown in any of the inner providers and no other provider returned any value, the exceptions
24// from inner providers are thrown using a CompoundException, you can get the exceptions by calling ->getExceptions()
25// on it.
26
27$builder = UnleashBuilder::create()
28 ->withBootstrapProvider($provider);

Custom Bootstrap Provider

Creating a custom bootstrap provider is very simple, just implement the BootstrapProvider interface and use your class in the builder:

1<?php
2
3use Unleash\Client\UnleashBuilder;
4use Unleash\Client\Bootstrap\BootstrapProvider;
5
6final class MyBootstrapProvider implements BootstrapProvider
7{
8 public function getBootstrap() : array|JsonSerializable|Traversable|null
9 {
10 // TODO: Implement getBootstrap() method.
11 }
12}
13
14$builder = UnleashBuilder::create()
15 ->withBootstrapProvider(new MyBootstrapProvider());

Disabling Communication With Unleash Server

It may be useful to disable communication with the Unleash server for local development and using a bootstrap instead.

Note that when you disable communication with Unleash and don’t provide a bootstrap, an exception will be thrown.

Set the cache interval to 0 to always have a fresh bootstrap content.

The usually required parameters (app name, instance id, app url) are not required when communication is disabled.

1<?php
2
3use Unleash\Client\UnleashBuilder;
4
5$unleash = UnleashBuilder::create()
6 ->withBootstrap('{}')
7 ->withFetchingEnabled(false) // here we disable communication with Unleash server
8 ->withCacheTimeToLive(0) // disable the caching layer to always get a fresh bootstrap
9 ->build();

Strategies

Unleash servers can use multiple strategies for enabling or disabling features. Which strategy gets used is defined on the server. This implementation supports all v4 strategies. More here.

Default Strategy

This is the simplest of them and simply always returns true if the feature defines default as its chosen strategy and doesn’t need any context parameters.

IP Address Strategy

Enables feature based on the IP address. Takes current user’s IP address from the context object. You can provide your own IP address or use the default ($_SERVER['REMOTE_ADDR']). Providing your own is especially useful if you’re behind proxy and thus REMOTE_ADDR would return your proxy server’s IP address instead.

As of 1.4.0 the CIDR notation is supported

1<?php
2
3use Unleash\Client\UnleashBuilder;
4use Unleash\Client\Configuration\UnleashContext;
5
6$unleash = UnleashBuilder::create()
7 ->withAppName('Some app name')
8 ->withAppUrl('https://some-app-url.com')
9 ->withInstanceId('Some instance id')
10 ->build();
11
12// without context, using the auto detected IP
13$enabled = $unleash->isEnabled('some-feature');
14
15// with context
16$context = new UnleashContext(ipAddress: $_SERVER['HTTP_X_FORWARDED_FOR']);
17// or pre php 8 style
18$context = (new UnleashContext())
19 ->setIpAddress($_SERVER['HTTP_X_FORWARDED_FOR']);
20$enabled = $unleash->isEnabled('some-feature', $context);

User ID Strategy

Enables feature based on the user ID. The user ID can be any string. You must always provide your own user id via context.

1<?php
2
3use Unleash\Client\UnleashBuilder;
4use Unleash\Client\Configuration\UnleashContext;
5
6$unleash = UnleashBuilder::create()
7 ->withAppName('Some app name')
8 ->withAppUrl('https://some-app-url.com')
9 ->withInstanceId('Some instance id')
10 ->build();
11
12$context = new UnleashContext(currentUserId: 'some-user-id');
13$enabled = $unleash->isEnabled('some-feature', $context);

Gradual Rollout Strategy

Also known as flexible rollout. Allows you to enable feature for only a percentage of users based on their user id, session id or randomly. The default is to try in this order: user id, session id, random.

If you specify the user id type on your Unleash server, you must also provide the user id via context, same as in the User ID strategy. Session ID can also be provided via context, it defaults to the current session id via session_id() call.

This strategy requires a stickiness calculator that transforms the id (user, session or random) into a number between 1 and 100. You can provide your own or use the default \Unleash\Client\Stickiness\MurmurHashCalculator

1<?php
2
3use Unleash\Client\UnleashBuilder;
4use Unleash\Client\Configuration\UnleashContext;
5
6$unleash = UnleashBuilder::create()
7 ->withAppName('Some app name')
8 ->withAppUrl('https://some-app-url.com')
9 ->withInstanceId('Some instance id')
10 ->build();
11
12// assume the feature uses the default type which means that it will default to either session id (if session is started)
13// or randomly
14$unleash->isEnabled('some-feature');
15
16// still using the default strategy but this time with user id (which is the first to be used if present)
17$context = new UnleashContext(currentUserId: 'some-user-id');
18$unleash->isEnabled('some-feature', $context);
19
20// let's start the session to ensure the session id is used
21session_start();
22$unleash->isEnabled('some-feature');
23
24// or you can provide your own session id
25$context = new UnleashContext(sessionId: 'sess-123456');
26$unleash->isEnabled('some-feature', $context);
27
28// assume the feature is set to use the user id, the first call returns false (no context given), the second
29// one returns true/false based on the user id
30$unleash->isEnabled('some-feature');
31$context = new UnleashContext(currentUserId: 'some-user-id');
32$unleash->isEnabled('some-feature', $context);
33
34// the same goes for session, assume the session isn't started yet and the feature is set to use the session type
35$unleash->isEnabled('some-feature'); // returns false because no session is available
36
37$context = new UnleashContext(sessionId: 'some-session-id');
38$unleash->isEnabled('some-feature', $context); // works because you provided the session id manually
39
40session_start();
41$unleash->isEnabled('some-feature'); // works because the session is started
42
43// lastly you can force the feature to use the random type which always works
44$unleash->isEnabled('some-feature');

Hostname Strategy

This strategy allows you to match against a list of server hostnames (which are not the same as http hostnames).

If you don’t specify a hostname in context, it defaults to the current hostname using gethostname().

1<?php
2
3use Unleash\Client\UnleashBuilder;
4use Unleash\Client\Configuration\UnleashContext;
5
6$unleash = UnleashBuilder::create()
7 ->withAppName('Some app name')
8 ->withAppUrl('https://some-app-url.com')
9 ->withInstanceId('Some instance id')
10 ->build();
11
12// context with custom hostname
13$context = new UnleashContext(hostname: 'My-Cool-Hostname');
14$enabled = $unleash->isEnabled('some-feature', $context);
15
16// without custom hostname, defaults to gethostname() result or null
17$enabled = $unleash->isEnabled('some-feature');

This library also implements some deprecated strategies, namely gradualRolloutRandom, gradualRolloutSessionId and gradualRolloutUserId which all alias to the Gradual rollout strategy.

Context Provider

Manually creating relevant context can get tiring real fast. Luckily you can create your own context provider that will do it for you!

1<?php
2
3use Unleash\Client\ContextProvider\UnleashContextProvider;
4use Unleash\Client\Configuration\UnleashContext;
5use Unleash\Client\UnleashBuilder;
6
7final class MyContextProvider implements UnleashContextProvider
8{
9 public function getContext(): Context
10 {
11 $context = new UnleashContext();
12 $context->setCurrentUserId('user id from my app');
13
14 return $context;
15 }
16}
17
18$unleash = UnleashBuilder::create()
19 ->withAppName('Some app name')
20 ->withAppUrl('https://some-app-url.com')
21 ->withInstanceId('Some instance id')
22 // here we set the custom provider
23 ->withContextProvider(new MyContextProvider())
24 ->build();
25
26if ($unleash->isEnabled('someFeature')) { // this call will use your context provider with the provided user id
27
28}

Custom Strategies

To implement your own strategy you need to create a class implementing StrategyHandler (or AbstractStrategyHandler which contains some useful methods). Then you need to instruct the builder to use your custom strategy.

1<?php
2
3use Unleash\Client\Strategy\AbstractStrategyHandler;
4use Unleash\Client\DTO\Strategy;
5use Unleash\Client\Configuration\Context;
6use Unleash\Client\Strategy\DefaultStrategyHandler;
7
8class AprilFoolsStrategy extends AbstractStrategyHandler
9{
10 public function __construct(private DefaultStrategyHandler $original)
11 {
12 }
13
14 public function getStrategyName() : string
15 {
16 return 'aprilFools';
17 }
18
19 public function isEnabled(Strategy $strategy, Context $context) : bool
20 {
21 $date = new DateTimeImmutable();
22 if ((int) $date->format('n') === 4 && (int) $date->format('j') === 1) {
23 return (bool) random_int(0, 1);
24 }
25
26 return $this->original->isEnabled($strategy, $context);
27 }
28}

Now you must instruct the builder to use your new strategy

1<?php
2
3use Unleash\Client\UnleashBuilder;
4use Unleash\Client\Strategy\IpAddressStrategyHandler;
5
6$unleash = UnleashBuilder::create()
7 ->withAppName('Some app name')
8 ->withAppUrl('https://some-app-url.com')
9 ->withInstanceId('Some instance id')
10 ->withStrategy(new AprilFoolsStrategy()) // this will append your strategy to the existing list
11 ->build();
12
13// if you want to replace all strategies, use withStrategies() instead
14
15$unleash = UnleashBuilder::create()
16 ->withAppName('Some app name')
17 ->withAppUrl('https://some-app-url.com')
18 ->withInstanceId('Some instance id')
19 ->withStrategies(new AprilFoolsStrategy(), new IpAddressStrategyHandler())
20 // now the unleash object will have only the two strategies
21 ->build();

Variants

You can use multiple variants of one feature, for example for A/B testing. If no variant matches or the feature doesn’t have any variants, a default one will be returned which returns false for isEnabled(). You can also provide your own default variant.

Variant may or may not contain a payload.

1<?php
2
3use Unleash\Client\DTO\DefaultVariant;
4use Unleash\Client\UnleashBuilder;
5use Unleash\Client\Configuration\UnleashContext;
6use Unleash\Client\Enum\VariantPayloadType;
7use Unleash\Client\DTO\DefaultVariantPayload;
8
9$unleash = UnleashBuilder::create()
10 ->withAppName('Some app name')
11 ->withAppUrl('https://some-app-url.com')
12 ->withInstanceId('Some instance id')
13 ->build();
14
15$variant = $unleash->getVariant('nonexistentFeature');
16assert($variant->isEnabled() === false);
17
18// getVariant() does isEnabled() call in the background meaning that it will return the default falsy variant
19// whenever isEnabled() returns false
20$variant = $unleash->getVariant('existingFeatureThatThisUserDoesNotHaveAccessTo');
21assert($variant->isEnabled() === false);
22
23$variant = $unleash->getVariant('someFeature', new UnleashContext(currentUserId: '123'));
24if ($variant->isEnabled()) {
25 $payload = $variant->getPayload();
26 if ($payload !== null) {
27 if ($payload->getType() === VariantPayloadType::JSON) {
28 $jsonData = $payload->fromJson();
29 }
30 $stringPayload = $payload->getValue();
31 }
32}
33
34// providing custom default variant
35
36$variant = $unleash->getVariant('nonexistentFeature', fallbackVariant: new DefaultVariant(
37 'variantName',
38 enabled: true,
39 payload: new DefaultVariantPayload(VariantPayloadType::STRING, 'somePayload'),
40));
41assert($variant->getPayload()->getValue() === 'somePayload');

Client Registration

By default, the library automatically registers itself as an application in the Unleash server. If you want to prevent this, use withAutomaticRegistrationEnabled(false) in the builder.

1<?php
2
3use Unleash\Client\UnleashBuilder;
4
5$unleash = UnleashBuilder::create()
6 ->withAppName('Some App Name')
7 ->withAppUrl('https://somewhere.com')
8 ->withInstanceId('some-instance-id')
9 ->withAutomaticRegistrationEnabled(false)
10 ->build();
11
12// even though the client will not attempt to register, you can still use isEnabled()
13$unleash->isEnabled('someFeature');
14
15// if you want to register manually
16$unleash->register();
17
18// you can call the register method multiple times, the Unleash server doesn't mind
19$unleash->register();
20$unleash->register();

Metrics

By default, this library sends metrics which are simple statistics about whether user was granted access or not.

The metrics will be bundled and sent once the bundle created time crosses the configured threshold. By default this threshold is 30,000 milliseconds (30 seconds) meaning that when a new bundle gets created it won’t be sent sooner than in 30 seconds. That doesn’t mean it’s guaranteed that the metrics will be sent every 30 seconds, it only guarantees that the metrics won’t be sent sooner.

Example:

  1. user visits your site and this sdk gets triggered, no metric has been sent
  2. after five seconds user visits another page where again this sdk gets triggered, no metric sent
  3. user waits one minute before doing anything, no one else is accessing your site
  4. after one minute user visits another page, the metrics have been sent to the Unleash server

In the example above the metric bundle gets sent after 1 minute and 5 seconds because there was no one to trigger the code.

1<?php
2
3use Unleash\Client\UnleashBuilder;
4
5$unleash = UnleashBuilder::create()
6 ->withAppName('Some App Name')
7 ->withAppUrl('https://somewhere.com')
8 ->withInstanceId('some-instance-id')
9 ->withMetricsEnabled(false) // turn off metric sending
10 ->withMetricsEnabled(true) // turn on metric sending
11 ->withMetricsInterval(60_000) // interval in milliseconds (60 seconds)
12 ->withMetricsCacheHandler(new Psr16Cache(new RedisAdapter())) // use custom cache handler for metrics, defaults to standard cache handler
13 ->build();
14
15// the metric will be collected but not sent immediately
16$unleash->isEnabled('test');
17sleep(10);
18// now the metrics will get sent
19$unleash->isEnabled('test');

Custom Headers via Middleware

While middlewares for http client are not natively supported by this SDK, you can pass your own http client which supports them.

The most popular http client, guzzle, supports them out of the box and here’s an example of how to pass custom headers automatically (for more information visit official guzzle documentation on middlewares):

1<?php
2
3use GuzzleHttp\Client;
4use GuzzleHttp\Handler\CurlHandler;
5use GuzzleHttp\HandlerStack;
6use GuzzleHttp\Middleware;
7use Psr\Http\Message\RequestInterface;
8use Unleash\Client\UnleashBuilder;
9
10// any callable is valid, it may be a function reference, anonymous function or an invokable class
11
12// example invokable class
13final class AddHeaderMiddleware
14{
15 public function __construct(
16 private readonly string $headerName,
17 private readonly string $value,
18 ) {
19 }
20
21 public function __invoke(RequestInterface $request): RequestInterface
22 {
23 return $request->withHeader($this->headerName, $this->value);
24 }
25}
26
27// example anonymous function
28$addHeaderMiddleware = fn (string $headerName, string $headerValue)
29 => fn(RequestInterface $request)
30 => $request->withHeader($headerName, $headerValue);
31
32// create a handler stack that holds information about all middlewares
33$stack = HandlerStack::create(new CurlHandler());
34// mapRequest is a helper that simplifies modifying request
35$stack->push(Middleware::mapRequest(new AddHeaderMiddleware('X-My-Header', 'Some-Value')));
36// or with lambda
37$stack->push(Middleware::mapRequest($addHeaderMiddleware('X-My-Header2', 'Some-Value')));
38// assign the stack with middlewares as a handler
39$httpClient = new Client(['handler' => $stack]);
40
41$unleash = UnleashBuilder::create()
42 ->withHttpClient($httpClient) // assign the custom http client
43 ->withAppName('My-App')
44 ->withInstanceId('My-Instance')
45 ->withAppUrl('http://localhost:4242')
46 ->build();
47
48// now every http request will have X-My-Header header with value Some-Value
49$unleash->isEnabled('some-feature');

Constraints

Constraints are supported by this SDK and will be handled correctly by Unleash::isEnabled() if present.

GitLab Specifics

  • In GitLab you have to use the provided instance id, you cannot create your own.
  • No authorization header is necessary.
  • Instead of app name you need to specify the GitLab environment.
    • For this purpose you can use withGitlabEnvironment() method in builder, it’s an alias to withAppName() but communicates the intent better.
  • GitLab doesn’t use registration system, you can set the SDK to disable automatic registration and save one http call.
  • GitLab doesn’t read metrics, you can set the SDK to disable sending them and save some http calls.
1<?php
2
3use Unleash\Client\UnleashBuilder;
4
5$gitlabUnleash = UnleashBuilder::createForGitlab()
6 ->withInstanceId('H9sU9yVHVAiWFiLsH2Mo') // generated in GitLab
7 ->withAppUrl('https://git.example.com/api/v4/feature_flags/unleash/1')
8 ->withGitlabEnvironment('Production')
9 ->build();
10
11// the above is equivalent to
12$gitlabUnleash = UnleashBuilder::create()
13 ->withInstanceId('H9sU9yVHVAiWFiLsH2Mo')
14 ->withAppUrl('https://git.example.com/api/v4/feature_flags/unleash/1')
15 ->withGitlabEnvironment('Production')
16 ->withAutomaticRegistrationEnabled(false)
17 ->withMetricsEnabled(false)
18 ->build();

Check out our guide for more information on how to build and scale feature flag systems.