{"id":18469447,"url":"https://github.com/philspil66/gatekeeper","last_synced_at":"2025-04-08T10:32:45.310Z","repository":{"id":56394381,"uuid":"256203772","full_name":"philspil66/gatekeeper","owner":"philspil66","description":"Gatekeeper is a package to manage Feature Flagging within a Laravel project.","archived":false,"fork":false,"pushed_at":"2021-08-20T13:14:59.000Z","size":79,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T11:11:37.347Z","etag":null,"topics":["disabled-features","feature-flags","feature-toggle","feature-toggles","laravel","laravel-framework","php","php-framework"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/philspil66.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-16T12:08:14.000Z","updated_at":"2023-01-20T19:25:01.000Z","dependencies_parsed_at":"2022-08-15T18:00:15.184Z","dependency_job_id":null,"html_url":"https://github.com/philspil66/gatekeeper","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philspil66%2Fgatekeeper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philspil66%2Fgatekeeper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philspil66%2Fgatekeeper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philspil66%2Fgatekeeper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/philspil66","download_url":"https://codeload.github.com/philspil66/gatekeeper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247824256,"owners_count":21002237,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["disabled-features","feature-flags","feature-toggle","feature-toggles","laravel","laravel-framework","php","php-framework"],"created_at":"2024-11-06T10:10:26.919Z","updated_at":"2025-04-08T10:32:44.395Z","avatar_url":"https://github.com/philspil66.png","language":"PHP","readme":"# Gatekeeper\n\nGatekeeper is a package to manage Feature Flagging within a Laravel project.\n\n## What is Feature Flagging?\n\nFeature Flagging is basically a way to **have full control on the activation of a feature** in your applications.\n\nLet's make a couple of examples to give you an idea:\n\n* you just finished to work on the latest feature and you want to push it, but the marketing team wants you to deploy it in a second moment;\n* the new killer-feature is ready, but you want to enable it only for a specific set of users;\n\nWith Gatekeeper, you can:\n\n* easily **define new features** in your application;\n* **enable/disable features** globally;\n* **enable/disable features for specific users**, or **for whatever you want**;\n\nThere are many things to know about feature toggling: take a look to [this great article](http://martinfowler.com/articles/feature-toggles.html) for more info. It's a really nice and useful lecture.\n\n## Compatibility\n\nGatekeeper works with PHP 5.6 or above.\n\n## Install\n\nYou can install Gatekeeper with Composer.\n\n``` bash\n$ composer require philspil66/gatekeeper\n```\n\nAfter that, you need to **add the `FeatureServiceProvider` to the `app.php` config file**.\n\n```php\n...\nGatekeeper\\Provider\\FeatureServiceProvider::class,\n...\n```\n\nNow you have to **run migrations**, to add the tables Gatekeeper needs.\n\n```bash\n$ php artisan migrate\n```\n\n... and you're good to go!\n\n### Facade\n\nIf you want, you can also **add the `Feature` facade** to the `aliases` array in the `app.php` config file.\n\n```php\n...\n'Feature' =\u003e \\Gatekeeper\\Facade\\Feature::class,\n...\n```\n\nIf you don't like Facades, **inject the `FeatureManager`** class wherever you want!\n\n### Config File\n\nBy default, you can immediately use Gatekeeper. However, if you want to tweak some settings, feel free to **publish the config file** with\n\n```bash\n$ php artisan vendor:publish --provider=\"Gatekeeper\\Provider\\FeatureServiceProvider\"\n```\n\n## Basic Usage\n\nThere are two ways you can use features: working with them **globally** or **specifically for a specific entity**.\n\n### Globally Enabled/Disabled Features\n\n#### Declare a New Feature\n\nLet's say you have a new feature that you want to keep hidden until a certain moment. We will call it \"new_super_feature\". Let's **add it to our application**:\n\n```php\nGatekeeper::add('new_super_feature', false);\n```\n\nEasy, huh? As you can imagine, **the first argument is the feature name**. **The second is a boolean we specify to define the current status** of the feature.\n\n* `true` stands for **the feature is enabled for everyone**;\n* `false` stands for **the feature is hidden, no one can use it/see it**;\n\nAnd that's all.\n\n#### Check if a Feature is Enabled\n\nNow, let's imagine a better context for our example. We're building a CMS, and our \"new_super_feature\" is used to... clean our HTML code. Let's assume we have a controller like this one.\n\n```php\nclass CMSController extends Controller {\n    public function getPage($pageSlug) {\n        \n        // here we are getting our page code from some service\n        $content = PageService::getContentBySlug($pageSlug);\n        \n        // here we are showing our page code\n        return view('layout.pages', compact('content'));\n    }\n}\n```\n\nNow, we want to deploy the new service, but **we don't want to make it available for users**, because the marketing team asked us to release it the next week. Gatekeeper helps us with this:\n\n```php\nclass CMSController extends Controller {\n    public function getPage($pageSlug) {\n        \n        // here we are getting our page code from some service\n        $content = PageService::getContentBySlug($pageSlug);\n        \n        // feature flagging here!\n        if(Gatekeeper::isEnabled('new_super_feature')) {\n            $content = PageCleanerService::clean($content);\n        }\n        \n        // here we are showing our page code\n        return view('layout.pages', compact('content'));\n    }\n}\n```\n\nNow, **the specific service code will be executed only if the \"new_super_feature\" feature is enabled**.\n\n#### Change a Feature Activation Status\n\nObviously, using the `Feature` class we can easily **toggle the feature activation status**.\n\n```php\n// release the feature!\nGatekeeper::enable('new_super_feature');\n\n// hide the feature!\nGatekeeper::disable('new_super_feature');\n```\n\n#### Remove a Feature\n\nEven if it's not so used, you can also **delete a feature** easily with\n\n```php\nGatekeeper::remove('new_super_feature');\n```\n\nWarning: *be sure about what you do. If you remove a feature from the system, you will stumble upon exceptions if checks for the deleted features are still present in the codebase.*\n\n#### Work with Views\n\nI really love blade directives, they help me writing more elegant code. I prepared **a custom blade directive, `@feature`**:\n\n```php\n\u003cdiv\u003eThis is an example template div. Always visible.\u003c/div\u003e\n\n@feature('my_awesome_feature')\n    \u003cp\u003eThis paragraph will be visible only if \"my_awesome_feature\" is enabled!\u003c/p\u003e\n@endfeature\n\n\u003cdiv\u003eThis is another example template div. Always visible too.\u003c/div\u003e\n```\n\nA really nice shortcut!\n\n### Enable/Disable Features for Specific Users/Entities\n\nEven if the previous things we saw are useful, Gatekeeper **is not just about pushing the on/off button on a feature**. Sometimes, business necessities require more flexibility. Perhaos we want to rollout a feature only to specific users. Or, maybe, just for one tester user.\n\n#### Enable Features Management for Specific Users\n\nGatekeeper makes this possible, and also easier just as **adding a trait to our `User` class**.\n\nIn fact, all you need to do is to: \n\n* **add the `Gatekeeper\\Featurable\\Featurable` trait** to the `User` class;\n* let the same class **implement the `FeaturableInterface` interface**;\n\n```php\n...\n\nclass User extends Authenticatable implements FeaturableInterface\n{\n    use Notifiable, Featurable;\n    \n...\n```\n\nNothing more! Gatekeeper now already knows what to do.\n\n#### Status Priority\n\n*Please keep in mind that all you're going to read from now is not valid if a feature is already enabled globally. To activate a feature for specific users, you first need to disable it.*\n\nGatekeeper **first checks if the feature is enabled globally, then it goes down at entity-level**.\n\n#### Enable/Disable a Feature for a Specific User\n\n```php\n$user = Auth::user();\n\n// now, the feature \"my.feature\" is enabled ONLY for $user!\nGatekeeper::enableFor('my.feature', $user);\n\n// now, the feature \"my.feature\" is disabled for $user!\nGatekeeper::disableFor('my.feature', $user);\n\n```\n\n#### Check if a Feature is Enabled for a Specific User\n\n```php\n$user = Auth::user();\n\nif(Gatekeeper::isEnabledFor('my.feature', $user)) {\n    \n    // do amazing things!\n    \n}\n```\n### Artisan Commands\n\nYou may run the following commands to add or remove features.\n\n```bash\nphp artisan gatekeeper:add my-feature\n\nphp artisan gatekeeper:remove my-feature\n```\n\nYou may run the following commands to toggle the on or off state of the feature.\n\n```bash\nphp artisan gatekeeper:enable my-feature\n\nphp artisan gatekeeper:disable my-feature\n```\n\n#### Other Notes\n\nGatekeeper also provides a Blade directive to check if a feature is enabled for a specific user. You can use the `@featurefor` blade tags:\n```php\n@featurefor('my.feature', $user)\n    \n    // do $user related things here!\n    \n@endfeaturefor\n```\n\n## Advanced Things\n\nOk, now that we got the basics, let's raise the bar!\n\n### Enable Features Management for Other Entities\n\nAs I told before, you can easily add features management for Users just by using the `Featurable` trait and implementing the `FeaturableInterface` in the User model. However, when structuring the relationships, I decided to implement a **many-to-many polymorphic relationship**. This means that you can **add feature management to any model**!\n\nLet's make an example: imagine that **you have a `Role` model** you use to implement a basic roles systems for your users. This because you have admins and normal users. \n\nSo, **you rolled out the amazing killer feature but you want to enable it only for admins**. How to do this? Easy. Recap:\n\n* add the `Featurable` trait to the `Role` model;\n* be sure the `Role` model implements the `FeaturableInterface`;\n\nLet's think the role-user relationship as one-to-many one.\n\nYou will probably have a `role()` method on your `User` class, right? Good. You already know the rest:\n\n```php\n// $role is the admin role!\n$role = Auth::user()-\u003erole;\n\n...\n\nGatekeeper::enableFor('my.feature', $role);\n\n...\n\nif(Gatekeeper::isEnabledFor('my.feature', $role)) {\n\n    // this code will be executed only if the user is an admin!\n    \n}\n```\n\n\n## Change log\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n\n## Credits\n\n* [Phil Spilsbury](https://github.com/philspil66)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilspil66%2Fgatekeeper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphilspil66%2Fgatekeeper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilspil66%2Fgatekeeper/lists"}