dschuppelius/php-config-toolkit

Ein Toolkit für JSON-basierte Konfigurationsverwaltung

Installs: 638

Dependents: 1

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/dschuppelius/php-config-toolkit

v0.2.26 2026-01-10 22:12 UTC

README

PHP Version License

Ein JSON-basiertes Konfigurationsverwaltungs-Toolkit mit Plugin-basierter Architektur für verschiedene Konfigurationsdateiformate.

Features

  • Singleton-Pattern: Zentraler ConfigLoader für konsistente Konfigurationsverwaltung
  • Plugin-System: Automatische Erkennung und Laden von ConfigType-Klassen
  • Typ-Casting: Unterstützt float, int, timestamp, date, datetime, bool, array, json und string
  • Validierung: Automatische Validierung gegen den passenden ConfigType
  • Mehrere Konfigurationsformate: Strukturierte Configs, Executable-Definitionen, Postman-Collections u.v.m.

Installation

composer require dschuppelius/php-config-toolkit

Anforderungen

Verwendung

Konfiguration laden

use ConfigToolkit\ConfigLoader;

$loader = ConfigLoader::getInstance();
$loader->loadConfigFile('config.json');

// Wert abrufen
$value = $loader->get('Section', 'key');

Konfiguration validieren

use ConfigToolkit\ConfigValidator;

$errors = ConfigValidator::validate('config.json');

if (empty($errors)) {
    echo "Konfiguration ist gültig!";
} else {
    foreach ($errors as $error) {
        echo "Fehler: $error\n";
    }
}

Beispiel-Konfigurationsdatei

{
  "Database": [
    {"key": "host", "value": "localhost", "type": "text", "enabled": true},
    {"key": "port", "value": "3306", "type": "int", "enabled": true},
    {"key": "debug", "value": "true", "type": "bool", "enabled": true}
  ],
  "Cache": [
    {"key": "ttl", "value": "3600", "type": "int", "enabled": true}
  ]
}

Unterstützte ConfigTypes

ConfigType Beschreibung
StructuredConfigType Standard key/value/enabled-Struktur (Fallback)
AdvancedStructuredConfigType Erweiterte Struktur mit flachen Arrays
ExecutableConfigType Executable-Pfade mit Argumenten
CrossPlatformExecutableConfigType Plattformspezifische Executables (Windows/Linux)
PostmanConfigType Postman Collection-Exporte

Eigene ConfigTypes erstellen

  1. Erstelle eine neue Klasse in src/ConfigTypes/ die ConfigTypeAbstract erweitert
  2. Implementiere die Methoden matches(), parse() und validate()
  3. Der ClassLoader erkennt und registriert die Klasse automatisch
use ConfigToolkit\Contracts\Abstracts\ConfigTypeAbstract;

class MyConfigType extends ConfigTypeAbstract {
    public static function matches(array $data): bool {
        // Prüfe ob diese Klasse die Datenstruktur verarbeiten kann
        return isset($data['mySpecialKey']);
    }

    public function parse(array $data): array {
        // Daten in nutzbares Array umwandeln
        return $data;
    }

    public function validate(array $data): array {
        // Fehler-Array zurückgeben (leer wenn valide)
        return [];
    }
}

Tests ausführen

composer test
# oder
vendor/bin/phpunit

Lizenz

MIT License - siehe LICENSE für Details.

Autor

Daniel Jörg Schuppelius - schuppelius.org