PHPackages                             zumba/swivel - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. zumba/swivel

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

zumba/swivel
============

Strategy driven feature toggles

v5.0.0(2y ago)209135.7k—2.9%8[1 issues](https://github.com/zumba/swivel/issues)5MITPHPPHP ^8.0CI failing

Since Jan 23Pushed 2y ago45 watchersCompare

[ Source](https://github.com/zumba/swivel)[ Packagist](https://packagist.org/packages/zumba/swivel)[ Docs](http://tech.zumba.com)[ RSS](/packages/zumba-swivel/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (3)Versions (26)Used By (5)

Zumba ***Swivel***
==================

[](#zumba-swivel)

[![Build Status](https://camo.githubusercontent.com/3e5bd3d970b2870dee0f6df90e9d47f30a18d1fe70b6de7c27af53bbd2c55b79/68747470733a2f2f7472617669732d63692e6f72672f7a756d62612f73776976656c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/zumba/swivel)[![Coverage Status](https://camo.githubusercontent.com/02f4c889aef68d72fae9cd1f04b86c573681936ba68822f112d9835dec612493/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f7a756d62612f73776976656c2f62616467652e737667)](https://coveralls.io/r/zumba/swivel)

***Swivel*** is a fresh spin on an old idea: [Feature Flags](http://en.wikipedia.org/wiki/Feature_toggle) (toggles, bits, switches, etc.).

Typical Feature Flags are *all* or *nothing*: either the feature is on for everyone, or it is off for everyone.

```
// Old School Feature Flag

if ($flagIsOn) {
    // Do something new
} else {
    // Do something old
}
```

Typical Feature Flags are based on boolean conditionals with few abstractions (if this, then that). Although powerful in their simplicity, this typically leads to increased [cyclomatic complexity](http://en.wikipedia.org/wiki/Cyclomatic_complexity) and eventual technical debt.

Swivel is Different
-------------------

[](#swivel-is-different)

***Swivel*** is fundamentally different from Typical Feature Flags in two ways:

- Features can be enabled for a subset of an application's users.
- Features are not simple conditionals; a developer defines one or more strategies (behaviors) and ***Swivel*** takes care of determining which strategy to use.

### Buckets

[](#buckets)

With ***Swivel***, users are separated into one of ten "buckets," allowing a feature to be enabled for a subset of users. The advantages of this approach are clear:

- Deploying a new feature to 10% of users enables developers to catch unforeseen bugs/problems with new code without negatively affecting all users. These kind of deployments are called [Canary Releases](http://martinfowler.com/bliki/CanaryRelease.html). As soon as it is determined that new code is safe, roll out the new feature to more users in increments (30%, 50%, etc.); eventually the feature can enabled for all users, safely.
- A/B testing becomes a breeze. Imagine running up to 9 versions of a new feature with one group kept in reserve as a control. Is feature "A" negatively affecting revenue metrics for 10% of your users? No problem: turn it off and go with version "B" instead. This is easy to do with ***Swivel***.

### Behaviors

[](#behaviors)

Agile code needs to be simple and easy to change. Typical Feature Flags allow developers to quickly iterate when business rules change or new features are implemented, but this can often lead to complex, under engineered, brittle blocks of code.

***Swivel*** encourages the developer to implement changes to business logic as independent, high level *strategies* rather than simple, low level deviations.

Example: Quick Look
-------------------

[](#example-quick-look)

```
$formula = $swivel->forFeature('AwesomeSauce')
    ->addBehavior('formulaSpicy', [$this, 'getNewSpicyFormula'])
    ->addBehavior('formulaSaucy', [$this, 'getNewSaucyFormula'])
    ->defaultBehavior([$this, 'getFormula'])
    ->execute();
```

Getting Started
---------------

[](#getting-started)

The first thing you'll want to do is generate a random number between 1 and 10 for each user in your application. We call this the user's "bucket" index. This is what is used by ***Swivel*** to determine which features are enabled for that user.

**Note:** As a best practice, once a user is assigned to a bucket they should remain in that bucket forever. You'll want to store this value in a session or cookie like you would other basic user info.

Next, you'll need to create a map of features. This map indicates which buckets should have certain features enabled. Here is an example of a simple feature map:

```
$map = [
    // This is a parent feature slug, arbitrarily named "Payment."
    // The "Payment" feature is enabled for users in buckets 4, 5, and 6
    'Payment' => [4,5,6],

    // This is a behavior slug.  It is a subset of the parent slug,
    // and it is only enabled for users in buckets 4 and 5
    'Payment.Test' => [4, 5],

    // Behavior slugs can be nested.
    // This one is only enabled for users in bucket 5.
    'Payment.Test.VersionA' => [5]
];
```

When your application starts, configure ***Swivel*** and create a new manager instance:

```
// Get this value from the session or from persistent storage.
$userBucket = 5; // $_SESSION['bucket'];

// Get the feature map data from persistent storage.
$mapData = [ 'Feature' => [4,5,6], 'Feature.Test' => [4,5] ];

// Make a new configuration object
$config = new \Zumba\Swivel\Config($mapData, $userBucket);

// Make a new Swivel Manager.  This is your primary API to the Swivel library.
$swivel = new \Zumba\Swivel\Manager($config);
```

Way to go! ***Swivel*** is now ready to use.

### Using Strategies

[](#using-strategies)

Now that you have a new ***Swivel*** manager created, you can start using it in your application. To use ***Swivel*** you need to define behaviors for features of your code; ***Swivel*** will decide which behavior to execute based on the current user's bucket and the feature map you loaded in the configuration step.

#### Strategy Example

[](#strategy-example)

Say you have coded a new search algorithm for your website. The Search feature of your site is integral to your business, so you only want to roll out the new algorithm to 10% of your users at first. You decide to only enable the algorithm for users in bucket `5`. You configure ***Swivel*** and register it with your application:

```
$map = [ 'Search' => [5], 'Search.NewAlgorithm' => [5] ];
$config = new \Zumba\Swivel\Config($map, $_SESSION['bucketIndex']);
$swivel = new \Zumba\Swivel\Manager($config);

// ServiceLocator is fictional in this example.  Use your own framework or repository to store the
// swivel instance.
ServiceLocator::add('Swivel', $swivel);
```

In your code to search the site, you define two distinct strategies, and tell ***Swivel*** about them:

```
public function search($params = []) {
    $swivel = ServiceLocator::get('Swivel');
    return $swivel->forFeature('Search')
        ->addBehavior('NewAlgorithm', [$this, 'awesomeSearch'], [$params])
        ->defaultBehavior([$this, 'normalSearch'], [$params])
        ->execute();
}

protected function normalSearch($params) {
    // Tried and True method.
}

protected function awesomeSearch($params) {
    // Super cool new search method.
}
```

Now, when you call `search`, ***Swivel*** will execute `awesomeSearch` for users in bucket `5`, and `normalSearch` for all other users.

***Swivel*** API
----------------

[](#swivel-api)

### Zumba\\Swivel\\Config

[](#zumbaswivelconfig)

#### constructor($map, $index, $logger)

[](#constructormap-index-logger)

Used to configure your ***Swivel*** Manager instance.

ParamTypeDetails**$map**
*(optional)**mixed*Can be one of the following:
- `array` — an array of feature/behavior slugs as keys and enabled buckets as values.
- `\Zumba\Swivel\MapInterface` — An instance of a configured feature map.
- `\Zumba\Swivel\DriverInterface` — An instance of a ***Swivel*** driver that will build a `MapInterface` object.

**$index**
*(optional)**integer*The user's predefined bucket index. A number between 1 and 10.**$logger**
*(optional)*`LoggerInterface`An optional logger that implements `\Psr\Log\LoggerInterface`##### Examples

[](#examples)

```
$features = [ 'Feature' => [1,2,3] ];
$bucket = 3;

// array
$config = new \Zumba\Swivel\Config($features, $bucket);

// \Zumba\Swivel\Map
$config = new \Zumba\Swivel\Config(new \Zumba\Swivel\Map($features), $bucket);

 // $driver implements \Zumba\Swivel\DriverInterface
$config = new \Zumba\Swivel\Config($driver, $bucket);
```

### Zumba\\Swivel\\Manager

[](#zumbaswivelmanager)

#### constructor($config)

[](#constructorconfig)

This is the primary ***Swivel*** object that you will use in your app.

ParamTypeDetails**$config**`Config`A `\Zumba\Swivel\Config` instance.##### Examples

[](#examples-1)

```
$config = new \Zumba\Swivel\Config($features, $bucket);
$swivel = new \Zumba\Swivel\Manager($config);
```

#### forFeature($slug)

[](#forfeatureslug)

Create a point of deviation in your code. Returns a new `Zumba\Swivel\Builder` that accepts multiple behaviors, default behaviors, and executes the appropriate code for the user's bucket.

ParamTypeDetails**$slug***string*The first section of a feature map slug. i.e., for the feature slug `"Test.Version.Two"`, the `$slug` would be `"Test"`##### Examples

[](#examples-2)

```
$builder = $swivel->forFeature('Test');
```

#### invoke($slug, $a, $b)

[](#invokeslug-a-b)

Shorthand syntactic sugar for invoking a simple feature behavior. Useful for ternary style code.

ParamTypeDetails**$slug***string*The first section of a feature map slug. i.e., for the feature slug `"Test.Version.Two"`, the `$slug` would be `"Test"`**$a***callable*The strategy to execute if the `$slug` is enabled for the user's bucket.**$b**
*(optional)**callable*The strategy to execute if the `$slug` is not enabled for the user's bucket. If omitted, `invoke` will return `null` if the feature slug is not enabled.##### Examples

[](#examples-3)

```
// without Swivel
$result = $newSearch ? $this->search() : $this->noOp();

// Zumba\Swivel\Manager::invoke
$result = $swivel->invoke('Search.New', [$this, 'search'], [$this, 'noOp']);
$result = $swivel->invoke('Search.New', [$this, 'search']);
```

#### returnValue($slug, $a, $b)

[](#returnvalueslug-a-b)

Shorthand syntactic sugar for invoking a simple feature behavior using `Builder::addValue`. Useful for ternary style code.

ParamTypeDetails**$slug***string*The first section of a feature map slug. i.e., for the feature slug `"Test.Version.Two"`, the `$slug` would be `"Test"`**$a***mixed*The value to return if the `$slug` is enabled for the user's bucket.**$b**
*(optional)**mixed*The value to return if the `$slug` is not enabled for the user's bucket. If omitted, `returnValue` will return `null` if the feature slug is not enabled.##### Examples

[](#examples-4)

```
// without Swivel
$result = $newSearch ? 'Everything' : null;

// Zumba\Swivel\Manager::returnValue
$result = $swivel->returnValue('Search.New', 'Everything', null);
$result = $swivel->returnValue('Search.New', 'Everything');
```

### Zumba\\Swivel\\Builder

[](#zumbaswivelbuilder)

The `Builder` API is the primary way that you will write ***Swivel*** code. You get a new instance of the `Builder` when you call `Manager::forFeature`.

#### addBehavior($slug, $strategy, $args)

[](#addbehaviorslug-strategy-args)

Lazily adds a behavior to this feature that will only be executed if the feature is enabled for the user's bucket.

ParamTypeDetails**$slug***string*The second section of a feature map slug. i.e., for the feature slug `"Test.Version.Two"`, the `$slug` here would be `"Version.Two"`**$strategy***callable*The strategy to execute if the `$slug` is enabled for the user's bucket. Since version 2.0.0 `$strategy` must be a callable. If you want to return a simple value, use `Builder::addValue` instead.**$args**
*(optional)**array*Parameters to pass to the `$strategy` callable if it is executed.##### Examples

[](#examples-5)

```
$builder = $swivel->forFeature('Test');

$builder
    // Inline function.  This one will return 'ab'
    ->addBehavior('versionA', function($a, $b) { return $a . $b; }, ['a', 'b'])

    // Callable.  Will return the result of $obj->someMethod('c', 'd');
    ->addBehavior('versionB', [$obj, 'someMethod'], ['c', 'd'])

     // Since version 2.0.0, this will throw a \LogicException.  Use `addValue` instead.
    ->addBehavior('versionC', 'result');
```

#### addValue($slug, $value)

[](#addvalueslug-value)

Lazily adds a behavior to this feature that will return the value provided. It will only be executed if the feature is enabled for the user's bucket.

ParamTypeDetails**$slug***string*The second section of a feature map slug. i.e., for the feature slug `"Test.Version.Two"`, the `$slug` here would be `"Version.Two"`**$value***mixed*The value to return if the `$slug` is enabled for the user's bucket. If `$value` is a callable it will not be executed. If you want ***Swivel*** to execute a callable, use `Builder::addBehavior` instead.##### Examples

[](#examples-6)

```
$builder = $swivel->forFeature('Test');

$builder
    // This will return `'result'`
    ->addValue('versionA', 'result')

    // Callable.  This will not be executed; Swivel will just return the unexecuted callable.
    ->addValue('versionB', [$obj, 'someMethod']);
```

#### defaultBehavior($strategy, $args)

[](#defaultbehaviorstrategy-args)

Lazily adds a behavior to this feature that will only be executed if no other feature behaviors are enabled for the user's bucket.

ParamTypeDetails**$strategy***callable*The strategy to execute if no other feature behaviors are enabled for the user's bucket. Since version 2.0.0 `$strategy` must be a callable. If you want to return a simple value, use `Builder::defaultValue` instead.**$args**
*(optional)**array*Parameters to pass to the `$strategy` callable if it is executed.##### Examples

[](#examples-7)

```
$swivel
    ->forFeature('Test');
    ->addBehavior('New.Version', [$this, 'someMethod'], $args)
    ->defaultBehavior([$this, 'defaultMethod'], $args);
```

#### defaultValue($value)

[](#defaultvaluevalue)

Lazily adds a behavior to this feature that will return the provided value. It will only be executed if no other feature behaviors are enabled for the user's bucket.

ParamTypeDetails**$value***mixed*The value to return if no other feature behaviors are enabled for the user's bucket. If you want ***Swivel*** to execute a callable, use `Builder::defaultBehavior` instead.##### Examples

[](#examples-8)

```
$swivel
    ->forFeature('Test');
    ->addBehavior('New.Version', [$this, 'someMethod'], $args)
    ->defaultValue('some default value');
```

#### execute()

[](#execute)

Executes the appropriate behavior strategy based on the user's bucket.

##### Examples

[](#examples-9)

```
// $result will contain either the result of
// $this->someMethod(1, 2, 3) or $this->defaultMethod('test')
// depending on the user's bucket
$result = $swivel
    ->forFeature('Test');
    ->addBehavior('New.Version', [$this, 'someMethod'], [1, 2, 3])
    ->defaultBehavior([$this, 'defaultMethod'], ['test'])
    ->execute();
```

#### noDefault()

[](#nodefault)

If you do not need to define a default behavior to be executed when a feature is not enabled for a user's bucket, call `noDefault` on the `Builder`. ***Swivel*** will throw a `\LogicException` if you neglect to define a default behavior and do not call `noDefault`. Likewise, ***Swivel*** will throw a `\LogicException` if you call both `noDefault` and `defaultBehavior` on the same `Builder` instance.

##### Examples

[](#examples-10)

```
$swivel
    ->forFeature('Test');
    ->addBehavior('A', [$obj, 'someMethod'])
    ->execute(); // throws \LogicException here.

$swivel
    ->forFeature('Test');
    ->addBehavior('A', [$obj, 'someMethod'])
    ->noDefault()
    ->execute(); // no exception thrown.

$swivel
    ->forFeature('Test');
    ->addBehavior('A', [$obj, 'someMethod'])
    ->defaultBehavior([$obj, 'anotherMethod'])
    ->noDefault() // throws \LogicException here.
    ->execute();
```

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity48

Moderate usage in the ecosystem

Community32

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 51.3% 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 ~140 days

Recently: every ~329 days

Total

25

Last Release

776d ago

Major Versions

v1.3.x-dev → v2.1.32017-09-18

v2.1.4 → v3.0.02018-11-20

v2.1.5 → v3.0.12019-07-15

v3.0.2 → v4.0.02021-05-17

v4.0.2 → v5.0.02024-04-03

PHP version history (4 changes)v0.0.1PHP &gt;=5.4

v2.1.4PHP &gt;=5.6

v4.0.0PHP &gt;=7.2

v5.0.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/26548?v=4)[Juan Basso](/maintainers/jrbasso)[@jrbasso](https://github.com/jrbasso)

![](https://avatars.githubusercontent.com/u/157755?v=4)[Chris Saylor](/maintainers/cjsaylor)[@cjsaylor](https://github.com/cjsaylor)

![](https://avatars.githubusercontent.com/u/497048?v=4)[Stephen Young](/maintainers/young-steveo)[@young-steveo](https://github.com/young-steveo)

---

Top Contributors

[![young-steveo](https://avatars.githubusercontent.com/u/497048?v=4)](https://github.com/young-steveo "young-steveo (60 commits)")[![jrbasso](https://avatars.githubusercontent.com/u/26548?v=4)](https://github.com/jrbasso "jrbasso (28 commits)")[![cjsaylor](https://avatars.githubusercontent.com/u/157755?v=4)](https://github.com/cjsaylor "cjsaylor (11 commits)")[![mglinski](https://avatars.githubusercontent.com/u/78872?v=4)](https://github.com/mglinski "mglinski (9 commits)")[![jonyo](https://avatars.githubusercontent.com/u/2767294?v=4)](https://github.com/jonyo "jonyo (3 commits)")[![ctrlrsf](https://avatars.githubusercontent.com/u/6004738?v=4)](https://github.com/ctrlrsf "ctrlrsf (3 commits)")[![DanaLuther](https://avatars.githubusercontent.com/u/285624?v=4)](https://github.com/DanaLuther "DanaLuther (2 commits)")[![dmatsinger](https://avatars.githubusercontent.com/u/1301894?v=4)](https://github.com/dmatsinger "dmatsinger (1 commits)")

---

Tags

featureflagtogglebitstrategy

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/zumba-swivel/health.svg)

```
[![Health](https://phpackages.com/badges/zumba-swivel/health.svg)](https://phpackages.com/packages/zumba-swivel)
```

###  Alternatives

[opensoft/rollout

Feature switches or flags for PHP

2571.8M5](/packages/opensoft-rollout)[qandidate/toggle

Feature toggling for your PHP application.

3812.0M9](/packages/qandidate-toggle)[francescomalatesta/laravel-feature

A simple package to manage feature flagging in a Laravel project.

211206.4k](/packages/francescomalatesta-laravel-feature)[flagception/flagception-bundle

Feature toggle bundle on steroids.

283.8M](/packages/flagception-flagception-bundle)[ajgarlag/feature-flag-bundle

Provides a feature flag mechanism

1417.8k](/packages/ajgarlag-feature-flag-bundle)[novaway/feature-flag-bundle

Very KISS bundle to manage features flag

24278.1k](/packages/novaway-feature-flag-bundle)

PHPackages © 2026

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