dbtlr/php-env-builder

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

A PHP library that assists in automating the building of .env files.

v1.0.0 2018-08-17 14:17 UTC

This package is not auto-updated.

Last update: 2021-11-27 14:18:39 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Makes building .env files from the command-line simple.

For loading and using .env files in your development workflow, I highly suggest looking to the vlucas/phpdotenv package, which helps abstract out the process of loading environmental variables. This library is designed to help you build a local .env library that you can add to your .gitignore file, without the extra .env.example file that most people add to document how to build it.

Why .env?

Installation

The recommended method of installing this library is via Composer.

Run the following command from your project root:

$ composer require --dev dbtlr/php-env-builder

Usage as a Composer Script

Because Composer provides its own way of working with the console IO, the extra.php-env-builder config is the proper way of setting up your questions and variable names in this use case.

In order to have your .env file built when you run composer install or composer update you should provide config similar to this:

{
    "scripts": {
        "build-env": "Dbtlr\\PHPEnvBuilder\\ComposerScriptRunner::build",
        "post-install-cmd": "@build-env",
        "post-update-cmd": "@build-env"
    },
    "extra": {
        "php-env-builder": {
            "envFile": ".env",
            "questions": [
                {
                    "name": "MYSQL_HOST",
                    "prompt": "What is the hostname for the MySQL server?",
                    "default": "127.0.0.1",
                    "required": true
                },
                {
                    "name": "MYSQL_PORT",
                    "prompt": "The port for the MySQL server?",
                    "default": "3306",
                    "required": true
                },
                {
                    "name": "MYSQL_USER",
                    "prompt": "What is the MySQL user?",
                    "default": "app",
                    "required": true
                },
                {
                    "name": "MYSQL_PASSWORD",
                    "prompt": "What is the MySQL password?",
                    "default": "app-password",
                    "required": true
                }
            ]
        }
    }
}

All extra.php-env-builder options

  • questions - default: [] (required) // An array of questions that contain at least the name and prompt elements.
  • envFile - default: .env // Either the absolute location or one that is relative to your package.json file.
  • clobber - default: false // Will an existing .env file be overwritten on build?
  • loadEnv - default: false // If an existing file is being clobbered, will it be loaded to provide defaults?
  • verbose - default: false // Extra output

General Usage

If you would like to roll your own script, the syntax itself is fairly straightforward.

Note: This is a less expected use case, since the console IO needs to be provided by Composer, in order for it to accept input from inside a Composer script. This may be useful if you want to run this instead from a Makefile or an npm run postinstall

require_once __DIR__ . "/vendor/autoload.php";

$config = [
    'verbose' => true,
    'loadEnv' => true,
];

$builder = new \Dbtlr\PHPEnvBuilder\Builder('/path/to/.env', $config);

$builder->ask(
    'name',              // ENV variable name
    'What is your name?' // Command prompt
    '',                  // Default answer
    true                 // Is required?
);

$builder->run(); // Run the builder and return the answers.
$builder->write(); // Write the answers to the file.

Running tests?

All tests are run using PHPUnit. Make sure you have at least PHP 7.1 installed, as well as Composer.

To run tests, simply run:

composer install
composer test

Want to contribute?

Read more here