Skip to content

A flexible and efficient configuration management component for PHP applications

License

Notifications You must be signed in to change notification settings

KaririCode-Framework/kariricode-configurator

Folders and files

NameName
Last commit message
Last commit date
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024
Sep 28, 2024
Sep 28, 2024
Sep 28, 2024
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024
Sep 28, 2024
Sep 28, 2024
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024
Sep 27, 2024

Repository files navigation

KaririCode Framework: Configuration Component

en pt-br

Docker Makefile PHP PHPUnit

A flexible and powerful configuration management component for the KaririCode Framework, providing robust configuration handling capabilities for PHP applications.

Features

  • 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

Installation

To install the KaririCode Configuration component, run the following command:

composer require kariricode/configuration

Basic Usage

Step 1: Setting Up Configuration Files

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

Step 2: Initializing the Configuration Manager

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');

Step 3: Accessing Configuration Values

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();

Step 4: Using Environment-Specific Configuration

You can load environment-specific configuration files:

$environment = getenv('APP_ENV') ?: 'production';
$config->load(__DIR__ . "/../config/{$environment}/app.php");

Advanced Usage

Custom Loaders

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());

Merge Strategies

The component supports different merge strategies for combining configurations:

use KaririCode\Configurator\MergeStrategy\StrictMerge;

$config = new Configuration(
    mergeStrategy: new StrictMerge()
);

Validation

The component includes automatic validation of configuration values:

use KaririCode\Configurator\Validator\AutoValidator;

$config = new Configuration(
    validator: new AutoValidator()
);

Testing

To run tests for the KaririCode Configuration Component, use PHPUnit:

make test

For test coverage:

make coverage

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support and Community

Acknowledgments

  • 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