Download the PHP package timdev/typed-config without Composer
On this page you can find all versions of the php package timdev/typed-config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download timdev/typed-config
More information about timdev/typed-config
Files in timdev/typed-config
Package typed-config
Short Description Strongly-typed configuration for PHP applications
License MIT
Informations about the package typed-config
timdev/typed-config
What's This?
A small library to provide strictly typed access to data in an array, intended for use with a nested array of application configuration data. Nested values are dereferenced with dotted strings.
Usage
Install
Install this package with composer:
Usage
In a nutshell:
Assumptions and Design
In order to use this library effectively, it's important to understand its design and the assumptions that inform that design.
Assumption: All valid configuration values are defined
As demonstrated in example above, client code attempting to access an undefined value will throw. Our opinion is that a library or application should ship a complete default configuration. This is always a win for users. This design means that users are alerted early when they try to access an invalid key due to a typo or transcription error.
If you think you need an optional configuration parameter, you should instead
provide a default such as null
or perhaps []
.
Design: Relies on PHP's assert()
assert is a language construct with a very useful mechanism: they are
optimized out in production. This library uses assert()
to type-check your
configuration values before they're returned from the various accessor-methods.
If you're unfamiliar with the configuration values that affect how assert
works, you should review the documentation and your environment's configuration
before using this library. The short version is that you'll want to ensure that
assert.exception = 1
and zend.assertion = 1
in your development and testing
environments.
Because this library depends on assert(), the type checks are optimized away in
production environments (where zend.assertion = 0
). This reduces some overhead
but means that configuration access that would throw an AssertionError
in
development will instead:
- Throw a
TypeError
, or - Not throw anything, in the case of a hash/list mismatch.
Motivation
We developed this while building an application with mezzio, where configuration data are generally pulled from an array. Arrays are nice, but often cause you to have to:
- Code defensively whenever you pull some value from your
$config
array. - Add assertions or comments to let your static analyzer of choice figure out what is happening.
This library is an attempt to improve that situation.
Alternatives
There are other libraries that approach the problem from different perspectives.
selective-php/config takes a similar approach in that it provides strictly typed methods to fetch (possibly nullable) values. Unlike this library, it enforces types by casting values as it returns them. We take a more strict posture, and consider mismatched types to be an error in the software. Its nullable-typed methods return null if a config key is not defined, while we consider that to be an error as well.
setbased/typed-config is also similar. Like the previously discussed library, it will attempt to cast whatever it finds in the config data to the required type, and is more permissive about undefined config keys than this library is.
league/config takes an even more strict
approach by helping you define a strict schema for your configuration data. We
really like that approach (at least for some contexts), but are somewhat
bewildered that it provides a single get(string $key): mixed
accessor.
To Do
- [ ] Do some benchmarks and determine if our usage of assert is actually providing any tangible benefit.
All versions of typed-config with dependencies
symfony/polyfill-php80 Version ^1.23
symfony/polyfill-php81 Version ^1.23