Config.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Config;
  12. use Hyperf\Collection\Arr;
  13. use Hyperf\Contract\ConfigInterface;
  14. use function Hyperf\Collection\data_get;
  15. use function Hyperf\Collection\data_set;
  16. class Config implements ConfigInterface
  17. {
  18. public function __construct(private array $configs)
  19. {
  20. }
  21. /**
  22. * Finds an entry of the container by its identifier and returns it.
  23. *
  24. * @param string $key identifier of the entry to look for
  25. * @param mixed $default default value of the entry when does not found
  26. * @return mixed entry
  27. */
  28. public function get(string $key, mixed $default = null): mixed
  29. {
  30. return data_get($this->configs, $key, $default);
  31. }
  32. /**
  33. * Returns true if the container can return an entry for the given identifier.
  34. * Returns false otherwise.
  35. *
  36. * @param string $key identifier of the entry to look for
  37. */
  38. public function has(string $key): bool
  39. {
  40. return Arr::has($this->configs, $key);
  41. }
  42. /**
  43. * Set a value to the container by its identifier.
  44. *
  45. * @param string $key identifier of the entry to set
  46. * @param mixed $value the value that save to container
  47. */
  48. public function set(string $key, mixed $value): void
  49. {
  50. data_set($this->configs, $key, $value);
  51. }
  52. }