Env.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Illuminate\Support;
  3. use Dotenv\Repository\Adapter\PutenvAdapter;
  4. use Dotenv\Repository\RepositoryBuilder;
  5. use PhpOption\Option;
  6. use RuntimeException;
  7. class Env
  8. {
  9. /**
  10. * Indicates if the putenv adapter is enabled.
  11. *
  12. * @var bool
  13. */
  14. protected static $putenv = true;
  15. /**
  16. * The environment repository instance.
  17. *
  18. * @var \Dotenv\Repository\RepositoryInterface|null
  19. */
  20. protected static $repository;
  21. /**
  22. * Enable the putenv adapter.
  23. *
  24. * @return void
  25. */
  26. public static function enablePutenv()
  27. {
  28. static::$putenv = true;
  29. static::$repository = null;
  30. }
  31. /**
  32. * Disable the putenv adapter.
  33. *
  34. * @return void
  35. */
  36. public static function disablePutenv()
  37. {
  38. static::$putenv = false;
  39. static::$repository = null;
  40. }
  41. /**
  42. * Get the environment repository instance.
  43. *
  44. * @return \Dotenv\Repository\RepositoryInterface
  45. */
  46. public static function getRepository()
  47. {
  48. if (static::$repository === null) {
  49. $builder = RepositoryBuilder::createWithDefaultAdapters();
  50. if (static::$putenv) {
  51. $builder = $builder->addAdapter(PutenvAdapter::class);
  52. }
  53. static::$repository = $builder->immutable()->make();
  54. }
  55. return static::$repository;
  56. }
  57. /**
  58. * Get the value of an environment variable.
  59. *
  60. * @param string $key
  61. * @param mixed $default
  62. * @return mixed
  63. */
  64. public static function get($key, $default = null)
  65. {
  66. return self::getOption($key)->getOrCall(fn () => value($default));
  67. }
  68. /**
  69. * Get the value of a required environment variable.
  70. *
  71. * @param string $key
  72. * @return mixed
  73. *
  74. * @throws \RuntimeException
  75. */
  76. public static function getOrFail($key)
  77. {
  78. return self::getOption($key)->getOrThrow(new RuntimeException("Environment variable [$key] has no value."));
  79. }
  80. /**
  81. * Get the possible option for this environment variable.
  82. *
  83. * @param string $key
  84. * @return \PhpOption\Option|\PhpOption\Some
  85. */
  86. protected static function getOption($key)
  87. {
  88. return Option::fromValue(static::getRepository()->get($key))
  89. ->map(function ($value) {
  90. switch (strtolower($value)) {
  91. case 'true':
  92. case '(true)':
  93. return true;
  94. case 'false':
  95. case '(false)':
  96. return false;
  97. case 'empty':
  98. case '(empty)':
  99. return '';
  100. case 'null':
  101. case '(null)':
  102. return;
  103. }
  104. if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
  105. return $matches[2];
  106. }
  107. return $value;
  108. });
  109. }
  110. }