Search by

portrino / companienv

portrino-dev

Companion for .env files

Package info

github.com/portrino/companienv

pkg:composer/portrino/companienv

Statistics

Installs: 12 766

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

5.0.0 2026-11-28 17:39 UTC

This package is auto-updated.

Last update: 2026-11-28 17:40:05 UTC


README

Latest Stable Version License Total Downloads Tests

Companienv

Your companion for .env files. Everybody knows about 12 factor and environments variables now. A lot of frameworks such as Symfony are using a .env file to configure the application, but we don't have anything to help users to complete their local .env file.

Companienv will helps you manage the .env files, from a reference .env.dist version in your code repository. Companienv can:

Usage

  1. Require portrino/companienv as your project dependency:
composer req portrino/companienv
  1. Run your companion:
vendor/bin/companienv

Composer automation

You can run Companienv automatically after composer install or composer update commands by configuring the scripts in your composer.json file:

{
    "scripts": {
        "post-install-cmd": [
            "Companienv\\Composer\\ScriptHandler::run"
        ],
        "post-update-cmd": [
            "Companienv\\Composer\\ScriptHandler::run"
        ]
    }
}

By default, the file used as a template is .env.dist and the written file is .env. You can change these defaults within your composer.json file:

{
    "extra": {
        "companienv-parameters": [
            {
                "file": ".env.foo",
                "dist-file": ".env.foo.dist"
            }
        ]
    }
}

The .env.dist file

All your configuration is directly in your .env.dist file, as comments. The configuration is divided in blocks that will be displayed to the user for a greater understanding of the configuration. Here are the fondations for Companienv:

  • Blocks. They logically group variables together. They are defined by a title (line starting with a double-comment ##) and a description (every comment line directly bellow)
  • Attributes. Defined by a line starting with #+, an attribute is associated to one or multiple variables. These attributes are the entrypoint for extensions. In the example above, it says that the JWT_* variables are associated with an RSA key pair, so Companienv will automatically offer the user to generate one for them.
  • Comments. Lines starting by #~ will be ignored by Companienv.

Example of .env.dist. file

# .env.dist

## Welcome in the configuration of [my-project]
#
#~ Please run the `bin/start` command.
#~ These lines starting with `~` are not going to be displayed to the user

## GitHub
# In order to be able to login with GitHub, you need to create a GitHub
# application. To get access to the code repositories, you need to create a
# GitHub integration.
#
#+file-to-propagate(GITHUB_INTEGRATION_KEY_PATH)
#
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_INTEGRATION_ID=
GITHUB_INTEGRATION_KEY_PATH=
GITHUB_SECRET=

## Security
# We need sauce! Well, no, we need an SSL certificate.
#
#+rsa-pair(JWT_PRIVATE_KEY_PATH JWT_PUBLIC_KEY_PATH JWT_PRIVATE_KEY_PASS_PHRASE)
#
JWT_PRIVATE_KEY_PATH=/runtime/keys/jwt-private.pem
JWT_PUBLIC_KEY_PATH=/runtime/keys/jwt-public.pem
JWT_PRIVATE_KEY_PASS_PHRASE=

## Another block
# With its (optional) description
AND_OTHER_VARIABLES=

Built-in extensions

only-if extension

Some of the blocks of your .env file might not even be relevant if some other variable was disabling a given feature.

Example: This will only ask for the INTERCOM_APPLICATION_ID variable if INTERCOM_ENABLED has the value (current or entered by the user) true.

## Support & Feedback
# If you would like to allow your users to get some support from you, give you
# some feedback and this sort of things, select the integrations you'd like.
#
#+only-if(INTERCOM_APPLICATION_ID):(INTERCOM_ENABLED=true)
#
INTERCOM_ENABLED=false
INTERCOM_APPLICATION_ID=none

file-to-propagate extension

Will ask the path of an existing file and copy it to the destination mentioned in the reference. A path that does not exist is rejected and asked again.

Example: this will ask the user to give the path of an existing file. It will copy this file to the path /runtime/keys/firebase.json, relative to the root directory of the project.

#+file-to-propagate(FIREBASE_SERVICE_ACCOUNT_PATH)
FIREBASE_SERVICE_ACCOUNT_PATH=/runtime/keys/firebase.json

rsa-pair extension

If the public/private key pair does not exist, Companienv will offer to generate one for the user.

#+rsa-pair(JWT_PRIVATE_KEY_PATH JWT_PUBLIC_KEY_PATH JWT_PRIVATE_KEY_PASS_PHRASE)
JWT_PRIVATE_KEY_PATH=/runtime/keys/jwt-private.pem
JWT_PUBLIC_KEY_PATH=/runtime/keys/jwt-public.pem
JWT_PRIVATE_KEY_PASS_PHRASE=

ssl-certificate-extension

Similar to the RSA keys pair: Companienv will offer to generate a self-signed SSL certificate if it does not exist yet.

#+ssl-certificate(SSL_CERTIFICATE_PRIVATE_KEY_PATH SSL_CERTIFICATE_CERTIFICATE_PATH SSL_CERTIFICATE_DOMAIN_NAME)
SSL_CERTIFICATE_PRIVATE_KEY_PATH=/runtime/keys/server.key
SSL_CERTIFICATE_CERTIFICATE_PATH=/runtime/keys/server.crt
SSL_CERTIFICATE_DOMAIN_NAME=

Key pairs and certificates in non-interactive mode (e.g. CI)

Without interaction, the generation of the RSA key pair and the SSL certificate is confirmed automatically:

  • rsa-pair uses an empty pass phrase. The private key is still written in the encrypted PEM format, but anyone with access to the file can use it. Tested with OpenSSL 3.5; older OpenSSL versions may require a pass phrase of at least 4 characters and fail.
  • ssl-certificate uses the current or .env.dist value of the domain variable; if it is empty, the certificate has no common name (CN).
  • New keys or certificates are generated whenever the files are missing. If Companienv runs in a build or deployment pipeline, make sure the files are kept between deployments (e.g. in a shared directory that is excluded from the deployment), otherwise every deployment creates new keys and invalidates e.g. all issued tokens.

secret extension

Asks the given variables without echoing the input, e.g. for passwords or API tokens. The default of the .env.dist (e.g. a dummy password) is displayed as usual, a current value from the .env file never (only (****)). A new value has to be entered twice; pressing enter keeps the displayed default or current value. Variables are only treated as secrets when they are marked explicitly, there is no detection by name.

#+secret(DATABASE_PASSWORD API_TOKEN)
DATABASE_PASSWORD=
API_TOKEN=

choice extension

Suggests the given values, e.g. the possible contexts or environments. In a terminal they are shown as a list to pick from with the arrow keys, otherwise they are listed above the question. Any other value can be entered as well, and pressing enter keeps the current or default value. The values are separated by spaces and must not contain spaces or ) themselves.

#+choice(TYPO3_CONTEXT):(Production Development/Staging Development/Local)
TYPO3_CONTEXT=Development/Local

number extension

Asks a whole number, optionally within a range (min max). Invalid input is rejected and asked again; in a terminal the number can also be counted up or down with the arrow keys. Without a default the variable has to be entered. An invalid range or a default outside the range is reported before anything is asked, also in non-interactive mode.

#+number(DB_PORT REDIS_PORT):(1 65535)
DB_PORT=
REDIS_PORT=6379

boolean extension

Asks yes or no and writes the answer in the (lower-case) notation of the default: true/false, 1/0, yes/no or on/off. Without a default, true/false is written. Works well together with the only-if extension. An invalid default is reported before anything is asked.

#+boolean(INTERCOM_ENABLED APP_DEBUG)
INTERCOM_ENABLED=false
APP_DEBUG=1

Your own extensions

You can easily create and use your own extensions with Companienv. In order to do so, you'll have to start Companienv with your own PHP file and use the registerExtension method of the Application:

use Companienv\Application;
use Companienv\Extension;

$application = new Application($rootDirectory);
$application->registerExtension(new class() implements Extension {
    // Implements the interface...
});
$application->run();

Upgrading to the next major version

  • At the end, Companienv shows a summary of the values written in this run (secrets and the pass phrase of an rsa-pair masked). Each value is still written right away, so an aborted wizard keeps the values entered so far.
  • Existing lines of the .env file are no longer reformatted: only the entry of the variable being set is changed or appended, and the result is verified with symfony/dotenv before it is written.
  • Values containing special characters are written in single quotes ('it'"'"'s' for a single quote), so they are stored exactly as entered.
  • The Companienv\DotEnvEditor classes have been removed, values are read with symfony/dotenv only.
  • Companienv\IO\Interaction has the new methods askHidden(), askWithSuggestions() and askNumber(), askConfirmation() got a $default and ask() a $validator parameter, and there are the new methods section(), table() and spin(); custom implementations have to add them.
  • Secrets have to be marked with the new secret extension to be asked without echoing them.
  • Values can be suggested with the new choice extension, numbers validated with the new number extension, flags asked as yes/no with the new boolean extension.
  • Attributes with several mappings, e.g. #+only-if(FOO):(BAR=true BAZ=true), now keep all of them instead of only the last one.
  • Each block title shows the progress, e.g. [2/5] Database.
  • In a terminal the questions are rendered with laravel/prompts; without a terminal (piped input, CI) and on Windows without WSL plain symfony/console questions are used as before.

Acknowledgements & Credits

This project is inspired by and based on these amazing open-source projects: