PHPackages                             jasny/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. [Database &amp; ORM](/categories/database)
4. /
5. jasny/config

Abandoned → [php-di/php-di](/?search=php-di%2Fphp-di)ArchivedLibrary[Database &amp; ORM](/categories/database)

jasny/config
============

Configure your application

v2.0.1(6y ago)2122.3k↓33.3%4MITPHPPHP &gt;=7.1

Since Nov 5Pushed 6y ago4 watchersCompare

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

READMEChangelog (1)Dependencies (9)Versions (17)Used By (0)

Jasny's Config class
====================

[](#jasnys-config-class)

[![Build Status](https://camo.githubusercontent.com/113686f8102f37f1a0e96586ea94e44a235387b04cb74a3b97849bfa2cf0624d/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6a61736e792f636f6e6669672e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/jasny/config)[![Code Coverage](https://camo.githubusercontent.com/bc3624caf7e0eb43c4f64ac0a42d370a1f15b289d4508de0289e69ba2d7b4845/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f636f6e6669672f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/config/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/0b6340aaefe1d6c428d2f1520954c27fd9193a13aa36eda0717a5ffb80ac09d7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f636f6e6669672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/config/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/8782b45c146e8efddd8e50ac7ff02610159a4d8e9781ad1048e701ade46a6e77/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f39643965653266652d313632322d343838332d623436652d6161323162313461333933312f6d696e692e706e67)](https://insight.sensiolabs.com/projects/9d9ee2fe-1622-4883-b46e-aa21b14a3931)

Configure your application. Implements [PSR-11](https://www.php-fig.org/psr/psr-11/) to be used as container.

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

[](#installation)

Jasny Config is registred at packagist as [jasny/config](https://packagist.org/packages/jasny/config) and can be easily installed using [composer](http://getcomposer.org/).

```
composer require jasny/config

```

Usage
-----

[](#usage)

```
use Jasny\Config;

$config = (new Config())
  ->load('settings.yml')
  ->load('settings.dev.yml', ['optional' => true]);
```

Loaders
-------

[](#loaders)

- Dir
- Ini
- Json
- Yaml
    - Symfony
    - Yaml extension
- Env
- DynamoDB

The `DelegateLoader` can pick a loader based on class name or file extension.

For every loader; a `Jasny\Config\LoadException` is thrown if the loader can't load the settings.

### Dir

[](#dir)

`DirLoader` loader will traverse through every file in a directory. It uses the filename (without extension) as key name.

optiontypedefaultrecursiveboolfalseTraverse recursively through subdirectoriesoptionalboolfalseReturn empty config is directory doesn't existbase\_pathstringgetcwd()Basepath for relative paths### Ini

[](#ini)

`IniLoader` parses an INI file using [`parse_ini_file`](http://php.net/parse_ini_file).

optiontypedefaultprocess\_sectionsboolfalseGroup settings by section namemodeenumINI\_SCANNER\_NORMALMode of parsing options (off = false, etc)optionalboolfalseReturn empty config is file doesn't existbase\_pathstringgetcwd()Basepath for relative paths### Json

[](#json)

`JsonLoader` parses a JSON file using [`json_decode`](http://php.net/json_decode).

optiontypedefaultoptionalboolfalseReturn empty config is file doesn't existbase\_pathstringgetcwd()Basepath for relative paths### Yaml

[](#yaml)

`YamlLoader` parses a YAML file using [`yaml_parse`](http://php.net/yaml_parse).

optiontypedefaultnumint0Document to extract from stream (0 is first doc)callbacksarray\[\]Content handlers for YAML nodesoptionalboolfalseReturn empty config is file doesn't existbase\_pathstringgetcwd()Basepath for relative pathsThe `YamlLoader` is used by default if the `yaml` extension is loaded.

#### Symfony\\Yaml

[](#symfonyyaml)

`YamlSymfonyLoader` is an alternative loader for YAML, using the [Symfony Yaml component](https://symfony.com/doc/current/components/yaml.html).

optiontypedefaultoptionalboolfalseReturn empty config is file doesn't existbase\_pathstringgetcwd()Basepath for relative pathsWhen constructing the loader a `Symfony\Component\Yaml\Parser` object may be passed.

```
use Symfony\Component\Yaml;

$parser = new class() extends Yaml\Parser() {
    public function parse($value, $options = 0) {
        return parent::parseFile($value, $options | Yaml\Yaml::PARSE_CONSTANT);
    }
}

$yamlLoader = new YamlSymfonyLoader($options, $parser);
```

### Env

[](#env)

`EnvLoader` maps environment variables to settings. In the mapping the settings key supports dot notation.

optiontypedefaultmaparray(required)key/value pairs from env name to setting key**Example**

```
$map = [
    "APP_SECRET" => "secret",
    "APP_DATABASE_PASSWORD" => "db.password"
];

$config->load('env', ['map' => $map]);
```

### DynamoDB

[](#dynamodb)

`DynamoDBLoader` allows loading settings from an AWS DynamoDB database. It expects all settings to be in a single item. It will *not* do a table scan. By default the whole item is taken as settings. Alternatively, use the `settings_field`option to specify a [Map](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.Document.Map) field that holds the settings.

optiontypedefaulttablestring(required)DynamoDB table namekey\_fieldstring'key'Indexed field (typically primary index)key\_valuestring(required)Value to select item onsettings\_fieldstringnullwithin the item that holds the settings**Example**

```
$dynamodb = Aws\DynamoDb\DynamoDbClient::factory([
    'region' => 'eu-west-1',
    'version' => '2012-08-10'
]);
 *
$config = new Jasny\Config();

$config->load($dynamodb, [
   'table' => 'config',
   'key_field' => 'key',
   'key_value' => 'myapp'
]);
```

### DelegateLoader

[](#delegateloader)

The `DelegateLoader` is a map with loaders, than can automatically pick a loader based on the file extension or class name.

By default it holds all the loaders of this library. You may pass an array of loaders as dependency injection.

```
use Jasny\Config\Loader;

$loader = new Loader\DelegateLoader([
    'yaml' => new Loader\YamlLoader(['base_path' => __DIR__ . '/config']),
    'json' => new Loader\JsonLoader(['base_path' => __DIR__])
]);

$config = new Config([], $loader);
$config->load('composer.json');
$config->load('settings.yml');
```

Config
------

[](#config)

The `Config` object is a dynamic object; settings are added as public properties. It extends the `stdClass` object.

Upon construction you can pass settings as associative array or `stdClass` object.

```
$config = new Jasny\Config([
    'foo' => 'bar',
    'db' => [
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'god'
    ]
]);
```

Optionally you can pass a loader. If your config is only in JSON files, consider passing the `JsonLoader`. This saves the creation of the DelegateLoader and loaders for all other file types.

### load()

[](#load)

Load new setting from file or other data source and add it to the config.

`load(string|object $source, array $options = [])`

*This is a fluent interface, so the method returns `$this`.*

**Example**

```
$config = (new Config())
    ->load('composer.json')
    ->load('config/settings.yml')
    ->load($dynamoDB, ['table' => 'config', 'key_value' => 'myapp']);
```

### merge()

[](#merge)

Merge one or more `Config` (or `stdClass`) objects into the config.

`merge(stdClass $settings, ...)`

*This is a fluent interface, so the method returns `$this`.*

### get()

[](#get)

The `Config` object implements the [PSR-11 `ContainerInterface`](https://www.php-fig.org/psr/psr-11/#31-psrcontainercontainerinterface). The `get` method finds a setting in the config by key. The dot notation may be used to get child properties.

`mixed get(string $key)`

If the setting doesn't exist, a `Jasny\Config\Exception\NotFoundException` is thrown.

**Example**

```
$config->get('foo'); // bar
$config->get('db.host'); // localhost

// throws a NotFoundException
$config->get('something_random');
```

### has()

[](#has)

The `has` method is part of the PSR-11 `ContainerInterface`. It checks if a setting exists. As with `get()`, the key supports the dot notation.

`bool has(string $key)`

### saveAsScript() / loadFromScript()

[](#saveasscript--loadfromscript)

The `saveAsScript()` method create parsable string representation of a variable using [`var_export`](https://php.net/var_export) and store it as a PHP script.

As described in the [500x faster caching](https://medium.com/@dylanwenzlau/500x-faster-caching-than-redis-memcache-apc-in-php-hhvm-dcd26e8447ad) article, storing the parsed configuration as a PHP script can make loading it much faster.

The `loadFromScript()` method can be used to load the settings. It's recommended to use this function rather than just `file_exists` and `include`, as checking the file exists would always hit the file system. This function checks the opcode cache first.

```
$config = Config::loadFromScript('tmp/settings.php');

if (!isset($config)) {
    $config = (new Config())
        ->load('composer.json')
        ->load('config/settings.yml');

    $config->saveAsScript('tmp/settings.php');
}
```

###  Health Score

37

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 88.9% 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 ~202 days

Recently: every ~373 days

Total

14

Last Release

2305d ago

Major Versions

v1.3.2 → v2.x-dev2018-07-26

PHP version history (3 changes)v1.0.0PHP &gt;=5.3.0

v1.1.2PHP &gt;=5.4.0

v2.x-devPHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (64 commits)")[![svenstm](https://avatars.githubusercontent.com/u/1632578?v=4)](https://github.com/svenstm "svenstm (6 commits)")[![inri13666](https://avatars.githubusercontent.com/u/1381253?v=4)](https://github.com/inri13666 "inri13666 (1 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

configurationphppsr-11jsonconfigurationconfigyamlmysqldbini

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[hassankhan/config

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

97513.5M169](/packages/hassankhan-config)[arcanedev/laravel-settings

This package allows you to persists configs/settings for Laravel projects.

74131.4k6](/packages/arcanedev-laravel-settings)[aplus/config

Aplus Framework Config Library

161.6M3](/packages/aplus-config)[thewunder/conphigure

Framework Agnostic Configuration Library

3115.9k](/packages/thewunder-conphigure)[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.2k1](/packages/m1-vars)[davidepastore/slim-config

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

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

PHPackages © 2026

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