StoreBuilder.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. namespace Dotenv\Store;
  4. use Dotenv\Store\File\Paths;
  5. final class StoreBuilder
  6. {
  7. /**
  8. * The of default name.
  9. */
  10. private const DEFAULT_NAME = '.env';
  11. /**
  12. * The paths to search within.
  13. *
  14. * @var string[]
  15. */
  16. private $paths;
  17. /**
  18. * The file names to search for.
  19. *
  20. * @var string[]
  21. */
  22. private $names;
  23. /**
  24. * Should file loading short circuit?
  25. *
  26. * @var bool
  27. */
  28. private $shortCircuit;
  29. /**
  30. * The file encoding.
  31. *
  32. * @var string|null
  33. */
  34. private $fileEncoding;
  35. /**
  36. * Create a new store builder instance.
  37. *
  38. * @param string[] $paths
  39. * @param string[] $names
  40. * @param bool $shortCircuit
  41. * @param string|null $fileEncoding
  42. *
  43. * @return void
  44. */
  45. private function __construct(array $paths = [], array $names = [], bool $shortCircuit = false, string $fileEncoding = null)
  46. {
  47. $this->paths = $paths;
  48. $this->names = $names;
  49. $this->shortCircuit = $shortCircuit;
  50. $this->fileEncoding = $fileEncoding;
  51. }
  52. /**
  53. * Create a new store builder instance with no names.
  54. *
  55. * @return \Dotenv\Store\StoreBuilder
  56. */
  57. public static function createWithNoNames()
  58. {
  59. return new self();
  60. }
  61. /**
  62. * Create a new store builder instance with the default name.
  63. *
  64. * @return \Dotenv\Store\StoreBuilder
  65. */
  66. public static function createWithDefaultName()
  67. {
  68. return new self([], [self::DEFAULT_NAME]);
  69. }
  70. /**
  71. * Creates a store builder with the given path added.
  72. *
  73. * @param string $path
  74. *
  75. * @return \Dotenv\Store\StoreBuilder
  76. */
  77. public function addPath(string $path)
  78. {
  79. return new self(\array_merge($this->paths, [$path]), $this->names, $this->shortCircuit, $this->fileEncoding);
  80. }
  81. /**
  82. * Creates a store builder with the given name added.
  83. *
  84. * @param string $name
  85. *
  86. * @return \Dotenv\Store\StoreBuilder
  87. */
  88. public function addName(string $name)
  89. {
  90. return new self($this->paths, \array_merge($this->names, [$name]), $this->shortCircuit, $this->fileEncoding);
  91. }
  92. /**
  93. * Creates a store builder with short circuit mode enabled.
  94. *
  95. * @return \Dotenv\Store\StoreBuilder
  96. */
  97. public function shortCircuit()
  98. {
  99. return new self($this->paths, $this->names, true, $this->fileEncoding);
  100. }
  101. /**
  102. * Creates a store builder with the specified file encoding.
  103. *
  104. * @param string|null $fileEncoding
  105. *
  106. * @return \Dotenv\Store\StoreBuilder
  107. */
  108. public function fileEncoding(string $fileEncoding = null)
  109. {
  110. return new self($this->paths, $this->names, $this->shortCircuit, $fileEncoding);
  111. }
  112. /**
  113. * Creates a new store instance.
  114. *
  115. * @return \Dotenv\Store\StoreInterface
  116. */
  117. public function make()
  118. {
  119. return new FileStore(
  120. Paths::filePaths($this->paths, $this->names),
  121. $this->shortCircuit,
  122. $this->fileEncoding
  123. );
  124. }
  125. }