symplify-conversion/sst-sdk-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

symplify-conversion/sst-sdk-php

Composer 安装命令:

composer require symplify-conversion/sst-sdk-php

包简介

SDK for Symplify Conversion Server-Side Testing

README 文档

README

This is the PHP implementation of the Symplify Server-Side Testing SDK.

Due to the PHP concurrency model, the SDK is not keeping its own local cache for the config, there simply is no great way to organize the invalidation logic when the purpose of the caching is to keep the hot path fast.

Instead, we recommend you configure the HTTP client (can be injected to the SDK) to do HTTP caching. See Usage below.

Changes

See CHANGELOG.md

Requirements

Installing

Add the dependency through composer:

composer require "symplify-conversion/sst-sdk-php"

Usage

use SymplifyConversion\SSTSDK\Client as SymplifyClient;

// 1. configure the SDK and create an instance

$websiteID  = getenv('SYMPLIFY_WEBSITE_ID');
$sdk = SymplifyClient::withDefaults($websiteID);

// 2. Start off with the "default" values for everyone outside the test
//    and in the original variation.

// assuming $sku is from the request, and you have a $catalog service to look up prices in
$price = $catalog->getCurrentPrice($sku);
$discounts = [];

// 3. Implement your test variation code. (This i in a test project called "discount")
//    `findVariation` will ensure the visitor ID is in cookies, and that the same
//    visitor gets the same variation every request you test.

switch ($sdk->findVariation('discount')) {
case 'huge':
    $discounts[] = 0.1; // perhaps a contrived example with this fallthrough
case 'small':
    $discounts[] = 0.1;
}

// assuming renderProductPage is how you present products
renderProductPage($sku, $price, $discounts);

Note that with the curl HTTP client you get no local caching, and thus depend on our CDN for each request. You can use any HTTP client compatible with PSR-17 and PSR-18 instead of curl, and leverage their caching features. This is of course a bit more involved:

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Psr7\HttpFactory;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
use Kevinrob\GuzzleCache\Storage\Psr16CacheStorage;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kodus\Cache\FileCache;
use SymplifyConversion\SSTSDK\Client as SymplifyClient;
use SymplifyConversion\SSTSDK\Config\ClientConfig as SymplifyClientConfig;

// 1. configure the SDK and create an instance

$websiteID    = getenv('SSTSDK_WEBSITE_ID');

$cache           = new Psr16CacheStorage(new FileCache('/tmp/sstsdk-examples-httpcache', 500));
$cacheMiddleware = new CacheMiddleware(new PublicCacheStrategy($cache));
$stack           = HandlerStack::create();
$stack->push($cacheMiddleware, 'cache');

$clientConfig = (new SymplifyClientConfig($websiteID))
    ->withHttpClient(new HttpClient(['handler' => $stack]))
    ->withHttpRequests(new HttpFactory());

$sdk = new SymplifyClient($clientConfig);
// the constructor does not load config automatically
$sdk->loadConfig();

// steps 2 and 3 are the same as in the previous example

See more examples of code using the SDK in examples.

Cookies

To ensure visitors get the same variation consistently, the SDK needs to read and write cookies. See SST-documentation repository for general cookie setup information and examples folder for code examples.

If you do not have a CookieJar class already, it is advised to use the SymplifyConversion\SSTSDK\Cookies\DefaultCookieJar class.

<?php

use SymplifyConversion\SSTSDK\Client as SymplifyClient;
use SymplifyConversion\SSTSDK\Cookies\DefaultCookieJar;

$websiteID  = "4711";
$cookieDomain = getenv('SSTSDK_COOKIE_DOMAIN');

$client = SymplifyClient::withDefaults($websiteID);
$cookieJar = new DefaultCookieJar($cookieDomain);

foreach ($client->listProjects() as $projectName) {
    echo " * $projectName" . PHP_EOL;
    $variationName = $client->findVariation($projectName, [], $cookieJar);
    echo "   - assigned variation: " . $variationName . PHP_EOL;
}

Custom audience

It's possible to limit for which requests/visitors a certain test project should apply by using "audience" rules. See Audiences.md for details.

The audience is evaluated when your server calls findVariation, and if the rules you have setup in the audience references "custom attributes" your server must provide the values of these attributes for each request.

For example, you might want a test project to only apply for visitors from a certain country. The audience can be configured in your project, using a custom attribute "country", and then your server provides it when finding the variation on each request:

// fictional helper function to get discounts for each request we serve
function getDiscounts($sdk) {
    // This code assumes you have a `lookupGeoIP` helper function in your project.
    $customAttributes = array('country' => lookupGeoIp($usersIPAddress)->getCountry());

    // Custom attributes are passed as an array of key/value pairs, in this case we set 'country'
    // and assume the audience is configured with the "string-attribute" rule to look for specific countries.
    $gotVariation = $sdk->findVariation('Discounts, May 2022', $customAttributes);
    
    switch ($gotVariation) {
        case 'huge':
            return [0.25];
        case 'small':
            return [0.1];
    }

    // `findVariation` returns null if the project audience does not match for
    // a given request. We handle that by a fallthrough return here.
    return [];
}

SDK Development

See CONTRIBUTING.md or RELEASING.md.

symplify-conversion/sst-sdk-php 适用场景与选型建议

symplify-conversion/sst-sdk-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.76k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 symplify-conversion/sst-sdk-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 9.76k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-21