onix-systems-php / hyperf-feature-flags
An extension for feature flags
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:extension
Requires
- ext-redis: *
- hyperf/di: ^3.1
- hyperf/framework: ^3.1
- hyperf/redis: ^3.1
- nesbot/carbon: ^2.72
- onix-systems-php/hyperf-actions-log: ^1.2
- onix-systems-php/hyperf-core: ^1.2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.48
- mockery/mockery: ^1.6
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- symfony/var-dumper: ^7.0
README
Includes the following general usage classes:
- Annotations:
- FeatureFlag;
- Aspects:
- FeatureFlagAspect;
- Constants:
- Actions;
- DTO:
- ResetFeatureFlagDTO;
- UpdateFeatureFlagDTO;
- Event:
- Action;
- Model:
- FeatureFlag;
- Repository:
- FeatureFlagRepository;
- Service:
- GetCurrentFeatureFlagService;
- GetFeatureFlagService;
- GetOverriddenFeatureFlagService;
- ResetFeatureFlagService;
- SetFeatureFlagService
- Other:
- RedisWrapper
Installation:
composer require onix-systems-php/hyperf-feature-flags
Publishing the config:
php bin/hyperf.php vendor:publish onix-systems-php/hyperf-feature-flags
Defining feature flags in config:
Format:
'name' => 'rule'
config/autoload/feature-flags.php
return [ 'slack-integration' => true, 'my-custom-feature' => "[date:now] > '2024-02-06' && [date:now] <= '2024-12-31'", 'my-awesome-feature' => '[config:slack.integration] === true || [feature:my-custom-feature] === false', ];
Attention! If you want to use dates, in your rules you must take your dates in singular quotes. Otherwise, the rule will always evaluate as true.
Basic Usage:
Examples:
- Via:
OnixSystemsPHP\HyperfFeatureFlags\Annotations\FeatureFlag
.
#[FeatureFlag('slack-integration', false)] private function sendSlackNotification(string $link, Feedback $feedback): bool { //... }
As we can see from this code, that sendSlackNotification
method will be executed if we slack-integrations
is true in our config, in our feature_flags
table or redis.
the second parameter in FeatureFlag
aspect was set to false then it indicates that if slack-integartion
is not exists anywhere by default aspect will return false and this method will also return false.
- Via:
OnixSystemsPHP\HyperfFeatureFlags\Services\GetFeatureFlagService
This example introduces how you can use it from your controller method which should return Psr\Http\Message\ResponseInterface
. Here we don't use annotation because we need to return not a primitive but an object.
So if $this->getFeatureFlagService->run('slack-integration')
will return false, we just return $this->response->json([])
. Otherwise, we go next.
class SlackController { public function __construct(private \OnixSystemsPHP\HyperfFeatureFlags\Services\GetFeatureFlagService $getFeatureFlagService) {} public function webhook(): Psr\Http\Message\ResponseInterface { if (!$this->getFeatureFlagService->run('slack-integration')) { return $this->response->json([]) } } }
For example, we have in our config these rules:
return [ 'my-awesome-feature' => true, 'my-custom-feature' => "[date:now] > '2050-31-12' || [config:slack.integration] || [feature:my-awesome-feature]", ]
What will be the value of this rule?
- The first part, will evaluate
[date:now] > '2050-31-12'
. Obviously, until2050-31-12
it will be false. - The second part, will evaluate
[config:slack.integration]
. It will takeintegration
value from theconfig/autoload/slack.php
file if you have. Assume, we don't haveslack-integration
in our config file. So it will benull
. - The third part, will evaluate
[feature:my-awesome-feature]
. Nothing fancy, it will take from our file/cofnig/autoload/feature_flags.php
,true
value. - Finally, evaluation of this rule
my-custom-feature
will befalse || null || true
. As the result of evaluation will betrue
.
Classes:
OnixSystemsPHP\HyperfFeatureFlags\Services\SetFeatureFlagService
This class allows you to set your feature flag to feature_flags
table.
The method run()
accepts one argument: UpdateFeatureFlagDTO
with name and rule of the feature flag.
It checks if the given user can set this feature flag. So you should implement policy logic.
OnixSystemsPHP\HyperfFeatureFlags\Services\ResetFeatureFlagService
This class allows you to reset your feature. In simply words it just deletes your flag from the database and redis.
The method run()
accepts one argument: ResetFeatureFlagDTO
with name and rule of the feature flag.
It checks if the given user can reset this feature flag. So you should implement policy logic.
OnixSystemsPHP\HyperfFeatureFlags\Services\GetCurrentFeatureFlagService
This class allows you to get all current feature flags.
The method run()
returns an associative array with name and value (which evaluates based on rule) of the feature flag.
OnixSystemsPHP\HyperfFeatureFlags\Services\GetOverriddenFeatureFlagsService
This class allows you to get all overridden features. In simply words it just grab your flags from the database and redis.
The method run()
returns an associative array with name and value of the feature flag.
OnixSystemsPHP\HyperfFeatureFlags\Services\GetFeatureFlagService
This class allows you to get value of your rule.
Firstly, it goes to redis and checks if it presents in it, then if yes takes it.
Secondly if no, goes to database if there is any flag with this name if yes, takes it.
Finally, goes to the config and return evaluated rule from the config even if there is such rule in config, it simply returns false
, in any of these cases it stores value to redis.
License
The MIT License (MIT). Please see License File for more information.