Skip to content

Commit

Permalink
Hello world!
Browse files Browse the repository at this point in the history
  • Loading branch information
lennyrouanet committed Oct 6, 2022
1 parent 78c43a7 commit b7852a5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Dependencies
vendor/*
composer.lock
57 changes: 55 additions & 2 deletions README.md
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);
```
34 changes: 34 additions & 0 deletions component/Manager.php
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;
}
}
26 changes: 26 additions & 0 deletions composer.json
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/"
}
}
}

0 comments on commit b7852a5

Please sign in to comment.