定制 winter/laravel-config-writer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

winter/laravel-config-writer

Composer 安装命令:

composer require winter/laravel-config-writer

包简介

Utility to create and update Laravel config and .env files

README 文档

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.

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 winter/laravel-config-writer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 355.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 23
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 5
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-01