|
1 | 1 | PHP Config Writer |
2 | 2 | ================= |
3 | 3 |
|
| 4 | +[![Latest Stable Version][icon-stable-version]][link-packagist] |
| 5 | +[![Latest Untable Version][icon-unstable-version]][link-packagist] |
| 6 | +[![Total Downloads][icon-downloads]][link-packagist] |
| 7 | +[![License][icon-license]][link-license] |
| 8 | +[![PHP][icon-php]][link-php] |
| 9 | + |
| 10 | +[![Linux Build Status][icon-travis]][link-travis] |
| 11 | +[![Windows Build Status][icon-appveyor]][link-appveyor] |
| 12 | +[![Code Coverage][icon-coverage]][link-coverage] |
| 13 | +[![Code Quality][icon-quality]][link-quality] |
| 14 | + |
4 | 15 | Lightweight configuration writer for PHP. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +### Requirements |
| 20 | + |
| 21 | +MonologPHPMailer requires *[PHP][link-php] 5.5.9* or higher. |
| 22 | +Also, the config directory needs to be writable by web server in order to save config file. |
| 23 | + |
| 24 | +### Using Composer |
| 25 | + |
| 26 | +The reccomended way to install ConfigWriter is with [Composer][link-composer], dependency manager for PHP. |
| 27 | + |
| 28 | +You should just require `filips123/config-writer` in your project. |
| 29 | + |
| 30 | +```bash |
| 31 | +composer require filips123/config-writer:^2.0 |
| 32 | +``` |
| 33 | + |
| 34 | +You would only need to include autoloader and namespace in your script. |
| 35 | + |
| 36 | +```php |
| 37 | +<?php |
| 38 | + |
| 39 | +use ConfigWriter\Config; |
| 40 | + |
| 41 | +require 'vendor/autoload.php'; |
| 42 | + |
| 43 | +$config = new Config; |
| 44 | +``` |
| 45 | + |
| 46 | +### Manually Installation |
| 47 | + |
| 48 | +Alternatively, you could download files from GitHub and then manually include them in your script. |
| 49 | + |
| 50 | +You whould need to include all files and namespace in your script. |
| 51 | + |
| 52 | +```php |
| 53 | +<?php |
| 54 | + |
| 55 | +use ConfigWriter\Config; |
| 56 | + |
| 57 | +require 'src/Exceptions/UnsupportedFormatException.php'; |
| 58 | +require 'src/Exceptions/WriteException.php'; |
| 59 | +require 'src/ConfigInterface.php'; |
| 60 | +require 'src/AbstractConfig.php'; |
| 61 | +require 'src/Config.php'; |
| 62 | +require 'src/Record.php'; |
| 63 | +require 'src/Writers/WriterInterface.php'; |
| 64 | +require 'src/Writers/PhpWriter.php'; |
| 65 | + |
| 66 | +$config = new Config; |
| 67 | +``` |
| 68 | + |
| 69 | +## Usage |
| 70 | + |
| 71 | +### Making the configuration |
| 72 | + |
| 73 | +Configuration making is possible using `ConfigWriter\Config` class. |
| 74 | + |
| 75 | +```php |
| 76 | +$config = new Config; |
| 77 | +``` |
| 78 | + |
| 79 | +It accepts two parameters, data and comment, and both are optional. |
| 80 | +Data parameter contains pre-set data for configuration and comment contains additional comment (or code) on top of the configuration file. |
| 81 | + |
| 82 | +```php |
| 83 | +$config = new Config( |
| 84 | + [ |
| 85 | + 'username' => 'user', |
| 86 | + 'password' => 'pass', |
| 87 | + ], |
| 88 | + <<<EOD |
| 89 | +/** |
| 90 | + * The configuration file. |
| 91 | + * |
| 92 | + * It contains configuration data. |
| 93 | + */ |
| 94 | +EOD; |
| 95 | +); |
| 96 | +``` |
| 97 | + |
| 98 | +### Adding records |
| 99 | + |
| 100 | +Records can be added using `ConfigWriter\Config::addRecord()` method. |
| 101 | + |
| 102 | +```php |
| 103 | +$config->addRecord('application', 'ConfigWriter'); |
| 104 | +``` |
| 105 | + |
| 106 | +They can also have comments, which will be generated in documentation. |
| 107 | + |
| 108 | +```php |
| 109 | +$config->addRecord('application', 'ConfigWriter', 'Application name'); |
| 110 | +``` |
| 111 | + |
| 112 | +### Adding sections |
| 113 | + |
| 114 | +Sections visually and functionally separate multiple records. They can be added using `ConfigWriter\Config::addSection()` method. |
| 115 | + |
| 116 | +```php |
| 117 | +$database = $config->addSection('database', [], 'Database settings'); |
| 118 | + |
| 119 | +$database->addRecord('host', 'localhost', 'Database host'); |
| 120 | +$database->addRecord('port', '3306', 'Database port'); |
| 121 | +``` |
| 122 | + |
| 123 | +They can also have pre-set data using second parameter. |
| 124 | + |
| 125 | +```php |
| 126 | +$config->addSection( |
| 127 | + 'database', |
| 128 | + [ |
| 129 | + 'host' => 'localhost', |
| 130 | + 'port' => '3306', |
| 131 | + ], |
| 132 | + 'Database settings'); |
| 133 | +``` |
| 134 | + |
| 135 | +### Saving configuration |
| 136 | + |
| 137 | +You can save configuration using `ConfigWriter\Config::toString()` or `ConfigWriter\Config::toFile()`. |
| 138 | + |
| 139 | +When saving to string, configuration writer is required, and when saving to file, writer will be automatically determined. |
| 140 | + |
| 141 | +```php |
| 142 | +$config->toString(new ConfigWriter\Writers\PhpWriter); |
| 143 | +$config->toFile('config.php'); |
| 144 | +``` |
| 145 | + |
| 146 | +Writers can also have specific options for writing. |
| 147 | + |
| 148 | +```php |
| 149 | +$config->toFile('config.php', new ConfigWriter\Writers\PhpWriter, ['indentation' => ' ']); |
| 150 | +``` |
| 151 | + |
| 152 | +The only supported writer is for PHP array, but more writers will be added later. |
| 153 | + |
| 154 | +## Versioning |
| 155 | +This project uses [SemVer][link-semver] for versioning. For the versions available, see the [tags on this repository][link-tags]. |
| 156 | + |
| 157 | +## License |
| 158 | +This project is licensed under the MIT license. See the [`LICENSE`][link-license-file] file for details. |
| 159 | + |
| 160 | +[icon-stable-version]: https://img.shields.io/packagist/v/filips123/config-writer.svg?style=flat-square&label=Latest+Stable+Version |
| 161 | +[icon-unstable-version]: https://img.shields.io/packagist/vpre/filips123/config-writer.svg?style=flat-square&label=Latest+Unstable+Version |
| 162 | +[icon-downloads]: https://img.shields.io/packagist/dt/filips123/config-writer.svg?style=flat-square&label=Downloads |
| 163 | +[icon-license]: https://img.shields.io/packagist/l/filips123/config-writer.svg?style=flat-square&label=License |
| 164 | +[icon-php]: https://img.shields.io/packagist/php-v/filips123/config-writer.svg?style=flat-square&label=PHP |
| 165 | +[icon-travis]: https://img.shields.io/travis/com/filips123/ConfigWriter.svg?style=flat-square&label=Linux+Build+Status |
| 166 | +[icon-appveyor]: https://img.shields.io/appveyor/ci/filips123/ConfigWriter.svg?style=flat-square&label=Windows+Build+Status |
| 167 | +[icon-coverage]: https://img.shields.io/scrutinizer/coverage/g/filips123/ConfigWriter.svg?style=flat-square&label=Code+Coverage |
| 168 | +[icon-quality]: https://img.shields.io/scrutinizer/g/filips123/ConfigWriter.svg?style=flat-square&label=Code+Quality |
| 169 | + |
| 170 | +[link-packagist]: https://packagist.org/packages/filips123/config-writer/ |
| 171 | +[link-license]: https://choosealicense.com/licenses/mit/ |
| 172 | +[link-php]: https://php.net/ |
| 173 | +[link-composer]: https://getcomposer.org/ |
| 174 | +[link-travis]: https://travis-ci.com/filips123/ConfigWriter/ |
| 175 | +[link-appveyor]: https://ci.appveyor.com/project/filips123/configwriter/ |
| 176 | +[link-coverage]: https://scrutinizer-ci.com/g/filips123/ConfigWriter/code-structure/ |
| 177 | +[link-quality]: https://scrutinizer-ci.com/g/filips123/ConfigWriter/ |
| 178 | +[link-semver]: https://semver.org/ |
| 179 | +[link-tags]: https://github.com/filips123/ConfigWriter/tags/ |
| 180 | +[link-license-file]: https://github.com/filips123/ConfigWriter/blob/master/LICENSE |
| 181 | + |
0 commit comments