PHP DotEnv File Writer: Manage configuration files in the dotenv format

Recommend this page to a friend!
  Info   View files Example   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2021-10-20 (4 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 22 This week: 22All time: 10,385 This week: 10Up
Version License PHP version Categories
dotenvwriter 1.0MIT/X Consortium ...7PHP 5, Configuration
Description Author

This class can manage configuration files in the dotenv format.

It provides functions to define the configuration values that will be part of a dotenv configuration file.

Currently, it can set values, delete values and write the configuration values to a dotenv file with a given path.

Picture of Miraz Mac
  Performance   Level  
Name: Miraz Mac <contact>
Classes: 13 packages by
Country: Bangladesh Bangladesh
Innovation award
Innovation award
Nominee: 4x

Details

DotEnvWriter

A PHP library to write values to .env (DotEnv) files

Installation

DotEnvWriter can be installed with Composer.

composer require mirazmac/dotenvwriter

Usage

$writer = new \MirazMac\DotEnv\Writer(__DIR__ . '/' . '.env');

$writer
->set('APP_NAME', 'My App')
->set('APP_URL', 'https://laravel.com')
->set('APP_DIR', '${BASE_DIR}/app')
// Third parameter set to TRUe to force quote a single word value
->set('APP_BUCKET', 's3-bucket', true);

// Write the values to file
$writer->write();

API

Methods

`__construct(string $path)`

Constructs a new instance.

Arguments

string `$path` The environment file path

` set(string $key, string $value, bool $forceQuote = false) : self`

Set the value of an environment variable, updated if exists, added if doesn't. The values must be passed as string, even if you are setting values like `TRUE`FALSE``. This method is chainable.

Arguments

string `$key` The key

string `$value` The value

bool `$forceQuote` By default the whether the value is wrapped in double quotes is determined automatically. However, you may wish to force quote a value.

` delete(string $key) : self`

Delete an environment variable if present. This method is chainable.

Arguments

string `$key` The key

`hasChanged() : bool`

States if one or more values has changed

`getContent() : string`

Returns the current content

`write(bool $force = false) : bool`

Write the contents to the `.env` file.

Arguments

bool `$force` By default we only write when something has changed, but you can force to write the file by setting this to TRUE.

F.A.Q

What can this do?

`DotEnvWriteris a PHP library that can write (add, update, delete) values to.env` files.

And how is this done?

Well.. I'm glad you asked.. it uses some bleeding edge AI algorithm.. just kidding, RegEx.. it uses RegEx to replace the values with proper quoting and escaping and writes them back to the filesystem. While RegEx is not the best solution but it gets the job done.

What if this corrupts my .env file?

Well, it shouldn't. It uses proper quoting and escaping before writing the values. If you want to make sure it generates a valid file, well, that's why tests are for. You can clone the repo and run `composer installand thenphpunit` to run the tests. The goal of this library is to generate a `.env` file that can be parsed without any errors using: vlucas/phpdotenv

How does it handle invalid `.env` files?

It doesn't. You see, it doesn't concern itself with validation of existing values. It simply replaces them via RegEx, and makes sure the changes that it has made is valid. But if you have an existing invalid file, it can do nothing to validate or fix that.

Can I use this to read values from an .env file?

Yes, you can. But you really shouldn't. Thus they aren't documented. This library is for writing to the file. To properly read and load values from `.env` files, use something robust like: vlucas/phpdotenv .

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

The library is inspired by and uses some RegEx from the msztorc/laravel-env package and is based on the same MIT license.

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (2 files)
Files folder imageusage (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file Writer.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file .env.example Data Auxiliary data
  Plain text file WriterTest.php Class Class source

  Files folder image Files  /  usage  
File Role Description
  Accessible without login Plain text file .env Data Auxiliary data
  Accessible without login Plain text file usage.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:22
This week:22
All time:10,385
This week:10Up
For more information send a message to info at phpclasses dot org.