-
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
1 parent
78c43a7
commit b7852a5
Showing
4 changed files
with
118 additions
and
2 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,3 @@ | ||
# Dependencies | ||
vendor/* | ||
composer.lock |
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 |
---|---|---|
@@ -1,2 +1,55 @@ | ||
# env | ||
Env | ||
# Env | ||
|
||
## Presentation | ||
|
||
The environment variable manager makes it easy to manipulate your environment variables in your projects. | ||
|
||
|
||
## Installation | ||
|
||
`composer require phant/env` | ||
|
||
|
||
## Technologies used | ||
|
||
- `PHP 8.1` | ||
- `Composer` for dependencies management (PHP) | ||
|
||
|
||
## Usage | ||
|
||
### Get | ||
|
||
If the variable is not defined, an exception will be thrown. | ||
|
||
```php | ||
use Phant\Env\Manager as Env; | ||
|
||
echo Env::get('MY_VAR'); | ||
``` | ||
|
||
### Get with default value | ||
|
||
```php | ||
use Phant\Env\Manager as Env; | ||
|
||
echo Env::get('MY_VAR', 'default value'); | ||
``` | ||
|
||
### Set | ||
|
||
If the variable is already defined, an exception will be thrown. | ||
|
||
```php | ||
use Phant\Env\Manager as Env; | ||
|
||
echo Env::set('MY_VAR', 'my new value'); | ||
``` | ||
|
||
### Set value on existing var | ||
|
||
```php | ||
use Phant\Env\Manager as Env; | ||
|
||
echo Env::set('MY_VAR', 'my new value', true); | ||
``` |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phant\Env; | ||
|
||
class Manager | ||
{ | ||
public static function set(string $key, string $value, bool $force = false): void | ||
{ | ||
if (!$force && false !== getenv($key)) { | ||
throw new \Exception('Env var "' . $key . '" is already defined'); | ||
} | ||
|
||
putenv($key, $value); | ||
} | ||
|
||
public static function get(string $key, string $default = null): ?string | ||
{ | ||
$value = getenv($key); | ||
|
||
if (false === $value) { | ||
if (is_null($default)) { | ||
$caller = debug_backtrace()[0]; | ||
|
||
throw new \Exception('Env var "' . $key . '" is not defined. (Required in file ' . $caller['file'] . ' at line ' . $caller['line'] . ')'); | ||
} | ||
|
||
return $default; | ||
} | ||
|
||
return $value; | ||
} | ||
} |
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,26 @@ | ||
{ | ||
"name": "phant/env", | ||
"description": "Manage env easily", | ||
"license": "MIT", | ||
"keywords": ["env manager", "env component", "SimpleEnv"], | ||
"authors": [ | ||
{ | ||
"name": "Lenny ROUANET", | ||
"email": "lennyrouanet@users.noreply.github.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.1" | ||
}, | ||
"require-dev": { | ||
"phpstan/phpstan": "^1.4" | ||
}, | ||
"scripts": { | ||
"analyse": "vendor/bin/phpstan analyse component --memory-limit=4G" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Phant\\Env\\": "component/" | ||
} | ||
} | ||
} |