EnvConfigurator.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
  11. use Symfony\Component\Config\Loader\ParamConfigurator;
  12. class EnvConfigurator extends ParamConfigurator
  13. {
  14. /**
  15. * @var string[]
  16. */
  17. private $stack;
  18. public function __construct(string $name)
  19. {
  20. $this->stack = explode(':', $name);
  21. }
  22. public function __toString(): string
  23. {
  24. return '%env('.implode(':', $this->stack).')%';
  25. }
  26. /**
  27. * @return $this
  28. */
  29. public function __call(string $name, array $arguments): self
  30. {
  31. $processor = strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1_\2', $name));
  32. $this->custom($processor, ...$arguments);
  33. return $this;
  34. }
  35. /**
  36. * @return $this
  37. */
  38. public function custom(string $processor, ...$args): self
  39. {
  40. array_unshift($this->stack, $processor, ...$args);
  41. return $this;
  42. }
  43. /**
  44. * @return $this
  45. */
  46. public function base64(): self
  47. {
  48. array_unshift($this->stack, 'base64');
  49. return $this;
  50. }
  51. /**
  52. * @return $this
  53. */
  54. public function bool(): self
  55. {
  56. array_unshift($this->stack, 'bool');
  57. return $this;
  58. }
  59. /**
  60. * @return $this
  61. */
  62. public function not(): self
  63. {
  64. array_unshift($this->stack, 'not');
  65. return $this;
  66. }
  67. /**
  68. * @return $this
  69. */
  70. public function const(): self
  71. {
  72. array_unshift($this->stack, 'const');
  73. return $this;
  74. }
  75. /**
  76. * @return $this
  77. */
  78. public function csv(): self
  79. {
  80. array_unshift($this->stack, 'csv');
  81. return $this;
  82. }
  83. /**
  84. * @return $this
  85. */
  86. public function file(): self
  87. {
  88. array_unshift($this->stack, 'file');
  89. return $this;
  90. }
  91. /**
  92. * @return $this
  93. */
  94. public function float(): self
  95. {
  96. array_unshift($this->stack, 'float');
  97. return $this;
  98. }
  99. /**
  100. * @return $this
  101. */
  102. public function int(): self
  103. {
  104. array_unshift($this->stack, 'int');
  105. return $this;
  106. }
  107. /**
  108. * @return $this
  109. */
  110. public function json(): self
  111. {
  112. array_unshift($this->stack, 'json');
  113. return $this;
  114. }
  115. /**
  116. * @return $this
  117. */
  118. public function key(string $key): self
  119. {
  120. array_unshift($this->stack, 'key', $key);
  121. return $this;
  122. }
  123. /**
  124. * @return $this
  125. */
  126. public function url(): self
  127. {
  128. array_unshift($this->stack, 'url');
  129. return $this;
  130. }
  131. /**
  132. * @return $this
  133. */
  134. public function queryString(): self
  135. {
  136. array_unshift($this->stack, 'query_string');
  137. return $this;
  138. }
  139. /**
  140. * @return $this
  141. */
  142. public function resolve(): self
  143. {
  144. array_unshift($this->stack, 'resolve');
  145. return $this;
  146. }
  147. /**
  148. * @return $this
  149. */
  150. public function default(string $fallbackParam): self
  151. {
  152. array_unshift($this->stack, 'default', $fallbackParam);
  153. return $this;
  154. }
  155. /**
  156. * @return $this
  157. */
  158. public function string(): self
  159. {
  160. array_unshift($this->stack, 'string');
  161. return $this;
  162. }
  163. /**
  164. * @return $this
  165. */
  166. public function trim(): self
  167. {
  168. array_unshift($this->stack, 'trim');
  169. return $this;
  170. }
  171. /**
  172. * @return $this
  173. */
  174. public function require(): self
  175. {
  176. array_unshift($this->stack, 'require');
  177. return $this;
  178. }
  179. }