Skip to content

Commit 2f24307

Browse files
Improved documentation for better clarity and understanding.
1 parent 50bc75a commit 2f24307

9 files changed

+198
-161
lines changed

.phpunit.result.cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":[],"times":{"Test\\DevCoder\\DotenvTest::testLoad":0.002,"Test\\DevCoder\\DotenvTest::testFileNotExist":0.001,"Test\\DevCoder\\DotenvTest::testIncompatibleProcessors":0.002,"Test\\DevCoder\\DotenvTest::testProcessBoolean":0.001,"Test\\DevCoder\\DotenvTest::testDontProcessBoolean":0.001,"Test\\DevCoder\\DotenvTest::testProcessQuotes":0.001,"Test\\DevCoder\\DotenvTest::testDontProcessQuotes":0.001,"Test\\DevCoder\\DotenvTest::testProcessNumbers":0.001}}

README.md

+54-49
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1-
# php-dotenv
2-
# Loads environment variables from .env file to getenv(), $_ENV and $_SERVER.
1+
# PHP-DotEnv
2+
33
[![Latest Stable Version](https://poser.pugx.org/devcoder-xyz/php-dotenv/v)](https://packagist.org/packages/devcoder-xyz/php-dotenv) [![Total Downloads](https://poser.pugx.org/devcoder-xyz/php-dotenv/downloads)](https://packagist.org/packages/devcoder-xyz/php-dotenv) [![Latest Unstable Version](https://poser.pugx.org/devcoder-xyz/php-dotenv/v/unstable)](//packagist.org/packages/devcoder-xyz/php-dotenv) [![License](https://poser.pugx.org/devcoder-xyz/php-dotenv/license)](https://packagist.org/packages/devcoder-xyz/php-dotenv)
44
[![PHP Version Require](http://poser.pugx.org/devcoder-xyz/php-dotenv/require/php)](https://packagist.org/packages/devcoder-xyz/php-dotenv)
55

6+
## Introduction
7+
PHP-DotEnv is a lightweight PHP library designed to simplify the management of environment variables in your PHP applications. It provides an elegant solution for loading configuration values from a `.env` file into the environment variables accessible via `getenv()`, `$_ENV`, and `$_SERVER`. This documentation aims to guide you through the installation, usage, and features of PHP-DotEnv.
8+
69
## Installation
710

8-
Use [Composer](https://getcomposer.org/)
11+
To install PHP-DotEnv, you can use [Composer](https://getcomposer.org/), the dependency manager for PHP.
912

1013
### Composer Require
11-
```
14+
```bash
1215
composer require devcoder-xyz/php-dotenv
1316
```
1417

1518
## Requirements
1619

17-
* PHP version 7.4
20+
- PHP version 7.4 or higher
1821

19-
**How to use ?**
22+
## Usage
2023

21-
```
24+
### 1. Define Environment Variables
25+
26+
Before using PHP-DotEnv, you need to define your environment variables in a `.env` file. This file should be placed in the root directory of your project. Each line in the file should follow the `KEY=VALUE` format.
27+
28+
```dotenv
2229
APP_ENV=dev
2330
DATABASE_DNS=mysql:host=localhost;dbname=test;
2431
DATABASE_USER="root"
@@ -28,7 +35,9 @@ NUMBER_LITERAL=0
2835
NULL_VALUE=null
2936
```
3037

31-
## Load the variables
38+
### 2. Load the Variables
39+
40+
After defining your environment variables, you can load them into your PHP application using PHP-DotEnv.
3241

3342
```php
3443
<?php
@@ -39,66 +48,62 @@ $absolutePathToEnvFile = __DIR__ . '/.env';
3948
(new DotEnv($absolutePathToEnvFile))->load();
4049
```
4150

42-
# Use them!
51+
### 3. Access Environment Variables
52+
53+
Once loaded, you can access the environment variables using PHP's `getenv()` function.
54+
4355
```php
4456
/**
45-
* string(33) "mysql:host=localhost;dbname=test;"
57+
* Retrieve the value of DATABASE_DNS
4658
*/
4759
var_dump(getenv('DATABASE_DNS'));
60+
```
4861

49-
/**
50-
* Removes double and single quotes from the variable:
51-
*
52-
* string(4) "root"
53-
*/
54-
var_dump(getenv('DATABASE_USER'));
62+
### Automatic Type Conversion
5563

56-
/**
57-
* Processes booleans as such:
58-
*
59-
* bool(true)
60-
*/
61-
var_dump(getenv('MODULE_ENABLED'));
64+
PHP-DotEnv provides automatic type conversion for certain types of values:
6265

63-
/**
64-
* Process the numeric value:
65-
*
66-
* int(0)
67-
*/
68-
var_dump(getenv('NUMBER_LITERAL'));
66+
- Booleans: Processed as `true` or `false`.
67+
- Quoted Strings: Surrounding quotes are removed.
68+
- Null Values: Converted to `null`.
69+
- Numeric Values: Converted to integers or floats.
6970

70-
/**
71-
* Check for literal null values:
72-
*
73-
* NULL
74-
*/
75-
var_dump(getenv('NULL_VALUE'));
76-
```
71+
## Processors
72+
73+
PHP-DotEnv allows you to define custom processors to handle specific types of values in your `.env` file. These processors enable you to control how values are parsed and converted.
7774

78-
Ideal for small project
75+
### BooleanProcessor
7976

80-
Simple and easy!
77+
The `BooleanProcessor` converts boolean values specified in the `.env` file to PHP boolean types (`true` or `false`).
8178

82-
# Processors
79+
```dotenv
80+
MODULE_ENABLED=true
81+
```
8382

84-
Also the variables are parsed according to the configuration passed as parameter to the constructor. The available processors are:
83+
### QuotedProcessor
8584

86-
## BooleanProcessor
85+
The `QuotedProcessor` removes surrounding quotes from quoted strings in the `.env` file.
8786

88-
``VARIABLE=false`` will be processed to ```bool(false)```
87+
```dotenv
88+
DATABASE_USER="root"
89+
```
8990

90-
NOTE: ``VARIABLE="true"`` will be processed to ```string(4) "true"```
91+
### NullProcessor
9192

92-
## QuotedProcessor
93+
The `NullProcessor` converts the string "null" to the PHP `null` value.
9394

94-
``VARIABLE="anything"`` will be processed to ```string(8) "anything"```
95+
```dotenv
96+
NULL_VALUE=null
97+
```
9598

96-
## NullProcessor
99+
### NumberProcessor
97100

98-
``VARIABLE=null`` will be processed to ```NULL```
101+
The `NumberProcessor` converts numeric values to integers or floats.
99102

100-
## NumberProcessor
103+
```dotenv
104+
NUMBER_LITERAL=0
105+
```
101106

102-
``VARIABLE=0`` will be processed to ```int(0)```
107+
## Conclusion
103108

104-
``VARIABLE=0.1`` will be processed to ```float(0.1)```
109+
PHP-DotEnv offers a straightforward and efficient solution for managing environment variables in PHP applications. By providing automatic type conversion and customizable processors, it simplifies the process of loading and handling configuration values from `.env` files. Whether you're working on a small project or a large-scale application, PHP-DotEnv can help streamline your development process and ensure smooth configuration management. Explore its features, integrate it into your projects, and experience the convenience it brings to your PHP development workflow.

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "devcoder-xyz/php-dotenv",
3-
"description": "Parses .env files",
3+
"description": "PHP-DotEnv is a lightweight PHP library designed to simplify the management of environment variables in your PHP applications.",
44
"type": "library",
55
"license": "MIT",
66
"authors": [

0 commit comments

Comments
 (0)