winter/laravel-config-writer
Composer 安装命令:
composer require winter/laravel-config-writer
包简介
Utility to create and update Laravel config and .env files
README 文档
README
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.
winter/laravel-config-writer 适用场景与选型建议
winter/laravel-config-writer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 355.13k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2022 年 08 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 winter/laravel-config-writer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 winter/laravel-config-writer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 355.13k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 23
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-01