winter/laravel-config-writer

Utility to create and update Laravel config and .env files

v1.1.0 2023-08-09 15:37 UTC

This package is auto-updated.

Last update: 2024-03-09 16:50:55 UTC


README

Version Tests License Discord

A utility to easily create and modify Laravel-style PHP configuration files and environment files whilst maintaining the formatting and comments contained within. This utility works by parsing the configuration files using the PHP Parser library to convert the configuration into an abstract syntax tree, then carefully modifying the configuration values as required.

This library was originally written as part of the Storm library in Winter CMS, but has since been extracted and repurposed as a standalone library.

Installation

composer require winter/laravel-config-writer

Usage

PHP array files

You can modify Laravel-style PHP configuration files - PHP files that return a single array - by using the Winter\LaravelConfigWriter\ArrayFile class. Use the open method to open an existing file for modification, or to create a new config file.

use Winter\LaravelConfigWriter\ArrayFile;

$config = ArrayFile::open(base_path('config/app.php'));

You can set values using the set method. This method can be used fluently, or can be called with a single key and value or an array of keys and values.

$config->set('name', 'Winter CMS');

$config
    ->set('locale', 'en_US')
    ->set('fallbackLocale', 'en');

$config->set([
    'trustedHosts' => true,
    'trustedProxies' => '*',
]);

You can also set deep values in an array value by specifying the key in dot notation, or as a nested array.

$config->set('connections.mysql.host', 'localhost');

$config->set([
    'connections' => [
        'sqlite' => [
            'database' => 'database.sqlite',
            'driver' => 'sqlite',
            'foreign_key_constraints' => true,
            'prefix' => '',
            'url' => null,
        ],
    ],
]);

To finalise all your changes, use the write method to write the changes to the open file.

$config->write();

If desired, you may also write the changes to another file altogether.

$config->write('path/to/newfile.php');

Or you can simply render the changes as a string.

$config->render();

Function calls as values

Function calls can be added to your configuration file by using the function method. The first parameter of the function method defines the function to call, and the second parameter accepts an array of parameters to provide to the function.

$config->set('name', $config->function('env', ['APP_NAME', 'Winter CMS']));

Constants as values

Constants can be added to your configuration file by using the constant method. The only parameter required is the name of the constant.

$config->set('foo.bar', $config->constant('My\Class::CONSTANT'));

Sorting the configuration file

You can sort the configuration keys alphabetically by using the sort method. This will sort all current configuration values.

$config->sort();

By default, this will sort the keys alphabetically in ascending order. To sort in the opposite direction, include the ArrayFile::SORT_DESC parameter.

$config->sort(ArrayFile::SORT_DESC);

Environment files

This utility library also allows manipulation of environment files, typically found as .env files in a project. The Winter\LaravelConfigWriter\EnvFile::open() method allows you to open or create an environment file for modification.

use Winter\LaravelConfigWriter\EnvFile;

$env = EnvFile::open(base_path('.env'));

You can set values using the set method. This method can be used fluently, or can be called with a single key and value or an array of keys and values.

$env->set('APP_NAME', 'Winter CMS');

$env
    ->set('APP_URL', 'https://wintercms.com')
    ->set('APP_ENV', 'production');

$env->set([
    'DB_CONNECTION' => 'sqlite',
    'DB_DATABASE' => 'database.sqlite',
]);

Note: Arrays are not supported in environment files.

You can add an empty line into the environment file by using the addEmptyLine method. This allows you to separate groups of environment variables.

$env->set('FOO', 'bar');
$env->addEmptyLine();
$env->set('BAR', 'foo');

To finalise all your changes, use the write method to write the changes to the open file.

$env->write();

If desired, you may also write the changes to another file altogether.

$env->write(base_path('.env.local'));

Or you can simply render the changes as a string.

$env->render();

License

This utility library is open-sourced software licensed under the MIT license.

Security vulnerabilities

Please review our security policy on how to report security vulnerabilities.