decodelabs/dovetail

Comprehensive config solution

v0.2.8 2024-09-04 20:58 UTC

This package is auto-updated.

Last update: 2024-12-04 21:41:12 UTC


README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

Comprehensive config solution for PHP

Dovetail provides a simple, flexible and powerful way to manage configuration data in PHP applications.

Get news and updates on the DecodeLabs blog.

Installation

Install via Composer:

composer require decodelabs/dovetail

Usage

Importing

Dovetail uses Veneer to provide a unified frontage under DecodeLabs\Dovetail. You can access all the primary functionality via this static frontage without compromising testing and dependency injection.

Env

Dovetail utilises vlucas/phpdotenv to load environment variables from a .env file in your project root. This is automatically loaded when you first access the Env facade.

use DecodeLabs\Dovetail;

$dbHost = Dovetail::envString('DB_HOST', 'localhost'); // String
$dbPort = Dovetail::envInt('DB_PORT', 3306); // Int
$debug = Dovetail::envBool('DEBUG', false); // Bool
$test = Dovetail::env('TEST', 'default'); // Mixed

Config

Dovetail provides structures to allow loading config files from any custom location, into Repository container tree objects, and presented in domain specific Config objects which can then provide custom data access methods according to your needs.

Sensitive data should be loaded from a .env file and not stored in config files - use the Dovetail::env*() methods or $_ENV to inject these values into your config.

# config/database.php
use DecodeLabs\Dovetail;

return [
    'adapter' => 'mysql',
    'host' => $_ENV['DB_HOST'] ?? 'localhost',
    // or
    'port' => Dovetail::envInt('DB_PORT', 3306),
];
# app/Config/Database.php
use DecodeLabs\Dovetail\Config;
use DecodeLabs\Dovetail\ConfigTrait;

class Database implements Config
{
    use ConfigTrait;

    public function getAdapter(): string
    {
        return $this->data['adapter'] ?? 'mysql';
    }

    public function getHost(): string
    {
        return $this->data['host'] ?? 'localhost';
    }

    public function getPort(): int
    {
        return $this->data['port'] ?? 3306;
    }
}
use DecodeLabs\Dovetail;

$config = Dovetail::load('database');
$adapter = $config->getAdapter(); // 'mysql'

Licensing

Dovetail is licensed under the proprietary License. See LICENSE for the full license text.