Download the PHP package simoneddy/config without Composer
On this page you can find all versions of the php package simoneddy/config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download simoneddy/config
More information about simoneddy/config
Files in simoneddy/config
Package config
Short Description basic read only config class with file/dir loader and dot notation
License MIT
Informations about the package config
Config
Basic Config class with file loader.
Changes in 2.0.0
- The ConfigLoader class has been renamed PathLoader
- Using the PathLoader->load method no longer returns a Config object. Instead it now returns an array of values loaded from the filesystem.
- Construction of Config objects removed from PathLoader.
- Added a ConfigFactory class to handle construction when using static factory methods.
- Added a MutableConfig class that extends Config with methods for setting and unsetting values.
Changes in 1.1
- Can now load CSV files
Installation
This library can be installed with composer:
Dependencies
- PHP >=8.0
- symfony/yaml (optional for yaml support)
Usage
A Config object can be created by either passing values to the class constructor or by using one of the various factory methods:
Internally, this factory method uses the PathLoader class to scan the provided path and load all supported filetypes into an array. This array is given to the ConfigFactory which constructs the neccessary config class.
Unsupported files will be ignored.
If the path is a directory the PathLoader will attempt to load values from every supported file the directory contains. This also applies to any subdirectories and their contents.
Files are loaded as key-value pairs, using the base filename (minus extension) as the key, while the file contents is the value.
For example:
PLEASE NOTE YAML files will be ignored unless the symfony/yaml package is also installed.
You can also use the Config class constructor and provide your config values directly as the sole argument:
Once the config object is created it can be accessed like an array, with dot notation to specify nested keys:
Internally, array access methods use get($key)
and has($key)
methods. These methods can also be used directly:
The Eddy\Config\Config
object is read only and values cannot be modified after the object is instantiated. For mutability use the Eddy\Confi\MutableConfig
class, which adds set and remove methods, as well as properly implementing \ArrayAccess::offsetUnset
and \ArrayAccess::offsetSet
:
Of course, all setting and unsetting can utilise dot notation for nested values:
By design, the set method (and subsequently offsetSet) will merge nested key => value pairs where neccessary. If you want to completely overwrite the parent key you can use the overwrite method:
The config object contains a toArray
method that returns all config values as an array. The returned array maintains config keys and structure.
Seriali(s/z)ing
The Config class implements both PHPs JsonSerializable interfaces and serializing magic methods to PHP 8 standards. No deprecation warnings here!
Supported Filetypes
The ConfigLoader supports the following filetypes:
- PHP
- JSON
- CSV (parses as an array with the filename as key)
- YAML (requires symfony/yaml as a peer dependency)
The given path can also be a directory containing supported files and subdirectories. The directory structure will be used to determine nesting.