PHP Classes

How to Use a PHP Config Object that Applications Cannot Change Using the Package Jaxon Config: Save configuration values in immutable objects

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-03-02 (6 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
jaxon-config 0.9.0BSD License8Configuration, PSR, PHP 8
Collaborate with this project 

Author

jaxon-config - github.com

Description

This package can save configuration values in immutable objects.

It provides a class that can save and read configuration option values that applications cannot change.

When one or more configuration entries are added or changed, a new configuration object is created with the modified values.

The configuration options can initially be loaded from a file or a string in JSON or YAML formats.

Picture of Thierry Feuzeu
  Performance   Level  
Name: Thierry Feuzeu <contact>
Classes: 14 packages by
Country: Cameroon Cameroon
Innovation award
Innovation award
Nominee: 1x

Instructions

Jaxon Config saves config options in immutable objects.

Install

composer require jaxon-php/jaxon-config

Usage

Create a config setter.

$setter = new \Jaxon\Config\ConfigSetter();

Create a config object with initial value.

/ @var \Jaxon\Config\Config */
$config = $setter->newConfig([
    'a' => [
        'b' => [
            'c' => 'Value',
        ],
    ],
]);

Create a config reader.

$reader = new \Jaxon\Config\ConfigReader($setter);

Read config options from a file.

// A new config object is returned.
// From a PHP file.
$config = $reader->load($config, '/path/to/config/file.php');
// Or from a YAML file.
$config = $reader->load($config, '/path/to/config/file.yaml');
// Or from a JSON file.
$config = $reader->load($config, '/path/to/config/file.json');

Create an empty config object and set values.

/ @var \Jaxon\Config\Config */
$config = $setter->newConfig();
// A new config object is returned.
$config = $setter->setOptions($config, [
    'a' => [
        'b' => [
            'c' => 'Value',
        ],
    ],
]);

Read values.

$config->getOption('a'); // Returns ['b' => ['c' => 'Value']]
$config->getOption('a.b'); // Returns ['c' => 'Value']
$config->getOption('a.b.c'); // Returns 'Value'

Set a single value.

// A new config object is returned.
$config = $setter->setOption($config, 'a.b.d', 'Another value');

Read values.

$config->getOption('a'); // Returns ['b' => ['c' => 'Value', 'd' => 'Another value']]
$config->getOption('a.b'); // Returns ['c' => 'Value', 'd' => 'Another value']
$config->getOption('a.b.c'); // Returns 'Value'
$config->getOption('a.b.d'); // Returns 'Another value'

Set values with a prefix.

// A new config object is returned.
$config = $setter->setOptions($config, [
    'd' => [
        'e' => 'Overwritten value',
    ],
    'f' => ['Array', 'Of', 'Values'],
], 'a.b');

Read values.

$config->getOption('a.b'); // Returns ['c' => 'Value', 'd' => ['e' => 'Overwritten value']]
$config->getOption('a.b.d'); // Returns ['e' => 'Overwritten value']
$config->getOption('a.b.d.e'); // Returns 'Overwritten value'
$config->getOption('a.b.f'); // Returns ['Array', 'Of', 'Values']

Create a new config object.

/ @var \Jaxon\Config\Config */
$config = $setter->newConfig([
    'b' => [
        'c' => 'Value',
    ],
    'd' => 'Value',
    'e' => 'Value',
    'f' => 'Value',
], 'a');

Read values.

$config->getOption('a'); // Returns ['b' => ['c' => 'Value'], 'd' => 'Value', 'e' => 'Value', 'f' => 'Value']

Remove an entry.

// A new config object is returned.
$config = $setter->unsetOption($config, 'a.e');

Read values.

$config->getOption('a'); // Returns ['b' => ['c' => 'Value'], 'd' => 'Value', 'f' => 'Value']

Remove multiple entries.

// A new config object is returned.
$config = $setter->unsetOptions($config, ['a.f', 'a.b']);

Read values.

$config->getOption('a'); // Returns ['d' => 'Value']
  Files folder image Files (27)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (3 files, 2 directories)
Files folder imagetests (2 directories)
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (27)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (27)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file test.yml Data Auxiliary data

  Files folder image Files (27)  /  src  
File Role Description
Files folder imageException (5 files)
Files folder imageReader (4 files)
  Plain text file Config.php Class Class source
  Plain text file ConfigReader.php Class Class source
  Plain text file ConfigSetter.php Class Class source

  Files folder image Files (27)  /  src  /  Exception  
File Role Description
  Plain text file DataDepth.php Class Class source
  Plain text file FileAccess.php Class Class source
  Plain text file FileContent.php Class Class source
  Plain text file FileExtension.php Class Class source
  Plain text file YamlExtension.php Class Class source

  Files folder image Files (27)  /  src  /  Reader  
File Role Description
  Plain text file JsonReader.php Class Class source
  Plain text file PhpReader.php Class Class source
  Plain text file Value.php Class Class source
  Plain text file YamlReader.php Class Class source

  Files folder image Files (27)  /  tests  
File Role Description
Files folder imageconfig (8 files)
Files folder imageTestConfig (1 file)

  Files folder image Files (27)  /  tests  /  config  
File Role Description
  Accessible without login Plain text file array.php Aux. Configuration script
  Accessible without login Plain text file config.ini Data Auxiliary data
  Accessible without login Plain text file config.json Data Auxiliary data
  Accessible without login Plain text file config.php Aux. Configuration script
  Accessible without login Plain text file config.yaml Data Auxiliary data
  Accessible without login Plain text file error.json Data Auxiliary data
  Accessible without login Plain text file error.php Aux. Configuration script
  Accessible without login Plain text file error.yml Data Auxiliary data

  Files folder image Files (27)  /  tests  /  TestConfig  
File Role Description
  Plain text file ConfigTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0