Download the PHP package unleash/client without Composer

On this page you can find all versions of the php package unleash/client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package client

Tests Tests (8.x) Tests (7.x) Coverage Status Download

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:

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.

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:

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):

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

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

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:

Required parameters

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

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

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

Optional parameters

Some optional parameters can be set, these include:

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.

Proxy SDK

By default the SDK uses the Server-side endpoints on the Unleash API. You can also use the Proxy SDK, which is a lightweight SDK that uses the Client-side 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:

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:

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 30 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):

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.

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:

These correspond to bootstrap providers:

Examples of bootstraps:

Using bootstrap providers directly:

Using multiple bootstrap providers:

Custom bootstrap provider

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

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.

Tip: 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.

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

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.

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

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().

Note: 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!

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.

Now you must instruct the builder to use your new strategy

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.

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.

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.

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):

Constraints

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

GitLab specifics

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


All versions of client with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
ext-json Version *
psr/http-client Version ^1.0
psr/http-client-implementation Version ^1.0
psr/http-factory Version ^1.0
psr/http-factory-implementation Version ^1.0
psr/simple-cache Version ^1.0 | ^2.0 | ^3.0
psr/simple-cache-implementation Version ^1.0 | ^2.0 | ^3.0
lastguest/murmurhash Version ^2.1
psr/http-message Version ^1.0 | ^2.0
php-http/discovery Version ^1.14
symfony/event-dispatcher Version ^5.0 | ^6.0 | ^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package unleash/client contains the following files

Loading the files please wait ....