laravel / pennant

A simple, lightweight library for managing feature flags.
https://laravel.com/docs/pennant
MIT License
482 stars 49 forks source link

Forget all features for one scope #120

Closed cthorne91 closed 3 months ago

cthorne91 commented 3 months ago

Hello!

I'm curious if anybody would find this useful. Also, it's possible this feature is available, but I'm missing it in the documentation.

My use case: I have class based features defined. An external system will notify my application server that feature flags have changed for a given scope. There is no way for that system to tell me the feature/value pairs of each flag, only that flags have changed for that user. My goal is to simply remove the cache for each feature for the given scope and let the Feature::for($user)->active(FeatureA::class) in my application code reach out to that external system again. For now I'm considering deleting the cache directly from the database. Something like:

DB::table('feature_flags')->where('scope', Feature::serializeScope($user))->delete();

but I would like to see an API like

Feature::for($user)->forgetAll();

Happy to attempt a PR if this is something others would find useful.

timacdonald commented 3 months ago

@cthorne91, you may already use the forget method against a specific scope instead of calling delete directly against the DB.

Feature::for($user)->forget('feature-name');

// or...

Feature::for($user)->forget(['feature-name-1', 'feature-name-2']);

You could also call Feature::for($user)->forget(Feature::defined()) to forget all registered features.

Could be nice to wrap this up in a dedicated forgetAll API. We already have a load and loadAll method which are a similar pairing.