Download the PHP package brightnucleus/config without Composer
On this page you can find all versions of the php package brightnucleus/config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brightnucleus/config
More information about brightnucleus/config
Files in brightnucleus/config
Package config
Short Description Minimal, reusable Config component.
License MIT
Informations about the package config
Bright Nucleus Config Component
This is a very lean Config component to help you write reusable code. It only offers basic functionality and is meant to be used in libraries and small projects. If you need a Config component for more complex projects, you should take a look at the Symfony Config Component.
Table Of Contents
- Installation
- Basic Usage
- Working With Config Data
- Checking The Existence Of A Key
- Getting The Value Of A Key
- Nested Keys
- Example - Configuration File
- Example - Configurable Class
- Example - Getting The Config Into The Class
- Example - Class That Loads Default Config Unless Config Was Injected
- Example - Merging Several Configs Into One
- Working With Config Data
- Config Formats
- Advanced Usage
- Configuration Schema
- Configuration Validation
- Custom Implementations
- Contributing
Installation
The best way to use this component is through Composer:
Basic Usage
A class that wants to be configurable should accept a ConfigInterface
in its constructor, so that the Config can be injected. The surrounding code then should inject an instance of an object (for example the generic Config
that is provided with this component). This way, the class that accepts the Config can be written in a 100% reusable way, while all project-specific stuff will be injected through the Config.
Working With Config Data
Checking The Existence Of A Key
To check whether the configuration has a certain key, you can use the ConfigInterface::hasKey($key)
method, or, if you are using the ConfigTrait
in your class, you can use the convenience method $this->hasConfigKey($key)
.
Getting The Value Of A Key
To get the configuration value of a certain key, you can use the ConfigInterface::getKey($key)
method, or, if you are using the ConfigTrait
in your class, you can use the convenience method $this->getConfigKey($key)
.
If you use closures in your Config file, you can also use the convenience function $this->getConfigCallable( $key, array $args )
provided by the ConfigTrait
, which will immediately execute the closure by passing it the provided arguments, and return the resultant value instead.
Nested Keys
If your keys are nested, you can provide multiple levels of keys in one request. So, whenever you need to provide a key and want to use a nested one, you can either provide a comma-separated list of keys ( $this->getConfigKey( 'level1', 'level2', 'level3' );
) or a string that contains the list of keys separated by a delimiter ( $this->getConfigKey( 'level1/level2/level3' );
).
You can freely mix-and-match these two approaches as you like.
The default delimiters are: /
, \
and .
. You can choose different delimiters by passing an array of delimiters as a fourth argument to the Config
s constructor.
Example - Configuration File
The snippet below shows the basic structure of a config file.
Example - Configurable Class
Here is an example setup of how you could feed this configuration into a plugin.
Example - Getting The Config Into The Class
You can combine all of your configurations into 1 single file and only pass "Sub-Configurations" to the individual components using the getSubConfig()
method. This way, you can avoid an additional file access and an additional validation pass for each component.
To create a new instance of a ConfigInterface
, you should use the ConfigFactory
. The basic method ConfigFactory::create()
can take either an array of values, or one or more file names (with absolute paths) as strings.
If you provide a comma-separated list of file names, they are processed consecutively until the first one could be loaded successfully.
There's a convenience function ConfigFactory::createSubConfig()
to immediately fetch a sub-config from a loaded config file. This allows you to quickly bypass the vendor/package prefixes and only pass in the relevant data into the new object.
Here's how you can pass the configuration file into the class:
Example - Class That Loads Default Config Unless Config Was Injected
The ConfigTrait
provides some convenience functionality that lets you write classes that can receive an injected Config, but fall back to a default configuration file if non was injected.
Here's you can code such a class:
Example - Merging Several Configs Into One
You can provide a comma-separated list of file names to the ConfigFactory::merge()
method. They are loaded consecutively and merged into one coherent Config. For each duplicate Config key, the value in the later files will override the value in the earlier files.
For our example, we'll define a new Config file called override_settings.php
, that overrides a key that was already set in the default Config file.
Config Formats
The Bright Nucleus Config component can be extended to load a multitude of different file formats. The base package includes a very minimal PHPLoader
class. It can load basic PHP files that just return
an array.
Additional packages that add other formats like JSON or YAML are planned and will be released soon.
Custom loaders are lazily instantiated only when needed.
Advanced Usage
Configuration Schema
TODO
Configuration Validation
TODO
Custom Implementations
TODO
Contributing
All feedback / bug reports / pull requests are welcome.
License
This code is released under the MIT license.
For the full copyright and license information, please view the LICENSE
file distributed with this source code.