|
| 1 | +# Event system in Unleash PHP SDK |
| 2 | + |
| 3 | +This SDK supports events using [`symfony/event-dispatcher`](https://packagist.org/packages/symfony/event-dispatcher). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +The event dispatcher is not a mandatory component of this SDK, so you need to install it by running: |
| 8 | + |
| 9 | +`composer require symfony/event-dispatcher` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +You can add event subscribers to the builder object: |
| 14 | + |
| 15 | +```php |
| 16 | +<?php |
| 17 | + |
| 18 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 19 | +use Unleash\Client\Event\FeatureToggleDisabledEvent; |
| 20 | +use Unleash\Client\Event\FeatureToggleMissingStrategyHandlerEvent; |
| 21 | +use Unleash\Client\Event\FeatureToggleNotFoundEvent; |
| 22 | +use Unleash\Client\Event\FeatureVariantBeforeFallbackReturnedEvent; |
| 23 | +use Unleash\Client\UnleashBuilder; |
| 24 | +use Unleash\Client\Event\UnleashEvents; |
| 25 | + |
| 26 | +class MyEventSubscriber implements EventSubscriberInterface |
| 27 | +{ |
| 28 | + public static function getSubscribedEvents(): array |
| 29 | + { |
| 30 | + return [ |
| 31 | + UnleashEvents::FEATURE_TOGGLE_DISABLED => 'onFeatureDisabled', |
| 32 | + UnleashEvents::FEATURE_TOGGLE_MISSING_STRATEGY_HANDLER => 'onNoStrategyHandler', |
| 33 | + UnleashEvents::FEATURE_TOGGLE_NOT_FOUND => 'onFeatureNotFound', |
| 34 | + ]; |
| 35 | + } |
| 36 | + |
| 37 | + public function onFeatureDisabled(FeatureToggleDisabledEvent $event) |
| 38 | + { |
| 39 | + // todo |
| 40 | + } |
| 41 | + |
| 42 | + public function onNoStrategyHandler(FeatureToggleMissingStrategyHandlerEvent $event) |
| 43 | + { |
| 44 | + // todo |
| 45 | + } |
| 46 | + |
| 47 | + public function onFeatureNotFound(FeatureToggleNotFoundEvent $event) |
| 48 | + { |
| 49 | + // todo |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +$unleash = UnleashBuilder::create() |
| 54 | + ->withAppName('My App') |
| 55 | + ->withAppUrl('http://localhost:4242') |
| 56 | + ->withInstanceId('test') |
| 57 | + ->withEventSubscriber(new MyEventSubscriber()) |
| 58 | + ->build(); |
| 59 | + |
| 60 | +$unleash->isEnabled('test'); |
| 61 | +``` |
| 62 | + |
| 63 | +The relevant methods will be called in the above example when their respective event occurs. |
| 64 | + |
| 65 | +### List of events: |
| 66 | + |
| 67 | +- `\Unleash\Client\Event\UnleashEvents::FEATURE_TOGGLE_NOT_FOUND` - when a feature with the name isn't found on the |
| 68 | + unleash server (or in the bootstrap if it's used). Event object: `Unleash\Client\Event\FeatureToggleNotFoundEvent` |
| 69 | +- `\Unleash\Client\Event\UnleashEvents::FEATURE_TOGGLE_DISABLED` - when a feature is found but it's disabled. |
| 70 | + Event object: Unleash\Client\Event\FeatureToggleDisabledEvent |
| 71 | +- `\Unleash\Client\Event\UnleashEvents::FEATURE_TOGGLE_MISSING_STRATEGY_HANDLER` - when there is no suitable strategy handler |
| 72 | + implemented for any of the feature's strategies. Event object: `Unleash\Client\Event\FeatureToggleMissingStrategyHandlerEvent` |
| 73 | + |
| 74 | +## FEATURE_TOGGLE_NOT_FOUND event |
| 75 | + |
| 76 | +Example: |
| 77 | + |
| 78 | +```php |
| 79 | +<?php |
| 80 | + |
| 81 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 82 | +use Unleash\Client\Event\FeatureToggleNotFoundEvent; |
| 83 | +use Unleash\Client\Event\UnleashEvents; |
| 84 | +use Unleash\Client\DTO\DefaultFeature; |
| 85 | +use Unleash\Client\UnleashBuilder; |
| 86 | + |
| 87 | +final class MyEventSubscriber implements EventSubscriberInterface |
| 88 | +{ |
| 89 | + public static function getSubscribedEvents() |
| 90 | + { |
| 91 | + return [ |
| 92 | + UnleashEvents::FEATURE_TOGGLE_NOT_FOUND => 'onNotFound', |
| 93 | + ]; |
| 94 | + } |
| 95 | + |
| 96 | + public function onNotFound(FeatureToggleNotFoundEvent $event): void |
| 97 | + { |
| 98 | + // methods: |
| 99 | + $event->getFeatureName(); // string |
| 100 | + $event->getContext(); // instance of Context |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +$unleash = UnleashBuilder::create() |
| 105 | + ->withEventSubscriber(new MyEventSubscriber()) |
| 106 | + ->build(); |
| 107 | +``` |
| 108 | + |
| 109 | +## FEATURE_TOGGLE_DISABLED event |
| 110 | + |
| 111 | +Example: |
| 112 | + |
| 113 | +```php |
| 114 | +<?php |
| 115 | + |
| 116 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 117 | +use Unleash\Client\Event\FeatureToggleDisabledEvent; |
| 118 | +use Unleash\Client\Event\UnleashEvents; |
| 119 | +use Unleash\Client\DTO\DefaultFeature; |
| 120 | +use Unleash\Client\DTO\DefaultStrategy; |
| 121 | + |
| 122 | +final class MyEventSubscriber implements EventSubscriberInterface |
| 123 | +{ |
| 124 | + public static function getSubscribedEvents() |
| 125 | + { |
| 126 | + return [ |
| 127 | + UnleashEvents::FEATURE_TOGGLE_DISABLED => 'onFeatureDisabled', |
| 128 | + ]; |
| 129 | + } |
| 130 | + |
| 131 | + public function onFeatureDisabled(FeatureToggleDisabledEvent $event): void |
| 132 | + { |
| 133 | + // methods: |
| 134 | + $event->getContext(); // instance of Context |
| 135 | + $event->getFeature(); // instance of Feature |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## FEATURE_TOGGLE_MISSING_STRATEGY_HANDLER event |
| 141 | + |
| 142 | +Triggered when no strategy handler can be found for any of the strategies. |
| 143 | + |
| 144 | +Example: |
| 145 | + |
| 146 | +```php |
| 147 | +<?php |
| 148 | + |
| 149 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 150 | +use Unleash\Client\Event\UnleashEvents; |
| 151 | +use Unleash\Client\UnleashBuilder; |
| 152 | +use Unleash\Client\Event\FeatureToggleMissingStrategyHandlerEvent; |
| 153 | +use Unleash\Client\Strategy\DefaultStrategyHandler; |
| 154 | + |
| 155 | +final class MyEventSubscriber implements EventSubscriberInterface |
| 156 | +{ |
| 157 | + public static function getSubscribedEvents() |
| 158 | + { |
| 159 | + return [ |
| 160 | + UnleashEvents::FEATURE_TOGGLE_MISSING_STRATEGY_HANDLER => 'onMissingStrategyHandler', |
| 161 | + ]; |
| 162 | + } |
| 163 | + |
| 164 | + public function onMissingStrategyHandler(FeatureToggleMissingStrategyHandlerEvent $event): void |
| 165 | + { |
| 166 | + // methods: |
| 167 | + $event->getContext(); // instance of Context |
| 168 | + $event->getFeature(); // instance of Feature |
| 169 | + // get strategies |
| 170 | + $event->getFeature()->getStrategies(); // iterable of Strategy instances |
| 171 | + } |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +## Customizing event dispatcher |
| 176 | + |
| 177 | +If you already use event dispatcher in your app, you can provide it to the builder: |
| 178 | + |
| 179 | +```php |
| 180 | +<?php |
| 181 | + |
| 182 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 183 | +use Unleash\Client\UnleashBuilder; |
| 184 | + |
| 185 | +$eventDispatcher = new EventDispatcher(); |
| 186 | + |
| 187 | +// do something with event dispatcher |
| 188 | + |
| 189 | +$unleash = UnleashBuilder::create() |
| 190 | + ->withEventDispatcher($eventDispatcher) |
| 191 | + // add other unleash configurations |
| 192 | + ->build(); |
| 193 | +``` |
| 194 | + |
| 195 | +All event subscribers/listeners registered directly in the event dispatcher work as usual: |
| 196 | + |
| 197 | +```php |
| 198 | +<?php |
| 199 | + |
| 200 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 201 | +use Unleash\Client\UnleashBuilder; |
| 202 | +use Unleash\Client\Event\UnleashEvents; |
| 203 | +use Unleash\Client\Event\FeatureToggleDisabledEvent; |
| 204 | + |
| 205 | +$eventDispatcher = new EventDispatcher(); |
| 206 | + |
| 207 | +$eventDispatcher->addSubscriber(new MyEventSubscriber()); |
| 208 | +$eventDispatcher->addListener(UnleashEvents::FEATURE_TOGGLE_DISABLED, function (FeatureToggleDisabledEvent $event) { |
| 209 | + // todo |
| 210 | +}); |
| 211 | + |
| 212 | + |
| 213 | +$unleash = UnleashBuilder::create() |
| 214 | + ->withEventDispatcher($eventDispatcher) |
| 215 | + // add other unleash configurations |
| 216 | + ->build(); |
| 217 | +``` |
| 218 | + |
| 219 | +> Tip for PhpStorm users: Use the [Symfony plugin](https://plugins.jetbrains.com/plugin/7219-symfony-support) |
| 220 | +> for help with autocompletion of events, afterwards it looks like this: |
| 221 | +
|
| 222 | + |
| 223 | + |
0 commit comments