Download the PHP package v-dem/queasy-config without Composer
On this page you can find all versions of the php package v-dem/queasy-config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download v-dem/queasy-config
More information about v-dem/queasy-config
Files in v-dem/queasy-config
Package queasy-config
Short Description Configuration classes (currently supports PHP, INI, XML and JSON configs), part of QuEasy PHP Framework
License LGPL-3.0-only
Homepage https://github.com/v-dem/queasy-config/
Informations about the package queasy-config
QuEasy PHP Framework - Configuration
Package v-dem/queasy-config
This package contains a set of the classes intended for reading configuration files. Formats currently supported are:
- PHP
- INI
- JSON
- XML
- CLI (command-line)
Features
- Easy to use - just like nested arrays or objects. Also it's possible to use
foreach()
with config instances. - Support for default option values.
- Support for multi-file configurations. You can split your config into many files as you wish without changing program code.
- Options inheritance. If an option is missing at current config level, it will look for this option on upper levels.
- Unified config interface. You can switch between config formats without changing your code.
- Easy to extend with other config formats.
- Regular expressions support (it's possible to get config options by regular expression).
Requirements
- PHP version 5.3 or higher
Reference
See Wiki page.
Installation
> composer require v-dem/queasy-config:master-dev
Usage
Let's imagine we have the following config.php
:
Or config.ini
:
Or config.json
:
Or config.xml
:
You can mix different config types, for example top-level config of PHP type can refer to config files of other types.
Creating config instance
Include Composer autoloader:
Create config instance (config file type will be detected by file name extension):
Accessing config instance
Now you can address config sections and options these ways:
Or:
It's possible to use a default value if an option is missing:
A bit shorter way:
It's also possible to point that an option is required, and to throw ConfigException
if this option is missing:
How to check if a section or an option is present in config:
If you don't want to check each section for presence when accessing a very nested option, you can use this trick:
A bit shorter way:
Multi-file configs
config.php
:
queries.php
:
Accessing:
Almost the same for other config formats:
config.ini
:
There can be any PHP code after
@queasy:
so it's possible to use PHP constants etc. Be careful,eval()
function is used to execute this expression.Different config formats can be mixed this way.
Merging configs
You can use Config
's merge()
method to merge two configs. For example, you can have a default configuration and allow users to add or override some options:
Using CLI config type
As an addition it's possible to use command-line arguments as config options source for CLI scripts (just use .cli
extension, it will create appropriate loader):
Options should be passed this way (unfortunately only this is supported currently):
> php test.php option1=123 option2="some text"
I think it's useful to utilize merge()
method there - default config file and optional arguments from command line.
Testing
Tests can be run with miminum PHP 7.2 version due to PHPUnit requirements. To run them use
> composer test