PHPackages                             markwalet/environment-manager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. markwalet/environment-manager

Abandoned → [markwalet/dotenv-manager](/?search=markwalet%2Fdotenv-manager)ArchivedLibrary[Utility &amp; Helpers](/categories/utility)

markwalet/environment-manager
=============================

A Laravel package that helps you edit the .env file programmatically.

v0.2.0(7y ago)11351MITPHPPHP ^7.0

Since Nov 27Pushed 7y ago1 watchersCompare

[ Source](https://github.com/markwalet/environment-manager)[ Packagist](https://packagist.org/packages/markwalet/environment-manager)[ RSS](/packages/markwalet-environment-manager/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

Laravel environment manager
===========================

[](#laravel-environment-manager)

[![Build Status](https://camo.githubusercontent.com/183eceb1b6d855e7bb34c178a762be0ca1da1f6f7b6bce2eb0c68ff8590f365c/68747470733a2f2f7472617669732d63692e6f72672f6d61726b77616c65742f656e7669726f6e6d656e742d6d616e616765722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/markwalet/environment-manager)[![Total Downloads](https://camo.githubusercontent.com/747775fc529b9db8dca57c8a6c718e7878a3c8d785cff9bb30d34833b814845f/68747470733a2f2f706f7365722e707567782e6f72672f6d61726b77616c65742f656e7669726f6e6d656e742d6d616e616765722f646f776e6c6f616473)](https://packagist.org/packages/markwalet/environment-manager)[![Latest Stable Version](https://camo.githubusercontent.com/9b637a7952623f227b296819dfc691973bc722458e250ad321edfe327b0c7362/68747470733a2f2f706f7365722e707567782e6f72672f6d61726b77616c65742f656e7669726f6e6d656e742d6d616e616765722f762f737461626c65)](https://packagist.org/packages/markwalet/environment-manager)[![License](https://camo.githubusercontent.com/7cfc09d67638d32f2184b571f505c67779cd0668dada111b387050e45c8e2513/68747470733a2f2f706f7365722e707567782e6f72672f6d61726b77616c65742f656e7669726f6e6d656e742d6d616e616765722f6c6963656e7365)](https://packagist.org/packages/markwalet/environment-manager)

A Laravel package that helps you edit the .env file programmatically.

Installation
------------

[](#installation)

You can install this package with composer.

```
composer require markwalet/environment-manager
```

### Laravel

[](#laravel)

Although this package is written for Laravel projects, the package can also be used for other frameworks or vanilla PHP projects.

Laravel 5.5 used Package auto-discovery, so you don't have to register the service provider. If you want to register the service provider manually, add the following line to your `config/app.php` file:

```
MarkWalet\EnvironmentManager\EnvironmentManagerServiceProvider::class
```

The service provider is also compatible with Laravel Lumen. Just add the following line to your `bootstrap/app.php` file:

```
$app->register(\MarkWalet\EnvironmentManager\EnvironmentManagerServiceProvider::class);
```

Usage
-----

[](#usage)

You can get the environment manager by resolving it with the app Laravel service container. This will give you a singleton `MarkWalet\EnvironmentManager\Environment` instance with the right dependencies.

```
$environment = app(Environment::class);
```

You can also manually set up an `Environment` class when you are not using Laravel.

Once you have a environment instance you can add lines to the environment:

```
$environment->add("FOO", "Bar")->after("OTHER_KEY");
```

If you don't specify a location for the new value, the value will be added at the end of the file.

You can also update environment variables:

```
$environment->update("EXISTING_KEY", "updatedValue");
```

This will replace the original value of `EXISTING_KEY` with `updatedValue`.

All values will be automatically formatted to a valid environment value.

### Mutating multiple lines

[](#mutating-multiple-lines)

You can use the `mutate()` method when you want to apply multiple changes to the file. The syntax looks a lot like the syntax in Laravel migrations:

```
/**
 * Original content:
 *
 * TEST1=value1
 * TEST2=value2
 * TEST3=value3
 */

$environment->mutate(function(EnvironmentBuilder $builder){
    $builder->add('TEST4', 'escaped value');
    $builder->update('TEST2', 'updated')->after('TEST3');
    $builder->delete('TEST1');
});

/**
 * New content:
 *
 * TEST3=value3
 * TEST2=updated
 * TEST4="escaped value"
 */
```

### Available methods

[](#available-methods)

Below you will find every available method and its underlying class. These methods can be called on the `Environment` class itself and the `EnvironmentBuilder` you get when mutating the environment.

MethodReturns`add(string $key, $value = null)`[Addition](src/Changes/Addition.php)`create(string $key, $value = null)`[Addition](src/Changes/Addition.php)`set(string $key, $value = null)`[Update](src/Changes/Update.php)`update(string $key, $value = null)`[Update](src/Changes/Update.php)`move(string $key)`[Move](src/Changes/Move.php)`delete(string $key)`[Delete](src/Changes/Delete.php)`unset(string $key)`[Delete](src/Changes/Delete.php)Extending the builder
---------------------

[](#extending-the-builder)

You can also extend the builder with your own implementation of the `Change` class. In the example below we are making a class that can increment a value quickly:

```
use MarkWalet\EnvironmentManager\Changes\Change;

class Increment extends Change
{
    use HasKey;

    function __construct(string $key)
    {
        $this->key = $key;
    }

    /**
     * Apply the pending change to the given content.
     *
     * @param $content
     *
     * @return mixed
     */
    public function apply(string $content): string
    {
        $search = '/'.$this->getKey().'=(.*)/';
        preg_match($search, $content, $matches);
        $value = $matches[1];

        $replacement = $this->getKey().'='.($value + 1);

        return preg_replace($search, $replacement, $content);
    }
}

$environment->extend('increment', Increment::class);
```

After we extended the builder we can call it through the environment object:

```
/**
 * Original content:
 *
 * TEST1=value1
 * INCREMENT=56
 */
$environment->increment('INCREMENT');

/**
 * New content:
 *
 * TEST1=value1
 * INCREMENT=57
 */
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~60 days

Total

5

Last Release

2850d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a37ff925cc06953e11b0c2ed9b9eac8f090bb4780df7964ad9f0b76543521dfc?d=identicon)[markwalet](/maintainers/markwalet)

---

Top Contributors

[![markwalet](https://avatars.githubusercontent.com/u/11446771?v=4)](https://github.com/markwalet "markwalet (16 commits)")

---

Tags

builderdotenvlaravelphplaravelenvironmentdotenv

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/markwalet-environment-manager/health.svg)

```
[![Health](https://phpackages.com/badges/markwalet-environment-manager/health.svg)](https://phpackages.com/packages/markwalet-environment-manager)
```

###  Alternatives

[vlucas/phpdotenv

Loads environment variables from `.env` to `getenv()`, `$\_ENV` and `$\_SERVER` automagically.

13.5k602.4M5.4k](/packages/vlucas-phpdotenv)[msztorc/laravel-env

Laravel env helper commands

7855.4k](/packages/msztorc-laravel-env)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
