Home - Stefanius67/Config GitHub Wiki
Overview
This package provides a general interface that grant access to configuration settings of different sources and/or formats.
Following Formats are supported so far:
- JSON
- INI (flat INI file like 'usual' windwos INI-Files supporting sections and entries)
- XML
- YAML (needs php extension 'yaml' installed)
- NEON
- directly from an Array (content may result from a DB query)
In addition, the package offers the possibility of merging several configurations from different sources and / or in different formats into one object, which can then be used by any module.
There is thus the possibility of e.g. Merge global and local or general and user-specific configurations without the processing module having to know where what information comes from.
A NullConfig
class is also included that can be used
- for testing without any configuration available
- to implement the 'Null object' design pattern
Usage
- Create an instance of the class that supports the desired format.
- Pass this instance to any module that supports the
ConfigInterface
- inside the module get the needed config settings with
the getXXX($strPath, $default)
- Methods
This package is ideal for implementing the 'Dependency injection' design pattern (DI) if a component should be configurable from the outside.
An instance that implements ConfigInterface
is injected (either directly as a parameter of the constructor or via a method, e.g. setConfig ()
). The class that uses this instance gets everything it needs from it, without having to know the format and/or the origin of the configuration data more precisely.
Without DI, the configuration would have to be created directly in the class using it, which is not very good for testing and expanding.
The instance that is injected can be created by a controller or a factory, depending on the requirements or application, without having to change the component using it.
Good practice can be, that the class that needs a configuration, creates an instance of NullConfig
by default, if no external instance is injected ('Null Object' design pattern). When doing so, it is no longer necessary to constantly check for null
everywhere in the code, which makes it much easier and clearer.