ghostwriter/config

Provides an object that maps configuration keys to values.

Fund package maintenance!
ghostwriter

Installs: 1 443 906

Dependents: 10

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

1.0.0 2025-01-07 22:42 UTC

README

Automation Supported PHP Version Mutation Coverage Code Coverage Type Coverage Latest Version on Packagist Downloads

Provides an object that maps configuration keys to values.

Installation

You can install the package via composer:

composer require ghostwriter/config

Star ⭐️ this repo if you find it useful

You can also star (🌟) this repo to find it easier later.

Usage

Given the following configuration directory structure

  • path/to/config/directory/app.php
  • path/to/config/directory/database.php
  • path/to/config/directory/file.php
$directory = 'path/to/config/directory';
$file = 'path/to/config/directory/file.php';
$options = [
    'settings' => [
        'enable' => true,
    ],
];

$config = Config::new($options);
$config->has('settings.disabled'); // false
$config->get('settings.disabled'); // null
$config->get('settings.disabled', 'default'); // 'default'

$config->set('settings.disabled', false); // true
$config->has('settings.disabled'); // true

$config->get('settings.disabled'); // false

$config->toArray(); // ['settings' => ['enable'=>true,'disabled'=>false]]

$config->remove('settings.disabled');

$config->get('settings.disabled'); // null

$config->toArray(); // ['settings' => ['enable'=>true]]
// from an array
$configFactory = ConfigFactory::new(); // or new ConfigFactory()
$config = $configFactory->create($options); 
$config->toArray(); // ['settings' => ['enable'=>true]]

$config->has('settings'); // true
$config->has('settings.enable'); // true
$config->get('settings.enable'); // true
// from a directory
$configFactory = ConfigFactory::new(); // or new ConfigFactory()
$config = $configFactory->createFromDirectory($options);
$config->toArray(); // output below
// [
//      'app' => ['name'=>'App','version'=>'1.0.0'],
//      'database' => ['host'=>'localhost','port'=>3306]
//      'file' => ['path'=>'/path/to/file']
// ]
// from a file
$configFactory = ConfigFactory::new(); // or new ConfigFactory()
$config = $configFactory->createFromFile($file);
$config->toArray(); // ['path'=>'/path/to/file']

API

interface ConfigInterface
{
    public function get(string $key, mixed $default = null): mixed;

    public function has(string $key): bool;

    /**
     * @param array<string,mixed> $config
     */
    public function merge(array $config): self;

    public function remove(string $key): void;

    public function set(string $key, mixed $value): void;

    /**
     * @return array<string,mixed>
     */
    public function toArray(): array;
}
interface ConfigFactoryInterface
{
    /**
     * @param array<string,mixed> $config
     */
    public function create(array $config = []): ConfigInterface;

    public function createFromDirectory(string $directory): ConfigInterface;

    public function createFromFile(string $file): ConfigInterface;
}

Changelog

Please see CHANGELOG.md for more information on what has changed recently.

Credits

License

Please see LICENSE for more information on the license that applies to this project.

Security

Please see SECURITY.md for more information on security disclosure process.