PHPackages                             tobento/service-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. tobento/service-config

ActiveLibrary

tobento/service-config
======================

Loading and managing configuration data for PHP applications.

2.0(5mo ago)02733MITPHPPHP &gt;=8.4

Since Oct 31Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-config)[ Packagist](https://packagist.org/packages/tobento/service-config)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-config/feed)WikiDiscussions 2.x Synced today

READMEChangelog (6)Dependencies (5)Versions (8)Used By (3)

Config Service
==============

[](#config-service)

The Config Service provides a way for managing configuration data in an application.

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

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
    - [Simple Example](#simple-example)
- [Documentation](#documentation)
    - [Create Config](#create-config)
    - [Set Data](#set-data)
    - [Load Data](#load-data)
        - [PHP Loader](#php-loader)
        - [JSON Loader](#json-loader)
    - [Get Data](#get-data)
    - [Has Data](#has-data)
    - [Translations](#translations)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the config service running this command.

```
composer require tobento/service-config

```

Requirements
------------

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design
- Translation support

Simple Example
--------------

[](#simple-example)

Here is a simple example of how to use the config service:

```
use Tobento\Service\Config\Config;
use Tobento\Service\Config\PhpLoader;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Dir\Dirs;

// create config:
$config = new Config(new Translations());

// adding a loader:
$dirs = new Dirs()->dir('home/private/config');

$config->addLoader(new PhpLoader($dirs));

// loading data from a file:
$config->load(file: 'app.php', key: 'app');

// or set data directly:
$config->set('database', ['name' => 'db_name']);

// Get config data:
$appName = $config->get('app.name');

$dbName = $config->get('database.name');
```

Documentation
=============

[](#documentation)

Create Config
-------------

[](#create-config)

```
use Tobento\Service\Config\Config;
use Tobento\Service\Config\ConfigInterface;
use Tobento\Service\Collection\Translations;

$trans = new Translations();

$config = new Config($trans);

var_dump($config instanceof ConfigInterface);
// bool(true)
```

Set Data
--------

[](#set-data)

```
use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

$config->set('database', [
    'host' => 'localhost',
    'name' => 'db_name',
]);

$config->set('sitename', 'A sitename');
```

**By dot notation**

You might set or add new data by using dot notation:

```
use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

$config->set('database', [
    'host' => 'localhost',
    'name' => 'db_name',
]);

// Add new data:
$config->set('database.driver', 'mysql');

// Set data (overwrites existing):
$config->set('database.name', 'db_name');
```

Load Data
---------

[](#load-data)

You might use loaders to load data from files.

### PHP Loader

[](#php-loader)

The php loader, loads data from php files returning an array of data.

```
use Tobento\Service\Config\Config;
use Tobento\Service\Config\PhpLoader;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Dir\Dirs;

// create config:
$config = new Config(new Translations());

// add loader:
$dirs = new Dirs()->dir('home/private/config');

$config->addLoader(new PhpLoader($dirs));

// loading data:
$config->load(file: 'database.php', key: 'database');
```

**database.php config file**

```
return [
    'host' => 'localhost',
    'name' => 'db_name',
];
```

**Only loading data**

You may omit the **key:** to load data without storing.

```
use Tobento\Service\Config\Config;
use Tobento\Service\Config\PhpLoader;
use Tobento\Service\Config\DataInterface;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Dir\Dirs;

// create config:
$config = new Config(new Translations());

// add loader:
$dirs = new Dirs()->dir('home/private/config');

$config->addLoader(new PhpLoader($dirs));

// just loading data:
$data = $config->load(file: 'database.php');

var_dump($data);
// array(3) { ... }

// or by the data method:
$data = $config->data(file: 'database.php');

var_dump($data instanceof DataInterface);
// bool(true)
```

### JSON Loader

[](#json-loader)

The json loader, loads data from json files.

```
use Tobento\Service\Config\Config;
use Tobento\Service\Config\JsonLoader;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Dir\Dirs;

// create config:
$config = new Config(new Translations());

// add loader:
$dirs = new Dirs()->dir('home/private/config');

$config->addLoader(new JsonLoader($dirs));

// loading data:
$config->load(file: 'database.json', key: 'database');
```

Get Data
--------

[](#get-data)

```
use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

$config->set('sitename', 'A sitename');
$config->set('app.name', 'An app name');

$sitename = $config->get('sitename');

// using dot notation:
$appname = $config->get('app.name');
```

**Default Value**

You might set a default value to return if the data requested doesn't exist, otherwise a ConfigNotFoundException is thrown.

```
use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Config\ConfigNotFoundException;

$config = new Config(new Translations());

$sitename = $config->get('sitename', 'Default Sitename');

// would throw ConfigNotFoundException:
$sitename = $config->get('sitename');
```

**A note on default value and data type**

You can use the default value to ensure the right data type is returned.

```
use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

$sites = $config->get('sites', 'Sites');

// returns the default value:
$sites = $config->get('sites', ['first', 'second']);

// returns the value set as the same data type:
$sites = $config->get('sites', 'Default Sites');
```

Has Data
--------

[](#has-data)

```
use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

$config->set('sitename', 'A sitename');
$config->set('app.name', 'An app name');
$config->set('app.name', 'App Name', 'de');

var_dump($config->has('sitename'));
// bool(true)

// using dot notation:
var_dump($config->has('app.name'));
// bool(true)

// translated:
var_dump($config->has('app.name', 'de'));
// bool(true)
```

Translations
------------

[](#translations)

You might want to load, set and get translated config data.

**Create Config**

You might configure the translations based your needs. For more info visit [Collection Service - Translations](https://github.com/tobento-ch/service-collection#translations)

```
use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$trans = new Translations();
$trans->setLocaleFallbacks(['it' => 'en']);
$trans->setLocaleMapping(['en-Us' => 'en']);

$config = new Config($trans);
```

**Set Data**

```
use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

// default locale:
$config->set('sitename', 'Sitename');

// de-CH locale:
$config->set('sitename', 'Seitenname', 'de-CH');
```

**Load Data**

```
use Tobento\Service\Config\Config;
use Tobento\Service\Config\PhpLoader;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Dir\Dirs;

// create config:
$config = new Config(new Translations());

// add loader:
$dirs = new Dirs()->dir('home/private/config');

$config->addLoader(new PhpLoader($dirs));

// loading default data:
$config->load(file: 'site.php', key: 'site');

// loading de locale data:
$config->load(
    file: 'de/site.php',
    key: 'site',
    locale: 'de'
);
```

**Get Data**

```
use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());
$config->set('sitename', 'Sitename');
$config->set('sitename', 'Seitenname', 'de');

var_dump($config->get('sitename'));
// string(8) "Sitename"

var_dump($config->get(key: 'sitename', locale: 'de'));
// string(10) "Seitenname"

// returns default as locale does not exist
// and no other fallback is set:
var_dump($config->get(key: 'sitename', locale: 'it'));
// string(8) "Sitename"

// returns default value set as locale does not exist:
var_dump($config->get(key: 'sitename', default: 'Site It', locale: 'it'));
// string(7) "Site It"
```

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance69

Regular maintenance activity

Popularity15

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~203 days

Recently: every ~213 days

Total

8

Last Release

179d ago

Major Versions

1.x-dev → 2.02025-09-24

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (21 commits)")

---

Tags

configurationSettingspackagetobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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