PHPackages                             rasuvaeff/yii3-feature-flags-db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. rasuvaeff/yii3-feature-flags-db

ActiveLibrary[Database &amp; ORM](/categories/database)

rasuvaeff/yii3-feature-flags-db
===============================

Database-backed feature flag provider for Yii3 applications

v1.0.1(3w ago)02BSD-3-ClausePHPPHP 8.3 - 8.5CI passing

Since Jun 14Pushed 1w agoCompare

[ Source](https://github.com/rasuvaeff/yii3-feature-flags-db)[ Packagist](https://packagist.org/packages/rasuvaeff/yii3-feature-flags-db)[ Docs](https://github.com/rasuvaeff/yii3-feature-flags-db)[ RSS](/packages/rasuvaeff-yii3-feature-flags-db/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (30)Versions (3)Used By (0)

rasuvaeff/yii3-feature-flags-db
===============================

[](#rasuvaeffyii3-feature-flags-db)

[![Stable Version](https://camo.githubusercontent.com/99ccc86b66bdbb9596ec1f25575bb5102e26e3278e57f27a238e810e4475a65e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7261737576616566662f796969332d666561747572652d666c6167732d64622e7376673f6c6162656c3d737461626c65)](https://packagist.org/packages/rasuvaeff/yii3-feature-flags-db)[![Total Downloads](https://camo.githubusercontent.com/69d44c30e94954a0ac5f8b4416131984f7bc7976cd9a514774f7ac3d2fb3006e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7261737576616566662f796969332d666561747572652d666c6167732d64622e737667)](https://packagist.org/packages/rasuvaeff/yii3-feature-flags-db)[![Build](https://camo.githubusercontent.com/9c3ae71db110c7e9ef59e4003e695ce58fc065a45394e27b0e9aff76d4579010/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d666561747572652d666c6167732d64622f6275696c642e796d6c3f6272616e63683d6d6173746572)](https://github.com/rasuvaeff/yii3-feature-flags-db/actions)[![Static Analysis](https://camo.githubusercontent.com/ccd11cd8f6ef386f72d73b28ba6715cff78265855dbc2c0954a84b26447cb0a4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d666561747572652d666c6167732d64622f7374617469632d616e616c797369732e796d6c3f6272616e63683d6d6173746572266c6162656c3d737461746963253230616e616c79736973)](https://github.com/rasuvaeff/yii3-feature-flags-db/actions)[![PHP](https://camo.githubusercontent.com/062c672b2640c323968cd23cc59467037e5e194b76c4c682797272e074e6aac8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7261737576616566662f796969332d666561747572652d666c6167732d64622f706870)](https://packagist.org/packages/rasuvaeff/yii3-feature-flags-db)[![License](https://camo.githubusercontent.com/931599221b01ac554f871d657e162fae5879ed7743f6fbfa0444a0cf499c0a1c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7261737576616566662f796969332d666561747572652d666c6167732d64622e737667)](LICENSE.md)[Русская версия](README.ru.md)

Database-backed feature flag provider for Yii3 applications. Implements the `FlagProvider` interface from `rasuvaeff/yii3-feature-flags` and reads flag configuration from a database table in a single query.

> Using an AI coding assistant? [llms.txt](llms.txt) contains a compact API reference you can ingest in your prompt context.

Requirements
------------

[](#requirements)

- PHP 8.3+
- `rasuvaeff/yii3-feature-flags` ^1.0
- `yiisoft/db` ^2.0
- `yiisoft/db-migration` ^2.0 (ships the table migration)
- `yiisoft/definitions` ^3.0 (DI `Reference` for `WritableFlagProvider`)
- a PSR-16 cache implementation — required transitively by `yiisoft/db` 2.0 (e.g. `yiisoft/cache`)

Installation
------------

[](#installation)

```
composer require rasuvaeff/yii3-feature-flags-db
```

With Yii3 config-plugin this package binds both `FlagProvider` and `WritableFlagProvider` to the same instance — do **not** also bind either key in your application or another backend, or `yiisoft/config` reports a `Duplicate key` error.

Database schema
---------------

[](#database-schema)

Create the `feature_flags` table (adjust types for your RDBMS):

```
CREATE TABLE feature_flags (
    name        VARCHAR(190) PRIMARY KEY,
    enabled     BOOLEAN      NOT NULL DEFAULT TRUE,
    salt        VARCHAR(190) NOT NULL DEFAULT '',
    rollout     SMALLINT     NOT NULL DEFAULT 100,
    kill_switch BOOLEAN      NOT NULL DEFAULT FALSE,
    environments TEXT        NOT NULL DEFAULT '[]'
);
```

ColumnTypeDefaultDescription`name``VARCHAR(190)` PK—Flag name (core regex: `/^[a-z][a-z0-9._-]*$/`)`enabled``BOOLEAN``true`Whether the flag is active`salt``VARCHAR(190)``''`Empty string falls back to flag name`rollout``SMALLINT``100`Percentage 0..100`kill_switch``BOOLEAN``false`Emergency off switch`environments``JSON`/`TEXT``'[]'`JSON array of strings### Migration

[](#migration)

The package ships a migration (`migrations/`) for [yiisoft/db-migration](https://github.com/yiisoft/db-migration). Register the source path in your app's `config/params.php`:

```
'yiisoft/db-migration' => [
    'sourcePaths' => [
        dirname(__DIR__) . '/vendor/rasuvaeff/yii3-feature-flags-db/migrations',
    ],
],
```

Then apply and revert it with Yii Console:

```
./yii migrate:up
./yii migrate:down --limit=1
```

The table name defaults to `feature_flags` and must match the `table` argument of `DbFlagProvider`. To use a custom name, bind the migration constructor argument:

```
M260605000000CreateFeatureFlagsTable::class => [
    '__construct()' => ['table' => 'my_feature_flags'],
],
```

Usage
-----

[](#usage)

### Basic DB provider

[](#basic-db-provider)

```
use Rasuvaeff\Yii3FeatureFlags\FeatureFlags;
use Rasuvaeff\Yii3FeatureFlagsDb\DbFlagProvider;

$provider = new DbFlagProvider(
    db: $connection,          // yiisoft/db ConnectionInterface
    table: 'feature_flags',   // optional, default is 'feature_flags'
);

$featureFlags = new FeatureFlags(provider: $provider);

if ($featureFlags->isEnabled('new-checkout')) {
    // new checkout flow
}
```

### With PSR-16 caching

[](#with-psr-16-caching)

```
use Rasuvaeff\Yii3FeatureFlagsDb\CachedFlagProvider;

$cached = new CachedFlagProvider(
    inner: $provider,
    cache: $psr16Cache,       // PSR-16 CacheInterface
    ttl: 60,                  // seconds
);

$featureFlags = new FeatureFlags(provider: $cached);
```

### Clear cache

[](#clear-cache)

```
$cached->clear();             // removes cached flags, next call reloads from DB
```

Writing flags
-------------

[](#writing-flags)

`DbFlagProvider` and `CachedFlagProvider` both implement `WritableFlagProvider`. Use them for programmatic CRUD or an admin UI.

```
use Rasuvaeff\Yii3FeatureFlags\Flag;
use Rasuvaeff\Yii3FeatureFlags\WritableFlagProvider;

/** @var WritableFlagProvider $provider */
$provider->save(flag: new Flag(
    name: 'new-checkout',
    enabled: true,
    rollout: 25,
    environments: ['production'],
));

$provider->remove(name: 'old-checkout');
```

- `save()` is an upsert keyed by `name` (insert or replace).
- `remove()` is idempotent: deleting a missing name is a no-op.
- `CachedFlagProvider` is write-through: after a successful `save()`/`remove()`it clears its cache before returning, so the next read reflects the change. When the inner provider is read-only (e.g. `ConfigFlagProvider`), write calls are silent no-ops — wrap a config provider safely without exceptions.
- Salt is normalized: `Flag::__construct()` replaces an empty salt with the flag name. On write the row stores `''` whenever `salt === name` so the round-trip read keeps the same invariant (`emptySaltFallsBackToName`).
- Environments are encoded through `FlagRowMapper::encodeEnvironments()` and decoded through `extractEnvironments()`. Round-trip is guaranteed.

### Writable DI binding

[](#writable-di-binding)

`config/di.php` binds `WritableFlagProvider` to the same instance as `FlagProvider` via `Yiisoft\Definitions\Reference`:

```
use Rasuvaeff\Yii3FeatureFlags\WritableFlagProvider;
use Yiisoft\Definitions\Reference;

return [
    // ...FlagProvider::class closure omitted for brevity...
    WritableFlagProvider::class => Reference::to(FlagProvider::class),
];
```

Inject `WritableFlagProvider` in write paths and `FlagProvider` in read paths; both resolve to the same object.

API reference
-------------

[](#api-reference)

ClassDescription`DbFlagProvider`Reads all flags from DB in one `SELECT *`; `implements WritableFlagProvider``CachedFlagProvider`PSR-16 decorator with write-through cache; `implements WritableFlagProvider``FlagRowMapper``@internal` row ↔ `Flag` mapper; also exposes `encodeEnvironments()``InvalidFlagRowException`Thrown when a DB row has invalid structureSecurity
--------

[](#security)

- Kill switch, rollout hash logic, and environment targeting remain in the core package — the DB adapter is only a configuration source.
- Invalid row data (missing columns, malformed JSON, wrong types, out-of-range rollout, invalid flag name) throws `InvalidFlagRowException` instead of silently enabling features. Core validation errors are wrapped, so callers only need to catch `InvalidFlagRowException`.
- No SQL injection risk: table name is quoted via yiisoft/db quoter.

Examples
--------

[](#examples)

See [examples/](examples/) for runnable scripts.

Development
-----------

[](#development)

```
composer build          # full gate: validate + normalize + cs + psalm + test
composer cs:fix         # auto-fix code style
composer psalm          # static analysis
composer test           # run tests
```

License
-------

[](#license)

BSD-3-Clause. See [LICENSE.md](LICENSE.md).

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance97

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~12 days

Total

2

Last Release

27d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0812d5572a7041dfe36e222d295b2e6dc55833a605350fcde58a51a5965ed30?d=identicon)[rasuvaeff](/maintainers/rasuvaeff)

---

Top Contributors

[![rasuvaeff](https://avatars.githubusercontent.com/u/1352718?v=4)](https://github.com/rasuvaeff "rasuvaeff (22 commits)")

---

Tags

databasefeature-flagsphpproviderpsr-15yii3databaseproviderdbyii3feature-flags

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rasuvaeff-yii3-feature-flags-db/health.svg)

```
[![Health](https://phpackages.com/badges/rasuvaeff-yii3-feature-flags-db/health.svg)](https://phpackages.com/packages/rasuvaeff-yii3-feature-flags-db)
```

###  Alternatives

[robmorgan/phinx

Phinx makes it ridiculously easy to manage the database migrations for your PHP app.

4.5k48.7M472](/packages/robmorgan-phinx)[spatie/laravel-translation-loader

Store your language lines in the database, yaml or other sources

8453.2M73](/packages/spatie-laravel-translation-loader)[kreait/firebase-bundle

Symfony Bundle for the Firebase Admin SDK

1555.1M2](/packages/kreait-firebase-bundle)[masom/lhm

Large Hadron Migrator for phinx

309.5k](/packages/masom-lhm)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
