PHPackages                             brightnucleus/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. brightnucleus/config

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

brightnucleus/config
====================

Minimal, reusable Config component.

v0.5.1(9mo ago)30152.4k↓30.6%3[5 issues](https://github.com/brightnucleus/config/issues)14MITPHPPHP &gt;=8.0CI failing

Since Jan 29Pushed 9mo ago3 watchersCompare

[ Source](https://github.com/brightnucleus/config)[ Packagist](https://packagist.org/packages/brightnucleus/config)[ RSS](/packages/brightnucleus-config/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (8)Versions (40)Used By (14)

Bright Nucleus Config Component
===============================

[](#bright-nucleus-config-component)

[![PHP Version Require](https://camo.githubusercontent.com/18766fd94ad5ab41c37d54701c416cdf4e049dcae84f229bb063d8efa8f106df/687474703a2f2f706f7365722e707567782e6f72672f6272696768746e75636c6575732f636f6e6669672f726571756972652f706870)](https://packagist.org/packages/brightnucleus/config)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/9353345304687c79444b12af3fa3a864494d18a770aac7c1ff0cd8aa9033ea57/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6272696768746e75636c6575732f636f6e6669672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/brightnucleus/config/?branch=main)[![Code Coverage](https://camo.githubusercontent.com/ad06ddf688d84e5a67f6837160a61fc8645a70a7e35319a9776ff97dd341f5d9/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6272696768746e75636c6575732f636f6e6669672f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/brightnucleus/config?branch=main)[![Build Status](https://github.com/brightnucleus/config/actions/workflows/testing.yml/badge.svg?branch=main)](https://github.com/brightnucleus/config/actions/workflows/testing.yml)[![Codacy Badge](https://camo.githubusercontent.com/df37f3de3ce48489dd6d2b00b3974347afdd340d874deab036aa2cc9058ae998/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3761633662366236363466313465626462363630643862303234326463336637)](https://app.codacy.com/gh/brightnucleus/config/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)[![Code Climate](https://camo.githubusercontent.com/575180c7a7fddc275f683cfdd473945766352667ee1fd04e917e61bd5d02dd78/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6272696768746e75636c6575732f636f6e6669672f6261646765732f6770612e737667)](https://codeclimate.com/github/brightnucleus/config)

[![Latest Stable Version](https://camo.githubusercontent.com/28ee22500388b6f5add13b4cdf7558de2f4a650344f28f1e4574b05451c790dd/68747470733a2f2f706f7365722e707567782e6f72672f6272696768746e75636c6575732f636f6e6669672f762f737461626c65)](https://packagist.org/packages/brightnucleus/config)[![Total Downloads](https://camo.githubusercontent.com/3bcc16b8fac6a24c36f223730fc3e9b435214350ff5229c68c451bfc1d2ce07b/68747470733a2f2f706f7365722e707567782e6f72672f6272696768746e75636c6575732f636f6e6669672f646f776e6c6f616473)](https://packagist.org/packages/brightnucleus/config)[![Latest Unstable Version](https://camo.githubusercontent.com/12085d71479595aa1849fcdd4ea5e151239040953ff57a99a4aa1dbcf8afeb4e/68747470733a2f2f706f7365722e707567782e6f72672f6272696768746e75636c6575732f636f6e6669672f762f756e737461626c65)](https://packagist.org/packages/brightnucleus/config)[![License](https://camo.githubusercontent.com/77d64fdf530ed3da88df7b21aa454afd02bf99cfee5b4527c5b43c11153fe461/68747470733a2f2f706f7365722e707567782e6f72672f6272696768746e75636c6575732f636f6e6669672f6c6963656e7365)](https://packagist.org/packages/brightnucleus/config)

This is a very lean Config component to help you write reusable code. It only offers basic functionality and is meant to be used in libraries and small projects. If you need a Config component for more complex projects, you should take a look at the [Symfony Config Component](http://symfony.com/doc/current/components/config/index.html).

Table Of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Basic Usage](#basic-usage)
    - [Working With Config Data](#working-with-config-data)
        - [Checking The Existence Of A Key](#checking-the-existence-of-a-key)
        - [Getting The Value Of A Key](#getting-the-value-of-a-key)
        - [Nested Keys](#nested-keys)
    - [Example - Configuration File](#example---configuration-file)
    - [Example - Configurable Class](#example---configurable-class)
    - [Example - Getting The Config Into The Class](#example---getting-the-config-into-the-class)
    - [Example - Class That Loads Default Config Unless Config Was Injected](#example---class-that-loads-default-config-unless-config-was-injected)
    - [Example - Merging Several Configs Into One](#example---merging-several-configs-into-one)
- [Config Formats](#config-formats)
- [Advanced Usage](#advanced-usage)
    - [Configuration Schema](#configuration-schema)
    - [Configuration Validation](#configuration-validation)
    - [Custom Implementations](#custom-implementations)
- [Contributing](#contributing)

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

[](#installation)

The best way to use this component is through Composer:

```
composer require brightnucleus/config
```

Basic Usage
-----------

[](#basic-usage)

A class that wants to be configurable should accept a `ConfigInterface` in its constructor, so that the Config can be injected. The surrounding code then should inject an instance of an object (for example the generic `Config` that is provided with this component). This way, the class that accepts the Config can be written in a 100% reusable way, while all project-specific stuff will be injected through the Config.

### Working With Config Data

[](#working-with-config-data)

#### Checking The Existence Of A Key

[](#checking-the-existence-of-a-key)

To check whether the configuration has a certain key, you can use the `ConfigInterface::hasKey($key)` method, or, if you are using the `ConfigTrait` in your class, you can use the convenience method `$this->hasConfigKey($key)`.

#### Getting The Value Of A Key

[](#getting-the-value-of-a-key)

To get the configuration value of a certain key, you can use the `ConfigInterface::getKey($key)` method, or, if you are using the `ConfigTrait` in your class, you can use the convenience method `$this->getConfigKey($key)`.

If you use closures in your Config file, you can also use the convenience function `$this->getConfigCallable( $key, array $args )` provided by the `ConfigTrait`, which will immediately execute the closure by passing it the provided arguments, and return the resultant value instead.

#### Nested Keys

[](#nested-keys)

If your keys are nested, you can provide multiple levels of keys in one request. So, whenever you need to provide a key and want to use a nested one, you can either provide a comma-separated list of keys ( `$this->getConfigKey( 'level1', 'level2', 'level3' );` ) or a string that contains the list of keys separated by a delimiter ( `$this->getConfigKey( 'level1/level2/level3' );` ).

You can freely mix-and-match these two approaches as you like.

The default delimiters are: `/`, `\` and `.`. You can choose different delimiters by passing an array of delimiters as a fourth argument to the `Config`s constructor.

### Example - Configuration File

[](#example---configuration-file)

The snippet below shows the basic structure of a config file.

```
