Skip to content

Commit

Permalink
v2
Browse files Browse the repository at this point in the history
  • Loading branch information
sabroan committed Aug 21, 2023
2 parents 8ffedf5 + 7eadc1d commit 334b7ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# Dotenv
A simple and tiny PHP `.env` loader.
A simple and tiny PHP `.env` loader, which does not inject values into `$_ENV` or OS.
## Installation
```
composer require env/dotenv
```
## Usage
The .env file should have a valid [.ini](https://wikipedia.org/wiki/INI_file) syntaxis, supported by [parse_ini_file](https://www.php.net/manual/en/function.parse-ini-file.php).

This package does not inject values into global `$_ENV` or OS wia [putenv()](https://www.php.net/manual/en/function.putenv.php) so it's up to you how you wan't proceed with parsed data.

### Load
Parse .env and inject it into `$_ENV` variable:
Parse `.env` and get values as array:
```php
$env = new \Env\Dotenv(
$env = \Env\Dotenv::toArray(
path: '.env',
overwrite: false, // by default: true
strict: false, // by default: true
);
```
The second argument determine if existing `$_ENV` keys should be overwritten.
### Parse
Additionaly, if you dont wan't to inject data into `$_ENV`, you can retrieve parsed data by:
```php
$env = \Env\Dotenv::load('.env');
```
If you need variables and fallback values, set strict to `false`
46 changes: 13 additions & 33 deletions src/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,30 @@
use \ErrorException;
use \ValueError;

use function \realpath;
use function \parse_ini_file;
use function \realpath;

use const \INI_SCANNER_RAW;
use const \INI_SCANNER_TYPED;

final class Dotenv
{
/**
* @param string $path path to .env file.
* @param bool $overwrite overwrite existing `$_ENV` keys?
*/
public function __construct(
string $path,
bool $overwrite = true
) {
$env = static::load($path);
$_ENV = ($overwrite)
? $env + $_ENV
: $_ENV + $env;
}

/**
* Parse and retrieve content from .env file.
*
* @param string $path path to .env file.
* @return array associative array of .env keys and values.
* @throws ValueError .env was not found at provided path.
* @throws ErrorException corrupted .env, unable to parse.
* @see https://www.php.net/manual/en/function.parse-ini-file.php
*/
public static function load(string $path): array
public static function toArray(string $path, bool $strict = true): array
{
$path = realpath($path);
if ($path === false) {
throw new ValueError("File $path not found");
$realpath = realpath($path);
if (false === $realpath) {
throw new ValueError("File `$path` not found");
}
$env = parse_ini_file(
filename: $path,
filename: $realpath,
process_sections: false,
scanner_mode: INI_SCANNER_TYPED
scanner_mode: ($strict)
? INI_SCANNER_RAW
: INI_SCANNER_TYPED
);
if ($env === false) {
throw new ErrorException("Unable to parse $path");
if ($env) {
return $env;
}
return $env;
throw new ErrorException("Unable to parse $realpath");
}
}

0 comments on commit 334b7ca

Please sign in to comment.