Adds ConfigChanged event for config-center (#5418)

This commit is contained in:
Deeka Wong 2023-02-16 12:55:02 +08:00 committed by GitHub
parent 55b3c4677c
commit f980943bf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 0 deletions

View File

@ -9,6 +9,7 @@
- [#5407](https://github.com/hyperf/hyperf/pull/5407) Support HTTP methods `Delete` and `Options` for swagger.
- [#5409](https://github.com/hyperf/hyperf/pull/5409) Adds `methods` for `Query\Builder` and `Paginator`.
- [#5414](https://github.com/hyperf/hyperf/pull/5414) Added `clone` method to `Hyperf\Database\Model\Builder`.
- [#5418](https://github.com/hyperf/hyperf/pull/5418) Added `ConfigChanged` event to `config-center`.
## Fixed

View File

@ -14,6 +14,7 @@ namespace Hyperf\ConfigCenter;
use Hyperf\ConfigCenter\Contract\ClientInterface;
use Hyperf\ConfigCenter\Contract\DriverInterface;
use Hyperf\ConfigCenter\Contract\PipeMessageInterface;
use Hyperf\ConfigCenter\Event\ConfigChanged;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Coordinator\Constants;
@ -22,6 +23,7 @@ use Hyperf\Process\ProcessCollector;
use Hyperf\Utils\Coroutine;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Swoole\Server;
use Throwable;
@ -65,6 +67,7 @@ abstract class AbstractDriver implements DriverInterface
$config = $this->pull();
if ($config !== $prevConfig) {
$this->syncConfig($config);
$this->event(new ConfigChanged($config, $prevConfig));
}
$prevConfig = $config;
} catch (Throwable $exception) {
@ -100,6 +103,11 @@ abstract class AbstractDriver implements DriverInterface
return $this;
}
protected function event(object $event)
{
$this->container->get(EventDispatcherInterface::class)?->dispatch($event);
}
protected function syncConfig(array $config)
{
if (class_exists(ProcessCollector::class) && ! ProcessCollector::isEmpty()) {

View File

@ -0,0 +1,19 @@
<?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\Event;
class ConfigChanged
{
public function __construct(public array $current, public array $previous)
{
}
}

View File

@ -31,6 +31,12 @@ class ContainerStub
$container = Mockery::mock(Container::class);
ApplicationContext::setContainer($container);
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn(new class() {
public function dispatch()
{
return true;
}
});
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn($config);
$container->shouldReceive('get')->with(StdoutLoggerInterface::class)->andReturnUsing(function () {
$logger = Mockery::mock(StdoutLoggerInterface::class);