Functions.php 833 B

1234567891011121314151617181920212223242526272829303132
  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\Context\ApplicationContext;
  13. use Hyperf\Contract\ConfigInterface;
  14. use RuntimeException;
  15. function config(string $key, mixed $default = null): mixed
  16. {
  17. if (! ApplicationContext::hasContainer()) {
  18. throw new RuntimeException('The application context lacks the container.');
  19. }
  20. $container = ApplicationContext::getContainer();
  21. if (! $container->has(ConfigInterface::class)) {
  22. throw new RuntimeException('ConfigInterface is missing in container.');
  23. }
  24. return $container->get(ConfigInterface::class)->get($key, $default);
  25. }