PHP Classes


Recommend this page to a friend!
  Classes of Filip Štamcar   PHP Configuration Writer   Download  
Role: Documentation
Content type: text/markdown
Description: Read me
Class: PHP Configuration Writer
Generate configuration files as PHP scripts
Author: By
Last change:
Date: 4 years ago
Size: 5,929 bytes


Class file image Download

PHP Config Writer

[![Latest Stable Version][icon-stable-version]][link-packagist] [![Latest Untable Version][icon-unstable-version]][link-packagist] [![Total Downloads][icon-downloads]][link-packagist] [![License][icon-license]][link-license] [![PHP][icon-php]][link-php]

[![Linux Build Status][icon-travis]][link-travis] [![Windows Build Status][icon-appveyor]][link-appveyor] [![Code Coverage][icon-coverage]][link-coverage] [![Code Quality][icon-quality]][link-quality]

Lightweight configuration writer for PHP.



MonologPHPMailer requires [PHP][link-php] 5.5.9 or higher. Also, the config directory needs to be writable by web server in order to save config file.

Using Composer

The reccomended way to install ConfigWriter is with [Composer][link-composer], dependency manager for PHP.

You should just require filips123/config-writer in your project.

composer require filips123/config-writer:^2.0

You would only need to include autoloader and namespace in your script.


use ConfigWriter\Config;

require 'vendor/autoload.php';

$config = new Config;

Manually Installation

Alternatively, you could download files from GitHub and then manually include them in your script.

You whould need to include all files and namespace in your script.


use ConfigWriter\Config;

require 'src/Exceptions/UnsupportedFormatException.php';
require 'src/Exceptions/WriteException.php';
require 'src/ConfigInterface.php';
require 'src/AbstractConfig.php';
require 'src/Config.php';
require 'src/Record.php';
require 'src/Writers/WriterInterface.php';
require 'src/Writers/PhpWriter.php';

$config = new Config;


Making the configuration

Configuration making is possible using ConfigWriter\Config class.

$config = new Config;

It accepts two parameters, data and comment, and both are optional. Data parameter contains pre-set data for configuration and comment contains additional comment (or code) on top of the configuration file.

$config = new Config(
        'username' => 'user',
        'password' => 'pass',
 * The configuration file.
 * It contains configuration data.

Adding records

Records can be added using ConfigWriter\Config::addRecord() method.

$config->addRecord('application', 'ConfigWriter');

They can also have comments, which will be generated in documentation.

$config->addRecord('application', 'ConfigWriter', 'Application name');

Adding sections

Sections visually and functionally separate multiple records. They can be added using ConfigWriter\Config::addSection() method.

$database = $config->addSection('database', [], 'Database settings');

$database->addRecord('host', 'localhost', 'Database host');
$database->addRecord('port', '3306', 'Database port');

They can also have pre-set data using second parameter.

        'host' => 'localhost',
        'port' => '3306',
    'Database settings');

Saving configuration

You can save configuration using ConfigWriter\Config::toString() or ConfigWriter\Config::toFile().

When saving to string, configuration writer is required, and when saving to file, writer will be automatically determined.

$config->toString(new ConfigWriter\Writers\PhpWriter);

Writers can also have specific options for writing.

$config->toFile('config.php', new ConfigWriter\Writers\PhpWriter, ['indentation' => '	']);

The only supported writer is for PHP array, but more writers will be added later.


This project uses [SemVer][link-semver] for versioning. For the versions available, see the [tags on this repository][link-tags].


This project is licensed under the MIT license. See the [LICENSE][link-license-file] file for details.

[icon-stable-version]: [icon-unstable-version]: [icon-downloads]: [icon-license]: [icon-php]: [icon-travis]: [icon-appveyor]: [icon-coverage]: [icon-quality]:

[link-packagist]: [link-license]: [link-php]: [link-composer]: [link-travis]: [link-appveyor]: [link-coverage]: [link-quality]: [link-semver]: [link-tags]: [link-license-file]: