DriverFactory.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\ConfigCenter;
  12. use Hyperf\ConfigCenter\Contract\DriverInterface;
  13. use Hyperf\Contract\ConfigInterface;
  14. use function Hyperf\Support\make;
  15. class DriverFactory
  16. {
  17. public function __construct(protected ConfigInterface $config)
  18. {
  19. }
  20. public function create(string $driver, array $properties = []): DriverInterface
  21. {
  22. $defaultDriver = $this->config->get('config_center.driver', '');
  23. $config = $this->config->get('config_center.drivers.' . $driver, []);
  24. $class = $config['driver'] ?? $defaultDriver;
  25. $instance = make($class, $config);
  26. foreach ($properties as $method => $value) {
  27. if (method_exists($instance, $method)) {
  28. $instance->{$method}($value);
  29. }
  30. }
  31. return $instance;
  32. }
  33. }