devly / config-loader
There is no license information available for the latest version (1.0.1) of this package.
Load configurations from multiple sources into a `Devly\Repository` object.
1.0.1
2024-02-01 15:59 UTC
Requires
- php: >=7.4
- devly/repository: ^1.0
Requires (Dev)
- ext-json: *
- ext-libxml: *
- ext-simplexml: *
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.2
- devly/coding-standard: ^1.0
- mikey179/vfsstream: ^1.6
- nette/neon: ^3.3
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5
- symfony/yaml: ^5.4
Suggests
- ext-json: Needed to support Json configuration files
- ext-libxml: Needed to support XML configuration files
- ext-simplexml: Needed to support XML configuration files
- nette/neon: Needed to support Neon configuration files
- symfony/yaml: Needed to support Yaml configuration files
This package is auto-updated.
Last update: 2025-01-30 18:32:58 UTC
README
Load configurations from multiple sources into a Repository object. Supported file formats are: PHP, JSON, INI, XML, Yaml & Neon.
Install
Install using composer
:
$ composer require devly/config-loader
Usage
Create Loader object
$safeMode = false // Set 'true' to skip exceptions $loader = new \Devly\ConfigLoader\Loader($safeMode);
Load configurations from file
// Load config from a single file $config = $loader->load('path/to/config.php'); // Load config from a list of files $segment = true; // Segment configuration by file name $files = [ 'path/to/config/app.php', 'path/to/config/database.php', ]; $config = $loader->load($files, $segment); var_dump($config->all()); // array(1) { // 'app' => ..., // 'database' => ... // }
Load configuration files from a directory
// Load config from a single file $formats = ['php', 'json']; // load only php and json files. keep null to load all the files in the directory $segment = true; // Segment configuration by file names $config = $loader->load('path/to/config', $segment, $formats);
Cache configurations
Configurations can be cached to a file, a MySQL database or a custom storage provider.
Cache to file
The only thing needed to cache configurations to file is to set a full path to cache directory:
// When creating new Loader instance $loader = new Loader($safeMode, 'full/path/to/cache'); // Or using the `setStorage()` method: $loader->setStorage('full/path/to/cache');
Cache to MySQL database
$name = 'db_name'; $username = 'db_user'; $password = 'db_password'; $host = '172.0.0.1'; // Default 'localhost' // Create instance of DatabaseStorage $storage = new \Devly\ConfigLoader\Storages\DatabaseStorage($name, $username, $password, $host) // Creating new Loader instance $loader = new Loader($safeMode, $storage); // Or using the `setStorage()` method: $loader->setStorage($storage);
Custom cache storage
class CustomStorage implements \Devly\ConfigLoader\Contracts\IStorage { ... } $storage = new CustomStorage(); // Creating new Loader instance $loader = new Loader($safeMode, $storage); // Or using the `setStorage()` method: $loader->setStorage($storage);