PHPackages                             locomotivemtl/charcoal-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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. locomotivemtl/charcoal-config

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

locomotivemtl/charcoal-config
=============================

Charcoal component for configuration data and object modeling

0.10.1(6y ago)024.5k16MITPHPPHP &gt;=5.6.0 || &gt;=7.0CI failing

Since Aug 25Pushed 6y ago12 watchersCompare

[ Source](https://github.com/locomotivemtl/charcoal-config)[ Packagist](https://packagist.org/packages/locomotivemtl/charcoal-config)[ Docs](https://locomotivemtl.github.io/charcoal-config/)[ RSS](/packages/locomotivemtl-charcoal-config/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (5)Versions (17)Used By (16)

Charcoal Config
===============

[](#charcoal-config)

[![License](https://camo.githubusercontent.com/403697b73d3f7be34a38a3029a871174bd4817a4705141738bcc310513cc1d00/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c6f636f6d6f746976656d746c2f63686172636f616c2d636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/locomotivemtl/charcoal-config)[![Latest Stable Version](https://camo.githubusercontent.com/062497267cdc6822e5910b7dc6b9454053848e7436eeaa0862247288d38a37db/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6f636f6d6f746976656d746c2f63686172636f616c2d636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/locomotivemtl/charcoal-config)[![Code Quality](https://camo.githubusercontent.com/ab73f76ec7f9ec6401a5334df71a8e8039a1862d00eaa95851d51af1ccb641ab/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6c6f636f6d6f746976656d746c2f63686172636f616c2d636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/locomotivemtl/charcoal-config/)[![Coverage Status](https://camo.githubusercontent.com/19dd542b6e14d568681b66d94fcc8d9fa715699cbb10677115047a4533752e4d/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6c6f636f6d6f746976656d746c2f63686172636f616c2d636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/locomotivemtl/charcoal-config)[![SensioLabs Insight](https://camo.githubusercontent.com/69ef2e6abe432dd987db966f0c8df4acca6cb12c5e16fdb5470fc71ff9c25c86/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f32376164323035662d343230382d346661362d396463662d3533346233613163306161612e7376673f7374796c653d666c61742d737175617265)](https://insight.sensiolabs.com/projects/27ad205f-4208-4fa6-9dcf-534b3a1c0aaa)[![Build Status](https://camo.githubusercontent.com/a6a247a33a9cef6aed655e48b1d9a2772c629b3e6f8ff94a3f66d381d8412bf5/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6c6f636f6d6f746976656d746c2f63686172636f616c2d636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/locomotivemtl/charcoal-config)

A Charcoal component for organizing configuration data and designing object data models.

This component is the glue for much of the [Charcoal framework](https://packagist.org/packages/locomotivemtl/charcoal-app).

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
    - [Requirements](#requirements)
- [Entity &amp; Config](#entity--config)
    - [Entity](#entity)
    - [Config](#config)
- [Features](#features)
    - [File Loader](#file-loader)
    - [Key Separator Lookup](#key-separator-lookup)
    - [Delegated Data Lookup](#delegates-lookup)
    - [Array Access](#array-access)
    - [Interoperability](#interoperability)
    - [Configurable objects](#configurable-objects)
- [Development](#development)
    - [API Documentation](#api-documentation)
    - [Development Dependencies](#development-dependencies)
    - [Coding Style](#coding-style)
- [Credits](#credits)
- [License](#license)

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

[](#installation)

The preferred (and only supported) method is with Composer:

```
$ composer require locomotivemtl/charcoal-config
```

### Requirements

[](#requirements)

- [**PHP 5.6+**](https://php.net): *PHP 7* is recommended.

#### PSR

[](#psr)

- [**PSR-11**](https://www.php-fig.org/psr/psr-11/): Common interface for dependency containers. For [interoperable configsets](#interoperability).

Entity &amp; Config
-------------------

[](#entity--config)

The Config component simplifies access to object data. It provides a property-based user interface for retrieving and storing arbitrary data within application code. Data is organized into two primary object types: *Entity* and *Config*.

### Entity

[](#entity)

Entities represent simple data-object containers designed as a flexible foundation for domain model objects.
Examples: a single result from a repository or serve as the basis for each component of an MVC system.

- **Class**: [`Charcoal\Config\AbstractEntity`](src/Charcoal/Config/AbstractEntity.php)
- **Methods**: `keys`, `data`, `setData`, `has`, `get`, `set`
- **Interface**: [`Charcoal\Config\EntityInterface`](src/Charcoal/Config/EntityInterface.php)
    - `ArrayAccess`
    - `JsonSerializable`
    - `Serializable`

### Config

[](#config)

Configs are advanced *Entities* designed for runtime configuration values with support for loading files and storing hierarchical data.
Examples: application preferences, service options, and factory settings.

- **Class**: [`Charcoal\Config\AbstractConfig`](src/Charcoal/Config/AbstractConfig.php)
    - `IteratorAggregate`
    - `Psr\Container\ContainerInterface`
- **Methods**: `defaults`, `merge`, `addFile`
- **Interface**: [`Charcoal\Config\ConfigInterface`](src/Charcoal/Config/ConfigInterface.php)
    - `Charcoal\Config\EntityInterface`
    - `Charcoal\Config\FileAwareInterface`
    - `Charcoal\Config\SeparatorAwareInterface`
    - `Charcoal\Config\DelegatesAwareInterface`

Features
--------

[](#features)

- [Read data from INI, JSON, PHP, and YAML files](#file-loader)
- [Customizable separator for nested lookup](#key-separator-lookup)
- [Share configuration entries](#delegates-lookup)
- [Array accessible entities](#array-access)
- [Interoperable datasets](#interoperability)
- [Configurable objects](#configurable-objects)

### File Loader

[](#file-loader)

The *Config* container currently supports four file formats: INI, JSON, PHP, and YAML.

A configuration file can be imported into a Config object via the `addFile($path)` method, or by direct instantiation:

```
use Charcoal\Config\GenericConfig as Config;

$cfg = new Config('config.json');
$cfg->addFile('config.yml');
```

The file's extension will be used to determine how to import the file. The file will be parsed and, if its an array, will be merged into the container.

If you want to load a configuration file *without* adding its content to the Config, use `loadFile($path)` instead. The file will be parsed and returned regardless if its an array.

```
$data = $cfg->loadFile('config.php');
```

Check out the [documentation](docs/file-loader.md) and [examples](tests/Charcoal/Config/Fixture/pass) for more information.

### Key Separator Lookup

[](#key-separator-lookup)

It is possible to lookup, retrieve, assign, or merge values in multi-dimensional arrays using *key separators*.

In Config objects, the default separator is the period character (`.`). The token can be retrieved with the `separator()` method and customized using the `setSeparator()` method.

```
use Charcoal\Config\GenericConfig as Config;

$cfg = new Config();
$cfg->setSeparator('/');
$cfg->setData([
    'database' => [
        'params' => [
            'name' => 'mydb',
            'user' => 'myname',
            'pass' => 'secret',
        ]
    ]
]);

echo $cfg['database/params/name']; // "mydb"
```

Check out the [documentation](docs/separator-lookup.md) for more information.

### Delegates Lookup

[](#delegates-lookup)

Delegates allow several objects to share values and act as fallbacks when the current object cannot resolve a given data key.

In Config objects, *delegate objects* are regsitered to an internal stack. If a data key cannot be resolved, the Config iterates over each delegate in the stack and stops on the first match containing a value that is not `NULL`.

```
use Charcoal\Config\GenericConfig as Config;

$cfg = new Config([
    'driver' => null,
    'host'   => 'localhost',
]);
$delegate = new Config([
    'driver' => 'pdo_mysql',
    'host'   => 'example.com',
    'port'   => 11211,
]);

$cfg->addDelegate($delegate);

echo $cfg['driver']; // "pdo_mysql"
echo $cfg['host']; // "localhost"
echo $cfg['port']; // 11211
```

Check out the [documentation](docs/delegates-lookup.md) for more information.

### Array Access

[](#array-access)

The Entity object implements the `ArrayAccess` interface and therefore can be used with array style:

```
$cfg = new \Charcoal\Config\GenericConfig();

// Assigns a value to "foobar"
$cfg['foobar'] = 42;

// Returns 42
echo $cfg['foobar'];

// Returns TRUE
isset($cfg['foobar']);

// Returns FALSE
isset($cfg['xyzzy']);

// Invalidates the "foobar" key
unset($cfg['foobar']);
```

> 👉 A data key MUST be a string otherwise `InvalidArgumentException` is thrown.

### Interoperability

[](#interoperability)

The Config object implements [PSR-11](psr-11): `Psr\Container\ContainerInterface`.

This interface exposes two methods: `get()` and `has()`. These methods are implemented by the Entity object as aliases of `ArrayAccess::offsetGet()` and `ArrayAccess::offsetExists()`.

```
$config = new \Charcoal\Config\GenericConfig([
    'foobar' => 42
]);

// Returns 42
$config->get('foobar');

// Returns TRUE
$config->has('foobar');

// Returns FALSE
$config->has('xyzzy');
```

> 👉 A call to the `get()` method with a non-existing key DOES NOT throw an exception.

### Configurable Objects

[](#configurable-objects)

Also provided in this package is a *Configurable* mixin:

- `Charcoal\Config\ConfigrableInterface`
- `Charcoal\Config\ConfigurableTrait`

Configurable objects (which could have been called "*Config Aware*") can have an associated Config object that can help define various properties, states, or other.

The Config object can be assigned with `setConfig()` and retrieved with `config()`.

An added benefit of `ConfigurableTrait` is the `createConfig($data)` method which is used to create a Config object if one is not assigned. This method can be overridden in sub-classes to customize the instance returned and whatever initial state might be needed.

Check out the [documentation](docs/configurable-objects.md) for examples and more information.

Development
-----------

[](#development)

To install the development environment:

```
$ composer install
```

To run the scripts (phplint, phpcs, and phpunit):

```
$ composer test
```

### API Documentation

[](#api-documentation)

- The auto-generated `phpDocumentor` API documentation is available at:

- The auto-generated `apigen` API documentation is available at:
    [https://codedoc.pub/locomotivemtl/charcoal-config/master/](https://codedoc.pub/locomotivemtl/charcoal-config/master/index.html)

### Development Dependencies

[](#development-dependencies)

- [php-coveralls/php-coveralls](https://packagist.org/packages/php-coveralls/php-coveralls)
- [phpunit/phpunit](https://packagist.org/packages/phpunit/phpunit)
- [squizlabs/php\_codesniffer](https://packagist.org/packages/squizlabs/php_codesniffer)

### Coding Style

[](#coding-style)

The charcoal-config module follows the Charcoal coding-style:

- [*PSR-1*](https://www.php-fig.org/psr/psr-1/)
- [*PSR-2*](https://www.php-fig.org/psr/psr-2/)
- [*PSR-4*](https://www.php-fig.org/psr/psr-4/), autoloading is therefore provided by *Composer*.
- [*phpDocumentor*](http://phpdoc.org/) comments.
- [phpcs.xml.dist](phpcs.xml.dist) and [.editorconfig](.editorconfig) for coding standards.

> Coding style validation / enforcement can be performed with `composer phpcs`. An auto-fixer is also available with `composer phpcbf`.

> This module should also throw no error when running `phpstan analyse -l7 src/` 👍.

Credits
-------

[](#credits)

- [Mathieu Ducharme](https://github.com/mducharme)
- [Locomotive](https://locomotive.ca/)

License
-------

[](#license)

Charcoal is licensed under the MIT license. See [LICENSE](LICENSE) for details.

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community23

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 55.8% 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 ~109 days

Recently: every ~251 days

Total

15

Last Release

2433d ago

PHP version history (3 changes)v0.1.0PHP &gt;=5.5.0

0.7PHP &gt;=5.6.0

0.9.0PHP &gt;=5.6.0 || &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfb071c0ff7ce9500c528a003a2c53124248debc3e5bf367c17f89f5e6136125?d=identicon)[mducharme](/maintainers/mducharme)

---

Top Contributors

[![mducharme](https://avatars.githubusercontent.com/u/12157?v=4)](https://github.com/mducharme "mducharme (67 commits)")[![mcaskill](https://avatars.githubusercontent.com/u/29353?v=4)](https://github.com/mcaskill "mcaskill (52 commits)")[![BeneRoch](https://avatars.githubusercontent.com/u/3017380?v=4)](https://github.com/BeneRoch "BeneRoch (1 commits)")

---

Tags

charcoalconfigconfigurationphpjsonconfigurationxmlyamlymlcharcoalini

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/locomotivemtl-charcoal-config/health.svg)

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

###  Alternatives

[hassankhan/config

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

1.0k13.8M186](/packages/hassankhan-config)[m1/vars

Vars is a simple to use and easily extendable configuration loader with in built loaders for ini, json, PHP, toml, XML and yaml/yml file types. It also comes with in built support for Silex and more frameworks to come soon.

69124.3k1](/packages/m1-vars)[symfony/json-streamer

Provides powerful methods to read/write data structures from/into JSON streams.

18641.1k32](/packages/symfony-json-streamer)[thewunder/conphigure

Framework Agnostic Configuration Library

3120.6k](/packages/thewunder-conphigure)[davidepastore/slim-config

A slim middleware to read configuration from different files based on hassankhan/config

359.2k1](/packages/davidepastore-slim-config)

PHPackages © 2026

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