vanilla/garden-sites

包含本地和托管vanilla论坛站点管理和定义的接口和实现。

v1.6.0 2024-04-10 21:18 UTC

This package is auto-updated.

Last update: 2024-09-10 22:11:26 UTC


README

用于管理来自本地或编排服务器的站点和集群列表的库。

安装

composer require vanilla/garden-sites

用法

使用此库的入口是Garden\Sites\LocalSiteProviderGarden\Sites\OrchSiteProvider

LocalSiteProvider

use Garden\Sites\Local\LocalSiteProvider;

$provider = new LocalSiteProvider("/path/to/site/configs");

值得注意的是,站点配置的路径必须是PHP进程可读的目录。

本地站点提供者通过读取给定目录中的基于PHP的配置文件来工作。这读取了由vnla docker设置识别的所有站点。要使站点配置被识别,它必须满足以下条件

  • 文件名称符合以下模式之一
    • /*.php - 变为 *.vanilla.localhost
    • /vanilla.localhost/*.php - 变为 vanilla.localhost/*
    • /e2e-tests.vanilla.localhost/*.php - 变为 e2e-tests.vanilla.localhost/*
  • 文件包含有效的PHP配置文件。
  • 配置包含以下值
    • Vanilla.SiteID
    • Vanilla.AccountID
    • 可选的Vanilla.ClusterID。默认为cl00000

集群

默认情况下,所有本地站点都在同一个集群cl00000上。您可以通过在站点中添加Vanilla.ClusterID配置来绕过此设置。

集群配置可以添加到/clusters目录中,并命名为/clusters/cl*.php。这些应该是与站点类似的PHP配置文件。这些文件中的配置将与站点配置合并。

OrchSiteProvider

orch站点提供者从远程编排HTTP服务器加载站点和集群。站点、集群和配置缓存1分钟。

use Garden\Sites\Clients\OrchHttpClient;
use Garden\Sites\Orch\OrchSiteProvider;
use Garden\Sites\Orch\OrchCluster;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Contracts\Cache\CacheInterface;

$orchHttpClient = new OrchHttpClient("https://orch.vanilla.localhost", "access-token-here");
$siteProvider = new OrchSiteProvider($orchHttpClient, [OrchCluster::REGION_AMS1_PROD1]);

// It is highly recommended to set a user-agent for network requests.
$siteProvider->setUserAgent("my-service:1.0");

/**
 * Site providers do various caching of results. By default an in-memory cache is used, but especially with an orch-client
 * it is recommended to configure a persistent cache like memcached or redis.
 * Caches must implement {@link CacheInterface}
 */

$cache = new RedisAdapter(/** Configuration here. */);
// or
$cache = new MemcachedAdapter(/** Configuration here. */);

$siteProvider->setCache($cache);

# Region can be changed later
$siteProvider->setRegionIDs([OrchCluster::REGION_YUL1_PROD1, OrchCluster::REGION_AMS1_PROD1]);

编排提供者需要配置认证的OrchHttpClient和要从中加载站点的区域/网络。

使用站点提供者

OrchSiteProviderLocalSiteProvider都从SiteProvider扩展,并实现类似的功能。

use Garden\Sites\Exceptions\ClusterNotFoundException;
use Garden\Sites\Exceptions\SiteNotFoundException;
use Garden\Sites\SiteProvider;

function doSomethingWithProvider(SiteProvider $siteProvider)
{
    /**
     * Look up a site by ID.
     * Can throw an {@link SiteNotFoundException}
     */
    $site = $siteProvider->getSite(100);

    // List all sites
    $allSites = $siteProvider->getSites();

    /**
     * Look up a cluster by ID.
     * Can throw an {@link ClusterNotFoundException}
     */
    $cluster = $siteProvider->getCluster("cl10001");

    // List all clusters
    $allClusters = $siteProvider->getClusters();
}

使用集群和站点

use Garden\Sites\Site;
use Garden\Sites\Cluster;

function doSomethingWithCluster(Cluster $cluster)
{
    // A few getters
    $clusterID = $cluster->getClusterID();
    $regionID = $cluster->getRegionID();
}

function doSomethingWithSite(Site $site)
{
    // A few getters
    $siteID = $site->getSiteID();
    $accountID = $site->getAccountID();
    $clusterID = $site->getClusterID();
    $baseUrl = $site->getBaseUrl();

    // HTTP Requests
    // This is a `garden-http` configured with the site's baseUrl
    // and set to throw on errors.
    $response = $site->httpClient()->get("/url", ["query" => "params"]);
    $response = $site->httpClient()->post("/url", ["body" => "here"]);
    $response = $site->httpClient()->patch("/url", ["body" => "here"]);
    $response = $site->httpClient()->put("/url", ["body" => "here"]);
    $response = $site->httpClient()->delete("/url");

    // System auth for an http request
    $site
        ->httpClient()
        ->withSystemAuth()
        ->get("/some-resource");

    // Ensure there is no auth on a request
    $site
        ->httpClient()
        ->withNoAuth()
        ->get("/some-resource");

    // Access the cluster
    $cluster = $site->getCluster();

    // Configs
    $configVal = $site->getConfigValueByKey("some.key.here", "fallback");

    // Configs are cached on the `Garden\Sites\Site` instance
    // You can clear them here.
    $site->clearConfigCache();

    // Check services hostnames the site should be using.
    $baseUrl = $site->getQueueServiceBaseUrl();
    $baseUrl = $site->getSearchServiceBaseUrl();
}