Loads Variables by reading a file from disk and: - stripping comments beginning with a # - parsing lines that look shell variable setters, e.g export key = value, key="value"
Example #1
0
 /**
  * @inheritDoc
  */
 public function boot()
 {
     $filePath = rtrim($this->kernel->getRootPath(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.env';
     $envLoader = new Loader($filePath);
     if ($this->kernel->isCli()) {
         /** @var InputInterface $consoleInput */
         $consoleInput = $this->container->get(InputInterface::class);
         $env = $consoleInput->getParameterOption(['--env', '-e']);
         if ($env) {
             $envLoader->setEnvironmentVariable('APP_ENV', $env);
         }
     }
 }
Example #2
0
 /**
  * Dotenv constructor.
  * @param string $filePath
  */
 public function __construct($filePath)
 {
     parent::__construct($filePath);
     if (file_exists($filePath)) {
         $this->load();
     }
 }
Example #3
0
 public function clearEnvironmentVariable($name)
 {
     parent::cleanEnvironmentVariable($name);
     if (!$this->immutable) {
         unset($this->notenv_data[$name]);
     }
 }
Example #4
0
 /**
  * Assert that the callback returns true for each variable.
  *
  * @param callable $callback
  * @param string   $message
  *
  * @return \Dotenv\Validator
  */
 protected function assertCallback($callback, $message = 'failed callback assertion')
 {
     if (!is_callable($callback)) {
         throw new \InvalidArgumentException('Callback must be callable');
     }
     $variablesFailingAssertion = array();
     foreach ($this->variables as $variableName) {
         $variableValue = $this->loader->getEnvironmentVariable($variableName);
         if (call_user_func($callback, $variableValue) === false) {
             $variablesFailingAssertion[] = $variableName . " {$message}";
         }
     }
     if (count($variablesFailingAssertion) > 0) {
         throw new \RuntimeException(sprintf('One or more environment variables failed assertions: %s', implode(', ', $variablesFailingAssertion)));
     }
     return $this;
 }
Example #5
0
 /**
  * @override
  * @inheritDoc
  */
 public function clearEnvironmentVariable($name)
 {
     if (strpos($name, 'INI_') === 0) {
         $normalized = str_replace('INI_', '', $name);
         $normalized = strtolower($normalized);
         $this->invoker->call('ini_restore', [$normalized]);
     }
     parent::clearEnvironmentVariable($name);
 }
Example #6
0
 /**
  * Load `.env` file in given directory.
  *
  * @return array
  */
 public function overload()
 {
     $this->loader = new Loader($this->filePath, $immutable = false);
     return $this->loader->load();
 }
Example #7
0
 /**
  * Actually load the data.
  *
  * @param bool $overload
  *
  * @return array
  */
 protected function loadData($overload = false)
 {
     $this->loader = new Loader($this->filePath, !$overload);
     return $this->loader->load();
 }