在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):francescomalatesta/laravel-feature开源软件地址(OpenSource Url):https://github.com/francescomalatesta/laravel-feature开源编程语言(OpenSource Language):PHP 99.1%开源软件介绍(OpenSource Introduction):Laravel-FeatureLaravel-Feature is a package fully dedicated to feature toggling in your application, in the easiest way. For Laravel, of course. It was inspired by the AdEspresso Feature Flag Bundle. Feature-What?Feature toggling is basically a way to have full control on the activation of a feature in your applications. Let's make a couple of examples to give you an idea:
With Laravel-Feature, you can:
There are many things to know about feature toggling: take a look to this great article for more info. It's a really nice and useful lecture. InstallYou can install Laravel-Feature with Composer. $ composer require francescomalatesta/laravel-feature After that, you need to add the ...
LaravelFeature\Provider\FeatureServiceProvider::class,
... Now you have to run migrations, to add the tables Laravel-Feature needs. $ php artisan migrate ... and you're good to go! FacadeIf you want, you can also add the ...
'Feature' => \LaravelFeature\Facade\Feature::class,
... If you don't like Facades, inject the Config FileBy default, you can immediately use Laravel-Feature. However, if you want to tweak some settings, feel free to publish the config file with $ php artisan vendor:publish --provider="LaravelFeature\Provider\FeatureServiceProvider" Basic UsageThere are two ways you can use features: working with them globally or specifically for a specific entity. Globally Enabled/Disabled FeaturesDeclare a New FeatureLet's say you have a new feature that you want to keep hidden until a certain moment. We will call it "page_code_cleaner". Let's add it to our application: Feature::add('page_code_cleaner', false); Easy, 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.
And that's all. Check if a Feature is EnabledNow, let's imagine a better context for our example. We're building a CMS, and our "page_code_cleaner" is used to... clean our HTML code. Let's assume we have a controller like this one. class CMSController extends Controller {
public function getPage($pageSlug) {
// here we are getting our page code from some service
$content = PageService::getContentBySlug($pageSlug);
// here we are showing our page code
return view('layout.pages', compact('content'));
}
} Now, 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. LaravelFeature helps us with this: class CMSController extends Controller {
public function getPage($pageSlug) {
// here we are getting our page code from some service
$content = PageService::getContentBySlug($pageSlug);
// feature flagging here!
if(Feature::isEnabled('page_code_cleaner')) {
$content = PageCleanerService::clean($content);
}
// here we are showing our page code
return view('layout.pages', compact('content'));
}
} Ta-dah! Now, the specific service code will be executed only if the "page_code_cleaner" feature is enabled. Change a Feature Activation StatusObviously, using the // release the feature!
Feature::enable('page_code_cleaner');
// hide the feature!
Feature::disable('page_code_cleaner'); Remove a FeatureEven if it's not so used, you can also delete a feature easily with Feature::remove('page_code_cleaner'); Warning: 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. Work with ViewsI really love blade directives, they help me writing more elegant code. I prepared a custom blade directive, <div>This is an example template div. Always visible.</div>
@feature('my_awesome_feature')
<p>This paragraph will be visible only if "my_awesome_feature" is enabled!</p>
@endfeature
<div>This is another example template div. Always visible too.</div> A really nice shortcut! Enable/Disable Features for Specific Users/EntitiesEven if the previous things we saw are useful, LaravelFeature is not just about pushing the on/off button on a feature. Sometimes, business necessities require more flexibility. Think about a Canary Release: we want to rollout a feature only to specific users. Or, maybe, just for one tester user. Enable Features Management for Specific UsersLaravelFeature makes this possible, and also easier just as adding a trait to our In fact, all you need to do is to:
...
class User extends Authenticatable implements FeaturableInterface
{
use Notifiable, Featurable;
... Nothing more! LaravelFeature now already knows what to do. Status PriorityPlease 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. Laravel-Feature first checks if the feature is enabled globally, then it goes down at entity-level. Enable/Disable a Feature for a Specific User$user = Auth::user();
// now, the feature "my.feature" is enabled ONLY for $user!
Feature::enableFor('my.feature', $user);
// now, the feature "my.feature" is disabled for $user!
Feature::disableFor('my.feature', $user); Check if a Feature is Enabled for a Specific User$user = Auth::user();
if(Feature::isEnabledFor('my.feature', $user)) {
// do amazing things!
} Other NotesLaravelFeature also provides a Blade directive to check if a feature is enabled for a specific user. You can use the @featurefor('my.feature', $user)
// do $user related things here!
@endfeaturefor Advanced ThingsOk, now that we got the basics, let's raise the bar! Enable Features Management for Other EntitiesAs I told before, you can easily add features management for Users just by using the Let's make an example: imagine that you have a So, you rolled out the amazing killer feature but you want to enable it only for admins. How to do this? Easy. Recap:
Let's think the role-user relationship as one-to-many one. You will probably have a // $role is the admin role!
$role = Auth::user()->role;
...
Feature::enableFor('my.feature', $role);
...
if(Feature::isEnabledFor('my.feature', $role)) {
// this code will be executed only if the user is an admin!
} Scan Directories for FeaturesOne of the nice bonuses of the package that inspired me when making this package, is the ability to "scan" views, find I created a simple artisan command to do this. $ php artisan feature:scan The command will use a dedicated service to fetch the Try it, you will like it! Note: if you have published the config file, you will be able to change the list of scanned directories. Using a Custom Features RepositoryImagine that you want to change the place or the way you store features. For some crazy reason, you want to store it on a static file, or on Dropbox. Now, Eloquent doesn't have a Dropbox driver, so you can't use this package. Bye. Just joking! When making this package, I wanted to be sure to create a fully reusable logic if the developer doesn't want to use Eloquent anymore. To do this, I created a nice interface for the Job, and created some bindings in the Laravel Service Container. Nothing really complex, anyway. The interface I am talking about is <?php
namespace LaravelFeature\Domain\Repository;
use LaravelFeature\Domain\Model\Feature;
use LaravelFeature\Featurable\FeaturableInterface;
interface FeatureRepositoryInterface
{
public function save(Feature $feature);
public function remove(Feature $feature);
public function findByName($featureName);
public function enableFor($featureName, FeaturableInterface $featurable);
public function disableFor($featureName, FeaturableInterface $featurable);
public function isEnabledFor($featureName, FeaturableInterface $featurable);
} This interface has some methods. Let's quickly explain them:
So, you will need to create a new Finally, you will have to change the repository binding in the 'repository' => LaravelFeature\Repository\EloquentFeatureRepository::class will become... 'repository' => My\Wonderful\DropboxFeatureRepository::class Done! By the way, don't forget to let the entities you need to implement the <?php
namespace LaravelFeature\Featurable;
interface FeaturableInterface
{
public function hasFeature($featureName);
} The only method, here, is To have a better idea of the mechanism, feel free to look to the Change logPlease see CHANGELOG for more information on what has changed recently. TestingYou can start tests with a simple: $ phpunit or, also $ composer test There are two separate test suites:
$ phpunit --testsuite=unit
$ phpunit --testsuite=integration ContributingPlease see CONTRIBUTING and CONDUCT for details. SecurityIf you discover any security related issues, please email [email protected] instead of using the issue tracker. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论