Skip to content

Commit

Permalink
Added support for both 2nd and 3rd versions of phpdotenv
Browse files Browse the repository at this point in the history
Implements #25
  • Loading branch information
hiqsol committed Mar 22, 2019
1 parent 9f61a78 commit 597227a
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/readers/EnvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace hiqdev\composer\config\readers;

use Dotenv\Dotenv;
use hiqdev\composer\config\exceptions\UnsupportedFileTypeException;

/**
Expand All @@ -21,15 +22,31 @@ class EnvReader extends AbstractReader
{
public function readRaw($path)
{
if (!class_exists('Dotenv\Dotenv')) {
if (!class_exists(Dotenv::class)) {
throw new UnsupportedFileTypeException('for .env support require `vlucas/phpdotenv` in your composer.json');
}
$info = pathinfo($path);
$dotenv = new \Dotenv\Dotenv($info['dirname'], $info['basename']);
$dotenv = $this->createDotenv($info['dirname'], $info['basename']);
$oldenvs = $_ENV;
$dotenv->load();
$newenvs = $_ENV;

return array_diff_assoc($newenvs, $oldenvs);
}

/**
* Creates Dotenv object.
* Supports both 2 and 3 version of `phpdotenv`
* @param mixed $dir
* @param mixed $file
* @return Dotenv
*/
private function createDotenv($dir, $file)
{
if (method_exists(Dotenv::class, 'create')) {
return Dotenv::create($dir, $file);
} else {
return new Dotenv($dir, $file);
}
}
}

0 comments on commit 597227a

Please sign in to comment.