Download the PHP package freezemage0/config without Composer
On this page you can find all versions of the php package freezemage0/config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download freezemage0/config
More information about freezemage0/config
Files in freezemage0/config
Package config
Short Description Small configuration files management library.
License GPL-2.0-or-later
Informations about the package config
Configuration
freezemage0/config
is a library that provides an easy way to read and create configuration files.
Supports .json
, .ini
and .php
formats.
Installation
You can install this package using composer:
composer require freezemage0/config
Usage
ConfigFactory
ConfigFactory automatically resolves the Importer and Exporter for the configuration. The resolving is based on file extension.
Usage example:
Creation of configuration object
The returned config file will be ready to decode and encode JSON data from config.json
file.
Registering of custom Importers and Exporters
You may want to make your own importer or exporter.
In that case you can register them using ConfigFactory::registerImporter()
and ConfigFactory::registerExporter()
methods.
All custom importers and exporters MUST implement ImporterInterface
and ExporterInterface
respectively.
ImmutableConfig
Basic configuration class, which provides methods to read, set and save configurations.
Instances of this class are immutable, meaning that any call to set()
will return a new instance.
Basic usage:
Retrieving specific value by key can be done by calling ImmutableConfig::get()
method.
Retrieving all configuration values can be done by calling ImmutableConfig::getConfig()
method.
The configuration is loaded on-demand.
Key chaining
The get/set methods of ImmutableConfig support key chaining
.
It means that you can use dot (.) to concatenate the nested keys of configuration in order to get the nested value.
Here is the configuration:
You can retrieve username
for database
connection using the following call:
Feature management
Local management
To disable/enable key chaining
for current instance of ImmutableConfig
, call ImmutableConfig->disableKeyChaining()
.
Global management
Example on how to disable/enable key chaining
for all instances that will be created by ConfigFactory
object:
Saving
You can also create/edit configuration using ImmutableConfig::set()
method.
When you set value, you get a new instance of ImmutableConfig
with a new key => value
pair.
This is done in order to preserve immutability of the original configuration until you are ready to save it.
If you create a new instance of ImmutableConfig
with no file to import, then it will be created.
If the Exporter
within ImmutableConfig
doesn't have filename, then it will be generated based on configuration content.
The same configuration leads to the same filename.
The generated configuration name can be retrieved via ImmutableConfig->getExporter()->getFilename()
Section extraction
You can extract a section from the config and work with it as a separate instance of ImmutableConfig
.
The extracted ImmutableConfig
section will behave exactly as if it was root section.
Be wary that calling ImmutableConfig->save()
will rewrite the contents of original config file,
unless you do something like the following:
Configuration converting
You may want to convert configuration from .json
to .php
format.
The following example does exactly that:
Precautions
Nested sections are not supported for IniExporter
(as they are generally not support by anyone).\
Calling IniExporter->export()
with nested section will throw UnsupportedNestingException
.
All versions of config with dependencies
ext-json Version *