PHPackages                             sven/file-config - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. sven/file-config

ActiveLibrary[File &amp; Storage](/categories/file-storage)

sven/file-config
================

Store and read configuration values using files on disk

v3.2.0(4y ago)2730.5k↓50%5[1 PRs](https://github.com/svenluijten/file-config/pulls)1MITPHPPHP ^8.0CI passing

Since Jan 19Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/svenluijten/file-config)[ Packagist](https://packagist.org/packages/sven/file-config)[ RSS](/packages/sven-file-config/feed)WikiDiscussions 3.x Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (12)Used By (1)

[![file-config](https://user-images.githubusercontent.com/11269635/35174536-129cc67e-fd70-11e7-8b87-d2ba8cc24ec8.jpg)](https://user-images.githubusercontent.com/11269635/35174536-129cc67e-fd70-11e7-8b87-d2ba8cc24ec8.jpg)

File Config
===========

[](#file-config)

[![Latest Version on Packagist](https://camo.githubusercontent.com/7e2a0c110438808cbdefa96433574ded76ca7422aa29894b1160a6568ec114c1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7376656e2f66696c652d636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sven/file-config)[![Total Downloads](https://camo.githubusercontent.com/5669251e988941fca8a20fbd9700dbd8af56ae650d467e6e8ad8e5d09f63d621/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7376656e2f66696c652d636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sven/file-config)[![Software License](https://camo.githubusercontent.com/6c711032aff1ca0eb6b211aa6cb3649ce7fd64a7714e1181d4bb457f9680e7cf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/d2a7ae7a841c6db48cdba8bb7848b5f83ea79e5ed027a164919e025ed0bff38f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7376656e6c75696a74656e2f66696c652d636f6e6669672f74657374732e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/svenluijten/file-config/actions/workflows/tests.yml)[![StyleCI](https://camo.githubusercontent.com/daf37f9ebadddf642ed7f072b4bef6ae086a93dad54f136f38154b818fbdb299/68747470733a2f2f7374796c6563692e696f2f7265706f732f3131373839313830332f736869656c64)](https://styleci.io/repos/117891803)

This package provides a persistent config store as flat files with an easy to use and understand API. This is perfect if the config file should be stored in userland, or somewhere the user is allowed to edit it manually.

Installation
------------

[](#installation)

You'll have to follow a couple of simple steps to install this package.

### Downloading

[](#downloading)

Via [composer](http://getcomposer.org):

```
$ composer require sven/file-config:^3.1
```

Or add the package to your dependencies in `composer.json` and run `composer update sven/file-config` on the command line to download the package:

```
{
    "require": {
        "sven/file-config": "^3.1"
    }
}
```

Available drivers
-----------------

[](#available-drivers)

- [`Json`](./src/Drivers/Json.php) - For `.json` files.
- [`DotEnv`](./src/Drivers/DotEnv.php) - For `.env` files.
- [`Env` (deprecated)](./src/Drivers/Env.php)

You can also write your own driver to use in your own applications. To write your own, read [writing your own driver](#writing-your-own-driver) in this document.

Usage
-----

[](#usage)

To get started, construct a new instance of `\Sven\FileConfig\Store`, providing it with a `\Sven\FileConfig\File`object, and an implementation of the `\Sven\FileConfig\Drivers\Driver` interface. We'll use the pre-installed `Json` driver in the examples.

```
use Sven\FileConfig\Store;
use Sven\FileConfig\Drivers\Json;

$config = new Store('/path/to/file.json', new Json());
```

You can interact with your newly created `$config` object via the `get`, `set`, and `delete`methods.

### Examples

[](#examples)

Let's take a look at some examples.

#### Getting a value from the file

[](#getting-a-value-from-the-file)

To retrieve a value from the configuration file, use the `get` method. Let's assume our (prettified) JSON configuration file looks like this:

```
{
    "database": {
        "name": "test",
        "host": "localhost",
        "user": "admin",
        "password": "root"
    }
}
```

We can get the entire `database` array:

```
$config->get('database');
// ~> ['name' => 'test', 'host' => 'localhost', 'user' => 'admin', 'password' => root']
```

... or get only the `database.host` property using dot-notation:

```
$config->get('database.host');
// ~> 'localhost'
```

If the given key can not be found, `null` is returned by default. You may override this by passing a second argument to `get`:

```
$config->get('database.does_not_exist', 'default');
// ~> 'default'
```

#### Setting a value in the file

[](#setting-a-value-in-the-file)

To add or change a value in the configuration file, you may use the `set` method. Note that you have to call the `persist` method to write the changes you made to the file. You may also use the `fresh` method to retrieve a "fresh" instance of the `Store`, where the values will be read from the file again.

```
$config->set('database.user', 'new-username');
$config->persist();

$freshConfig = $config->fresh();
$freshConfig->get('database.user');
// ~> 'new-username'
```

The file will end up looking like this after you've called the `persist` method:

```
{
    "database": {
        "name": "test",
        "host": "localhost",
        "user": "new-username",
        "password": "root"
    }
}
```

#### Deleting an entry from the file

[](#deleting-an-entry-from-the-file)

To remove one of the configuration options from the file, use the `delete` method. Again, don't forget to call `persist` to write the new contents to the file!

```
$config->delete('database.user');
$config->persist();
```

```
{
    "database": {
        "name": "test",
        "host": "localhost",
        "password": "root"
    }
}
```

Writing your own driver
-----------------------

[](#writing-your-own-driver)

You may want to use a file format for your configuration that's not (yet) included in this package. Thankfully, writing a driver is as straightforward as turning your file's contents into a PHP array.

To create a driver, create a class that implements the `\Sven\FileConfig\Drivers\Driver`interface. Then add 2 methods to your class: `import` and `export`. The `import` method will receive the contents of the file as an argument, and expects a PHP array to be returned.

The `export` method is the exact reverse: it receives a PHP array, and expects the new contents of the file to be returned (as a string). To see how this works in more detail, take a look at [the pre-installed `json` driver](src/Drivers/Json.php).

Contributing
------------

[](#contributing)

All contributions (pull requests, issues and feature requests) are welcome. Make sure to read through the [CONTRIBUTING.md](CONTRIBUTING.md) first, though. See the [contributors page](../../graphs/contributors) for all contributors.

License
-------

[](#license)

`sven/file-config` is licensed under the MIT License (MIT). Please see the [license file](LICENSE.md) for more information.

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance58

Moderate activity, may be stable

Popularity38

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 71.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~418 days

Recently: every ~717 days

Total

8

Last Release

113d ago

Major Versions

v1.0.0 → v2.0.02018-03-18

2.x-dev → v3.0.02019-09-15

PHP version history (3 changes)1.x-devPHP ^7.1

v3.1.0PHP ^7.1 || ^8.0

v3.2.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/6682e025b83b7a93b4d43c5c9b0b2245d790d72352758c47b81ba14858f45a8a?d=identicon)[svenluijten](/maintainers/svenluijten)

---

Top Contributors

[![svenluijten](https://avatars.githubusercontent.com/u/11269635?v=4)](https://github.com/svenluijten "svenluijten (57 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (19 commits)")[![moebrowne](https://avatars.githubusercontent.com/u/8448512?v=4)](https://github.com/moebrowne "moebrowne (3 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (1 commits)")

---

Tags

configurationflat-filehacktoberfestpackagephpuser-configuserlandjsonconfigurationconfigfileenvflatflatfile

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sven-file-config/health.svg)

```
[![Health](https://phpackages.com/badges/sven-file-config/health.svg)](https://phpackages.com/packages/sven-file-config)
```

###  Alternatives

[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[jamesmoss/flywheel

A lightweight, flat-file, document database

33215.4k5](/packages/jamesmoss-flywheel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
