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

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

sirix/sirix-config
==================

Utils to load, parse and work with configuration on Mezzio projects

3.0.0(1mo ago)01.7k—4.9%2MITPHPPHP ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0CI passing

Since Jan 28Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/sirix777/sirix-config)[ Packagist](https://packagist.org/packages/sirix/sirix-config)[ Docs](https://github.com/sirix777)[ Fund](https://buymeacoffee.com/sirix)[ GitHub Sponsors](https://github.com/sirix777)[ RSS](/packages/sirix-sirix-config/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (5)Dependencies (18)Versions (6)Used By (2)

Sirix config
============

[](#sirix-config)

[![Latest Stable Version](https://camo.githubusercontent.com/2117f8b1f5035d1b2da240315ace101abf3fb27e3742c133d7a123206138938d/687474703a2f2f706f7365722e707567782e6f72672f73697269782f73697269782d636f6e6669672f76)](https://packagist.org/packages/sirix/sirix-config) [![Total Downloads](https://camo.githubusercontent.com/e789cd4b83a3e6da36a49c82c033313a604f4d19ba63170e2b32523aed9c3ab9/687474703a2f2f706f7365722e707567782e6f72672f73697269782f73697269782d636f6e6669672f646f776e6c6f616473)](https://packagist.org/packages/sirix/sirix-config) [![Latest Unstable Version](https://camo.githubusercontent.com/a1871ae2275551c7f653f5fb7f26977ed8ec91e59f79b1bb6dafb92ec089237f/687474703a2f2f706f7365722e707567782e6f72672f73697269782f73697269782d636f6e6669672f762f756e737461626c65)](https://packagist.org/packages/sirix/sirix-config) [![License](https://camo.githubusercontent.com/0b28e3909442388c3fd83ee8230a81400a64eb8a60e2dbc5f092052f5f991ab8/687474703a2f2f706f7365722e707567782e6f72672f73697269782f73697269782d636f6e6669672f6c6963656e7365)](https://packagist.org/packages/sirix/sirix-config) [![PHP Version Require](https://camo.githubusercontent.com/fab277b48ee9e98a1210d6c036c1c57d5062f5f0ce906043adc898a9e50dcf20/687474703a2f2f706f7365722e707567782e6f72672f73697269782f73697269782d636f6e6669672f726571756972652f706870)](https://packagist.org/packages/sirix/sirix-config)

Utils to load, parse and work with configuration on Mezzio projects.

This library is **based on** the [`shlinkio/shlink-config`](https://github.com/shlinkio/shlink-config) project, with some modifications and extensions for our specific requirements.

---

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

[](#installation)

```
composer install sirix/sirix-config
```

> This library is also a mezzio module which provides its own `ConfigProvider`. Add it to your configuration to get everything automatically set up.

Included utils
--------------

[](#included-utils)

### Global functions

[](#global-functions)

All functions are available globally (no `use function` needed):

- `env(string $key, mixed $default = null, ?string $type = null): mixed` — Read an environment variable with optional type casting.
- `parseEnvVar(string $value): string|int|bool|null` — Parse a raw env value (handles `true`, `false`, `null`, `empty`, numeric).
- `castEnvValue(string $value, string $type): mixed` — Cast a raw env string to the specified type.
- `formatEnvVarValue(mixed $value): string` — Format a value for use as an env var string.
- `formatEnvVarValueOrNull(mixed $value): string|null` — Same as above, returns `null` for non-scalar/non-array values.
- `loadConfigFromGlob(string $globPattern): array` — Load and merge all PHP config files matching a glob pattern.
- `loadEnvVarsFromConfig(string $configPath, ?array $allowedEnvVars = null): void` — Load config and set env vars from it.
- `putNotYetDefinedEnv(string $key, mixed $value): void` — Set an env var only if it is not already defined.

#### `env()` function

[](#env-function)

```
// Without type (auto-detect: bool, int, string, null)
env('DEBUG');              // true
env('PORT');               // 8080
env('NAME', 'default');    // 'default' if NAME is not set

// With explicit type casting
env('PORT', '8080', 'int');       // (int) 8080
env('RATE', '3.14', 'float');     // (float) 3.14
env('NAME', 'foo', 'string');     // 'foo'
env('DEBUG', 'true', 'bool');     // true
env('HOSTS', 'a,b,c', 'array');   // ['a', 'b', 'c']
```

Supported types: `int`/`integer`, `float`/`double`, `string`, `bool`/`boolean`, `array` (comma-separated).

### Config providers and factories

[](#config-providers-and-factories)

- `EnvVarLoaderProvider`: A config provider which loads the entries of the loaded config into env vars and always returns empty. Designed to be the first config provider in the pipeline.
- `DottedAccessConfigAbstractFactory`: An abstract factory that lets any config param to be fetched as a service by using the `config.foo.bar` notation.
- `ValinorConfigFactory`: A PSR-11 factory that lets you map arbitrary objects from arrays, using [cuyz/valinor](https://github.com/CuyZ/Valinor).

    In order to use it, you have to register the object to map as a service, and the ValinorConfigFactory with static access using the service that returns the raw array with the data as the static method name:

    ```
