italystrap / config
ItalyStrap Config Module - a simple and useful configuration package the OOP way
Installs: 2 435
Dependents: 10
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 1
Open Issues: 6
Requires
- php: >=7.4
- ext-json: *
- italystrap/storage: ^1.0.0
Requires (Dev)
- codeception/module-asserts: ^1.0
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.2
- humanmade/psalm-plugin-wordpress: ^2.0.3
- infection/codeception-adapter: ^0.4.1
- infection/infection: ^0.26.6
- italystrap/debug: ^2.1
- italystrap/event: dev-master
- italystrap/storage-tests: dev-master
- lucatume/function-mocker-le: ^1.0
- lucatume/wp-browser: ^3.1
- phpbench/phpbench: ^1.2
- phpcompatibility/php-compatibility: ^9.3
- phpmetrics/phpmetrics: ^2.8
- phpspec/prophecy-phpunit: ^2.0
- rector/rector: ^0.15.17
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^4.26
README
ItalyStrap Config Module - a simple and useful configuration package the OOP way
Table Of Contents
Installation
The best way to use this package is through Composer:
composer require italystrap/config
Basic Usage
This package is meant to be used in your application to manage configuration settings. Values can be accessed using dot notation or array notation or a single key, and you can also set, update, delete, and check if a key exists.
Let's see a simple example:
use ItalyStrap\Config\Config; $config = new Config([ 'key' => 'value', 'key2' => 'value2', 'key3' => [ 'key4' => 'value4', ], ]); $value = $config->get('key'); // Output: 'value' // You can access using dot notation $value = $config->get('key3.key4'); // Output: 'value4' $value = $config->get('key3.key5', 'mixed default value'); // Output: 'mixed default value' // You can also access using array notation $value = $config->get(['key3', 'key4']); // Output: 'value4' // It is possible to set a value $isSet = $config->set('key5', 'new value'); // Output: true $value = $config->get('key5'); // Output: 'new value' // You can also set a value using array notation or dot notation $isSet = $config->set(['key6', 'key7'], 'new value'); // or $isSet = $config->set('key6.key7', 'new value'); // Output: true $value = $config->get('key6.key7'); // or $value = $config->get(['key6', 'key7']); // Output: 'new value' // You can delete a value using dot notation or array notation or a single key $isDeleted = $config->delete('key6.key7'); // or $isDeleted = $config->delete(['key6', 'key7']); // or $isDeleted = $config->delete('key6'); // Output: true // You can check if a key exists using dot notation or array notation or a single key $exists = $config->has('key6.key7'); // or $exists = $config->has(['key6', 'key7']); // or $exists = $config->has('key6'); // Output: true if exists, false otherwise // You can update a value using dot notation or array notation or a single key // It works in the same way as the set method $isUpdated = $config->update('key', 'updated value'); // or $isUpdated = $config->update(['key6', 'key7'], 'updated value'); // or $isUpdated = $config->update('key6.key7', 'updated value'); // Output: true // This package also has multiple methods to work with arrays like PSR-6 $config->getMultiple(['key', 'key2', 'key3.key4'], 'default'); $config->setMultiple(['key', 'key2', 'key3.key4'], 'value'); $config->deleteMultiple(['key', 'key2', 'key3.key4']); // You can merge iterables after the Config object is created $config->merge([ 'key' => 'value', 'key2' => 'value2', 'key3' => [ 'key4' => 'value4', ], ]); // You can also get all the configuration settings as an array $all = $config->toArray(); // Output: ['key' => 'value', 'key2' => 'value2', 'key3' => ['key4' => 'value4']] // You can use the object and pass it to a \json_encode $json = \json_encode($config); // Output: '{"key":"value","key2":"value2","key3":{"key4":"value4"}}'
Advanced Usage
You can see more advanced example in the tests' folder.
Deprecation
List of all deprecated method that will be removed in the next major release.
Config::push()
=>Config::set()
Config::add()
=>Config::set()
Config::remove()
=>Config::delete()
Config::all()
=>Config::toArray()
ConfigThemeMods::class
Contributing
All feedback / bug reports / pull requests are welcome.
License
Copyright (c) 2019 Enea Overclokk, ItalyStrap
This code is licensed under the MIT.
Credits
Ideas for the Config::class from:
For the Notation Array Search:
For some ideas: