Skip to content

Commit

Permalink
Add context provider to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Aug 19, 2021
1 parent f5f8b83 commit b6f741c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,42 @@ $unleash->isEnabled('some-feature');
> Note: This library also implements some deprecated strategies, namely `gradualRolloutRandom`, `gradualRolloutSessionId`
> and `gradualRolloutUserId` which all alias to the Gradual rollout strategy.
### Context provider

Manually creating relevant context can get tiring real fast. Luckily you can create your own context provider that
will do it for you!

```php
<?php

use Unleash\Client\ContextProvider\UnleashContextProvider;
use Unleash\Client\Configuration\UnleashContext;
use Unleash\Client\UnleashBuilder;

final class MyContextProvider implements UnleashContextProvider
{
public function getContext(): Context
{
$context = new UnleashContext();
$context->setCurrentUserId('user id from my app');

return $context;
}
}

$unleash = UnleashBuilder::create()
->withAppName('Some app name')
->withAppUrl('https://some-app-url.com')
->withInstanceId('Some instance id')
// here we set the custom provider
->withContextProvider(new MyContextProvider())
->build();

if ($unleash->isEnabled('someFeature')) { // this call will use your context provider with the provided user id

}
```

### Custom strategies

To implement your own strategy you need to create a class implementing `StrategyHandler` (or `AbstractStrategyHandler`
Expand Down

0 comments on commit b6f741c

Please sign in to comment.