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

Overview

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?

DotEnvWriter is 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 install and then phpunit 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.

You might also like...
Google-like values converter

Google-like values converter. Support for different types of conversions, for examples: 1 kilometer - meters 1 dollar - THB 1 kilogram - meters ...

Composer plugin replacing placeholders in the scripts section by dynamic values

Composer Substitution Plugin The Composer Substitution plugin replaces placeholders in the scripts section by dynamic values. It also permits to cache

Read and show values from form valid

read-and-show-values-from-form-valid Escribe un programa PHP que permita al usuario rellenar un formulario de registro con los datos de nombre, contra

Miniset allows you to create compact sets of fields that either combine into a string of classes, or return a simple array of values

Miniset allows you to create compact sets of fields that either combine into a string of classes, or return a simple array of values. Miniset

The package provides the ability to write logs into the database synchronously or asynchronously, along with other logging channels.

Laravel database log channel The package provides the ability to write logs into the database synchronously or asynchronously, along with other loggin

🖍 Write beautiful blog articles using Markdown inside your Laravel app.

Blogged Write beautiful blog articles using Markdown inside your Laravel app. Blogged 🖍 Blogged is a carefully designed Laravel package provides an e

Smd imagery - A Textpattern CMS plugin for managing images in the Write panel.

smd_imagery Insert images into your Write panel. Very handy for people who run photoblog or image-heavy sites, or those who categorise images for incl

This tool can write the monolog standard log directly to clickhouse in real time via the tcp protocol

log2ck This tool can write the monolog standard log directly to clickhouse in real time via the tcp protocol. If you can write regular rules, other st

Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project.
Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project.

ConfigStruct Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project. It

Releases(0.1)
Owner
Miraz Mac
we as a society need to rise u🅱️
Miraz Mac
⚡️Lightning-fast linter for .env files. Written in Rust 🦀

⚡️ Lightning-fast linter for .env files. Written in Rust ?? Dotenv-linter can check / fix / compare .env files for problems that may cause the applica

null 1.5k Dec 31, 2022
Columnar analytics for PHP - a pure PHP library to read and write simple columnar files in a performant way.

Columnar Analytics (in pure PHP) On GitHub: https://github.com/envoymediagroup/columna About the project What does it do? This library allows you to w

Envoy Media Group 2 Sep 26, 2022
Basic class library to read, write and view files using PHP.

File Basic class library to read, write and view files using PHP. Supported PHP Versions Build Status (dev) Main Aim of The Library The main aim of th

WebFiori 2 May 13, 2022
⚙️ A WordPress plugin to set WordPress options from a .env file.

dotenv A WordPress plugin to set WordPress options from a .env file. Any WPENV_ prefixed variables in the .env will be used to override the WordPress

Brad Parbs 13 Oct 6, 2022
JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

Eboubaker Eboubaker 2 Jul 31, 2022
Write to Laravel Config files and maintain file integrity

Laravel Config Writer Write to Laravel Config files and maintain file integrity. This library is an extension of the Config component used by Laravel.

Sam Geo 158 Dec 30, 2022
Deeper is a easy way to compare if 2 objects is equal based on values in these objects. This library is heavily inspired in Golang's reflect.DeepEqual().

Deeper Deeper is a easy way to compare if 2 objects is equal based on values in these objects. This library is heavily inspired in Golang's reflect.De

Joubert RedRat 4 Feb 12, 2022
Provides the functionality to compare PHP values for equality.

sebastian/comparator This component provides the functionality to compare PHP values for equality. Installation You can add this library as a local, p

Sebastian Bergmann 6.7k Jan 1, 2023
Sanitize and escape every values in your PHP Application

PHP Sanitizer Sanitize and escape every values in your PHP Application. This solution will make PHP developer life easy, very easy and developers woul

Maniruzzaman Akash 10 Oct 2, 2022
Humanize values that are readable only for developers.

PHP Humanizer Tests - 4.x Readme for 4.x version Humanize values to make them readable for regular people ;) Installation Run the following command: c

Coduo 1.5k Dec 21, 2022