Skip to content

Commit c816582

Browse files
update to version 1.0.3
1 parent 1afd04b commit c816582

File tree

9 files changed

+2244
-304
lines changed

9 files changed

+2244
-304
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
.idea/
2+
3+
# phpunit generated files
4+
.phpunit.cache
5+
build/
6+
7+
vendor/

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,21 @@ $dotenv->restart();
4949
// to
5050
$dotenv->reload(); // Reload no longer accept default values
5151
```
52+
53+
## v1.0.3
54+
55+
### Changes
56+
- replace `file()` with `fopen()` for improved performance when reading env files
57+
- The constructor now accepts three parameters:
58+
- `array|string|null` **$file**
59+
- `string|null` **$envKey**
60+
- `bool` **$overwrite**
61+
- improved `load` method:
62+
- On production mode, env files are loaded only once (for performance)
63+
64+
### Added
65+
- New method `has()` to check for the existence of an env variable:
66+
```php
67+
$dotenv->has('APP_LOCALE');
68+
```
69+
- You can now control whether to overwrite existing environment variables when loading multiple env files, via the $overwrite parameter.

README.md

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ The class supports loading variables from multiple files.
2020
- **PHP** version 8.0 or newer is required
2121

2222
## Installation & Setup
23-
- You can just download the code from repo and use or download using composer.
2423

25-
### Download Using Composer
26-
- If you don't have composer, install [composer](https://getcomposer.org/download/) first.
27-
- create file `composer.json` at your project root directory.
28-
- Add this to `composer.json`
29-
```php
24+
### Using Composer
25+
> If Composer is not installed, follow the [official guide](https://getcomposer.org/download/).
26+
27+
1. Create a `composer.json` file at your project root directory (if you don't have one):
28+
```json
3029
{
3130
"require": {
3231
"naingaunglwin-dev/dotenv": "^1.0"
3332
}
3433
}
3534
```
35+
3636
- Run the following command in your terminal from the project's root directory:
3737
```bash
3838
composer install
@@ -44,81 +44,93 @@ composer require naingaunglwin-dev/dotenv
4444
```
4545

4646
## Features
47-
- **Base Path Handling**: The base path of the project is automatically set to the root level of the project, but you can provide a custom base path when initializing the class.
48-
- **Environment Groups**: Group environment variables based on their prefix.
49-
- **File Validation**: Ensures that the specified .env file(s) exist in the project directory.
50-
- **Environment Update**: Synchronizes environment variables with the PHP $_SERVER and $_ENV superglobals.
5147

52-
## Configuration
53-
- You can customize the base path for locating environment files by passing it to the constructor:
48+
- ✅ Automatically uses the project root, but allows setting a custom path when initializing.
49+
- ✅ Load from multiple .env files
50+
- ✅ Customize file names and base path
51+
- ✅ Support for grouped environment variables
52+
- ✅ Auto-sync with `$_ENV`, `$_SERVER`, and `getenv()`
53+
- ✅ Reload environment at runtime
54+
- ✅ Check if a variable exists with `has()`
55+
- ✅ Supports default fallback values
56+
57+
## Constructor Signature
5458
```php
55-
$dotenv = new Dotenv(null, '/custom/path/');
59+
public function __construct(
60+
array|string|null $files = null,
61+
string|null $envKey = null,
62+
bool $overwrite = true
63+
)
5664
```
57-
58-
- If no file is provided, the Dotenv class will search for the following default files in the specified or root directory:
59-
60-
- .env
61-
- .env.local
62-
- .env.development
63-
- .env.production
64-
- .env.dev
65-
- .env.prod
65+
- `$files`: Optional string or array of .env file names. Defaults to standard files like `.env`, `.env.local`, etc.
66+
- `$envKey`: Custom key for detecting the app environment (e.g., `APP_ENV`)
67+
- `$overwrite`: Whether to overwrite existing variables (true by default)
68+
69+
70+
- If no file is provided, the Dotenv class will search for the following default files in root directory:
71+
- .env
72+
- .env.local,
73+
- .env.development
74+
- .env.production
75+
- .env.testing
76+
- .env.dev
77+
- .env.prod
78+
- .env.test
79+
- .env.staging
6680

6781
## Usage
6882

6983
### Loading Environment Variables
70-
- To load environment variables, simply initialize the Dotenv class.
71-
- By default, it looks for common .env files (such as .env, .env.local, .env.production, etc.) in the project root directory.
84+
- You can pass your environment file(s) with either a full path or just the file name.
85+
- If you pass a full path, the file will be loaded directly from that path.
86+
- If you pass just the file name, it will be resolved relative to the project root directory.
7287

7388
```php
7489
<?php
7590

76-
require_once "vendor/autoload.php";
77-
7891
use NAL\Dotenv\Dotenv;
7992

80-
// Initialize Dotenv and load environment variables
81-
$dotenv = new Dotenv();
82-
```
93+
// Load a file using its full path
94+
$dotenv = new Dotenv('/home/user/project/config/.env.custom');
8395

84-
- You can also specify custom environment files to load by passing the file name(s) to the constructor:
85-
```php
86-
$dotenv = new Dotenv('.env.testing');
87-
```
96+
// Load a file from the project root by name
97+
$dotenv = new Dotenv('.env.production');
8898

89-
- Or load multiple files:
90-
```php
91-
$dotenv = new Dotenv(['.env.dev', '.env.prod']);
99+
// Load multiple files
100+
$dotenv = new Dotenv(['.env.local', '/home/user/project/.env.override']);
92101
```
93102

94103
### Accessing Environment Variables
95-
- Once the environment files are loaded, you can retrieve environment variables using the get() method. If no key is provided, it will return all variables.
96104

97105
```php
98106
// Get a specific variable
99-
$appName = $dotenv->get('APP_NAME');
107+
$dotenv->load();
100108

101-
// Get all variables
102-
$envVariables = $dotenv->get();
109+
$host = $dotenv->get('DB_HOST'); // Get a variable
110+
$debug = $dotenv->get('DEBUG', false); // With fallback
111+
$all = $dotenv->get(); // Get all loaded variables
103112
```
104113

105-
- You can also provide a default value if the key is not found:
114+
### Check Existence
106115
```php
107-
$debugMode = $dotenv->get('DEBUG_MODE', false);
116+
if ($dotenv->has('APP_SECRET')) {
117+
// APP_SECRET is defined
118+
}
108119
```
109120

110121
### Grouping Environment Variables
111122
- You can access grouped environment variables with the group() method.
112123
- This can be useful if you have variables structured by prefix (e.g., APP_NAME, APP_ENV).
113124

114125
```php
115-
$appGroup = $dotenv->group('APP');
126+
$grouped = $dotenv->group('APP');
127+
// e.g., ['APP_NAME', 'APP_ENV', 'APP_KEY']
116128
```
117129

118130
### Reloading Environment Variables
119131
- You can reload environment variables by calling the `reload()` method. This method clears the previously loaded variables and reloads them from the specified files.
120132
```php
121-
$dotenv->reload();
133+
$dotenv->reload(); // Clear and reload loaded files
122134
```
123135

124136
## Exception Handling
@@ -129,22 +141,25 @@ $dotenv->reload();
129141

130142
### Example
131143
```php
144+
<?php
145+
132146
use NAL\Dotenv\Dotenv;
133147

134148
try {
135-
// Initialize dotenv
136-
$dotenv = new Dotenv();
137-
138-
// Load variables from a specific .env file
139-
$dotenv->load('.env');
149+
$dotenv = new Dotenv(['.env']);
150+
$dotenv->load();
140151

141-
// Get environment variables
142-
$dbHost = $dotenv->get('DB_HOST', 'localhost');
143-
144-
// Get all environment variables
145-
$allEnv = $dotenv->get();
152+
$dbUser = $dotenv->get('DB_USER', 'root');
146153

147154
} catch (\Exception $e) {
148-
echo 'Error loading environment variables: ' . $e->getMessage();
155+
echo 'Dotenv Error: ' . $e->getMessage();
149156
}
150157
```
158+
159+
## Running Tests
160+
161+
- To run the test suite, execute the following command
162+
```bash
163+
vendor/bin/phpunit tests/DotenvTest.php
164+
```
165+
- This will run all test cases defined in the DotenvTest.php file.

composer.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "naingaunglwin-dev/dotenv",
33
"description": "Simple PHP Dotenv Library",
44
"minimum-stability": "dev",
5-
"version": "1.0.2",
5+
"version": "1.0.3",
66
"prefer-stable": true,
77
"license": "MIT",
88
"authors": [
@@ -12,11 +12,15 @@
1212
}
1313
],
1414
"require": {
15-
"php": "^8.0"
15+
"php": "^8.2"
1616
},
1717
"autoload": {
1818
"psr-4": {
19-
"NAL\\Dotenv\\": "src/"
19+
"NAL\\Dotenv\\": "src/",
20+
"NALDotenvTests\\": "tests/"
2021
}
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^12.1"
2125
}
22-
}
26+
}

0 commit comments

Comments
 (0)