sunny-solaris/config-editor
Composer 安装命令:
composer require sunny-solaris/config-editor
包简介
A powerful PHP configuration file editor using nikic/php-parser. Programmatically read and modify PHP config files while preserving formatting and structure.
README 文档
README
A powerful PHP configuration file editor that allows you to programmatically read and modify PHP configuration files while preserving formatting and structure. Built with nikic/php-parser.
Features
- 📝 Read & Modify PHP config files programmatically
- 🔧 Preserve Formatting - Maintains original code formatting and structure
- 🎯 Dot Notation - Access nested arrays using dot notation (e.g.,
database.connections.mysql.host) - 🔄 Merge Config - Merge configuration files intelligently
- 🚀 Laravel Support - Works seamlessly with Laravel 12+
- 🐘 PHP 8.2+ - Modern PHP with typed properties and union types
- ⚙️ Raw Code - Support for raw PHP expressions via
setRaw()andaddRaw()
Requirements
- PHP 8.2 or higher
- nikic/php-parser ^4.13 or ^5.0
Installation
Install the package via Composer:
composer require solaris/config-editor
Quick Start
Basic Usage
use Solaris\ConfigEditor\ConfigEditor; // Load a config file $config = new ConfigEditor('config/database.php'); // Set a value $config->set('connections.mysql.host', '127.0.0.1'); $config->set('connections.mysql.port', 3306); // Get a value (has method) if ($config->has('connections.mysql.username')) { // Do something } // Add a new key (throws exception if key exists) $config->add('new_setting', 'value'); // Delete a key $config->delete('connections.mysql'); // Save changes $config->save();
Working with Raw PHP Code
For complex values like closures or PHP expressions:
// Set raw PHP code $config->setRaw('cache.stores.redis.client', 'env("REDIS_CLIENT", "phpredis")'); // Add raw PHP code $config->addRaw('logging.channels.custom', 'Log::channel("custom")'); // Push raw value to array $config->pushRaw('providers', 'AppServiceProvider::class');
Array Operations
// Add value to existing array $config->push('providers', 'MyServiceProvider::class'); // Push raw PHP expression to array $config->pushRaw('middleware', 'TrustProxies::class');
Merging Configurations
$config = new ConfigEditor('config/app.php'); // Merge another config file $config->merge('config/app.local.php') ->save();
Fluent Interface
Most methods return $this, allowing method chaining:
$config = new ConfigEditor('config/database.php'); $config ->set('connections.mysql.host', 'localhost') ->set('connections.mysql.database', 'myapp') ->set('connections.mysql.prefix', 'app_') ->merge('config/database.local.php') ->save();
API Reference
Constructor
new ConfigEditor(string $path)
Loads a PHP configuration file. Path can be with or without .php extension.
Throws: RuntimeException if file not found or is not a valid config file.
Methods
set(string $key, mixed $value): void
Set a configuration value. Creates intermediate arrays if needed.
add(string $key, mixed $value): void
Add a new configuration key. Throws exception if key already exists.
setRaw(string $key, string $code): self
Set a value using raw PHP code.
addRaw(string $key, string $code): self
Add a new value using raw PHP code.
push(string $key, mixed $value): void
Add a value to an existing array.
pushRaw(string $key, string $code): self
Add a raw PHP expression to an existing array.
delete(string $key): void
Delete a configuration key.
has(string $key): bool
Check if a configuration key exists.
merge(string $path): self
Merge another configuration file. Returns self for chaining.
save(?string $filename = null): self
Save changes to file. If filename is provided, saves to that file instead.
Laravel Integration
This package works seamlessly with Laravel's configuration files:
use Solaris\ConfigEditor\ConfigEditor; // Edit Laravel config file $config = new ConfigEditor(config_path('database.php')); $config ->set('default', 'pgsql') ->set('connections.pgsql.host', env('DB_HOST', 'localhost')) ->save();
Laravel Service Provider (Optional)
You can create a service provider to register this as a singleton:
namespace App\Providers; use Illuminate\Support\ServiceProvider; use Solaris\ConfigEditor\ConfigEditor; class ConfigEditorProvider extends ServiceProvider { public function register() { $this->app->singleton('config-editor', function ($app) { return new ConfigEditor(config_path('app.php')); }); } }
Then use it in your application:
app('config-editor')->set('key', 'value')->save();
Examples
Update Database Configuration
$config = new ConfigEditor('config/database.php'); $config ->set('connections.mysql.host', 'db.example.com') ->set('connections.mysql.username', 'dbuser') ->set('connections.mysql.password', 'secret') ->set('connections.mysql.database', 'production_db') ->save();
Add New Service Provider
$config = new ConfigEditor('config/app.php'); $config->push('providers', 'App\\Providers\\CustomServiceProvider::class'); $config->save();
Merge Environment-Specific Config
$config = new ConfigEditor('config/app.php'); if (env('APP_ENV') === 'production') { $config->merge('config/app.production.php'); } $config->save();
Testing
Run the test suite:
composer test
Changelog
See CHANGELOG.md for release notes and version history.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This package is open-sourced software licensed under the MIT license.
Support
For issues and questions, please use the GitHub Issues page.
Made with ❤️ by Solaris
sunny-solaris/config-editor 适用场景与选型建议
sunny-solaris/config-editor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 81 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「configuration」 「parser」 「editor」 「config」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sunny-solaris/config-editor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sunny-solaris/config-editor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sunny-solaris/config-editor 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Configuration component
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
An MT940 bank statement parser for PHP
Laravel integration for the jsonapi.org parser
Utils to load, parse and work with configuration on Mezzio projects
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
统计信息
- 总下载量: 81
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-14