PHP Classes

Queasy PHP Config: Read a configuration from files in several formats

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 59 All time: 10,513 This week: 488Up
Version License PHP version Categories
queasy-config 1.0.0Custom (specified...5PHP 5, Files and Folders, Configuration
Description 

Author

This class can read a configuration from files in several formats.

It provides several classes that can read configuration values from different source types and return them to the application in an array.

Currently, it can read configuration values:

- PHP scripts with arrays
- INI files
- JSON files
- XML files
- Parameter values from the command line (CLI)

Picture of Vitaly
  Performance   Level  
Name: Vitaly <contact>
Classes: 3 packages by
Country: Ukraine Ukraine
Age: 41
All time rank: 421373 in Ukraine Ukraine
Week rank: 192 Up4 in Ukraine Ukraine Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php

return array(
   
'include-section' => new queasy\config\Config(__DIR__ . '/correct-compound-part.php'),
   
'parent-key' => 'parent-value'
);



Details

Codacy Badge Build Status codecov Mutation testing badge Total Downloads License

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).

Planned features

  • YAML support.

Requirements

  • PHP version 5.3 or higher

Documentation

See our Wiki page.

Installation

> composer require v-dem/queasy-config:master-dev

Usage

Let's imagine we have the following config.php:

return [
    'connection' => [
        'driver' => 'mysql',
        'host' => 'localhost',
        'name' => 'test',
        'user' => 'root',
        'password' => 'secret'
    ]
];

Or config.ini:

[connection]
driver = mysql
host = localhost
name = test
user = root
password = secret

Or config.json:

{
    "connection": {
        "driver": "mysql",
        "host": "localhost",
        "name": "test",
        "user": "root",
        "password": "secret"
    }
}

Or config.xml:

<?xml version="1.0">
<config>
    <connection
        driver="mysql"
        host="localhost"
        name="test"
        user="root"
        password="secret" />
</config>

> 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:

require_once('vendor/autoload.php');

Create config instance (config file type will be detected by file name extension):

$config = new queasy\config\Config('config.php'); // Can be also '.ini', '.json' or '.xml'

Accessing config instance

Now you can address config sections and options these ways:

$databaseName = $config->database->name;

Or:

$databaseName = $config['database']['name'];

It's possible to use a default value if an option is missing:

// If 'host' is missing in config, 'localhost' will be used by default
$databaseHost = $config['database']->get('host', 'localhost');

A bit shorter way:

// If 'host' is missing in config, 'localhost' will be used by default
$databaseHost = $config'database';

It's also possible to point that an option is required, and to throw ConfigException if this option is missing:

// Throw ConfigException if 'name' is missing
$databaseName = $config['database']->need('name');

How to check if a section or an option is present in config:

$hasDatabaseName = isset($config['database']);
$hasDatabaseName = isset($config['database']['name']);

If you don't want to check each section for presence when accessing a very nested option, you can use this trick:

// $databaseName will contain 'default' if 'name' and/or 'database' options are missing
$databaseName = $config->get('database', [])->get('name', 'default');

A bit shorter way:

// $databaseName will contain 'default' if 'name' and/or 'database' options are missing
$databaseName = $config('database', [])('name', 'default');

Multi-file configs

config.php:

return [
    'connection' => [
        'driver' => 'mysql',
        'host' => 'localhost',
        'name' => 'test',
        'user' => 'root',
        'password' => 'secret'
    ],
    'queries' => new queasy\config\Config('queries.php') // Can be config of another type (INI, JSON etc)
];

queries.php:

return [
    'selectActiveUsers' => 'SELECT * FROM `users` WHERE `is_active` = 1'
];

Accessing:

$config = new queasy\config\Config('config.php');
$query = $config['queries']['selectActiveUsers'];

Almost the same for other config formats:

config.ini:

[connection]
driver = mysql
host = localhost
name = test
user = root
password = secret
queries = "@queasy:new queasy\config\Config('queries.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:

$defaultConfig = new queasy\config\Config('defaults.php');
$optionalConfig = new queasy\config\Config($arrayWithOptionsToAddOrOverride);
$defaultConfig->merge($optionalConfig);

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):

$config = new queasy\config\Config('.cli');

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

  Files folder image Files (60)  
File Role Description
Files folder imagesrc (9 files, 1 directory)
Files folder imagetests (2 directories)
Accessible without login Plain text file .phpdoc-md Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file codecov.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files (60)  /  src  
File Role Description
Files folder imageloader (16 files)
  Accessible without login Plain text file AbstractConfig.php Class Class source
  Accessible without login Plain text file Config.php Class Class source
  Accessible without login Plain text file ConfigAwareInterface.php Class Class source
  Accessible without login Plain text file ConfigAwareTrait.php Class Class source
  Accessible without login Plain text file ConfigException.php Class Class source
  Accessible without login Plain text file ConfigInterface.php Class Class source
  Accessible without login Plain text file InvalidPathException.php Class Class source
  Accessible without login Plain text file MissingOptionException.php Class Class source
  Accessible without login Plain text file ReadOnlyException.php Class Class source

  Files folder image Files (60)  /  src  /  loader  
File Role Description
  Accessible without login Plain text file AbstractLoader.php Class Class source
  Accessible without login Plain text file AlreadyRegisteredException.php Class Class source
  Accessible without login Plain text file CliLoader.php Class Class source
  Accessible without login Plain text file ConfigLoaderException.php Class Class source
  Accessible without login Plain text file CorruptedException.php Class Class source
  Accessible without login Plain text file FileSystemLoader.php Class Class source
  Accessible without login Plain text file IniLoader.php Class Class source
  Accessible without login Plain text file JsonLoader.php Class Class source
  Accessible without login Plain text file LoaderFactory.php Class Class source
  Accessible without login Plain text file LoaderInterface.php Class Class source
  Accessible without login Plain text file LoaderNotFoundException.php Class Class source
  Accessible without login Plain text file NotFoundException.php Class Class source
  Accessible without login Plain text file NotImplementedException.php Class Class source
  Accessible without login Plain text file NotReadableException.php Class Class source
  Accessible without login Plain text file PhpLoader.php Class Class source
  Accessible without login Plain text file XmlLoader.php Class Class source

  Files folder image Files (60)  /  tests  
File Role Description
Files folder imageresources (19 files)
Files folder imagesrc (1 file, 1 directory)

  Files folder image Files (60)  /  tests  /  resources  
File Role Description
  Accessible without login Plain text file correct-compound-part.ini Data Auxiliary data
  Accessible without login Plain text file correct-compound-part.php Aux. Auxiliary script
  Accessible without login Plain text file correct-compound.ini Data Auxiliary data
  Accessible without login Plain text file correct-compound.php Example Example script
  Accessible without login Plain text file correct-empty.json Data Auxiliary data
  Accessible without login Plain text file correct-empty.php Aux. Auxiliary script
  Accessible without login Plain text file correct-empty.xml Data Auxiliary data
  Accessible without login Plain text file correct.ini Data Auxiliary data
  Accessible without login Plain text file correct.json Data Auxiliary data
  Accessible without login Plain text file correct.php Aux. Auxiliary script
  Accessible without login Plain text file correct.xml Data Auxiliary data
  Accessible without login Plain text file incorrect-not-empty.ini Data Auxiliary data
  Accessible without login Plain text file incorrect-not-empty.json Data Auxiliary data
  Accessible without login Plain text file incorrect-not-empty.php Aux. Auxiliary script
  Accessible without login Plain text file incorrect-not-empty.xml Data Auxiliary data
  Accessible without login Plain text file incorrect-not-empty2.php Aux. Auxiliary script
  Accessible without login Plain text file incorrect-return-int.php Aux. Auxiliary script
  Accessible without login Plain text file incorrect-return-nothing.php Aux. Auxiliary script
  Accessible without login Plain text file incorrect-return-string.php Aux. Auxiliary script

  Files folder image Files (60)  /  tests  /  src  
File Role Description
Files folder imageloader (8 files)
  Accessible without login Plain text file ConfigTest.php Class Class source

  Files folder image Files (60)  /  tests  /  src  /  loader  
File Role Description
  Accessible without login Plain text file CliLoaderTest.php Class Class source
  Accessible without login Plain text file CustomLoader.php Class Class source
  Accessible without login Plain text file IniLoaderTest.php Class Class source
  Accessible without login Plain text file JsonLoaderTest.php Class Class source
  Accessible without login Plain text file LoaderFactoryTest.php Class Class source
  Accessible without login Plain text file PhpLoaderTest.php Class Class source
  Accessible without login Plain text file WrongCustomLoader.php Class Class source
  Accessible without login Plain text file XmlLoaderTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:59
This week:0
All time:10,513
This week:488Up