Upgrade the minimum php version to 8.0 for config and config-center.

This commit is contained in:
李铭昕 2021-11-03 19:41:05 +08:00 committed by GitHub
parent 42b713eaa4
commit c878ddb1c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 92 additions and 358 deletions

View File

@ -16,7 +16,7 @@ use Psr\Container\ContainerInterface;
class AliyunAcmDriver extends AbstractDriver
{
protected $driverName = 'aliyun_acm';
protected string $driverName = 'aliyun_acm';
public function __construct(ContainerInterface $container)
{

View File

@ -17,34 +17,18 @@ use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Guzzle\ClientFactory as GuzzleClientFactory;
use Hyperf\Utils\Codec\Json;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
class Client implements ClientInterface
{
/**
* @var array
*/
public $fetchConfig;
private GuzzleHttp\Client $client;
/**
* @var null|GuzzleHttp\Client
*/
private $client;
private ConfigInterface $config;
/**
* @var ConfigInterface
*/
private $config;
private LoggerInterface $logger;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
/**
* @var array
*/
private $servers;
private array $servers;
/**
* @var array[]
@ -61,11 +45,6 @@ class Client implements ClientInterface
public function pull(): array
{
$client = $this->client;
if (! $client instanceof GuzzleHttp\Client) {
throw new RuntimeException('aliyun acm: Invalid http client.');
}
// ACM config
$endpoint = $this->config->get('config_center.drivers.aliyun_acm.endpoint', 'acm.aliyun.com');
$namespace = $this->config->get('config_center.drivers.aliyun_acm.namespace', '');
@ -93,7 +72,7 @@ class Client implements ClientInterface
try {
if (! $this->servers) {
// server list
$response = $client->get("http://{$endpoint}:8080/diamond-server/diamond");
$response = $this->client->get("http://{$endpoint}:8080/diamond-server/diamond");
if ($response->getStatusCode() !== 200) {
throw new RuntimeException('Get server list failed from Aliyun ACM.');
}
@ -102,7 +81,7 @@ class Client implements ClientInterface
$server = $this->servers[array_rand($this->servers)];
// Get config
$response = $client->get("http://{$server}:8080/diamond-server/config.co", [
$response = $this->client->get("http://{$server}:8080/diamond-server/config.co", [
'headers' => array_merge([
'Spas-AccessKey' => $accessKey,
'timeStamp' => $timestamp,

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Hyperf\ConfigApollo;
use Hyperf\ConfigCenter\AbstractDriver;
use Hyperf\ConfigCenter\Contract\ClientInterface;
use Hyperf\Engine\Channel;
use Hyperf\Utils\Coordinator\Constants;
use Hyperf\Utils\Coordinator\CoordinatorManager;
@ -21,14 +22,14 @@ use Psr\Http\Message\ResponseInterface;
class ApolloDriver extends AbstractDriver
{
protected string $driverName = 'apollo';
protected array $notifications = [];
/**
* @var ClientInterface
* @var \Hyperf\ConfigApollo\ClientInterface
*/
protected $client;
protected $driverName = 'apollo';
protected $notifications = [];
protected ClientInterface $client;
public function __construct(ContainerInterface $container)
{
@ -165,7 +166,7 @@ class ApolloDriver extends AbstractDriver
return $value;
}
protected function updateConfig(array $config)
protected function updateConfig(array $config): void
{
$mergedConfigs = [];
foreach ($config as $c) {

View File

@ -20,41 +20,14 @@ use RuntimeException;
class Client implements ClientInterface
{
/**
* @var ConfigInterface
*/
protected $config;
/**
* @var Option
*/
protected $option;
/**
* @var array
*/
protected $cache = [];
/**
* @var Closure
*/
protected $httpClientFactory;
/**
* @var \Hyperf\Contract\StdoutLoggerInterface
*/
protected $logger;
protected array $cache = [];
public function __construct(
Option $option,
Closure $httpClientFactory,
ConfigInterface $config,
StdoutLoggerInterface $logger
protected Option $option,
protected Closure $httpClientFactory,
protected ConfigInterface $config,
protected StdoutLoggerInterface $logger
) {
$this->option = $option;
$this->httpClientFactory = $httpClientFactory;
$this->config = $config;
$this->logger = $logger;
}
public function pull(): array
@ -95,7 +68,7 @@ class Client implements ClientInterface
'query' => $query,
'headers' => $headers,
]);
if ($response->getStatusCode() === 200 && strpos($response->getHeaderLine('Content-Type'), 'application/json') !== false) {
if ($response->getStatusCode() === 200 && str_contains($response->getHeaderLine('Content-Type'), 'application/json')) {
$body = json_decode((string) $response->getBody(), true);
$result = $body['configurations'] ?? [];
$this->cache[$cacheKey] = [
@ -133,7 +106,7 @@ class Client implements ClientInterface
'notifications' => json_encode(array_values($notifications)),
],
]);
} catch (\Exception $exceptiosn) {
} catch (\Exception) {
// Do nothing
return null;
}

View File

@ -15,45 +15,21 @@ use Hyperf\Utils\Str;
class Option
{
/**
* @var string
*/
private $server = '';
private string $server = '';
/**
* @var string
*/
private $appid = '';
private string $appid = '';
/**
* @var array
*/
private $namespaces = [];
private array $namespaces = [];
/**
* @var string
*/
private $cluster = 'default';
private string $cluster = 'default';
/**
* @var string
*/
private $clientIp = '127.0.0.1';
private string $clientIp = '127.0.0.1';
/**
* @var int
*/
private $pullTimeout = 10;
private int $pullTimeout = 10;
/**
* @var int
*/
private $intervalTimeout = 60;
private int $intervalTimeout = 60;
/**
* @var string
*/
private $secret;
private string $secret = '';
public function buildBaseUrl(): string
{
@ -84,7 +60,7 @@ class Option
return $this->server;
}
public function setServer(string $server): self
public function setServer(string $server): static
{
if (! Str::startsWith($server, ['http://', 'https://'])) {
$server = 'http://' . $server;
@ -98,7 +74,7 @@ class Option
return $this->appid;
}
public function setAppid(string $appid): self
public function setAppid(string $appid): static
{
$this->appid = $appid;
return $this;
@ -109,7 +85,7 @@ class Option
return $this->namespaces;
}
public function setNamespaces(array $namespaces): self
public function setNamespaces(array $namespaces): static
{
$this->namespaces = $namespaces;
return $this;
@ -120,7 +96,7 @@ class Option
return $this->cluster;
}
public function setCluster(string $cluster): self
public function setCluster(string $cluster): static
{
$this->cluster = $cluster;
return $this;
@ -131,7 +107,7 @@ class Option
return $this->clientIp;
}
public function setClientIp(string $clientIp): self
public function setClientIp(string $clientIp): static
{
$this->clientIp = $clientIp;
return $this;
@ -142,7 +118,7 @@ class Option
return $this->pullTimeout;
}
public function setPullTimeout(int $pullTimeout): self
public function setPullTimeout(int $pullTimeout): static
{
$this->pullTimeout = $pullTimeout;
return $this;
@ -153,13 +129,13 @@ class Option
return $this->intervalTimeout;
}
public function setIntervalTimeout(int $intervalTimeout): self
public function setIntervalTimeout(int $intervalTimeout): static
{
$this->intervalTimeout = $intervalTimeout;
return $this;
}
public function setSecret(string $secret): self
public function setSecret(string $secret): static
{
$this->secret = $secret;
return $this;

View File

@ -21,6 +21,7 @@ use Hyperf\Utils\Coordinator\Constants;
use Hyperf\Utils\Coordinator\CoordinatorManager;
use Hyperf\Utils\Coroutine;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Swoole\Server;
abstract class AbstractDriver implements DriverInterface
@ -30,39 +31,18 @@ abstract class AbstractDriver implements DriverInterface
*/
protected $server;
/**
* @var null|ConfigInterface
*/
protected $config;
protected ConfigInterface $config;
/**
* @var StdoutLoggerInterface
*/
protected $logger;
protected LoggerInterface $logger;
/**
* @var ClientInterface
*/
protected $client;
protected ClientInterface $client;
/**
* @var ContainerInterface
*/
protected $container;
protected string $pipeMessage = PipeMessage::class;
/**
* @var null|string
*/
protected $pipeMessage = PipeMessage::class;
protected string $driverName = '';
/**
* @var string
*/
protected $driverName = '';
public function __construct(ContainerInterface $container)
public function __construct(protected ContainerInterface $container)
{
$this->container = $container;
$this->config = $container->get(ConfigInterface::class);
$this->logger = $container->get(StdoutLoggerInterface::class);
}
@ -94,7 +74,7 @@ abstract class AbstractDriver implements DriverInterface
});
}
public function fetchConfig()
public function fetchConfig(): void
{
if (method_exists($this->client, 'pull')) {
$config = $this->pull();
@ -132,7 +112,7 @@ abstract class AbstractDriver implements DriverInterface
return $this->client->pull();
}
protected function updateConfig(array $config)
protected function updateConfig(array $config): void
{
foreach ($config as $key => $value) {
if (is_string($key)) {

View File

@ -11,7 +11,6 @@ declare(strict_types=1);
*/
namespace Hyperf\ConfigCenter;
use Hyperf\ConfigCenter\Listener\BootApplicationListener;
use Hyperf\ConfigCenter\Listener\CreateMessageFetcherLoopListener;
use Hyperf\ConfigCenter\Listener\FetchConfigOnBootListener;
use Hyperf\ConfigCenter\Listener\OnPipeMessageListener;
@ -30,7 +29,6 @@ class ConfigProvider
ConfigFetcherProcess::class,
],
'listeners' => [
BootApplicationListener::class,
FetchConfigOnBootListener::class,
CreateMessageFetcherLoopListener::class,
OnPipeMessageListener::class,

View File

@ -13,7 +13,7 @@ namespace Hyperf\ConfigCenter\Contract;
interface DriverInterface
{
public function fetchConfig();
public function fetchConfig(): void;
public function createMessageFetcherLoop(): void;

View File

@ -16,14 +16,8 @@ use Hyperf\Contract\ConfigInterface;
class DriverFactory
{
/**
* @var ConfigInterface
*/
protected $config;
public function __construct(ConfigInterface $config)
public function __construct(protected ConfigInterface $config)
{
$this->config = $config;
}
public function create(string $driver, array $properties = []): DriverInterface

View File

@ -1,59 +0,0 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\ConfigCenter\Listener;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BootApplication;
use InvalidArgumentException;
/**
* @deprecated v3.0
*/
class BootApplicationListener implements ListenerInterface
{
protected $config;
public function __construct(ConfigInterface $config)
{
$this->config = $config;
}
public function listen(): array
{
return [
BootApplication::class,
];
}
public function process(object $event)
{
if ($this->config->get('config_center.enable', false)) {
return;
}
$configs = [
'config_apollo',
'aliyun_acm',
'config_etcd',
'zookeeper',
];
foreach ($configs as $config) {
if ($this->config->get($config . '.enable', false)) {
throw new InvalidArgumentException(
sprintf('Config [%s] is not supported, please use config_center instead.', $config)
);
}
}
}
}

View File

@ -22,26 +22,11 @@ use Hyperf\Process\Event\PipeMessage as UserProcessPipeMessage;
class OnPipeMessageListener implements ListenerInterface
{
/**
* @var \Hyperf\ConfigCenter\DriverFactory
*/
protected $driverFactory;
/**
* @var ConfigInterface
*/
protected $config;
/**
* @var StdoutLoggerInterface
*/
protected $logger;
public function __construct(DriverFactory $driverFactory, ConfigInterface $config, StdoutLoggerInterface $logger)
{
$this->driverFactory = $driverFactory;
$this->config = $config;
$this->logger = $logger;
public function __construct(
protected DriverFactory $driverFactory,
protected ConfigInterface $config,
protected StdoutLoggerInterface $logger
) {
}
/**

View File

@ -15,14 +15,8 @@ use Hyperf\ConfigCenter\Contract\PipeMessageInterface;
class PipeMessage implements PipeMessageInterface
{
/**
* @var array
*/
protected $data = [];
public function __construct(array $data)
public function __construct(protected array $data)
{
$this->data = $data;
}
public function getData(): array

View File

@ -18,6 +18,7 @@ use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Process\AbstractProcess;
use Hyperf\Process\ProcessManager;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Swoole\Server;
class ConfigFetcherProcess extends AbstractProcess
@ -29,20 +30,11 @@ class ConfigFetcherProcess extends AbstractProcess
*/
protected $server;
/**
* @var ConfigInterface
*/
protected $config;
protected ConfigInterface $config;
/**
* @var StdoutLoggerInterface
*/
protected $logger;
protected LoggerInterface $logger;
/**
* @var \Hyperf\ConfigCenter\DriverFactory
*/
protected $driverFactory;
protected DriverFactory $driverFactory;
public function __construct(ContainerInterface $container)
{

View File

@ -16,20 +16,8 @@ use Hyperf\Etcd\KVInterface;
class Client implements ClientInterface
{
/**
* @var KVInterface
*/
protected $client;
/**
* @var ConfigInterface
*/
protected $config;
public function __construct(KVInterface $client, ConfigInterface $config)
public function __construct(protected KVInterface $client, protected ConfigInterface $config)
{
$this->client = $client;
$this->config = $config;
}
public function pull(): array

View File

@ -12,22 +12,17 @@ declare(strict_types=1);
namespace Hyperf\ConfigEtcd;
use Hyperf\ConfigCenter\AbstractDriver;
use Hyperf\Contract\PackerInterface;
use Hyperf\Utils\Packer\JsonPacker;
use Psr\Container\ContainerInterface;
class EtcdDriver extends AbstractDriver
{
/**
* @var JsonPacker
*/
protected $packer;
protected PackerInterface $packer;
/**
* @var array
*/
protected $mapping;
protected array $mapping = [];
protected $driverName = 'etcd';
protected string $driverName = 'etcd';
public function __construct(ContainerInterface $container)
{
@ -37,7 +32,7 @@ class EtcdDriver extends AbstractDriver
$this->packer = $container->get($this->config->get('config_center.drivers.etcd.packer', JsonPacker::class));
}
protected function updateConfig(array $config)
protected function updateConfig(array $config): void
{
$configurations = $this->format($config);
foreach ($configurations as $kv) {

View File

@ -13,30 +13,15 @@ namespace Hyperf\ConfigEtcd;
class KV
{
/**
* @var null|string
*/
public $key;
public ?string $key;
/**
* @var string
*/
public $createRevision;
public ?string $createRevision;
/**
* @var string
*/
public $modRevision;
public ?string $modRevision;
/**
* @var string
*/
public $version;
public ?string $version;
/**
* @var null|string
*/
public $value;
public ?string $value;
public function __construct($data)
{

View File

@ -18,32 +18,18 @@ use Hyperf\Nacos\Exception\RequestException;
use Hyperf\Utils\Codec\Json;
use Hyperf\Utils\Codec\Xml;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
class Client implements ClientInterface
{
/**
* @var ContainerInterface
*/
protected $container;
protected ConfigInterface $config;
/**
* @var ConfigInterface
*/
protected $config;
protected Application $client;
/**
* @var Application
*/
protected $client;
protected LoggerInterface $logger;
/**
* @var StdoutLoggerInterface
*/
protected $logger;
public function __construct(ContainerInterface $container)
public function __construct(protected ContainerInterface $container)
{
$this->container = $container;
$this->config = $container->get(ConfigInterface::class);
$this->client = $container->get(NacosClient::class);
$this->logger = $container->get(StdoutLoggerInterface::class);
@ -70,10 +56,7 @@ class Client implements ClientInterface
return $config;
}
/**
* @return array|string
*/
public function decode(string $body, ?string $type = null)
public function decode(string $body, ?string $type = null): array|string
{
$type = strtolower((string) $type);
switch ($type) {

View File

@ -17,12 +17,7 @@ use Psr\Container\ContainerInterface;
class NacosDriver extends AbstractDriver
{
/**
* @var Client
*/
protected $client;
protected $driverName = 'nacos';
protected string $driverName = 'nacos';
public function __construct(ContainerInterface $container)
{
@ -30,7 +25,7 @@ class NacosDriver extends AbstractDriver
$this->client = $container->get(ClientInterface::class);
}
protected function updateConfig(array $config)
protected function updateConfig(array $config): void
{
$root = $this->config->get('config_center.drivers.nacos.default_key');
foreach ($config as $key => $conf) {

View File

@ -12,19 +12,12 @@ declare(strict_types=1);
namespace Hyperf\ConfigZookeeper;
use Hyperf\Contract\ConfigInterface;
use Psr\Container\ContainerInterface;
use Swoole\Zookeeper;
class Client implements ClientInterface
{
/**
* @var ConfigInterface
*/
private $config;
public function __construct(ContainerInterface $container)
public function __construct(private ConfigInterface $config)
{
$this->config = $container->get(ConfigInterface::class);
}
public function pull(): array

View File

@ -16,7 +16,7 @@ use Psr\Container\ContainerInterface;
class ZookeeperDriver extends AbstractDriver
{
protected $driverName = 'zookeeper';
protected string $driverName = 'zookeeper';
public function __construct(ContainerInterface $container)
{

View File

@ -14,17 +14,10 @@ namespace Hyperf\Config\Annotation;
use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation;
/**
* @Annotation
* @Target({"PROPERTY"})
*/
#[Attribute(Attribute::TARGET_PROPERTY)]
class Value extends AbstractAnnotation
{
/**
* @var string
*/
public $key;
public ?string $key = null;
public function __construct(...$value)
{

View File

@ -16,14 +16,8 @@ use Hyperf\Utils\Arr;
class Config implements ConfigInterface
{
/**
* @var array
*/
private $configs = [];
public function __construct(array $configs)
public function __construct(private array $configs)
{
$this->configs = $configs;
}
/**
@ -33,7 +27,7 @@ class Config implements ConfigInterface
* @param mixed $default default value of the entry when does not found
* @return mixed entry
*/
public function get(string $key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
return data_get($this->configs, $key, $default);
}
@ -43,9 +37,8 @@ class Config implements ConfigInterface
* Returns false otherwise.
*
* @param string $key identifier of the entry to look for
* @return bool
*/
public function has(string $key)
public function has(string $key): bool
{
return Arr::has($this->configs, $key);
}
@ -56,7 +49,7 @@ class Config implements ConfigInterface
* @param string $key identifier of the entry to set
* @param mixed $value the value that save to container
*/
public function set(string $key, $value)
public function set(string $key, mixed $value): void
{
data_set($this->configs, $key, $value);
}

View File

@ -21,10 +21,7 @@ use function method_exists;
*/
class ProviderConfig
{
/**
* @var array
*/
private static $providerConfigs = [];
private static array $providerConfigs = [];
/**
* Load and merge all provider configs from components.

View File

@ -20,16 +20,15 @@ interface ConfigInterface
* @param mixed $default default value of the entry when does not found
* @return mixed entry
*/
public function get(string $key, $default = null);
public function get(string $key, mixed $default = null): mixed;
/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* @param string $keys identifier of the entry to look for
* @return bool
*/
public function has(string $keys);
public function has(string $keys): bool;
/**
* Set a value to the container by its identifier.
@ -37,5 +36,5 @@ interface ConfigInterface
* @param string $key identifier of the entry to set
* @param mixed $value the value that save to container
*/
public function set(string $key, $value);
public function set(string $key, mixed $value): void;
}

View File

@ -29,13 +29,13 @@ class ProtocolManager
public function register(string $name, array $data)
{
return $this->config->set('protocols.' . $name, $data);
$this->config->set('protocols.' . $name, $data);
}
public function registerOrAppend(string $name, array $data)
{
$key = 'protocols.' . $name;
return $this->config->set($key, array_merge($this->config->get($key, []), $data));
$this->config->set($key, array_merge($this->config->get($key, []), $data));
}
public function getProtocol(string $name): array