PHPackages                             antfroger/progressive-bundle - 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. antfroger/progressive-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

antfroger/progressive-bundle
============================

The bundle to load Progressive, the library to progressively, quickly and simply enable new features

v1.1(4y ago)1111[1 PRs](https://github.com/antfroger/progressive-bundle/pulls)MITPHPPHP ^7.3 || ^8.0

Since Jan 18Pushed 2y ago1 watchersCompare

[ Source](https://github.com/antfroger/progressive-bundle)[ Packagist](https://packagist.org/packages/antfroger/progressive-bundle)[ Docs](https://github.com/antfroger/progressive-bundle)[ RSS](/packages/antfroger-progressive-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (10)Versions (3)Used By (0)

Progressive Bundle
==================

[](#progressive-bundle)

Symfony integration for the feature-flag library [Progressive](https://github.com/antfroger/progressive)

[![Build Status](https://github.com/antfroger/progressive-bundle/workflows/CI/badge.svg)](https://github.com/antfroger/progressive-bundle)[![Latest stable version](https://camo.githubusercontent.com/1edce4830058d2810b02c11ce4aeaf5f357e1edf408fab99ac7eb9568777ec5c/68747470733a2f2f706f7365722e707567782e6f72672f616e7466726f6765722f70726f67726573736976652d62756e646c652f76)](https://packagist.org/packages/antfroger/progressive-bundle "Latest stable version")

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

[](#installation)

Install this bundle using Composer:

```
composer require antfroger/progressive-bundle
```

Configuration
-------------

[](#configuration)

### 1. Enable the Bundle

[](#1-enable-the-bundle)

First, enable the bundle by adding it to the list of registered bundles in the `config/bundles.php` file of your project:

```
// config/bundles.php

return [
    // ...
    Af\ProgressiveBundle\AfProgressiveBundle::class => ['all' => true],
];
```

### 2. Configure the Bundle

[](#2-configure-the-bundle)

Create the configuration file `config/packages/af_progressive.yaml`.

The only required key is `config`. The key needs the path of the yaml file where you will configure the features of your application.
The minimal configuration looks like this:

```
# config/packages/af_progressive.yaml
af_progressive:
  config: '%kernel.project_dir%/config/features.yaml'
```

But, you can also define under the `context` key, variables that will be stored in the [Contex object](https://github.com/antfroger/progressive#context-object).

```
# config/packages/af_progressive.yaml
af_progressive:
  config: '%kernel.project_dir%/config/features.yaml'
  context:
    env: '%kernel.environment%'
```

Then, you need to create the file that will contain your features.
It must contain at least the `features` key:

```
# config/features.yaml
features: []
```

But quickly, you can start adding many more features:
*(every time you change this file, you may need to clear the cache og your application: `php bin/console cache:clear`)*

```
# config/features.yaml
features:
  dark-theme: true
  call-center:
    between-hours:
        start: 9
        end: 19
  homepage-v2:
    partial:
      env: ['dev', 'preprod']
      roles: ['ROLE_ADMIN']
  slack-message:
    env: ['dev', 'preprod']
  secret-feature:
    users: ['antoine']
  new-design:
    unanimous:
      env: ['dev', 'preprod']
      roles: ['ROLE_DEV', 'ROLE_ADMIN']
```

Look at [Progressive documentation](https://github.com/antfroger/progressive#usage) to know more about the features' configuration.

Usage
-----

[](#usage)

You can use Progressive in a controller using [Symfony's autowiring](https://symfony.com/doc/current/service_container/autowiring.html):

```
  public function info(Progressive $progressive): Response
  {
      if ($progressive->isEnabled('call-center')) {
          // Do what you want when the feature `call-center` is enabled
      }
  }
```

Or in a template:

```
{% if is_enabled('call-center') %}
    {# Do what you want when the feature `call-center` is enabled #}
{% endif %}
```

Rules
-----

[](#rules)

### Built-in rules

[](#built-in-rules)

Progressive comes with several built-in rules:

- `enabled: true|false`
    [`enabled`](https://github.com/antfroger/progressive#enabled-truefalse) enables (or disables) the feature for everyone, everywhere, all the time.

### Symfony specific rules

[](#symfony-specific-rules)

This bundle provides Symfony specific rules:

- [`env: []`](#environments-)
- [`roles: []`](#roles-)
- [`users: []`](#users-)

#### `env: []`

[](#env-)

`env` enables (or disables) the feature depending on the app environment.
The value is meant to be an array of environment' names.

```
features:
  send-slack-message:
    env: ['dev', 'preprod']
```

#### `roles: []`

[](#roles-)

`roles` only enables (or disables) the feature for specific roles.
The value is meant to be an array of roles' names.

*This example configuration enables the feature `new-amazing-homepage` only for admins and dev.*

```
features:
  new-amazing-homepage:
    roles: ['ROLE_ADMIN', 'ROLE_DEV']
```

#### `users: []`

[](#users-)

`users` is more fine-grained than `roles` because, it allows you to enable a feature at a user level.
The value is meant to be an array of usernames.

*This example configuration enables the feature `secret-feature` only for the users antoine and ted.*

```
features:
  secret-feature:
    users: ['antoine', 'ted']
```

### Create your own rules

[](#create-your-own-rules)

I'm sure that soon you will want to create your own rules to progressively enable features dependning on your application logic.
That's where custom rules come into play! (More information about **custom rules** on the [Progressive doc](https://github.com/antfroger/progressive#custom))

To create your own rules and use them in your `feature. yaml` file, you only need to create a class extending `Progressive\Rule\RuleInterface`.
That's it!
Symfony autowiring takes care of the rest.

Let's say you want to display a chat in your contact page, but only in working hours (for instance between 9am and 7pm).

1. First, create the rule:

```
// src/Progressive/BetweenHours.php
namespace App\Progressive;

use Progressive\ParameterBagInterface;
use Progressive\Rule\RuleInterface;

class BetweenHours implements RuleInterface
{
    /**
     * {@inheritdoc}
     */
    public function getName(): string
    {
        return 'between-hours';
    }

    /**
     * {@inheritdoc}
     */
    public function decide(ParameterBagInterface $bag, array $hours = []): bool
    {
        if (!isset($hours['start']) || !is_int($hours['start']) || !isset($hours['end']) || !is_int($hours['end'])) {
            return false;
        }

        $now = new \DateTime();
        $hour = $now->format('H');
        return $hours['start']
