Download the PHP package featurevisor/featurevisor-php without Composer
On this page you can find all versions of the php package featurevisor/featurevisor-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download featurevisor/featurevisor-php
More information about featurevisor/featurevisor-php
Files in featurevisor/featurevisor-php
Package featurevisor-php
Short Description Featurevisor SDK for PHP
License MIT
Homepage https://featurevisor.com
Informations about the package featurevisor-php
Featurevisor PHP SDK
This is a port of Featurevisor Javascript SDK v2.x to PHP, providing a way to evaluate feature flags, variations, and variables in your PHP applications.
This SDK is compatible with Featurevisor v2.0 projects and above.
Table of contents
- Installation
- Initialization
- Evaluation types
- Context
- Setting initial context
- Setting after initialization
- Replacing existing context
- Manually passing context
- Check if enabled
- Getting variation
- Getting variables
- Type specific methods
- Getting all evaluations
- Sticky
- Initialize with sticky
- Set sticky afterwards
- Setting datafile
- Updating datafile
- Interval-based update
- Logging
- Levels
- Customizing levels
- Handler
- Events
datafile_set
context_set
sticky_set
- Evaluation details
- Hooks
- Defining a hook
- Registering hooks
- Child instance
- Close
- CLI usage
- Test
- Benchmark
- Assess distribution
- Development of this package
- Setting up
- Running tests
- Releasing
- License
Installation
In your PHP application, install the SDK using Composer:
Initialization
The SDK can be initialized by passing datafile content directly:
Evaluation types
We can evaluate 3 types of values against a particular feature:
- Flag (
boolean
): whether the feature is enabled or not - Variation (
string
): the variation of the feature (if any) - Variables: variable values of the feature (if any)
These evaluations are run against the provided context.
Context
Contexts are attribute values that we pass to SDK for evaluating features against.
Think of the conditions that you define in your segments, which are used in your feature's rules.
They are plain objects:
Context can be passed to SDK instance in various different ways, depending on your needs:
Setting initial context
You can set context at the time of initialization:
This is useful for values that don't change too frequently and available at the time of application startup.
Setting after initialization
You can also set more context after the SDK has been initialized:
This will merge the new context with the existing one (if already set).
Replacing existing context
If you wish to fully replace the existing context, you can pass true
in second argument:
Manually passing context
You can optionally pass additional context manually for each and every evaluation separately, without needing to set it to the SDK instance affecting all evaluations:
When manually passing context, it will merge with existing context set to the SDK instance before evaluating the specific value.
Further details for each evaluation types are described below.
Check if enabled
Once the SDK is initialized, you can check if a feature is enabled or not:
You can also pass additional context per evaluation:
Getting variation
If your feature has any variations defined, you can evaluate them as follows:
Additional context per evaluation can also be passed:
Getting variables
Your features may also include variables, which can be evaluated as follows:
Additional context per evaluation can also be passed:
Type specific methods
Next to generic getVariable()
methods, there are also type specific methods available for convenience:
Getting all evaluations
You can get evaluations of all features available in the SDK instance:
This is handy especially when you want to pass all evaluations from a backend application to the frontend.
Sticky
For the lifecycle of the SDK instance in your application, you can set some features with sticky values, meaning that they will not be evaluated against the fetched datafile:
Initialize with sticky
Once initialized with sticky features, the SDK will look for values there first before evaluating the targeting conditions and going through the bucketing process.
Set sticky afterwards
You can also set sticky features after the SDK is initialized:
Setting datafile
You may also initialize the SDK without passing datafile
, and set it later on:
Updating datafile
You can set the datafile as many times as you want in your application, which will result in emitting a datafile_set
event that you can listen and react to accordingly.
The triggers for setting the datafile again can be:
- periodic updates based on an interval (like every 5 minutes), or
- reacting to:
- a specific event in your application (like a user action), or
- an event served via websocket or server-sent events (SSE)
Interval-based update
Here's an example of using interval-based update:
@TODO
Logging
By default, Featurevisor SDKs will print out logs to the console for info
level and above.
Levels
These are all the available log levels:
error
warn
info
debug
Customizing levels
If you choose debug
level to make the logs more verbose, you can set it at the time of SDK initialization.
Setting debug
level will print out all logs, including info
, warn
, and error
levels.
Alternatively, you can also set logLevel
directly:
You can also set log level from SDK instance afterwards:
Handler
You can also pass your own log handler, if you do not wish to print the logs to the console:
Further log levels like info
and debug
will help you understand how the feature variations and variables are evaluated in the runtime against given context.
Events
Featurevisor SDK implements a simple event emitter that allows you to listen to events that happen in the runtime.
You can listen to these events that can occur at various stages in your application:
@TODO: verify these events
datafile_set
The features
array will contain keys of features that have either been:
- added, or
- updated, or
- removed
compared to the previous datafile content that existed in the SDK instance.
context_set
sticky_set
Evaluation details
Besides logging with debug level enabled, you can also get more details about how the feature variations and variables are evaluated in the runtime against given context:
The returned object will always contain the following properties:
featureKey
: the feature keyreason
: the reason how the value was evaluated
And optionally these properties depending on whether you are evaluating a feature variation or a variable:
bucketValue
: the bucket value between 0 and 100,000ruleKey
: the rule keyerror
: the error objectenabled
: if feature itself is enabled or notvariation
: the variation objectvariationValue
: the variation valuevariableKey
: the variable keyvariableValue
: the variable valuevariableSchema
: the variable schema
Hooks
Hooks allow you to intercept the evaluation process and customize it further as per your needs.
Defining a hook
A hook is a simple object with a unique required name
and optional functions:
Registering hooks
You can register hooks at the time of SDK initialization:
Or after initialization:
Child instance
When dealing with purely client-side applications, it is understandable that there is only one user involved, like in browser or mobile applications.
But when using Featurevisor SDK in server-side applications, where a single server instance can handle multiple user requests simultaneously, it is important to isolate the context for each request.
That's where child instances come in handy:
Now you can pass the child instance where your individual request is being handled, and you can continue to evaluate features targeting that specific user alone:
Similar to parent SDK, child instances also support several additional methods:
setContext
setSticky
isEnabled
getVariation
getVariable
getVariableBoolean
getVariableString
getVariableInteger
getVariableDouble
getVariableArray
getVariableObject
getVariableJSON
getAllEvaluations
on
close
Close
Both primary and child instances support a .close()
method, that removes forgotten event listeners (via on
method) and cleans up any potential memory leaks.
CLI usage
This package also provides a CLI tool for running your Featurevisor project's test specs and benchmarking against this PHP SDK:
Test
Learn more about testing here.
Additional options that are available:
Benchmark
Learn more about benchmarking here.
Assess distribution
Learn more about assessing distribution here.
Development of this package
Setting up
Clone the repository, and install the dependencies using Composer:
Running tests
Releasing
- Manually create a new release on GitHub
- Tag it with a prefix of
v
, likev1.0.0
- GitHub Actions is set up to automatically notify Packagist about the new release
License
MIT © Fahad Heylaal