gmask/config-generator

Convert .env files to static .php configuration files.



Documentation

About

This package converts .env files to static .php, .json, or .yaml files.

For example, convert an .env file like this:

APP_NAME=MyApp
APP_ENV=local
APP_KEY=123
APP_DEBUG=true
APP_URL=http://localhost

EXAMPLE_NAMED_ARRAY="firstKey::firstValue,secondKey::secondValue,thirdKey::thirdValue"
EXAMPLE_NORMAL_ARRAY="firstValue,secondValue,thirdValue"

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

To a static .php file like this:

<?php

return [
    'app' => [
        'name' => 'MyApp',
        'env' => 'local',
        'key' => '123',
        'debug' => '1',
        'url' => 'http://localhost',
    ],
    'example' => [
        'namedArray' => [
            'firstKey' => 'firstValue',
            'secondKey' => 'secondValue',
            'thirdKey' => 'thirdValue',
        ],
        'normalArray' => [
            0 => 'firstValue',
            1 => 'secondValue',
            2 => 'thirdValue',
        ],
    ],
    'db' => [
        'connection' => 'mysql',
        'host' => '127.0.0.1',
        'port' => '3306',
        'database' => 'homestead',
        'username' => 'homestead',
        'password' => 'secret',
    ],
    'redis' => [
        'host' => '127.0.0.1',
        'password' => '',
        'port' => '6379',
    ],
];

Usage

Install:

composer require gmask/config-generator

You must provide the input file (must exist) and output target (does not need to exist, just ensure the path is writable).

<?php

function generateConfig()
{
    $envFile = base_path('test.env');
    $outputFilename = base_path('config/test.php');

    $generator = new \Gmask\ConfigGenerator($envFile, $outputFilename);
    $generator->generate()->php();

    $output->writeln('Config generated at ' . $outputFilename);
}

Syntax

Key/Value:

Input:

APP_EXAMPLE=HelloWorld

Output:

'app' => [
    'example' => 'HelloWorld',
],

Normal Array:

Input:

EXAMPLE_NORMAL_ARRAY="firstValue,secondValue,thirdValue"

Output:

'example' => [
    'normalArray' => [
        0 => 'firstValue',
        1 => 'secondValue',
        2 => 'thirdValue',
    ],
],

Named Keys Array:

Input:

EXAMPLE_NAMED_ARRAY="firstKey::firstValue,secondKey::secondValue,thirdKey::thirdValue"

Output:

'example' => [
    'namedArray' => [
        'firstKey' => 'firstValue',
        'secondKey' => 'secondValue',
        'thirdKey' => 'thirdValue',
    ],
],

Options

You can optionally pass an options array to the class:

$options = ['snakeCase', 'singleKeys'];

$generator = new \Gmask\ConfigGenerator($envFile, $outputFilename, $options);

snakeCase: returns parent key's using snake_case instead of the default camelCase.

singleKeys: if the setting is a single option you can prevent it from being converted to an array.

APP_ENV=local becomes:

'appEnv' => 'local',

Default:

'app' => [
    'env' => 'local',
],