bluepsyduck/factorio-mod-portal-client

用于访问Factorio模组门户的客户端。

1.4.2 2021-12-22 16:29 UTC

This package is auto-updated.

Last update: 2024-09-22 22:41:46 UTC


README

GitHub release (latest SemVer) GitHub build Codecov

此库实现了一个PHP客户端,用于访问Factorio模组门户的API。

用法

客户端附带了一个ConfigProvider,可以直接将其集成到Zend Expressive项目中。在其他环境中使用它需要额外的配置,这些配置在README文件中没有涵盖。

要在Zend Expressive环境中使用客户端,请将BluePsyduck\FactorioModPortalClient\ConfigProvider添加到您的配置聚合器中,并从容器中获取实际客户端或外观。

配置

客户端提供了零配置设置。然而,为了使用全部功能集,需要配置一些值

<?php

use BluePsyduck\FactorioModPortalClient\Constant\ConfigKey;

return [
    ConfigKey::MAIN => [
        ConfigKey::OPTIONS => [
            // Your Factorio username. This username is used to build a full download link, avoiding getting redirected
            // to the login page. 
            // See https://wiki.factorio.com/Mod_portal_API#Downloading_Mods for further details.
            ConfigKey::OPTION_USERNAME => 'your-username',
            // The token to your username.
            ConfigKey::OPTION_TOKEN => 'your-token',

            // The timeout in seconds to use for the request. Defaults to 10 seconds.
            ConfigKey::OPTION_TIMEOUT => 10,
        ],
    ],
];

示例

从模组门户API请求数据有两种基本方法:使用外观进行简单访问,或直接使用客户端以使用全部功能。

使用外观很简单:从容器中请求外观实例,传递请求并获取响应

<?php
/* @var \Psr\Container\ContainerInterface $container */

use BluePsyduck\FactorioModPortalClient\Client\Facade;
use BluePsyduck\FactorioModPortalClient\Request\ModRequest; 

/* @var Facade $facade */
$facade = $container->get(Facade::class);

$mod = $facade->getMod((new ModRequest())->setName('FARL'));

// Do something with the received mod.
var_dump($mod->getDownloadsCount());

使用客户端本身允许您使用客户端返回的Promises进行并行请求

<?php
/* @var \Psr\Container\ContainerInterface $container */

use BluePsyduck\FactorioModPortalClient\Client\ClientInterface;
use BluePsyduck\FactorioModPortalClient\Request\ModRequest; 
use BluePsyduck\FactorioModPortalClient\Response\ModResponse;
use function GuzzleHttp\Promise\all;

/* @var ClientInterface $client */
$client = $container->get(ClientInterface::class);

$request1 = (new ModRequest())->setName('FARL');
$request2 = (new ModRequest())->setName('FNEI');

/* @var ModResponse[] $responses*/
$responses = all([
    'FARL' => $client->sendRequest($request1),
    'FNEI' => $client->sendRequest($request2)
])->wait();

// Do something with the responses
var_dump($responses['FARL']->getSummary());
var_dump($responses['FNEI']->getSummary());

此外,外观和客户端都提供了将下载和资产路径转换为完整URL的方法

<?php 
/* @var BluePsyduck\FactorioModPortalClient\Client\Facade $facade */
/* @var BluePsyduck\FactorioModPortalClient\Response\ModResponse $response */

$downloadUrl = $facade->getDownloadUrl($response->getReleases()[0]->getDownloadUrl());
$thumbnail = $facade->getAssetUrl($response->getThumbnail());