Giter Club home page Giter Club logo

dot-env-editor's Introduction

dot-env-editor - A dead simple .env editor for PHP

A robust PHP package designed to simplify the management and manipulation of .env files within your projects. Effortlessly read, write, update, and delete environment variables with ease.

Dot-env-editor

Important

If you are looking to load/read environment variables then we highly recommend you vlucas/phpdotenv.

Features ๐Ÿ”ฅ

  • Effortlessly load and parse .env files
  • Get, set, and remove environment variables
  • Support for nesting env vars (e.g. DB_CONNECTION.host)
  • Ability to update existing vars or add new ones
  • Optionally keep backups of .env files before writing
  • Helper methods like only() to get subset of variables
  • Simple chaining methods for a fluent interface
  • Handles formatting values like booleans and strings
  • Preserves spacing and comments when writing back to the file
  • Compatible with various frameworks and environments, ensuring versatility in usage.
  • Built for PHP 8+ with strict typing

Requirements

  • PHP 8.1 or higher

Installation

Install via Composer:

composer require digimax/dot-env-editor

Usage

use Digimax\DotEnvEditor\DotEnvEditor;

$envPath = __DIR__ . '/.env';

$editor = new DotEnvEditor(
    $envPath,   // the path to the.env file
    true,       // whether to keep a backup of the .env file before writing
);

// or using the static method
$editor = DotEnvEditor::load($envPath, true);

// set backup directory
$editor->setBackupDir(__DIR__ . '/backups');

// Get all variables
var_dump($editor->all());

// Get a specific variable
echo $editor->get('AUTHOR_NAME');

// Set a variable
$editor->set('AUTHOR_NAME', 'Raziul Islam');

// set multiple variables
$editor->set([
    'AUTHOR_URL' => 'https://raziul.dev',
    'AUTHOR_COUNTRY' => 'Bangladesh',
]);

// Remove a variable
$editor->remove('AUTHOR_URL');

// write back to the file
$editor->write();

You can use chaining methods for a fluent interface ๐Ÿ˜˜

DotEnvEditor::load($envPath, true)
    ->setBackupDir(__DIR__ . '/backups')
    ->set([
        'AUTHOR_URL' => 'https://raziul.dev',
        'AUTHOR_COUNTRY' => 'Bangladesh',
    ])
    ->remove('AUTHOR_URL')
    ->write();

Usage with Laravel ๐Ÿ”ฅ

In your AppServiceProvider, register DotEnvEditor as a singleton:

use Digimax\DotEnvEditor\DotEnvEditor;

public function register(): void
{
    $this->app->singleton(DotEnvEditor::class, function () {
        return DotEnvEditor::load(base_path('.env'))
            ->setBackupDir(storage_path('env-backups')) // backup directory
            ->setBackupCount(5); // only keep latest 5 backup
    });
}

In your controller, you can inject the DotEnvEditor instance and use it to update environment variables:

public function update(DotEnvEditor $envEditor)
{
    // Perform form/data validation

    // save the changes
    $envEditor
        ->set([
            'AUTHOR_URL' => 'https://raziul.dev',
            'AUTHOR_COUNTRY' => 'Bangladesh',
        ])
        ->write();
}

Do you find this package useful?

If this package has helped to simplify your workflow, consider giving it a โญ๏ธ on GitHub. Your support encourages further development and improvements! ๐Ÿ’–

Support

For support, please open an issue on GitHb or submit a pull request.

License

The MIT License (MIT). Please see License File for more information.

dot-env-editor's People

Contributors

iraziul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.