定制 featurit/featurit-sdk-symfony 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

featurit/featurit-sdk-symfony

Composer 安装命令:

composer require featurit/featurit-sdk-symfony

包简介

Symfony SDK for FeaturIT

README 文档

README

Symfony wrapper of the PHP client for the FeaturIT Feature Flag management platform.

Description

This package aims to simplify the integration of the FeaturIT API in a Symfony project.

Getting started

Dependencies

  • PHP >= 8.0.2
  • symfony/framework-bundle >= 5.2
  • psr/http-client-implementation
  • psr/simple-cache-implementation

Installing

composer require featurit/featurit-sdk-symfony -W

If there's no package providing psr/http-client-implementation, visit https://packagist.org/providers/psr/http-client-implementation and choose the package that better suits your project.

If there's no package providing psr/simple-cache-implementation, visit https://packagist.org/providers/psr/simple-cache-implementation and choose the package that better suits your project.

Inside your config/bundles.php file, add:

    Featurit\Client\Symfony\FeaturitBundle::class => ['all' => true],

If you want to create your own configuration file in order to customize things like the default FeaturitUserContextProvider, create a file in config/packages/featurit.yaml with the following contents:

featurit:
    tenant_identifier: '%env(string:FEATURIT_TENANT_IDENTIFIER)%'
    environment_key: '%env(string:FEATURIT_ENVIRONMENT_KEY)%'
    enable_analytics: '%env(bool:FEATURIT_ENABLE_ANALYTICS)%'
    enable_tracking: '%env(bool:FEATURIT_ENABLE_TRACKING)%'
    cache_ttl_minutes: '%env(int:FEATURIT_CACHE_TTL_MINUTES)%'
    send_analytics_interval_minutes: '%env(int:FEATURIT_SEND_ANALYTICS_INTERVAL_MINUTES)%'
    featurit_user_context_provider: '@my_service_implementing_featurit_user_context_provider'

You also need to create the proper environment variables in your .env file.

Basic Usage

That's how you would use Featurit in one of your controllers, services, or anywhere inside your PHP codebase:

your_method(Featurit $featurit)
{
    if ($featurit->isActive('YOUR_FEATURE_NAME')) {
        your_feature_code();
    }
}

Or in order to check which is the version of your feature:

your_method(Featurit $featurit)
{
    if ($featurit->version('YOUR_FEATURE_NAME') == 'v1') {
        your_feature_code_for_v1();
    } else if ($featurit->version('YOUR_FEATURE_NAME') == 'v2') {
        your_feature_code_for_v2();
    }
}

Twig extension

For convenience we provide 2 twig functions which allow to render html depending on the Feature Flag values.

Inside your twig template, you can use them like this:

<div>
    <h2>This code will always be visible</h2>

    {% if feature_is_active('MY_ACTIVE_FEATURE') %}
        <h2>Welcome to MY_ACTIVE_FEATURE!</h2>
    {% endif %}

    {% if feature_version_equals('FEATURE_WITH_VERSIONS', 'v1') %}
        <h2>Welcome to v1!</h2>
    {% elseif feature_version_equals('FEATURE_WITH_VERSIONS', 'v2') %}
        <h2>Welcome to v2!</h2>
    {% endif %}
</div>

Defining your FeaturitUserContext

In order to show different versions of a feature to different users, Featurit needs to know about the attributes your user has in a certain context.

You can define the context using the as follows:

your_method(Featurit $featurit)
{
    $contextData = get_your_user_context_data();

    $featurit->setUserContext(
        new DefaultFeaturitUserContext(
            $contextData['userId'],
            $contextData['sessionId'],
            $contextData['ipAddress'],
            [
                'role' => $contextData['role'],
                ...
            ]
        )
    );
}

Defining a custom FeaturitUserContextProvider

This is an alternative to using $featurit->setUserContext(...);.

By default, Featurit SDK for Symfony comes with a default FeaturitUserContextProvider adapted for Symfony, but if you want to create your own, create a service un your services.yaml file as follows:

services:
...
    Namespace\For\MyFeaturitUserContextProvider
        arguments:
            - arg1
            - arg2
            - ...

And then add it to the featurit.yaml config file as:

featurit:
    ...
    featurit_user_context_provider: '@Namespace\For\MyFeaturitUserContextProvider'

Let's say that your platform users have a "role" attribute that you use to decide which features you show to each user. In that case you could create an implementation like:

<?php

namespace My\Namespace\Of\Choice;

use Featurit\Client\Modules\Segmentation\DefaultFeaturitUserContext;
use Featurit\Client\Modules\Segmentation\FeaturitUserContext;
use Featurit\Client\Modules\Segmentation\FeaturitUserContextProvider;

class MyCustomFeaturitUserContextProvider implements FeaturitUserContextProvider
{
    public function getUserContext(): FeaturitUserContext
    {
        $userId = my_logic_to_get_the_user_identifier();
        $sessionId = my_logic_to_get_the_session_id();
        $ipAddress = my_logic_to_get_the_ip_address();
        
        $role = my_logic_to_get_the_user_role();

        return new DefaultFeaturitUserContext(
            $userId,
            $sessionId,
            $ipAddress,
            [
                'role' => $role,
            ]
        );
    }
}

Then you must replace your implementation in the featurit.yaml file as explained before.

And that should do it, from now on your segmentation rules will use the role attribute.

Event Tracking

In order to track some event in your application, you can add this once the event has happened:

your_method(Featurit $featurit)
{
    $contextData = get_your_user_context_data();

    $featurit->setUserContext(
        new DefaultFeaturitUserContext(
            $contextData['userId'],
            $contextData['sessionId'],
            $contextData['ipAddress'],
            [
                'role' => $contextData['role'],
                ...
            ]
        )
    );
    
    $featurit->trackPerson();
    
    // trackPerson will track a new Person with the data set in the FeaturitUserContext.
    
    $featurit->track('YOUR_EVENT_NAME', [
        'an_event_property_name' => 'an_event_property_value',
        'another_event_property_name' => 'another_event_property_value',
    ]);
}

All the events you track in the same request will be accumulated and associated to the current FeaturitUserContext, if for some reason you want to send the event immediately, you can do as follows:

$featurit->flush();

Custom cache implementation

By default, FeaturIT SDK for Symfony provides a simple File cache. Once you have multiple servers, you will want to inject your custom Psr\SimpleCache\CacheInterface implementation using some distributed cache system.

This can be done as follows:

featurit:
    tenant_identifier: '%env(string:FEATURIT_TENANT_IDENTIFIER)%'
    environment_key: '%env(string:FEATURIT_ENVIRONMENT_KEY)%'
    enable_analytics: '%env(bool:FEATURIT_ENABLE_ANALYTICS)%'
    enable_tracking: '%env(bool:FEATURIT_ENABLE_TRACKING)%'
    cache_ttl_minutes: '%env(int:FEATURIT_CACHE_TTL_MINUTES)%'
    send_analytics_interval_minutes: '%env(int:FEATURIT_SEND_ANALYTICS_INTERVAL_MINUTES)%'
    featurit_user_context_provider: '@my_service_implementing_featurit_user_context_provider'
    cache: '@my_custom_psr_cache_interface_implementation'

Authors

FeaturIT

https://www.featurit.com

featurit.tech@gmail.com

featurit/featurit-sdk-symfony 适用场景与选型建议

featurit/featurit-sdk-symfony 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.34k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「feature」 「feature flag」 「feature toggle」 「a b testing」 「user targeting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 featurit/featurit-sdk-symfony 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 featurit/featurit-sdk-symfony 我们能提供哪些服务?
定制开发 / 二次开发

基于 featurit/featurit-sdk-symfony 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-06