Skip to content

Commit

Permalink
GITBOOK-10: No subject
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes authored and gitbook-bot committed Oct 23, 2024
1 parent b0c2cd7 commit ba79778
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 32 deletions.
28 changes: 16 additions & 12 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
* [Overview](bard/overview.md)
* [Commands](bard/commands.md)

## Symfony Bundles

* [Feature Toggle](symfony-bundles/feature-toggle.md)

## Contracts

* [Contracts Overview](contracts/index.md)
* [Common](contracts/common.md)
* [Cookie](contracts/cookie.md)
* [CQRS](contracts/cqrs.md)
* [Filesystem](contracts/filesystem.md)
* [Mailer](contracts/mailer.md)
* [Pager](contracts/pager.md)
* [Registry](contracts/registry.md)
* [State Machine](contracts/state-machine.md)

## 📦 Components

* [Assert](components/assert.md)
Expand Down Expand Up @@ -56,18 +72,6 @@
* [State Machine](components/state-machine.md)
* [Version](components/version.md)

## Contracts

* [Contracts Overview](contracts/index.md)
* [Common](contracts/common.md)
* [Cookie](contracts/cookie.md)
* [CQRS](contracts/cqrs.md)
* [Filesystem](contracts/filesystem.md)
* [Mailer](contracts/mailer.md)
* [Pager](contracts/pager.md)
* [Registry](contracts/registry.md)
* [State Machine](contracts/state-machine.md)

## 💁 Contributing

* [Contributing Overview](contributing/index.md)
Expand Down
35 changes: 15 additions & 20 deletions docs/components/feature-toggle.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
title: Feature Toggle - Overview
---

# Feature Toggle

## Installation

```shell
Expand All @@ -19,15 +21,15 @@ use SonsOfPHP\Component\FeatureToggle\Toggle\AlwaysEnabledToggle;

// Using a feature toggle provider
$provider = new InMemoryFeatureToggleProvider();
$provider->addFeature(new Feature('feature.example', new AlwaysEnabledToggle()));
$provider->add(new Feature('feature.example', new AlwaysEnabledToggle()));

$feature = $provider->getFeatureToggleByKey('feature.example');
$feature = $provider->get('feature.example');

// Checking if the feature is enabled
$isEnabled = $feature->isEnabled();
```

## Advanced Usage
### Advanced Usage

```php
<?php
Expand All @@ -46,14 +48,11 @@ $context['user'] = $user;
$isEnabled = $feature->isEnabled($context);
```

When you create your own toggles, you may need to introduce additional context
to the toggle to check if everything should be enabled or disabled. This is
where this comes into play at.
When you create your own toggles, you may need to introduce additional context to the toggle to check if everything should be enabled or disabled. This is where this comes into play at.

### Chain Toggle
#### Chain Toggle

The chain toggle allows you to use many toggles together. If ANY toggle returns
`true`, the feature is considered enabled.
The chain toggle allows you to use many toggles together. If ANY toggle returns `true`, the feature is considered enabled.

```php
<?php
Expand All @@ -71,10 +70,9 @@ $toggle = new ChainToggle([
$isEnabled = $toggle->isEnabled();
```

### Affirmative Toggle
#### Affirmative Toggle

Similar to the chain toggle, this will only return `true` when ALL toggles are
`true`.
Similar to the chain toggle, this will only return `true` when ALL toggles are `true`.

```php
<?php
Expand All @@ -92,7 +90,7 @@ $toggle = new ChainToggle([
$isEnabled = $toggle->isEnabled();
```

### Date Range Toggle
#### Date Range Toggle

The date range toggle will return `true` if it's within a given time range.

Expand All @@ -101,19 +99,17 @@ The date range toggle will return `true` if it's within a given time range.

use SonsOfPHP\Component\FeatureToggle\Toggle\DateRangeToggle;

$toggle = new ChainToggle(
$toggle = new DateRangeToggle(
start: new \DateTimeImmutable('2024-01-01'),
end: new \DateTimeImmutable('2024-12-31'),
);

// ...
```

## Create your own Toggle
### Create your own Toggle

Take a look at how some of the other toggles are implemented. Creating your own
toggles are very easy. You just need to make sure they implement the interface
`ToggleInterface`.
Take a look at how some of the other toggles are implemented. Creating your own toggles are very easy. You just need to make sure they implement the interface `ToggleInterface`.

```php
<?php
Expand All @@ -130,5 +126,4 @@ class MyCustomToggle implements ToggleInterface
}
```

Once you make your custom toggle, you can use it just like all the rest of the
toggles.
Once you make your custom toggle, you can use it just like all the rest of the toggles.
104 changes: 104 additions & 0 deletions docs/symfony-bundles/feature-toggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Feature Toggle

## Installation

Make sure Composer is installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md) of the Composer documentation.

### Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

```sh
composer require sonsofphp/feature-toggle-bundle
```

### Applications that don't use Symfony Flex

#### Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

```sh
composer require sonsofphp/feature-toggle-bundle
```

#### Step 2: Enable the Bundle

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

```php
// config/bundles.php

return [
// ...
SonsOfPHP\Bundle\FeatureToggleBundle\SonsOfPHPFeatureToggleBundle::class => ['all' => true],
];
```

## Configuration

```yaml
# config/packages/sons_of_php_feature_toggle.yaml
sons_of_php_feature_toggle:
features:
# You can create as many features as you want
enabled_key:
# Features can be enabled, disabled, or use a custom toggle
toggle: enabled
disabled_key:
toggle: disabled
custom_toggle_key:
toggle: app.toggle.admin_users
```
## Debug Command
You can debug your features by running the `debug:features` command.

```sh
php bin/console debug:features
```

This will give you a list of features and the toggles they are using.

## Twig Templates

You can check to see if the feature is enabled in twig templates by using the `is_feature_enabled` function.

```twig
{% raw %}
{% if is_feature_enabled('enabled_key') %}
Feature "enabled_key" is enabled
{% else %}
Feature "enabled_key" is disabled
{% endif %}
{% endraw %}
```

## Services

```php
<?php
use SonsOfPHP\Contract\FeatureToggle\FeatureToggleProviderInterface;
class ExampleService
{
public function __construct(
private FeatureToggleProviderInterface $featureToggleProvider,
) {}
public function doSomething()
{
if ($featureToggleProvider->get('enabled_key')->isEnabled()) {
// "enabled_key" is enabled
}
}
}
```

## Learn More

{% content-ref url="../components/feature-toggle.md" %}
[feature-toggle.md](../components/feature-toggle.md)
{% endcontent-ref %}

0 comments on commit ba79778

Please sign in to comment.