mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Adds ConfigChanged
event for config-center
(#5418)
This commit is contained in:
parent
55b3c4677c
commit
f980943bf1
@ -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
|
||||
|
||||
|
@ -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()) {
|
||||
|
19
src/config-center/src/Event/ConfigChanged.php
Normal file
19
src/config-center/src/Event/ConfigChanged.php
Normal 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)
|
||||
{
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user