Skip to content

Commit ba79778

Browse files
JoshuaEstesgitbook-bot
authored andcommitted
GITBOOK-10: No subject
1 parent b0c2cd7 commit ba79778

File tree

3 files changed

+135
-32
lines changed

3 files changed

+135
-32
lines changed

docs/SUMMARY.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
* [Overview](bard/overview.md)
1212
* [Commands](bard/commands.md)
1313

14+
## Symfony Bundles
15+
16+
* [Feature Toggle](symfony-bundles/feature-toggle.md)
17+
18+
## Contracts
19+
20+
* [Contracts Overview](contracts/index.md)
21+
* [Common](contracts/common.md)
22+
* [Cookie](contracts/cookie.md)
23+
* [CQRS](contracts/cqrs.md)
24+
* [Filesystem](contracts/filesystem.md)
25+
* [Mailer](contracts/mailer.md)
26+
* [Pager](contracts/pager.md)
27+
* [Registry](contracts/registry.md)
28+
* [State Machine](contracts/state-machine.md)
29+
1430
## 📦 Components
1531

1632
* [Assert](components/assert.md)
@@ -56,18 +72,6 @@
5672
* [State Machine](components/state-machine.md)
5773
* [Version](components/version.md)
5874

59-
## Contracts
60-
61-
* [Contracts Overview](contracts/index.md)
62-
* [Common](contracts/common.md)
63-
* [Cookie](contracts/cookie.md)
64-
* [CQRS](contracts/cqrs.md)
65-
* [Filesystem](contracts/filesystem.md)
66-
* [Mailer](contracts/mailer.md)
67-
* [Pager](contracts/pager.md)
68-
* [Registry](contracts/registry.md)
69-
* [State Machine](contracts/state-machine.md)
70-
7175
## 💁 Contributing
7276

7377
* [Contributing Overview](contributing/index.md)

docs/components/feature-toggle.md

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
title: Feature Toggle - Overview
33
---
44

5+
# Feature Toggle
6+
57
## Installation
68

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

2022
// Using a feature toggle provider
2123
$provider = new InMemoryFeatureToggleProvider();
22-
$provider->addFeature(new Feature('feature.example', new AlwaysEnabledToggle()));
24+
$provider->add(new Feature('feature.example', new AlwaysEnabledToggle()));
2325

24-
$feature = $provider->getFeatureToggleByKey('feature.example');
26+
$feature = $provider->get('feature.example');
2527

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

30-
## Advanced Usage
32+
### Advanced Usage
3133

3234
```php
3335
<?php
@@ -46,14 +48,11 @@ $context['user'] = $user;
4648
$isEnabled = $feature->isEnabled($context);
4749
```
4850

49-
When you create your own toggles, you may need to introduce additional context
50-
to the toggle to check if everything should be enabled or disabled. This is
51-
where this comes into play at.
51+
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.
5252

53-
### Chain Toggle
53+
#### Chain Toggle
5454

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

5857
```php
5958
<?php
@@ -71,10 +70,9 @@ $toggle = new ChainToggle([
7170
$isEnabled = $toggle->isEnabled();
7271
```
7372

74-
### Affirmative Toggle
73+
#### Affirmative Toggle
7574

76-
Similar to the chain toggle, this will only return `true` when ALL toggles are
77-
`true`.
75+
Similar to the chain toggle, this will only return `true` when ALL toggles are `true`.
7876

7977
```php
8078
<?php
@@ -92,7 +90,7 @@ $toggle = new ChainToggle([
9290
$isEnabled = $toggle->isEnabled();
9391
```
9492

95-
### Date Range Toggle
93+
#### Date Range Toggle
9694

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

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

102100
use SonsOfPHP\Component\FeatureToggle\Toggle\DateRangeToggle;
103101

104-
$toggle = new ChainToggle(
102+
$toggle = new DateRangeToggle(
105103
start: new \DateTimeImmutable('2024-01-01'),
106104
end: new \DateTimeImmutable('2024-12-31'),
107105
);
108106

109107
// ...
110108
```
111109

112-
## Create your own Toggle
110+
### Create your own Toggle
113111

114-
Take a look at how some of the other toggles are implemented. Creating your own
115-
toggles are very easy. You just need to make sure they implement the interface
116-
`ToggleInterface`.
112+
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`.
117113

118114
```php
119115
<?php
@@ -130,5 +126,4 @@ class MyCustomToggle implements ToggleInterface
130126
}
131127
```
132128

133-
Once you make your custom toggle, you can use it just like all the rest of the
134-
toggles.
129+
Once you make your custom toggle, you can use it just like all the rest of the toggles.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Feature Toggle
2+
3+
## Installation
4+
5+
Make sure Composer is installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md) of the Composer documentation.
6+
7+
### Applications that use Symfony Flex
8+
9+
Open a command console, enter your project directory and execute:
10+
11+
```sh
12+
composer require sonsofphp/feature-toggle-bundle
13+
```
14+
15+
### Applications that don't use Symfony Flex
16+
17+
#### Step 1: Download the Bundle
18+
19+
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
20+
21+
```sh
22+
composer require sonsofphp/feature-toggle-bundle
23+
```
24+
25+
#### Step 2: Enable the Bundle
26+
27+
Then, enable the bundle by adding it to the list of registered bundles in the `config/bundles.php` file of your project:
28+
29+
```php
30+
// config/bundles.php
31+
32+
return [
33+
// ...
34+
SonsOfPHP\Bundle\FeatureToggleBundle\SonsOfPHPFeatureToggleBundle::class => ['all' => true],
35+
];
36+
```
37+
38+
## Configuration
39+
40+
```yaml
41+
# config/packages/sons_of_php_feature_toggle.yaml
42+
sons_of_php_feature_toggle:
43+
features:
44+
# You can create as many features as you want
45+
enabled_key:
46+
# Features can be enabled, disabled, or use a custom toggle
47+
toggle: enabled
48+
disabled_key:
49+
toggle: disabled
50+
custom_toggle_key:
51+
toggle: app.toggle.admin_users
52+
```
53+
54+
## Debug Command
55+
56+
You can debug your features by running the `debug:features` command.
57+
58+
```sh
59+
php bin/console debug:features
60+
```
61+
62+
This will give you a list of features and the toggles they are using.
63+
64+
## Twig Templates
65+
66+
You can check to see if the feature is enabled in twig templates by using the `is_feature_enabled` function.
67+
68+
```twig
69+
{% raw %}
70+
{% if is_feature_enabled('enabled_key') %}
71+
Feature "enabled_key" is enabled
72+
{% else %}
73+
Feature "enabled_key" is disabled
74+
{% endif %}
75+
{% endraw %}
76+
```
77+
78+
## Services
79+
80+
```php
81+
<?php
82+
83+
use SonsOfPHP\Contract\FeatureToggle\FeatureToggleProviderInterface;
84+
85+
class ExampleService
86+
{
87+
public function __construct(
88+
private FeatureToggleProviderInterface $featureToggleProvider,
89+
) {}
90+
91+
public function doSomething()
92+
{
93+
if ($featureToggleProvider->get('enabled_key')->isEnabled()) {
94+
// "enabled_key" is enabled
95+
}
96+
}
97+
}
98+
```
99+
100+
## Learn More
101+
102+
{% content-ref url="../components/feature-toggle.md" %}
103+
[feature-toggle.md](../components/feature-toggle.md)
104+
{% endcontent-ref %}

0 commit comments

Comments
 (0)