https://github.com/defstudio/enum-features
A simple trait to enable a feature system using Enums
https://github.com/defstudio/enum-features
Last synced: about 1 year ago
JSON representation
A simple trait to enable a feature system using Enums
- Host: GitHub
- URL: https://github.com/defstudio/enum-features
- Owner: defstudio
- License: mit
- Created: 2023-07-03T20:17:46.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-08T17:47:40.000Z (almost 2 years ago)
- Last Synced: 2024-08-09T13:28:14.931Z (almost 2 years ago)
- Language: PHP
- Size: 80.1 KB
- Stars: 15
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Enum Features
[](https://packagist.org/packages/defstudio/enum-features)
[](https://github.com/defstudio/enum-features/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/defstudio/enum-features/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[](https://packagist.org/packages/defstudio/enum-features)
[](https://packagist.org/packages/defstudio/enum-features)
[](https://twitter.com/FabioIvona?ref_src=twsrc%5Etfw)
A simple trait to enable a feature system using Enums:
```php
if(AppFeature::welcome_email->active()){
Mail::to($newUser)->send(new WelcomeEmail($newUser));
}
```
## Installation
You can install the package via composer:
```bash
composer require defstudio/enum-features
```
## Usage
Features can be enabled on any enum by using the `DefinesFeatures` trait:
```php
use DefStudio\EnumFeatures\EnumFeatures;
enum AppFeature
{
use DefinesFeatures; // ← simply add this
case multi_language;
case impersonate;
case welcome_email;
/* Feature resolution */
//with a single method:
protected function resolve(Authenticatable $user = null) {
match($this){
case self::multi_language => true,
case self::impersonate => $user->isAdmin(),
default => false;
}
}
//or with a dedicated method:
protected function resolveImpersonate(Authenticatable $user = null){
return $user->isSuperAdmin();
}
}
```
and should be registered in your Provider
```php
class AppServiceProvider extends ServiceProvider
{
//..
public function boot(): void {
AppFeature::defineFeatures();
}
}
```
then, in code, a feature could be checked to be enabled:
```php
if(AppFeature::multi_language->active()){
//.. multi language specific code
}
```
An extensive documentation is available at
https://docs.defstudio.it/enum-features
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. [Follow Us](https://twitter.com/FabioIvona) on Twitter for more updates about this package.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Fabio Ivona](https://github.com/defstudio)
- [def:studio team](https://github.com/defstudio)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
## Support us
We at [def:studio](https://github.com/defstudio) strongly believe that open source is the foundation of all our business and we try to contribute to it by helping other projects to grow along with developing and maintaining our packages. You can support our work by sponsoring us on [github](https://github.com/sponsors/defstudio)!