symplify-conversion/sst-sdk-php

用于 Symplify Conversion 服务器端测试的 SDK

v0.5.0 2023-04-24 09:20 UTC

This package is auto-updated.

Last update: 2024-09-24 12:23:36 UTC


README

这是 Symplify 服务器端测试 SDK 的 PHP 实现。Symplify 服务器端测试 SDK

由于 PHP 并发模型,SDK 不保留自己的本地配置缓存,因为没有很好的方式来组织缓存失效逻辑,因为缓存的目的是保持热点路径快速。

相反,我们建议您配置 HTTP 客户端(可以注入到 SDK 中)以执行 HTTP 缓存。请参见下面的使用方法

更改

查看 CHANGELOG.md

需求

安装

通过 composer 添加依赖

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

使用方法

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);

注意,使用 curl HTTP 客户端时,您将没有本地缓存,因此每个请求都依赖于我们的 CDN。您可以使用与 PSR-17 和 PSR-18 兼容的任何 HTTP 客户端来代替 curl,并利用它们的缓存功能。当然,这会稍微复杂一些

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

examples 中查看 SDK 的更多使用示例。

Cookies

为了确保访客始终获得相同的一致性,SDK 需要读取和写入 cookies。有关一般 cookie 设置信息,请参阅 SST-documentation 存储库,有关代码示例,请参阅 examples 文件夹。

如果您还没有 CookieJar 类,建议使用 SymplifyConversion\SSTSDK\Cookies\DefaultCookieJar 类。

<?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;
}

自定义受众

通过使用“受众”规则,可以限制某些测试项目适用于哪些请求/访客。有关详细信息,请参阅 Audicences.md

当您的服务器调用 findVariation 时评估受众,如果您的受众设置中引用了“自定义属性”,则您的服务器必须为每个请求提供这些属性的值。

例如,您可能希望测试项目仅适用于来自特定国家的访客。受众可以在项目中配置,使用自定义属性“country”,然后在每次请求中查找变体时由您的服务器提供它

// 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 开发

请参阅 CONTRIBUTING.mdRELEASING.md