Просмотр файла vendor/vlucas/phpdotenv/src/Dotenv.php

Размер файла: 1.99Kb
  1. <?php
  2.  
  3. namespace Dotenv;
  4.  
  5. /**
  6. * This is the dotenv class.
  7. *
  8. * It's responsible for loading a `.env` file in the given directory and
  9. * setting the environment vars.
  10. */
  11. class Dotenv
  12. {
  13. /**
  14. * The file path.
  15. *
  16. * @var string
  17. */
  18. protected $filePath;
  19.  
  20. /**
  21. * The loader instance.
  22. *
  23. * @var \Dotenv\Loader|null
  24. */
  25. protected $loader;
  26.  
  27. /**
  28. * Create a new dotenv instance.
  29. *
  30. * @param string $path
  31. * @param string $file
  32. *
  33. * @return void
  34. */
  35. public function __construct($path, $file = '.env')
  36. {
  37. $this->filePath = $this->getFilePath($path, $file);
  38. $this->loader = new Loader($this->filePath, true);
  39. }
  40.  
  41. /**
  42. * Load environment file in given directory.
  43. *
  44. * @return array
  45. */
  46. public function load()
  47. {
  48. return $this->loadData();
  49. }
  50.  
  51. /**
  52. * Load environment file in given directory.
  53. *
  54. * @return array
  55. */
  56. public function overload()
  57. {
  58. return $this->loadData(true);
  59. }
  60.  
  61. /**
  62. * Returns the full path to the file.
  63. *
  64. * @param string $path
  65. * @param string $file
  66. *
  67. * @return string
  68. */
  69. protected function getFilePath($path, $file)
  70. {
  71. if (!is_string($file)) {
  72. $file = '.env';
  73. }
  74.  
  75. $filePath = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file;
  76.  
  77. return $filePath;
  78. }
  79.  
  80. /**
  81. * Actually load the data.
  82. *
  83. * @param bool $overload
  84. *
  85. * @return array
  86. */
  87. protected function loadData($overload = false)
  88. {
  89. $this->loader = new Loader($this->filePath, !$overload);
  90.  
  91. return $this->loader->load();
  92. }
  93.  
  94. /**
  95. * Required ensures that the specified variables exist, and returns a new validator object.
  96. *
  97. * @param string|string[] $variable
  98. *
  99. * @return \Dotenv\Validator
  100. */
  101. public function required($variable)
  102. {
  103. return new Validator((array) $variable, $this->loader);
  104. }
  105. }