Dotenv.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. declare(strict_types=1);
  3. namespace Dotenv;
  4. use Dotenv\Exception\InvalidPathException;
  5. use Dotenv\Loader\Loader;
  6. use Dotenv\Loader\LoaderInterface;
  7. use Dotenv\Parser\Parser;
  8. use Dotenv\Parser\ParserInterface;
  9. use Dotenv\Repository\Adapter\ArrayAdapter;
  10. use Dotenv\Repository\Adapter\PutenvAdapter;
  11. use Dotenv\Repository\RepositoryBuilder;
  12. use Dotenv\Repository\RepositoryInterface;
  13. use Dotenv\Store\StoreBuilder;
  14. use Dotenv\Store\StoreInterface;
  15. use Dotenv\Store\StringStore;
  16. class Dotenv
  17. {
  18. /**
  19. * The store instance.
  20. *
  21. * @var \Dotenv\Store\StoreInterface
  22. */
  23. private $store;
  24. /**
  25. * The parser instance.
  26. *
  27. * @var \Dotenv\Parser\ParserInterface
  28. */
  29. private $parser;
  30. /**
  31. * The loader instance.
  32. *
  33. * @var \Dotenv\Loader\LoaderInterface
  34. */
  35. private $loader;
  36. /**
  37. * The repository instance.
  38. *
  39. * @var \Dotenv\Repository\RepositoryInterface
  40. */
  41. private $repository;
  42. /**
  43. * Create a new dotenv instance.
  44. *
  45. * @param \Dotenv\Store\StoreInterface $store
  46. * @param \Dotenv\Parser\ParserInterface $parser
  47. * @param \Dotenv\Loader\LoaderInterface $loader
  48. * @param \Dotenv\Repository\RepositoryInterface $repository
  49. *
  50. * @return void
  51. */
  52. public function __construct(
  53. StoreInterface $store,
  54. ParserInterface $parser,
  55. LoaderInterface $loader,
  56. RepositoryInterface $repository
  57. ) {
  58. $this->store = $store;
  59. $this->parser = $parser;
  60. $this->loader = $loader;
  61. $this->repository = $repository;
  62. }
  63. /**
  64. * Create a new dotenv instance.
  65. *
  66. * @param \Dotenv\Repository\RepositoryInterface $repository
  67. * @param string|string[] $paths
  68. * @param string|string[]|null $names
  69. * @param bool $shortCircuit
  70. * @param string|null $fileEncoding
  71. *
  72. * @return \Dotenv\Dotenv
  73. */
  74. public static function create(RepositoryInterface $repository, $paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
  75. {
  76. $builder = $names === null ? StoreBuilder::createWithDefaultName() : StoreBuilder::createWithNoNames();
  77. foreach ((array) $paths as $path) {
  78. $builder = $builder->addPath($path);
  79. }
  80. foreach ((array) $names as $name) {
  81. $builder = $builder->addName($name);
  82. }
  83. if ($shortCircuit) {
  84. $builder = $builder->shortCircuit();
  85. }
  86. return new self($builder->fileEncoding($fileEncoding)->make(), new Parser(), new Loader(), $repository);
  87. }
  88. /**
  89. * Create a new mutable dotenv instance with default repository.
  90. *
  91. * @param string|string[] $paths
  92. * @param string|string[]|null $names
  93. * @param bool $shortCircuit
  94. * @param string|null $fileEncoding
  95. *
  96. * @return \Dotenv\Dotenv
  97. */
  98. public static function createMutable($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
  99. {
  100. $repository = RepositoryBuilder::createWithDefaultAdapters()->make();
  101. return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
  102. }
  103. /**
  104. * Create a new mutable dotenv instance with default repository with the putenv adapter.
  105. *
  106. * @param string|string[] $paths
  107. * @param string|string[]|null $names
  108. * @param bool $shortCircuit
  109. * @param string|null $fileEncoding
  110. *
  111. * @return \Dotenv\Dotenv
  112. */
  113. public static function createUnsafeMutable($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
  114. {
  115. $repository = RepositoryBuilder::createWithDefaultAdapters()
  116. ->addAdapter(PutenvAdapter::class)
  117. ->make();
  118. return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
  119. }
  120. /**
  121. * Create a new immutable dotenv instance with default repository.
  122. *
  123. * @param string|string[] $paths
  124. * @param string|string[]|null $names
  125. * @param bool $shortCircuit
  126. * @param string|null $fileEncoding
  127. *
  128. * @return \Dotenv\Dotenv
  129. */
  130. public static function createImmutable($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
  131. {
  132. $repository = RepositoryBuilder::createWithDefaultAdapters()->immutable()->make();
  133. return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
  134. }
  135. /**
  136. * Create a new immutable dotenv instance with default repository with the putenv adapter.
  137. *
  138. * @param string|string[] $paths
  139. * @param string|string[]|null $names
  140. * @param bool $shortCircuit
  141. * @param string|null $fileEncoding
  142. *
  143. * @return \Dotenv\Dotenv
  144. */
  145. public static function createUnsafeImmutable($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
  146. {
  147. $repository = RepositoryBuilder::createWithDefaultAdapters()
  148. ->addAdapter(PutenvAdapter::class)
  149. ->immutable()
  150. ->make();
  151. return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
  152. }
  153. /**
  154. * Create a new dotenv instance with an array backed repository.
  155. *
  156. * @param string|string[] $paths
  157. * @param string|string[]|null $names
  158. * @param bool $shortCircuit
  159. * @param string|null $fileEncoding
  160. *
  161. * @return \Dotenv\Dotenv
  162. */
  163. public static function createArrayBacked($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
  164. {
  165. $repository = RepositoryBuilder::createWithNoAdapters()->addAdapter(ArrayAdapter::class)->make();
  166. return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
  167. }
  168. /**
  169. * Parse the given content and resolve nested variables.
  170. *
  171. * This method behaves just like load(), only without mutating your actual
  172. * environment. We do this by using an array backed repository.
  173. *
  174. * @param string $content
  175. *
  176. * @throws \Dotenv\Exception\InvalidFileException
  177. *
  178. * @return array<string,string|null>
  179. */
  180. public static function parse(string $content)
  181. {
  182. $repository = RepositoryBuilder::createWithNoAdapters()->addAdapter(ArrayAdapter::class)->make();
  183. $phpdotenv = new self(new StringStore($content), new Parser(), new Loader(), $repository);
  184. return $phpdotenv->load();
  185. }
  186. /**
  187. * Read and load environment file(s).
  188. *
  189. * @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidEncodingException|\Dotenv\Exception\InvalidFileException
  190. *
  191. * @return array<string,string|null>
  192. */
  193. public function load()
  194. {
  195. $entries = $this->parser->parse($this->store->read());
  196. return $this->loader->load($this->repository, $entries);
  197. }
  198. /**
  199. * Read and load environment file(s), silently failing if no files can be read.
  200. *
  201. * @throws \Dotenv\Exception\InvalidEncodingException|\Dotenv\Exception\InvalidFileException
  202. *
  203. * @return array<string,string|null>
  204. */
  205. public function safeLoad()
  206. {
  207. try {
  208. return $this->load();
  209. } catch (InvalidPathException $e) {
  210. // suppressing exception
  211. return [];
  212. }
  213. }
  214. /**
  215. * Required ensures that the specified variables exist, and returns a new validator object.
  216. *
  217. * @param string|string[] $variables
  218. *
  219. * @return \Dotenv\Validator
  220. */
  221. public function required($variables)
  222. {
  223. return (new Validator($this->repository, (array) $variables))->required();
  224. }
  225. /**
  226. * Returns a new validator object that won't check if the specified variables exist.
  227. *
  228. * @param string|string[] $variables
  229. *
  230. * @return \Dotenv\Validator
  231. */
  232. public function ifPresent($variables)
  233. {
  234. return new Validator($this->repository, (array) $variables);
  235. }
  236. }