tenolo/apilyzer

一个用于轻松创建REST API客户端的库

dev-master / 1.0.x-dev 2019-08-02 07:35 UTC

This package is auto-updated.

Last update: 2024-08-29 04:49:05 UTC


README

tenolo

Latest Stable Version Latest Stable Version Total Downloads Monthly Downloads Latest Unstable Version License

Apilyzer

一个用于轻松创建REST API客户端的库。

安装说明

Composer

首先,您需要将 tenolo/apilyzer 添加到 composer.json 文件中

手动添加

{
   "require": {
        "tenolo/apilyzer": "~1.0"
    }
}

或者直接执行 composer require tenolo/apilyzer

请注意,dev-master 是最新开发版本。当然,您也可以使用显式的版本号,例如 1.0.*^1.0

HTTP 客户端和工厂

第二步,您需要在生产或开发环境中添加 HTTP-Client 和 HTTP-Factory 到您的项目。

您需要一个或多个实现以下包的库

  • psr/http-message
  • psr/http-client
  • psr/http-factory-implementation
  • php-http/client-implementation

这个库故意不提供这些包,以便每个API客户端都可以实现自己的。

我们建议使用 nyholm/psr7php-http/guzzle6-adapter

生产使用: composer require nyholm/psr7 php-http/guzzle6-adapter
开发使用: composer require --dev nyholm/psr7 php-http/guzzle6-adapter

使用说明

第一步

创建自己的 GatewayConfig 类。

<?php

namespace App\Api\Gateway;

use Tenolo\Apilyzer\Gateway\Config as BaseConfig;

/**
 * Class Config
 */
class Config extends BaseConfig
{

    /**
     * @inheritDoc
     */
    public function getGatewayUrl(): string
    {
        return 'https://BASE.URL.TO.API.com/';
    }
}
<?php

namespace App\Api\Gateway;

use Tenolo\Apilyzer\Gateway\Gateway as BaseGateway;
use Tenolo\Apilyzer\Manager\EndpointManager;
use Tenolo\Apilyzer\Manager\EndpointManagerInterface;

/**
 * Class Gateway
 */
class Gateway extends BaseGateway
{

    /** @var EndpointManagerInterface */
    protected $endpointManager;

    /**
     * @inheritDoc
     */
    protected function getEndpointManager(): EndpointManagerInterface
    {
        if ($this->endpointManager === null) {
            $this->endpointManager = $this->createEndpointManager();
        }

        return $this->endpointManager;
    }
    
    /**
    * @return EndpointManagerInterface
    */
    protected function createEndpointManager(): EndpointManagerInterface 
    {
        return new EndpointManager(__DIR__.'/../Endpoint');
    }
}