FileStore.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. declare(strict_types=1);
  3. namespace Dotenv\Store;
  4. use Dotenv\Exception\InvalidPathException;
  5. use Dotenv\Store\File\Reader;
  6. final class FileStore implements StoreInterface
  7. {
  8. /**
  9. * The file paths.
  10. *
  11. * @var string[]
  12. */
  13. private $filePaths;
  14. /**
  15. * Should file loading short circuit?
  16. *
  17. * @var bool
  18. */
  19. private $shortCircuit;
  20. /**
  21. * The file encoding.
  22. *
  23. * @var string|null
  24. */
  25. private $fileEncoding;
  26. /**
  27. * Create a new file store instance.
  28. *
  29. * @param string[] $filePaths
  30. * @param bool $shortCircuit
  31. * @param string|null $fileEncoding
  32. *
  33. * @return void
  34. */
  35. public function __construct(array $filePaths, bool $shortCircuit, string $fileEncoding = null)
  36. {
  37. $this->filePaths = $filePaths;
  38. $this->shortCircuit = $shortCircuit;
  39. $this->fileEncoding = $fileEncoding;
  40. }
  41. /**
  42. * Read the content of the environment file(s).
  43. *
  44. * @throws \Dotenv\Exception\InvalidEncodingException|\Dotenv\Exception\InvalidPathException
  45. *
  46. * @return string
  47. */
  48. public function read()
  49. {
  50. if ($this->filePaths === []) {
  51. throw new InvalidPathException('At least one environment file path must be provided.');
  52. }
  53. $contents = Reader::read($this->filePaths, $this->shortCircuit, $this->fileEncoding);
  54. if (\count($contents) > 0) {
  55. return \implode("\n", $contents);
  56. }
  57. throw new InvalidPathException(
  58. \sprintf('Unable to read any of the environment file(s) at [%s].', \implode(', ', $this->filePaths))
  59. );
  60. }
  61. }