From 7addc3584cc7b3224bb49039569cb4d8ee3ff41c Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Thu, 24 Jan 2019 18:59:31 +0000 Subject: [PATCH 1/3] Bumped version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 20253c8e..34a44c9c 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ }, "extra": { "branch-alias": { - "dev-master": "3.1-dev" + "dev-master": "3.2-dev" } } } From 95598b11fedc9c48bd508d65d42e8a4497e1b33c Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Thu, 24 Jan 2019 19:00:31 +0000 Subject: [PATCH 2/3] Support direct loading with a string --- src/Environment/Adapter/ArrayAdapter.php | 67 ++++++++++++++++++++++++ src/Loader.php | 35 +++++++++---- tests/Dotenv/LinesTest.php | 23 +------- tests/Dotenv/LoaderTest.php | 21 ++++++++ tests/fixtures/env/assertions.env | 3 ++ 5 files changed, 117 insertions(+), 32 deletions(-) create mode 100644 src/Environment/Adapter/ArrayAdapter.php diff --git a/src/Environment/Adapter/ArrayAdapter.php b/src/Environment/Adapter/ArrayAdapter.php new file mode 100644 index 00000000..f3feca16 --- /dev/null +++ b/src/Environment/Adapter/ArrayAdapter.php @@ -0,0 +1,67 @@ + + */ + private $variables = []; + + /** + * Determines if the adapter is supported. + * + * @return bool + */ + public function isSupported() + { + return true; + } + + /** + * Get an environment variable, if it exists. + * + * @param string $name + * + * @return \PhpOption\Option + */ + public function get($name) + { + if (array_key_exists($name, $this->variables)) { + return Some::create($this->variables[$name]); + } + + return None::create(); + } + + /** + * Set an environment variable. + * + * @param string $name + * @param string|null $value + * + * @return void + */ + public function set($name, $value = null) + { + $this->variables[$name] = $value; + } + + /** + * Clear an environment variable. + * + * @param string $name + * + * @return void + */ + public function clear($name) + { + unset($this->variables[$name]); + } +} diff --git a/src/Loader.php b/src/Loader.php index 36e84507..db1a3dcf 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -79,21 +79,37 @@ public function setImmutable($immutable = false) } /** - * Load `.env` file in given directory. + * Load the environment file from disk. * - * @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException + * @throws \Dotenv\Exception\InvalidFileException * * @return array */ public function load() + { + return $this->loadDirect( + self::findAndRead($this->filePaths) + ); + } + + /** + * Directly load the given string. + * + * @param string $content + * + * @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException + * + * @return array + */ + public function loadDirect($content) { return $this->processEntries( - Lines::process(self::readLines($this->filePaths)) + Lines::process(preg_split("/(\r\n|\n|\r)/", $content)) ); } /** - * Attempt to read the lines from the files in order. + * Attempt to read the files in order. * * @param string[] $filePaths * @@ -101,7 +117,7 @@ public function load() * * @return string[] */ - private static function readLines(array $filePaths) + private static function findAndRead(array $filePaths) { if ($filePaths === []) { throw new InvalidPathException('At least one environment file path must be provided.'); @@ -120,7 +136,7 @@ private static function readLines(array $filePaths) } /** - * Read from the file, auto detecting line endings. + * Read the given file. * * @param string $filePath * @@ -128,12 +144,9 @@ private static function readLines(array $filePaths) */ private static function readFromFile($filePath) { - $autodetect = ini_get('auto_detect_line_endings'); - ini_set('auto_detect_line_endings', '1'); - $lines = @file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); - ini_set('auto_detect_line_endings', $autodetect); + $content = @file_get_contents($filePath); - return Option::fromValue($lines, false); + return Option::fromValue($content, false); } /** diff --git a/tests/Dotenv/LinesTest.php b/tests/Dotenv/LinesTest.php index 1545af30..e88e9321 100644 --- a/tests/Dotenv/LinesTest.php +++ b/tests/Dotenv/LinesTest.php @@ -5,28 +5,9 @@ class LinesTest extends TestCase { - /** - * @var string|false - */ - protected $autodetect; - - public function setUp() - { - $this->autodetect = ini_get('auto_detect_line_endings'); - ini_set('auto_detect_line_endings', '1'); - } - - public function tearDown() - { - ini_set('auto_detect_line_endings', $this->autodetect); - } - public function testProcess() { - $content = file( - dirname(__DIR__).'/fixtures/env/assertions.env', - FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES - ); + $content = file_get_contents(dirname(__DIR__).'/fixtures/env/assertions.env'); $expected = [ 'ASSERTVAR1=val1', @@ -40,6 +21,6 @@ public function testProcess() "ASSERTVAR9=\"\n\"", ]; - $this->assertSame($expected, Lines::process($content)); + $this->assertSame($expected, Lines::process(preg_split("/(\r\n|\n|\r)/", $content))); } } diff --git a/tests/Dotenv/LoaderTest.php b/tests/Dotenv/LoaderTest.php index 2d7434b9..90bc71a6 100644 --- a/tests/Dotenv/LoaderTest.php +++ b/tests/Dotenv/LoaderTest.php @@ -1,5 +1,6 @@ assertCount(4, $loader->load()); } + + public function testLoaderWithNoAdapters() + { + $loader = (new Loader([], new DotenvFactory([]))); + + $content = "NVAR1=\"Hello\"\nNVAR2=\"World!\"\nNVAR3=\"{\$NVAR1} {\$NVAR2}\"\nNVAR4=\"\${NVAR1} \${NVAR2}\""; + $expected = ['NVAR1' => 'Hello', 'NVAR2' => 'World!', 'NVAR3' => '{$NVAR1} {$NVAR2}', 'NVAR4' => '${NVAR1} ${NVAR2}']; + + $this->assertSame($expected, $loader->loadDirect($content)); + } + + public function testLoaderWithArrayAdapter() + { + $loader = (new Loader([], new DotenvFactory([new ArrayAdapter()]))); + + $content = "NVAR1=\"Hello\"\nNVAR2=\"World!\"\nNVAR3=\"{\$NVAR1} {\$NVAR2}\"\nNVAR4=\"\${NVAR1} \${NVAR2}\""; + $expected = ['NVAR1' => 'Hello', 'NVAR2' => 'World!', 'NVAR3' => '{$NVAR1} {$NVAR2}', 'NVAR4' => 'Hello World!']; + + $this->assertSame($expected, $loader->loadDirect($content)); + } } diff --git a/tests/fixtures/env/assertions.env b/tests/fixtures/env/assertions.env index f82325df..b640e6a6 100644 --- a/tests/fixtures/env/assertions.env +++ b/tests/fixtures/env/assertions.env @@ -1,6 +1,7 @@ ASSERTVAR1=val1 ASSERTVAR2="" + ASSERTVAR3="val3 " ASSERTVAR4="0" # empty looking value ASSERTVAR5=#foo @@ -8,6 +9,8 @@ ASSERTVAR6="val1 val2" ASSERTVAR7=" val3" # + + ASSERTVAR8="val3 " ASSERTVAR9=" From 2e03131665c463195ab93b277ca3190a14de16f3 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Thu, 24 Jan 2019 19:53:44 +0000 Subject: [PATCH 3/3] Corrected phpdoc --- src/Loader.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Loader.php b/src/Loader.php index db1a3dcf..4cb98027 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -81,7 +81,7 @@ public function setImmutable($immutable = false) /** * Load the environment file from disk. * - * @throws \Dotenv\Exception\InvalidFileException + * @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException * * @return array */ @@ -97,7 +97,7 @@ public function load() * * @param string $content * - * @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException + * @throws \Dotenv\Exception\InvalidFileException * * @return array */