Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vlucas/phpdotenv
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v5.0.1
Choose a base ref
...
head repository: vlucas/phpdotenv
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v5.1.0
Choose a head ref
  • 5 commits
  • 5 files changed
  • 1 contributor

Commits on Jul 14, 2020

  1. Bumped version

    GrahamCampbell committed Jul 14, 2020

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    2ed1021 View commit details
  2. Implemented Dotenv::parse()

    Thanks for @taylorotwell for the name!
    GrahamCampbell committed Jul 14, 2020

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    ad9eb3f View commit details
  3. Merge branch '5.0'

    GrahamCampbell committed Jul 14, 2020

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    46702d3 View commit details
  4. Merge branch '5.0'

    GrahamCampbell committed Jul 14, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8ef2595 View commit details
  5. Merge branch '5.0'

    GrahamCampbell committed Jul 14, 2020

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    448c76d View commit details
Showing with 123 additions and 1 deletion.
  1. +18 −0 README.md
  2. +1 −1 composer.json
  3. +40 −0 src/Dotenv.php
  4. +37 −0 src/Store/StringStore.php
  5. +27 −0 tests/Dotenv/DotenvTest.php
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -310,6 +310,24 @@ VAR=value # comment
```


### Parsing Without Loading

Sometimes you just wanna parse the file and resolve the nested environment variables, by giving us a string, and have an array returned back to you. While this is already possible, it is a little fiddly, so we have provided a direct way to do this:

```php
// ['FOO' => 'Bar', 'BAZ' => 'Hello Bar']
Dotenv\Dotenv::parse("FOO=Bar\nBAZ=\"Hello \${FOO}\"");
```

This is exactly the same as:

```php
Dotenv\Dotenv::createArrayBacked(__DIR__)->load();
```

only, instead of providing the directory to find the file, you have directly provided the file contents.


### Usage Notes

When a new developer clones your codebase, they will have an additional
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.0-dev"
"dev-master": "5.1-dev"
}
}
}
40 changes: 40 additions & 0 deletions src/Dotenv.php
Original file line number Diff line number Diff line change
@@ -9,11 +9,13 @@
use Dotenv\Loader\LoaderInterface;
use Dotenv\Parser\Parser;
use Dotenv\Parser\ParserInterface;
use Dotenv\Repository\Adapter\ArrayAdapter;
use Dotenv\Repository\Adapter\PutenvAdapter;
use Dotenv\Repository\RepositoryBuilder;
use Dotenv\Repository\RepositoryInterface;
use Dotenv\Store\StoreBuilder;
use Dotenv\Store\StoreInterface;
use Dotenv\Store\StringStore;

class Dotenv
{
@@ -170,6 +172,44 @@ public static function createUnsafeImmutable($paths, $names = null, bool $shortC
return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
}

/**
* Create a new dotenv instance with an array backed repository.
*
* @param string|string[] $paths
* @param string|string[]|null $names
* @param bool $shortCircuit
* @param string|null $fileEncoding
*
* @return \Dotenv\Dotenv
*/
public static function createArrayBacked($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
{
$repository = RepositoryBuilder::createWithNoAdapters()->addAdapter(ArrayAdapter::class)->make();

return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
}

/**
* Parse the given content and resolve nested variables.
*
* This method behaves just like load(), only without mutating your actual
* environment. We do this by using an array backed repository.
*
* @param string $content
*
* @throws \Dotenv\Exception\InvalidFileException
*
* @return array<string,string|null>
*/
public static function parse(string $content)
{
$repository = RepositoryBuilder::createWithNoAdapters()->addAdapter(ArrayAdapter::class)->make();

$phpdotenv = new self(new StringStore($content), new Parser(), new Loader(), $repository);

return $phpdotenv->load();
}

/**
* Read and load environment file(s).
*
37 changes: 37 additions & 0 deletions src/Store/StringStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Dotenv\Store;

final class StringStore implements StoreInterface
{
/**
* The file content.
*
* @var string
*/
private $content;

/**
* Create a new string store instance.
*
* @param string $content
*
* @return void
*/
public function __construct(string $content)
{
$this->content = $content;
}

/**
* Read the content of the environment file(s).
*
* @return string
*/
public function read()
{
return $this->content;
}
}
27 changes: 27 additions & 0 deletions tests/Dotenv/DotenvTest.php
Original file line number Diff line number Diff line change
@@ -349,4 +349,31 @@ public function testDirectConstructor()
'NULL' => '',
], $dotenv->load());
}

public function testDotenvParseExample1()
{
$output = Dotenv::parse(
"BASE_DIR=\"/var/webroot/project-root\"\nCACHE_DIR=\"\${BASE_DIR}/cache\"\nTMP_DIR=\"\${BASE_DIR}/tmp\"\n"
);

self::assertSame($output, [
'BASE_DIR' => '/var/webroot/project-root',
'CACHE_DIR' => '/var/webroot/project-root/cache',
'TMP_DIR' => '/var/webroot/project-root/tmp',
]);
}

public function testDotenvParseExample2()
{
$output = Dotenv::parse("FOO=Bar\nBAZ=\"Hello \${FOO}\"");

self::assertSame($output, ['FOO' => 'Bar', 'BAZ' => 'Hello Bar']);
}

public function testDotenvParseEmptyCase()
{
$output = Dotenv::parse('');

self::assertSame($output, []);
}
}