ez-php / feature-flags
Simple feature flag evaluation for the ez-php framework — File, Database, Redis, and Array drivers with a static Flag facade
Requires
- php: ^8.5
- ez-php/contracts: ^2.0
Requires (Dev)
- ez-php/docker: ^2.0
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.0
Suggests
- ext-redis: Needed for Driver\RedisDriver
Provides
None
Conflicts
None
Replaces
None
- dev-main
- 2.5.6
- 2.5.5
- 2.5.4
- 2.5.3
- 2.5.1
- 2.5.0
- 2.4.11
- 2.4.10
- 2.4.9
- 2.4.8
- 2.4.7
- 2.4.6
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.9
- 2.3.8
- 2.3.7
- 2.3.6
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.14.0
- 1.13.1
- 1.13.0
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.0
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.1
This package is auto-updated.
Last update: 2026-09-30 19:50:23 UTC
README
Simple feature flag evaluation for the ez-php framework. No external service required — flags are stored in a PHP file, a database table, or a plain array.
Installation
composer require ez-php/feature-flags
Register the provider in provider/modules.php:
\EzPhp\FeatureFlags\FeatureFlagServiceProvider::class,
Usage
use EzPhp\FeatureFlags\Flag; if (Flag::enabled('new-checkout')) { // show new checkout flow } if (Flag::disabled('dark-mode')) { // show light theme } // Context-aware evaluation (e.g. per-user gradual rollouts) if (Flag::enabledFor('beta-search', $user->id)) { // show beta search to this user } if (Flag::disabledFor('new-ui', $user->id)) { // show legacy UI for this user } $all = Flag::all(); // ['new-checkout' => true, 'dark-mode' => false]
Configuration
Add a config/flags.php to your application:
return [ 'driver' => getenv('FLAGS_DRIVER') ?: 'file', // file | database | redis | array 'file' => getenv('FLAGS_FILE') ?: 'flags.php', 'redis' => [ 'host' => getenv('FLAGS_REDIS_HOST') ?: '127.0.0.1', 'port' => (int) (getenv('FLAGS_REDIS_PORT') ?: 6379), 'database' => (int) (getenv('FLAGS_REDIS_DATABASE') ?: 0), ], ];
filemust point outsideconfig/. The framework loads everyconfig/*.phpas a config namespace, so a definitions file atconfig/flags.phpwould be this config file — the driver would then reportdriverandfileas enabled flags and none of your real ones.
Drivers
| Driver | Config key value | Description |
|---|---|---|
file |
file |
Reads a PHP file returning array<string, bool> |
database |
database |
Reads a feature_flags table via PDO |
redis |
redis |
Reads feature_flags / feature_flags:contexts:<name> hashes via ext-redis |
array |
array |
Empty in-memory driver (CI / test environments) |
File driver
Create flags.php in your application root:
<?php return [ 'new-checkout' => true, 'dark-mode' => false, 'beta-search' => false, ];
Database driver
Create the feature_flags table (example migration):
$pdo->exec(' CREATE TABLE feature_flags ( name VARCHAR(255) NOT NULL PRIMARY KEY, enabled TINYINT(1) NOT NULL DEFAULT 0 ) ');
For per-context overrides (e.g. per-user beta rollouts), also create feature_flag_contexts:
$pdo->exec(' CREATE TABLE feature_flag_contexts ( name VARCHAR(255) NOT NULL, context_id VARCHAR(255) NOT NULL, enabled TINYINT(1) NOT NULL DEFAULT 0, PRIMARY KEY (name, context_id) ) ');
When enabledFor('flag', $userId) is called, the driver checks feature_flag_contexts first; if no matching row exists, it falls back to the global feature_flags value. A missing feature_flag_contexts table is silently treated as "no overrides" — no migration is required for basic use.
Insert flags directly via SQL or through your own admin interface:
INSERT INTO feature_flags (name, enabled) VALUES ('new-checkout', 1); INSERT INTO feature_flag_contexts (name, context_id, enabled) VALUES ('beta-search', '42', 1);
Redis driver
Same two-hash shape as the database driver's two tables:
$redis->hSet('feature_flags', 'new-checkout', '1'); $redis->hSet('feature_flags:contexts:beta-search', '42', '1'); // per-context override
enabledFor('beta-search', 42) checks the context hash first, then falls back to the
global feature_flags hash when no override exists — identical precedence to the
database driver.
Percentage rollouts
Release a flag to a stable slice of your users by adding rollouts to config/flags.php:
return [ 'driver' => 'file', 'file' => 'flags.php', 'rollouts' => [ 'new-checkout' => 25, // 25 % of contexts ], ];
Flag::enabledFor('new-checkout', $user->id); // same user → same answer, every time
The bucket is crc32(flag . '|' . id) % 100, so a user stays in when you raise the percentage (10 → 25 only adds users) and different flags pick different slices. A rollout replaces the stored value for that flag; Flag::enabled('new-checkout') (no context) is only true at 100. Flags without a rollout behave as before.
Behaviour
- Unknown flags default to
false—Flag::enabled('unknown')never throws. - Drivers never throw — all query/file failures are caught internally and result in
false. - The
Flagfacade throwsRuntimeExceptionwhen called beforeFeatureFlagServiceProviderhas been booted — this is a programmer error, not a runtime failure.
Testing
docker compose exec app composer test
Most tests run without external infrastructure (SQLite :memory: for the database driver, temp files for the file driver). RedisDriverTest requires a live Redis instance and is skipped automatically when ext-redis isn't loaded.
License
MIT