-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Dotenv | ||
A simple and tiny PHP `.env` loader. | ||
## 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). | ||
### Load | ||
Parse .env and inject it into `$_ENV` variable: | ||
```php | ||
$env = new \Env\Dotenv( | ||
path: '.env', | ||
overwrite: 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'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "env/dotenv", | ||
"type": "library", | ||
"description": "A simple, tiny and lightweight PHP .env loader", | ||
"keywords": ["environment", "env", "dotenv"], | ||
"license": "MIT", | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": "^8" | ||
}, | ||
"autoload" : { | ||
"psr-4": { | ||
"Env\\": "./src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Env; | ||
|
||
use \ErrorException; | ||
use \ValueError; | ||
|
||
use function \realpath; | ||
use function \parse_ini_file; | ||
|
||
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 | ||
{ | ||
$path = realpath($path); | ||
if ($path === false) { | ||
throw new ValueError("File $path not found"); | ||
} | ||
$env = parse_ini_file( | ||
filename: $path, | ||
process_sections: false, | ||
scanner_mode: INI_SCANNER_TYPED | ||
); | ||
if ($env === false) { | ||
throw new ErrorException("Unable to parse $path"); | ||
} | ||
return $env; | ||
} | ||
} |