diff --git a/CHANGELOG-3.0.md b/CHANGELOG-3.0.md index f76d7a254..ce850dcd1 100644 --- a/CHANGELOG-3.0.md +++ b/CHANGELOG-3.0.md @@ -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 diff --git a/src/config-center/src/AbstractDriver.php b/src/config-center/src/AbstractDriver.php index 3883d00f8..3a805b1d6 100644 --- a/src/config-center/src/AbstractDriver.php +++ b/src/config-center/src/AbstractDriver.php @@ -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()) { diff --git a/src/config-center/src/Event/ConfigChanged.php b/src/config-center/src/Event/ConfigChanged.php new file mode 100644 index 000000000..14a4aafd6 --- /dev/null +++ b/src/config-center/src/Event/ConfigChanged.php @@ -0,0 +1,19 @@ +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);