|
1 |
| -# KaririCode Framework: Configurator Component |
| 1 | +# KaririCode Framework: Configuration Component |
2 | 2 |
|
3 | 3 | [](README.md)
|
4 | 4 | [](README.pt-br.md)
|
|
7 | 7 | 
|
8 | 8 | 
|
9 | 9 | 
|
| 10 | + |
| 11 | +A flexible and powerful configuration management component for the KaririCode Framework, providing robust configuration handling capabilities for PHP applications. |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- Support for multiple configuration file formats (PHP, JSON, YAML) |
| 16 | +- Hierarchical configuration structure |
| 17 | +- Easy access to configuration values |
| 18 | +- Configuration validation |
| 19 | +- Merge strategies for combining multiple configuration sources |
| 20 | +- Extensible loader system for custom configuration sources |
| 21 | +- Secure handling of sensitive configuration data |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +To install the KaririCode Configuration component, run the following command: |
| 26 | + |
| 27 | +```bash |
| 28 | +composer require kariricode/configuration |
| 29 | +``` |
| 30 | + |
| 31 | +## Basic Usage |
| 32 | + |
| 33 | +### Step 1: Setting Up Configuration Files |
| 34 | + |
| 35 | +Create your configuration files in the supported formats (PHP, JSON, or YAML). For example: |
| 36 | + |
| 37 | +```php |
| 38 | +// config/app.php |
| 39 | +<?php |
| 40 | +return [ |
| 41 | + 'name' => 'MyApp', |
| 42 | + 'version' => '1.0.0', |
| 43 | + 'debug' => true, |
| 44 | +]; |
| 45 | +``` |
| 46 | + |
| 47 | +```json |
| 48 | +// config/database.json |
| 49 | +{ |
| 50 | + "host": "localhost", |
| 51 | + "port": 3306, |
| 52 | + "username": "root", |
| 53 | + "password": "secret" |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +```yaml |
| 58 | +# config/cache.yaml |
| 59 | +driver: redis |
| 60 | +host: localhost |
| 61 | +port: 6379 |
| 62 | +``` |
| 63 | +
|
| 64 | +### Step 2: Initializing the Configuration Manager |
| 65 | +
|
| 66 | +Set up the Configuration manager in your application's bootstrap file: |
| 67 | +
|
| 68 | +```php |
| 69 | +<?php |
| 70 | + |
| 71 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 72 | + |
| 73 | +use KaririCode\Configurator\Configuration; |
| 74 | +use KaririCode\Configurator\Loader\JsonLoader; |
| 75 | +use KaririCode\Configurator\Loader\PhpLoader; |
| 76 | +use KaririCode\Configurator\Loader\YamlLoader; |
| 77 | + |
| 78 | +$config = new Configuration(); |
| 79 | + |
| 80 | +$config->registerLoader(new PhpLoader()); |
| 81 | +$config->registerLoader(new JsonLoader()); |
| 82 | +$config->registerLoader(new YamlLoader()); |
| 83 | + |
| 84 | +// Load configuration files |
| 85 | +$config->load(__DIR__ . '/../config/app.php'); |
| 86 | +$config->load(__DIR__ . '/../config/database.json'); |
| 87 | +$config->load(__DIR__ . '/../config/cache.yaml'); |
| 88 | +``` |
| 89 | + |
| 90 | +### Step 3: Accessing Configuration Values |
| 91 | + |
| 92 | +Once the configuration is loaded, you can access values like this: |
| 93 | + |
| 94 | +```php |
| 95 | +// Get a single configuration value |
| 96 | +$appName = $config->get('app.name'); |
| 97 | +$dbHost = $config->get('database.host'); |
| 98 | +$cacheDriver = $config->get('cache.driver'); |
| 99 | + |
| 100 | +// Check if a configuration key exists |
| 101 | +if ($config->has('app.debug')) { |
| 102 | + // Do something |
| 103 | +} |
| 104 | + |
| 105 | +// Get all configuration values |
| 106 | +$allConfig = $config->all(); |
| 107 | +``` |
| 108 | + |
| 109 | +### Step 4: Using Environment-Specific Configuration |
| 110 | + |
| 111 | +You can load environment-specific configuration files: |
| 112 | + |
| 113 | +```php |
| 114 | +$environment = getenv('APP_ENV') ?: 'production'; |
| 115 | +$config->load(__DIR__ . "/../config/{$environment}/app.php"); |
| 116 | +``` |
| 117 | + |
| 118 | +## Advanced Usage |
| 119 | + |
| 120 | +### Custom Loaders |
| 121 | + |
| 122 | +You can create custom loaders for specific file types: |
| 123 | + |
| 124 | +```php |
| 125 | +use KaririCode\Configurator\Contract\Configurator\Loader; |
| 126 | + |
| 127 | +class XmlLoader implements Loader |
| 128 | +{ |
| 129 | + public function load(string $path): array |
| 130 | + { |
| 131 | + // Implementation for loading XML files |
| 132 | + } |
| 133 | + |
| 134 | + public function getTypes(): array |
| 135 | + { |
| 136 | + return ['xml']; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +$config->registerLoader(new XmlLoader()); |
| 141 | +``` |
| 142 | + |
| 143 | +### Merge Strategies |
| 144 | + |
| 145 | +The component supports different merge strategies for combining configurations: |
| 146 | + |
| 147 | +```php |
| 148 | +use KaririCode\Configurator\MergeStrategy\StrictMerge; |
| 149 | + |
| 150 | +$config = new Configuration( |
| 151 | + mergeStrategy: new StrictMerge() |
| 152 | +); |
| 153 | +``` |
| 154 | + |
| 155 | +### Validation |
| 156 | + |
| 157 | +The component includes automatic validation of configuration values: |
| 158 | + |
| 159 | +```php |
| 160 | +use KaririCode\Configurator\Validator\AutoValidator; |
| 161 | + |
| 162 | +$config = new Configuration( |
| 163 | + validator: new AutoValidator() |
| 164 | +); |
| 165 | +``` |
| 166 | + |
| 167 | +## Testing |
| 168 | + |
| 169 | +To run tests for the KaririCode Configuration Component, use PHPUnit: |
| 170 | + |
| 171 | +```bash |
| 172 | +make test |
| 173 | +``` |
| 174 | + |
| 175 | +For test coverage: |
| 176 | + |
| 177 | +```bash |
| 178 | +make coverage |
| 179 | +``` |
| 180 | + |
| 181 | +## License |
| 182 | + |
| 183 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 184 | + |
| 185 | +## Support and Community |
| 186 | + |
| 187 | +- **Documentation**: [https://kariricode.org](https://kariricode.org) |
| 188 | +- **Issue Tracker**: [GitHub Issues](https://github.com/KaririCode-Framework/kariricode-configurator/issues) |
| 189 | +- **Community**: [KaririCode Club Community](https://kariricode.club) |
| 190 | +- **Professional Support **: For enterprise-level support, contact us at [email protected] |
| 191 | + |
| 192 | +## Acknowledgments |
| 193 | + |
| 194 | +- The KaririCode Framework team and contributors. |
| 195 | +- The PHP community for their continuous support and inspiration. |
| 196 | + |
| 197 | +--- |
| 198 | + |
| 199 | +Built with ❤️ by the KaririCode team. Empowering developers to build more robust and flexible PHP applications. |
| 200 | + |
| 201 | +Maintained by Walmir Silva - [[email protected]](mailto:[email protected]) |
0 commit comments