ConfigLib is a PHP library for managing configuration files and storing it in NCC's data, while providing a command line interface for running functions such as editing configuration files inline or importing/exporting configuration files.
One of the biggest advantages of using something like ConfigLib is that it will allow for more complicated software to be configured more easily by following the documented instructions on how to alter configuration files, optionally you could use a builtin editor to edit the configuration file manually.
This project and many others from Nosial are available on multiple publicly available and free git repositories at
Issues & Pull Requests are frequently checked and to be referenced accordingly in commits and changes, Nosial remains dedicated to keep these repositories up to date when possible.
For questions & discussions see the public Telegram community at @NosialDiscussions. We do encourage community support and discussions, please be respectful and follow the rules of the community.
- ConfigLib
The library can be installed using ncc:
ncc install --package="nosial/configlib=latest@n64"or by adding the following to your project.yml file under the dependencies section:
dependencies:
net.nosial.configlib: nosial/configlib@n64If you don't have the n64 source configured, you can add it by running the following command:
ncc repository add --name n64 --type github --host git.n64.ccTo compile the library from source, you need to have ncc installed, then run the following command:
ncc buildBuilding and installing the library with current ncc requires PHP 8.4.1 or newer and ncc 3. Use a supported PHP release with the latest security fixes.
ConfigLib is both a library and a command line tool, the library can be used within your program to create a new configuration file and load in default entries, either on the first run or automatically during the installation process.
The goal to ConfigLib is to make it easy to setup configuration parameters for your program and to make it easy for a user to edit the configuration file without having to manually edit the file. This part of the documentation will explain both how to implement the library into your program and how to use the command line tool to edit the configuration file.
Configuration files are stored as json files in the data directory of ConfigLib which is located at
/var/ncc/data/net.nosial.configlib, this directory is created automatically when the library is installed.
To create a new configuration file, you can create a new \ConfigLib\Configuration() object and pass in the
name of the configuration file, for example:
require 'ncc';
import('net.nosial.configlib');
$config = new \ConfigLib\Configuration('myconfig');This will only initialize the object, to save the configuration file you need to call the save() method:
$config->save();You can set default values for the configuration file which will be created if the values do not exist in the configuration file,
require 'ncc';
import('net.nosial.configlib');
$config = new \ConfigLib\Configuration('com.symfony.yaml');
$config->setDefault('database.host', '127.0.0.1');
$config->setDefault('database.port', 3306);
$config->setDefault('database.username', 'root');
$config->setDefault('database.password', null);
$config->setDefault('database.name', 'test');
$config->save();The command line interface can be executed by running configlib from the command line or by running
ncc exec --package="net.nosial.configlib if configlib isn't in your global path.
For the rest of this documentation, we will assume that you have the configlib command in your global path.
To export a configuration file, run the following command:
configlib --config <config_name> --export <filename>Exported configuration files are stored as YAML files.
Note: if the filename is not specified, the configuration file will be exported to the current working directory with the name
<config_name>.yaml
To import a configuration file, you must specify a valid yaml file, run the following command:
configlib --config <config_name> --import <filename>There are two ways to edit a configuration file using ConfigLib
- Using an external editor
- Inline command line editor
When you use an external editor, ConfigLib will create a temporary YAML file and open it in the specified editor, when you save and close the file, ConfigLib will parse the YAML file and save the configuration file. If the YAML file is invalid, ConfigLib will not save the configuration file.
This is the recommended way to edit configuration files as it allows you to use your preferred editor and it allows you to use the full power of YAML.
To edit a configuration file using an external editor, run the following command:
configlib --config <config_name> --editor nanoNote: Changes will only be applied if you save the file and close the editor.
The inline command line editor is a simple editor that allows you to edit the configuration file from the command line, this is useful for automated scripts.
To view the contents of a configuration file, run the following command:
configlib --config <config_name>To view the value of a specific property, use the --property option:
configlib --config <config_name> --property database.usernameTo edit a property, specify both the --property and --value options:
configlib --config <config_name> --property database.username --value rootConfigLib can be interacted with using environment variables with two functions
- Import a YAML file
- Override configuration values
You can tell ConfigLib to load a YAML file by setting the CONFIGLIB_<CONFIG_NAME> environment variable with the path
to the YAML file as the value, for example:
export CONFIGLIB_MYCONFIG=/path/to/myconfig.yamlIf loading an entire YAML file is too much, you can override specific configuration values by setting the environment
variable CONFIGLIB_<CONFIG_NAME>_<PROPERTY_NAME> with the value you want to set, for example:
export CONFIGLIB_MYCONFIG_DATABASE_USERNAME=rootThis would override the database.username property in the myconfig configuration file even if the property is
already set, the environment variable will take precedence.
This project is licensed under the MIT License - see the LICENSE file for details