You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB
Markdown

# Gbili DotEnv
A simple PHP package to load environment variables from a `.env` file. Comments within the `.env` file, starting with a `#`, are supported.
## Installation
You can include this class in your project by copying the file, or use Composer if it's part of a package.
## Usage
### Loading Environment Variables
To load environment variables from a `.env` file located in the root directory of your project, use the `load` method:
```php
use Gbili\DotEnv;
DotEnv::load(__DIR__);
```
The `.env` file should contain one environment variable per line, in the format `VAR_NAME=value`. Comments can be included, starting with a `#`.
### Accessing Environment Variables
Once loaded, environment variables can be accessed using the `$_ENV` global variable:
```php
echo $_ENV['VAR_NAME']; // Outputs the value
```
### Handling Quotes
The `removeOuterQuotes` method in the class handles both single and double quotes around the values, allowing for flexibility in how you define your variables.
## Methods
- `loadFromFile($filepath)`: Loads environment variables from a given file path.
- `removeOuterQuotes(string $string, $quote = "'")`: Removes outer quotes from a given string.
- `MUTATE_ENV_addEnvVars(array $variables)`: Merges the given variables into the `$_ENV` superglobal.
- `load(string $rootDir)`: Loads environment variables from a `.env` file in the given root directory.
## Contributing
Feel free to submit issues or pull requests if you find any problems or have suggestions for improvements.
## License
[MIT License](./LICENSE)