{"id":18358630,"url":"https://github.com/svenluijten/file-config","last_synced_at":"2025-04-07T13:07:19.552Z","repository":{"id":29244556,"uuid":"117891803","full_name":"svenluijten/file-config","owner":"svenluijten","description":"📄 Store and read configuration values using files on disk.","archived":false,"fork":false,"pushed_at":"2025-02-10T14:22:19.000Z","size":101,"stargazers_count":27,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"3.x","last_synced_at":"2025-03-31T11:06:51.199Z","etag":null,"topics":["configuration","flat-file","hacktoberfest","package","php","user-config","userland"],"latest_commit_sha":null,"homepage":"https://git.io/fic","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/svenluijten.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","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}},"created_at":"2018-01-17T20:56:50.000Z","updated_at":"2025-02-10T14:22:21.000Z","dependencies_parsed_at":"2023-01-14T14:29:42.471Z","dependency_job_id":"c8fe66aa-4499-4875-811a-c6132b011682","html_url":"https://github.com/svenluijten/file-config","commit_stats":{"total_commits":61,"total_committers":4,"mean_commits":15.25,"dds":0.2786885245901639,"last_synced_commit":"cfed73b60870d9da704f41d9f0f65bc09d09f25b"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svenluijten%2Ffile-config","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svenluijten%2Ffile-config/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svenluijten%2Ffile-config/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svenluijten%2Ffile-config/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/svenluijten","download_url":"https://codeload.github.com/svenluijten/file-config/tar.gz/refs/heads/3.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247657281,"owners_count":20974345,"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":["configuration","flat-file","hacktoberfest","package","php","user-config","userland"],"created_at":"2024-11-05T22:18:48.432Z","updated_at":"2025-04-07T13:07:19.527Z","avatar_url":"https://github.com/svenluijten.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"![file-config](https://user-images.githubusercontent.com/11269635/35174536-129cc67e-fd70-11e7-8b87-d2ba8cc24ec8.jpg)\n\n# File Config\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Total Downloads][ico-downloads]][link-downloads]\n[![Software License][ico-license]](LICENSE.md)\n[![Build Status][ico-build]][link-build]\n[![StyleCI][ico-styleci]][link-styleci]\n\nThis package provides a persistent config store as flat files with an easy\nto use and understand API. This is perfect if the config file should be \nstored in userland, or somewhere the user is allowed to edit it manually.\n\n## Installation\nYou'll have to follow a couple of simple steps to install this package.\n\n### Downloading\nVia [composer](http://getcomposer.org):\n\n```bash\n$ composer require sven/file-config:^3.1\n```\n\nOr add the package to your dependencies in `composer.json` and run\n`composer update sven/file-config` on the command line to download\nthe package:\n\n```json\n{\n    \"require\": {\n        \"sven/file-config\": \"^3.1\"\n    }\n}\n```\n\n## Available drivers\n- [`Json`](./src/Drivers/Json.php) - For `.json` files.\n- [`DotEnv`](./src/Drivers/DotEnv.php) - For `.env` files.\n- [`Env` (deprecated)](./src/Drivers/Env.php)\n\nYou can also write your own driver to use in your own applications. To write your\nown, read [writing your own driver](#writing-your-own-driver) in this document.\n\n## Usage\nTo get started, construct a new instance of `\\Sven\\FileConfig\\Store`, providing it with a `\\Sven\\FileConfig\\File`\nobject, and an implementation of the `\\Sven\\FileConfig\\Drivers\\Driver` interface. We'll use the pre-installed \n`Json` driver in the examples.\n\n```php\nuse Sven\\FileConfig\\File;\nuse Sven\\FileConfig\\Store;\nuse Sven\\FileConfig\\Drivers\\Json;\n\n$file = new File('/path/to/file.json');\n$config = new Store($file, new Json());\n```\n\nYou can interact with your newly created `$config` object via the `get`, `set`, and `delete` \nmethods.\n\n### Examples\nLet's take a look at some examples.\n\n#### Getting a value from the file\nTo retrieve a value from the configuration file, use the `get` method. Let's assume our (prettified)\nJSON configuration file looks like this:\n\n```json\n{\n    \"database\": {\n        \"name\": \"test\",\n        \"host\": \"localhost\",\n        \"user\": \"admin\",\n        \"password\": \"root\"\n    }\n}\n```\n\nWe can get the entire `database` array:\n\n```php\n$config-\u003eget('database'); \n// ~\u003e ['name' =\u003e 'test', 'host' =\u003e 'localhost', 'user' =\u003e 'admin', 'password' =\u003e root']\n```\n\n... or get only the `database.host` property using dot-notation:\n\n```php\n$config-\u003eget('database.host'); \n// ~\u003e 'localhost'\n```\n\nIf the given key can not be found, `null` is returned by default. You may override this by\npassing a second argument to `get`:\n\n```php\n$config-\u003eget('database.does_not_exist', 'default');\n// ~\u003e 'default'\n```\n\n#### Setting a value in the file\nTo add or change a value in the configuration file, you may use the `set` method. Note that\nyou have to call the `persist` method to write the changes you made to the file. You may also\nuse the `fresh` method to retrieve a \"fresh\" instance of the `Store`, where the values will be\nread from the file again.\n\n```php\n$config-\u003eset('database.user', 'new-username');\n$config-\u003epersist();\n\n$freshConfig = $config-\u003efresh();\n$freshConfig-\u003eget('database.user');\n// ~\u003e 'new-username'\n```\n\nThe file will end up looking like this after you've called the `persist` method:\n\n```json\n{\n    \"database\": {\n        \"name\": \"test\",\n        \"host\": \"localhost\",\n        \"user\": \"new-username\",\n        \"password\": \"root\"\n    }\n}\n```\n\n#### Deleting an entry from the file\nTo remove one of the configuration options from the file, use the `delete` method. Again, don't forget\nto call `persist` to write the new contents to the file!\n\n```php\n$config-\u003edelete('database.user');\n$config-\u003epersist();\n```\n\n```json\n{\n    \"database\": {\n        \"name\": \"test\",\n        \"host\": \"localhost\",\n        \"password\": \"root\"\n    }\n}\n```\n\n## Writing your own driver\nYou may want to use a file format for your configuration that's not (yet) included in\nthis package. Thankfully, writing a driver is as straightforward as turning your file's\ncontents into a PHP array.\n\nTo create a driver, create a class that implements the `\\Sven\\FileConfig\\Drivers\\Driver`\ninterface. Then add 2 methods to your class: `import` and `export`. The `import` method\nwill receive the contents of the file as an argument, and expects a PHP array to be returned.\n\nThe `export` method is the exact reverse: it receives a PHP array, and expects the new contents\nof the file to be returned (as a string). To see how this works in more detail, take a look at \n[the pre-installed `json` driver](src/Drivers/Json.php).\n\n## Contributing\nAll contributions (pull requests, issues and feature requests) are\nwelcome. Make sure to read through the [CONTRIBUTING.md](CONTRIBUTING.md) first,\nthough. See the [contributors page](../../graphs/contributors) for all contributors.\n\n## License\n`sven/file-config` is licensed under the MIT License (MIT). Please see the\n[license file](LICENSE.md) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/sven/file-config.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-green.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/sven/file-config.svg?style=flat-square\n[ico-build]: https://img.shields.io/github/actions/workflow/status/svenluijten/file-config/tests.yml?style=flat-square\n[ico-styleci]: https://styleci.io/repos/117891803/shield\n\n[link-packagist]: https://packagist.org/packages/sven/file-config\n[link-downloads]: https://packagist.org/packages/sven/file-config\n[link-build]: https://github.com/svenluijten/file-config/actions/workflows/tests.yml\n[link-styleci]: https://styleci.io/repos/117891803\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvenluijten%2Ffile-config","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsvenluijten%2Ffile-config","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvenluijten%2Ffile-config/lists"}