2017 © Pedro Peláez
 

library dotenvwriter

Interface for editing .env files in PHP.

image

oohology/dotenvwriter

Interface for editing .env files in PHP.

  • Monday, April 23, 2018
  • by davidwoodmansee
  • Repository
  • 3 Watchers
  • 5 Stars
  • 1,563 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 20 Versions
  • 12 % Grown

The README.md

DotEnvWriter

Interface for editing .env files in PHP, (*1)

Build Status, (*2)


Note: This is probably not advisable for use in production, but could be handy for automating installation tasks, etc., (*3)

Basic Usage

In general, you will open a .env file, use the set method to append or replace some values, and then call the save method to write the result., (*4)

Open an environment file and replace a value:, (*5)

use DotEnvWriter\DotEnvWriter;

$env = new DotEnvWriter('../.env');
$env->set('ENVIRONMENT', 'dev');
$env->save();

Also supports fluent interface:, (*6)

(new DotEnvWriter('../.env'))
    ->set('DB_HOST', 'localhost')
    ->set('DB_NAME', 'test_db')
    ->save();

Output File

There are multiple ways to read from a source file, make some changes, and write the result to a different output file. The end result is the same, so choose whichever method best fits your use case:, (*7)

$writer = (new DotEnvWriter('.env.example'))
    ->setOutputPath('.env');
// ...
$writer->save();

Or alternately:, (*8)

$writer = (new DotEnvWriter('.env.example'));
// ...
$writer->save('.env');

Or using the load() method:, (*9)

$writer = (new DotEnvWriter('.env'))
    ->load('.env.example');
// ...
$writer->save();

Comments

The set() method takes a $comments parameter. If omitted (or set to null), any existing comment from the source file with be kept intact. If a $comment is provided it will overwrite the existing comment. Providing a zero-length $comment will cause the comment to be deleted., (*10)

Examples: Set a comment, (*11)

// source: API_KEY=""
$writer->set('API_KEY', '1234', 'four-digit code');
// result: API_KEY=1234 # four-digit code

Delete a comment, (*12)

// source: API_KEY= # four-digit code
$writer->set('API_KEY', '1234', '');
// result: API_KEY=1234

Keep existing comment, (*13)

// source: API_KEY= # four-digit code
$writer->set('API_KEY', '1234');
// result: API_KEY=1234 # four-digit code

Export

The parser supports a bash-style export prefix on any line. The set() method takes an $export variable as its 4th argument. Example: By default, keep the existing state, (*14)

// source: export API_KEY=""
$writer->set('API_KEY', '1234');
// result: export API_KEY=1234

Or change it by passing a boolean, (*15)

// source: export API_KEY=""
$writer->set('API_KEY', '1234', null, false);
// result: API_KEY=1234
$writer->set('API_KEY', '1234', null, true);
// result: export API_KEY=1234

Casting booleans

By default boolean values will not be stored as true or false., (*16)

To enable casting booleans you should call the castBooleans() method., (*17)

Example:, (*18)

Default behaviour, (*19)

$writer->set('REGISTRATION_OPENED', true);
// result: REGISTRATION_OPENED=1


$writer->set('REGISTRATION_OPENED', false);
// result: REGISTRATION_OPENED=

After calling castBooleans() method, (*20)

$writer->castBooleans();

$writer->set('REGISTRATION_OPENED', true);
// result: REGISTRATION_OPENED=true


$writer->set('REGISTRATION_OPENED', false);
// result: REGISTRATION_OPENED=false

Read the Value of a Variable

The get method allows you to find the value of an existing given environment variable. It returns false if the variable doesn't exist, or an array containing the details of the line from the source file., (*21)

Source: export ENV="dev" # dev or live?, (*22)

Get command: $writer->get('ENV');, (*23)

Result:, (*24)

[
    'line' => 'export ENV="dev" # dev or live?',
    'export' => 'export',
    'key' => 'ENV',
    'value' => 'dev',
    'comment' => 'dev or live?'
];

Writing Blank/Comment Lines

The line method appends a single unprocessed line of output to the file. It could be used to insert blank lines or comments. If you wish to append a new variable, the set method should be used instead to prevent duplicates., (*25)

$writer = (new DotEnvWriter)
    ->line()
    ->line('# App Settings')
    ->line()
    ->set('APP_ENV', 'dev');

The Versions

23/04 2018

dev-master

9999999-dev

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

23/04 2018

v1.3.1

1.3.1.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

20/04 2018

v1.3.0

1.3.0.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

23/03 2017

v1.2.3

1.2.3.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

23/03 2017

v1.2.2

1.2.2.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

22/03 2017

1.2.1

1.2.1.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

21/03 2017

v1.2.0

1.2.0.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

27/10 2016

v1.1.6

1.1.6.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

27/10 2016

v1.1.5

1.1.5.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

27/10 2016

v1.1.4

1.1.4.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

27/10 2016

v1.1.3

1.1.3.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

27/10 2016

v1.1.2

1.1.2.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

26/10 2016

v1.1.1

1.1.1.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

26/10 2016

v1.1.0

1.1.0.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

25/10 2016

v1.0.5

1.0.5.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

25/10 2016

v1.0.4

1.0.4.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

php writer dotenv

22/10 2016

v1.0.3

1.0.3.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

php writer dotenv

21/10 2016

v1.0.2

1.0.2.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

php writer dotenv

21/10 2016

v1.0.1

1.0.1.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

php writer dotenv

21/10 2016

v1.0.0

1.0.0.0

Interface for editing .env files in PHP.

  Sources   Download

BSD-3-Clause-Attribution

The Requires

  • php >=5.4.0

 

php writer dotenv