solidworx/toggler
Composer 安装命令:
composer require solidworx/toggler
包简介
Feature toggle library
README 文档
README
Toggler is a feature toggle library for PHP. It allows you to enable or disable features based on a toggle switch. This is useful in a continues deployment environment, where you can deploy not-yet-ready features which are disabled, and just enable them when the feature is complete.
Table of Contents
Requirements
Toggler requires PHP 8.1+ and Symfony 6.1+
Installation
Composer
$ composer require solidworx/toggler:^2.0
Usage
Quick Example
<?php use SolidWorx\Toggler\Toggle; use SolidWorx\Toggler\Storage\ArrayStorage; $features = [ 'foo' => true, 'bar' => false ]; $toggle = new Toggle(new ArrayStorage($features));
You can then check if a feature is active or not using the isActive call
<?php $toggle->isActive('foo'); // true $toggle->isActive('bar'); // false
StorageFactory
Toggler comes with many storage adapters to store the configuration. The most basic is the ArrayStorage class, which takes an array of features.
The StorageFactory class acts as a factory to create the config. You can pass it any value, and it will determine which storage adapter to use.
To get an instance of the config, you can use the static factory method
<?php use SolidWorx\Toggler\Storage\StorageFactory; $features = [ 'foo' => true, 'bar' => false ]; $config = StorageFactory::factory($features); // $config will be an instance of ArrayStorage // Using YAML $config = StorageFactory::factory('/path/to/config.yml'); // $config will be an instance of YamlFileStorage
Each feature flag need to be a truthy value in order to be enabled.
The following truthy values are accepted:
- (boolean) true
- (int) 1
- '1'
- 'on'
- 'yes'
- 'y'
Using callbacks
You can also use closures or callbacks to retrieve the value
<?php $features = [ 'foo' => function () { return true; }, 'bar' => [$myObject, 'checkBar'] ];
Storage Adapters
Toggler supports various storage adapters to store the config.
Array
The most basic config is using an array with the ArrayStorage adapter.
<?php use SolidWorx\Toggler\Storage\StorageFactory; use SolidWorx\Toggler\Storage\ArrayStorage; use SolidWorx\Toggler\Toggle; $features = [ 'foo' => true, 'bar' => false ]; $toggle = new Toggle(new ArrayStorage($features)); // Or using the StorageFactory factory $toggle = new Toggle(StorageFactory::factory($features));
ENV
Reads values from environment variables.
<?php use SolidWorx\Toggler\Storage\EnvStorage; use SolidWorx\Toggler\Toggle; $toggle = new Toggle(new EnvStorage()); $toggle->isActive('MY_AWESOME_FEATURE'); // true if the environment variable MY_AWESOME_FEATURE is set to a truthy value
YAML
In order to use yml files for config, you need to include the Symfony Yaml Component
To install and use the Yaml component, run the following command from the root of your project:
$ composer require symfony/yaml
Then you can define your config using a yaml file
// config.yml
foo: true
bar: false
Pass the path to the yml file to your config
<?php use SolidWorx\Toggler\Storage\StorageFactory; use SolidWorx\Toggler\Storage\YamlFileStorage; use SolidWorx\Toggler\Toggle; $toggle = new Toggle(new YamlFileStorage('/path/to/config.yml')); // Or using the StorageFactory factory $toggle = new Toggle(StorageFactory::factory('/path/to/config.yml'));
PHP File
You can store your config in a separate PHP file.
This fille needs to return an array with the config.
By default, PHP files always use the ArrayStorage adapter.
<?php // config.php return[ 'foo' => true, 'bar' => false, ];
Pass the path to the PHP file to your config
<?php use SolidWorx\Toggler\Storage\StorageFactory; use SolidWorx\Toggler\Toggle; $toggle = new Toggle(StorageFactory::factory('/path/to/config.php'));
Redis
You can use Redis to store the configs.
You will then need to either install the Predis library or the Redis PHP extension.
To install Predis, run the following command from the root of your project:
$ composer require predis/predis
The RedisStorage adapter can take any class instance of Redis, RedisArray, RedisCluster or Predis\Client.
<?php use SolidWorx\Toggler\Storage\RedisStorage; use SolidWorx\Toggler\Toggle; $redis = new \Redis(); $toggle = new Toggle(new RedisStorage($redis));
Database
You can use a database to store the configs.
Toggler comes with a PDOStorage adapter, which can be used with any database that supports PDO.
<?php use SolidWorx\Toggler\Storage\PDOStorage; use SolidWorx\Toggler\Toggle; $toggle = new Toggle(new PDOStorage('mysql:host=localhost', 'username', 'password', 'my_feature_table_name'));
Persistent Storage
Toggler supports persisting config values if a storage adapter implements the SolidWorx\Toggler\Storage\PersistenStorageInterface.
The following storage adapters currently supports persisting config values:
- YamlFileStorage
- RedisStorage
- PDOStorage
To update a feature, use the set method:
<?php $toggle->set('foo', true); // This will enable the foo feature $toggle->set('bar', false); // This will disable the bar feature
Toggle a feature based on context
To enable a feature only under specific conditions (E.G only enable it for users in a certain group, or only enable it for 10% of visitor etc)
Each feature in the config can take a callback, where you can return a truthy value based on any logic you want to add:
<?php $features = [ 'foo' => function (User $user) { return in_array('admin', $user->getGroups()); // Only enable features for users in the 'admin' group }, 'bar' => function () { return (crc32($_SERVER['REMOTE_ADDR']) % 100) < 25; // Only enable this features for about 25% of visitors }, 'baz' => function (Request $request) { return false !== strpos($request->headers->get('referer'), 'facebook.com'); // Only enable this features for users that come from Facebook } ];
Callbacks that takes any arguments, should be called with the context:
<?php $user = User::find(); // Get the current logged-in user if ($toggle->isActive('foo', [$user])) { } if ($toggle->isActive('bar', [$request])) { }
Using Symfony Expression Language
You can use the Symfony Expression Language Component to create expressions for your features.
To install and use the Expression Language component, run the following command from the root of your project:
$ composer require symfony/expression-language
Then you can create an expression for your feature:
<?php use Symfony\Component\ExpressionLanguage\Expression; $feaures = [ 'foo' => new Expression('valueOne > 10 and valueTwo < 10') ];
When checking the feature, you need to pass the context to use in your expression:
<?php if ($toggle->isActive('foo', ['valueOne' => 25, 'valueTwo' => 5])) { // Will return true }
Twig Integration
Toggler comes with an optional Twig extension, which allows you to toggle elements from Twig templates.
To use the extension, register it with Twig
<?php use SolidWorx\Toggler\Twig\Extension\ToggleExtension; $twig = new \Twig_Environment($loader); $twig->addExtension(new ToggleExtension($toggle));
or if you use symfony, register it as a service.
Note: When using the [Symfony Bundle](Symfony Integration), the twig extension is automatically registered.
Then you can use the toggle tag in twig templates:
{% toggle 'foo' %}
Some content that will only display if foo is enabled
{% endtoggle %}
To add an alternative if a feature is not available, use the else tag
{% toggle 'foo' %}
Some content that will only display if foo is enabled
{% else %}
Some content that will only display if foo is not enabled
{% endtoggle %}
To use context values with the tag, you can pass it using the with keyword:
{% toggle 'foo' with {"valueOne" : 12} %}
Some content that will only display if foo is enabled based on the context provided
{% endtoggle %}
You can also use the toggle() function for conditions
{{ toggle('foo') ? 'Foo is enabled' : 'Foo is NOT enabled' }}
Symfony Integration
Toggler comes with integration with the Symfony framework.
To enable toggler inside symfony, register the bundle
// config/bundles.php return array( ... SolidWorx\Toggler\Symfony\TogglerBundle::class => ['all' => true], ... );
Then create a config/packages/toggler.yaml config file, to enable features
toggler: config: features: foo: true bar: false # Callables is also supported baz: '@my.service.class' # Class must be callable (I.E implement the __invoke() method) foobar: ['@my.service.class', 'foobar'] # Will call the `foobar` method on the service class baz: ['My\Awesome\Feature\Class', 'checkFeature'] # Will call the static method `checkFeature` on the `My\Awesome\Feature\Class` class # The last two lines can be written as the following: foobar: '@my.service.class::foobar' baz: 'My\Awesome\Feature\Class::checkFeature'
If you want to use an expression for a feature config, you can use the @= syntax:
toggler: config: features: foo: '@=myValue > 10'
If you want to use a storage class, you can use the storage config parameter to define a service for the storage:
services: my.toggler.storage: class: SolidWorx\Toggler\Storage\RedisStorage arguments: ['@redis'] toggler: config: storage: '@my.toggler.storage'
Note: The features and storage options can't be used together. You must use either the one or the other. At least one of the two must be defined.
Note: When using the Symfony bundle, the twig extension is automatically registered.
Note: The symfony/security-core package is required with symfony/framework-bundle.
Console Commands
The Symfony Bundle comes with 2 pre-registered console commands.
Get the status of a feature
To see if a feature is enabled or not, run the following command
$ php bin/console toggler:get foo
This will output the status of a feature.
You can also get the status of multiple features by passing in multiple values:
$ php bin/console toggler:get foo bar baz
This will show whether the features foo, bar and baz is enabled or not.
Get the value using context values
To test if a feature will be enabled under certain conditions, you can pass context values to the command using either the -c or --context flags.
Multiple values for the context can be provided.
Note: Context values can only be strings. Objects are not supported.
$ php bin/console toggler:get foo -c myValue=10 -c anotherValue=25
Set the value of a feature
You can enable or disable a feature using the toggler:set command.
Note: You can only change the status of a feature if you are using a persistent storage.
$ php bin/console toggler:set foo true
This will enable the foo feature.
List all available features
You can list all available features using the toggler:list command.
$ php bin/console toggler:list
This will display all available features and their status.
Testing
To run the unit tests, execute the following command
$ vendor/bin/phpunit
Contributing
See CONTRIBUTING
License
Toggler is open-sourced software licensed under the MIT license
Please see the LICENSE file for the full license.
solidworx/toggler 适用场景与选型建议
solidworx/toggler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79.09k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2017 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「feature」 「toggle」 「feature switch」 「switch」 「feature toggle」 「feature library」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 solidworx/toggler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 solidworx/toggler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 solidworx/toggler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A block that displays featured content - large image, title, description and link.
Laravel Eloquent boolean & timestamp flagged attributes behavior.
Composer mirrors manager
A Laravel package giving you control over which database to use and when without having to micro-manage (also for broadcasting, cache, filesystem, logging and queue connections)
MaxAl Subscriptions is a flexible plans and subscription management system for Laravel, with the required tools to run your SAAS like services efficiently. It's simple architecture, accompanied by powerful underlying to afford solid platform for your business.
yii2 toggle column for update in gridview
统计信息
- 总下载量: 79.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-01-24