A flexible and powerful configuration management component for the KaririCode Framework, providing robust configuration handling capabilities for PHP applications.
- Support for multiple configuration file formats (PHP, JSON, YAML)
- Hierarchical configuration structure
- Easy access to configuration values
- Configuration validation
- Merge strategies for combining multiple configuration sources
- Extensible loader system for custom configuration sources
- Secure handling of sensitive configuration data
To install the KaririCode Configuration component, run the following command:
composer require kariricode/configuration
Create your configuration files in the supported formats (PHP, JSON, or YAML). For example:
// config/app.php
<?php
return [
'name' => 'MyApp',
'version' => '1.0.0',
'debug' => true,
];
// config/database.json
{
"host": "localhost",
"port": 3306,
"username": "root",
"password": "secret"
}
# config/cache.yaml
driver: redis
host: localhost
port: 6379
Set up the Configuration manager in your application's bootstrap file:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use KaririCode\Configurator\Configuration;
use KaririCode\Configurator\Loader\JsonLoader;
use KaririCode\Configurator\Loader\PhpLoader;
use KaririCode\Configurator\Loader\YamlLoader;
$config = new Configuration();
$config->registerLoader(new PhpLoader());
$config->registerLoader(new JsonLoader());
$config->registerLoader(new YamlLoader());
// Load configuration files
$config->load(__DIR__ . '/../config/app.php');
$config->load(__DIR__ . '/../config/database.json');
$config->load(__DIR__ . '/../config/cache.yaml');
Once the configuration is loaded, you can access values like this:
// Get a single configuration value
$appName = $config->get('app.name');
$dbHost = $config->get('database.host');
$cacheDriver = $config->get('cache.driver');
// Check if a configuration key exists
if ($config->has('app.debug')) {
// Do something
}
// Get all configuration values
$allConfig = $config->all();
You can load environment-specific configuration files:
$environment = getenv('APP_ENV') ?: 'production';
$config->load(__DIR__ . "/../config/{$environment}/app.php");
You can create custom loaders for specific file types:
use KaririCode\Configurator\Contract\Configurator\Loader;
class XmlLoader implements Loader
{
public function load(string $path): array
{
// Implementation for loading XML files
}
public function getTypes(): array
{
return ['xml'];
}
}
$config->registerLoader(new XmlLoader());
The component supports different merge strategies for combining configurations:
use KaririCode\Configurator\MergeStrategy\StrictMerge;
$config = new Configuration(
mergeStrategy: new StrictMerge()
);
The component includes automatic validation of configuration values:
use KaririCode\Configurator\Validator\AutoValidator;
$config = new Configuration(
validator: new AutoValidator()
);
To run tests for the KaririCode Configuration Component, use PHPUnit:
make test
For test coverage:
make coverage
This project is licensed under the MIT License - see the LICENSE file for details.
- Documentation: https://kariricode.org
- Issue Tracker: GitHub Issues
- Community: KaririCode Club Community
- Professional Support: For enterprise-level support, contact us at support@kariricode.org
- The KaririCode Framework team and contributors.
- The PHP community for their continuous support and inspiration.
Built with ❤️ by the KaririCode team. Empowering developers to build more robust and flexible PHP applications.
Maintained by Walmir Silva - walmir.silva@kariricode.org