Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not yet rated by the users | Total: 59 | All time: 10,513 This week: 488 |
Version | License | PHP version | Categories | |||
queasy-config 1.0.0 | Custom (specified... | 5 | PHP 5, Files and Folders, Configuration |
Description | Author | |
This class can read a configuration from files in several formats. |
<?php |
v-dem/queasy-config
This package contains a set of the classes intended for reading configuration files. Formats currently supported are:
See our Wiki page.
> composer require v-dem/queasy-config:master-dev
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.
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'
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');
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.
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);
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.
Tests can be run with miminum PHP 7.2 version due to PHPUnit requirements. To run them use
> composer test
Files (60) |
File | Role | Description | ||
---|---|---|---|---|
src (9 files, 1 directory) | ||||
tests (2 directories) | ||||
.phpdoc-md | Data | Auxiliary data | ||
.travis.yml | Data | Auxiliary data | ||
codecov.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Read me |
Files (60) | / | src |
File | Role | Description | ||
---|---|---|---|---|
loader (16 files) | ||||
AbstractConfig.php | Class | Class source | ||
Config.php | Class | Class source | ||
ConfigAwareInterface.php | Class | Class source | ||
ConfigAwareTrait.php | Class | Class source | ||
ConfigException.php | Class | Class source | ||
ConfigInterface.php | Class | Class source | ||
InvalidPathException.php | Class | Class source | ||
MissingOptionException.php | Class | Class source | ||
ReadOnlyException.php | Class | Class source |
Files (60) | / | src | / | loader |
File | Role | Description |
---|---|---|
AbstractLoader.php | Class | Class source |
AlreadyRegisteredException.php | Class | Class source |
CliLoader.php | Class | Class source |
ConfigLoaderException.php | Class | Class source |
CorruptedException.php | Class | Class source |
FileSystemLoader.php | Class | Class source |
IniLoader.php | Class | Class source |
JsonLoader.php | Class | Class source |
LoaderFactory.php | Class | Class source |
LoaderInterface.php | Class | Class source |
LoaderNotFoundException.php | Class | Class source |
NotFoundException.php | Class | Class source |
NotImplementedException.php | Class | Class source |
NotReadableException.php | Class | Class source |
PhpLoader.php | Class | Class source |
XmlLoader.php | Class | Class source |
Files (60) | / | tests | / | resources |
File | Role | Description |
---|---|---|
correct-compound-part.ini | Data | Auxiliary data |
correct-compound-part.php | Aux. | Auxiliary script |
correct-compound.ini | Data | Auxiliary data |
correct-compound.php | Example | Example script |
correct-empty.json | Data | Auxiliary data |
correct-empty.php | Aux. | Auxiliary script |
correct-empty.xml | Data | Auxiliary data |
correct.ini | Data | Auxiliary data |
correct.json | Data | Auxiliary data |
correct.php | Aux. | Auxiliary script |
correct.xml | Data | Auxiliary data |
incorrect-not-empty.ini | Data | Auxiliary data |
incorrect-not-empty.json | Data | Auxiliary data |
incorrect-not-empty.php | Aux. | Auxiliary script |
incorrect-not-empty.xml | Data | Auxiliary data |
incorrect-not-empty2.php | Aux. | Auxiliary script |
incorrect-return-int.php | Aux. | Auxiliary script |
incorrect-return-nothing.php | Aux. | Auxiliary script |
incorrect-return-string.php | Aux. | Auxiliary script |
Files (60) | / | tests | / | src | / | loader |
File | Role | Description |
---|---|---|
CliLoaderTest.php | Class | Class source |
CustomLoader.php | Class | Class source |
IniLoaderTest.php | Class | Class source |
JsonLoaderTest.php | Class | Class source |
LoaderFactoryTest.php | Class | Class source |
PhpLoaderTest.php | Class | Class source |
WrongCustomLoader.php | Class | Class source |
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 |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.