{"id":20924950,"url":"https://github.com/wintercms/laravel-config-writer","last_synced_at":"2025-05-13T16:31:44.394Z","repository":{"id":62355062,"uuid":"519957708","full_name":"wintercms/laravel-config-writer","owner":"wintercms","description":"Utility to create and update Laravel config and .env files","archived":false,"fork":false,"pushed_at":"2024-09-24T17:41:35.000Z","size":46,"stargazers_count":13,"open_issues_count":1,"forks_count":8,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-27T10:41:43.451Z","etag":null,"topics":["config","hacktoberfest","laravel","php","plugin","wintercms"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wintercms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"wintercms","open_collective":"wintercms"}},"created_at":"2022-08-01T04:01:06.000Z","updated_at":"2024-11-09T21:45:15.000Z","dependencies_parsed_at":"2025-04-19T08:33:42.181Z","dependency_job_id":null,"html_url":"https://github.com/wintercms/laravel-config-writer","commit_stats":{"total_commits":27,"total_committers":3,"mean_commits":9.0,"dds":"0.33333333333333337","last_synced_commit":"9b5d016beb0cbbfff4f1ee2d8184d6652417005e"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wintercms%2Flaravel-config-writer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wintercms%2Flaravel-config-writer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wintercms%2Flaravel-config-writer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wintercms%2Flaravel-config-writer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wintercms","download_url":"https://codeload.github.com/wintercms/laravel-config-writer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252044563,"owners_count":21685656,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","hacktoberfest","laravel","php","plugin","wintercms"],"created_at":"2024-11-18T20:28:16.742Z","updated_at":"2025-05-13T16:31:43.026Z","avatar_url":"https://github.com/wintercms.png","language":"PHP","funding_links":["https://github.com/sponsors/wintercms","https://opencollective.com/wintercms"],"categories":[],"sub_categories":[],"readme":"# Laravel Config Writer\n\n[![Version](https://img.shields.io/github/v/release/wintercms/laravel-config-writer?sort=semver\u0026style=flat-square)](https://github.com/wintercms/laravel-config-writer/releases)\n[![Tests](https://img.shields.io/github/actions/workflow/status/wintercms/laravel-config-writer/tests.yaml?\u0026label=tests\u0026style=flat-square)](https://github.com/wintercms/laravel-config-writer/actions)\n[![License](https://img.shields.io/github/license/wintercms/laravel-config-writer?label=open%20source\u0026style=flat-square)](https://packagist.org/packages/winter/laravel-config-writer)\n[![Discord](https://img.shields.io/discord/816852513684193281?label=discord\u0026style=flat-square)](https://discord.gg/D5MFSPH6Ux)\n\nA 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](https://github.com/nikic/php-parser) to convert the configuration into an abstract syntax tree, then carefully modifying the configuration values as required.\n\nThis library was originally written as part of the [Storm library](https://github.com/wintercms/storm) in [Winter CMS](https://wintercms.com), but has since been extracted and repurposed as a standalone library.\n\n## Installation\n\n```\ncomposer require winter/laravel-config-writer\n```\n\n## Usage\n\n### PHP array files\n\nYou 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.\n\n```php\nuse Winter\\LaravelConfigWriter\\ArrayFile;\n\n$config = ArrayFile::open(base_path('config/app.php'));\n```\n\nYou 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.\n\n```php\n$config-\u003eset('name', 'Winter CMS');\n\n$config\n    -\u003eset('locale', 'en_US')\n    -\u003eset('fallbackLocale', 'en');\n\n$config-\u003eset([\n    'trustedHosts' =\u003e true,\n    'trustedProxies' =\u003e '*',\n]);\n```\n\nYou can also set deep values in an array value by specifying the key in dot notation, or as a nested array.\n\n```php\n$config-\u003eset('connections.mysql.host', 'localhost');\n\n$config-\u003eset([\n    'connections' =\u003e [\n        'sqlite' =\u003e [\n            'database' =\u003e 'database.sqlite',\n            'driver' =\u003e 'sqlite',\n            'foreign_key_constraints' =\u003e true,\n            'prefix' =\u003e '',\n            'url' =\u003e null,\n        ],\n    ],\n]);\n```\n\nTo finalise all your changes, use the `write` method to write the changes to the open file.\n\n```php\n$config-\u003ewrite();\n```\n\nIf desired, you may also write the changes to another file altogether.\n\n```php\n$config-\u003ewrite('path/to/newfile.php');\n```\n\nOr you can simply render the changes as a string.\n\n```php\n$config-\u003erender();\n```\n\n#### Function calls as values\n\nFunction 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.\n\n```php\n$config-\u003eset('name', $config-\u003efunction('env', ['APP_NAME', 'Winter CMS']));\n```\n\n#### Constants as values\n\nConstants can be added to your configuration file by using the `constant` method. The only parameter required is the name of the constant.\n\n```php\n$config-\u003eset('foo.bar', $config-\u003econstant('My\\Class::CONSTANT'));\n```\n\n#### Sorting the configuration file\n\nYou can sort the configuration keys alphabetically by using the `sort` method. This will sort all current configuration values.\n\n```php\n$config-\u003esort();\n```\n\nBy default, this will sort the keys alphabetically in ascending order. To sort in the opposite direction, include the `ArrayFile::SORT_DESC` parameter.\n\n```php\n$config-\u003esort(ArrayFile::SORT_DESC);\n```\n\n### Environment files\n\nThis 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.\n\n```php\nuse Winter\\LaravelConfigWriter\\EnvFile;\n\n$env = EnvFile::open(base_path('.env'));\n```\n\nYou 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.\n\n```php\n$env-\u003eset('APP_NAME', 'Winter CMS');\n\n$env\n    -\u003eset('APP_URL', 'https://wintercms.com')\n    -\u003eset('APP_ENV', 'production');\n\n$env-\u003eset([\n    'DB_CONNECTION' =\u003e 'sqlite',\n    'DB_DATABASE' =\u003e 'database.sqlite',\n]);\n```\n\n\u003e **Note:** Arrays are not supported in environment files.\n\nYou can add an empty line into the environment file by using the `addEmptyLine` method. This allows you to separate groups of environment variables.\n\n```php\n$env-\u003eset('FOO', 'bar');\n$env-\u003eaddEmptyLine();\n$env-\u003eset('BAR', 'foo');\n```\n\nTo finalise all your changes, use the `write` method to write the changes to the open file.\n\n```php\n$env-\u003ewrite();\n```\n\nIf desired, you may also write the changes to another file altogether.\n\n```php\n$env-\u003ewrite(base_path('.env.local'));\n```\n\nOr you can simply render the changes as a string.\n\n```php\n$env-\u003erender();\n```\n\n## License\n\nThis utility library is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).\n\n## Security vulnerabilities\n\nPlease review our [security policy](https://github.com/wintercms/winter/security/policy) on how to report security vulnerabilities.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwintercms%2Flaravel-config-writer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwintercms%2Flaravel-config-writer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwintercms%2Flaravel-config-writer/lists"}