PHP code example of niirrty / niirrty.config

1. Go to this page and download the library: Download niirrty/niirrty.config library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

niirrty / niirrty.config example snippets




use Niirrty\Config\Provider\JSONConfigProvider;

try
{
   // Read configuration from JSON file
   $config = JSONConfigProvider::Init(
         __DIR__ . '/data/config.json', // JSON config file path
         [ 'json' ],                    // Allowed file extension(s)
         'JSON'                         // The provider name
      )
      ->read();
      
   // Output the config data, converted to a array
   print_r( $config->toArray() );
   
   // Save config (after changes) to an other file
   if ( ! \file_exists( __DIR__ . '/data/config-saved.json' ) )
   {
      // Set/Change value for section 'default' and item 'foo'
      $config[ 'default::foo' ] = true;
      // You can do the same by:
      # $config[ 'default' ][ 'foo' ] = true;
      // or by:
      # $config->setValue( 'default', 'foo', true );
      // assign new config file path to provider
      $config->getProvider()->setOption( 'file', __DIR__ . '/data/config-saved.json' );
      // Save the config with the owning provider
      $config->getProvider()->write( $config );
   }

}
catch ( \Throwable $ex )
{
   // Handle errors
   echo $ex;
}



use Niirrty\Config\Provider\JSONConfigProvider;
use Niirrty\Config\Provider\XMLConfigProvider;

try
{
   // Read configuration from JSON file
   $config = JSONConfigProvider::Init(
         __DIR__ . '/data/config.json', // JSON config file path
         [ 'json' ],                    // Allowed file extension(s)
         'JSON'                         // The provider name
      )
      ->read();
      
   // Output the config data, converted to a array
   print_r( $config->toArray() );
   
   // Set/Change value for section 'default' and item 'foo'
   $config[ 'default::foo' ] = true;
   // Save the config with a new XML provider
   XMLConfigProvider::Init(
         __DIR__ . '/data/config.json', // XML config file path
         [ 'xml' ],                     // Allowed file extension(s)
         'XML'                          // The provider name
      )
      ->write( $config );

}
catch ( \Throwable $ex )
{
   // Handle errors
   echo $ex;
}



return [

   [
      'name'         => 'Section name',
      'description'  => 'A optional section description…',
      'items'        => [
         [
            'name'         => 'config item name 1',
            'description'  => 'A optional item description…',
            'nullable'     => false,
            'type'         => 'bool',
            'value'        => false
         ],
         // more items…
      ],
      // more sections…
   ]

];



return [

   'Section name' => [
      'description'  => 'A optional section description…',
      'items'        => [
         'config item name 1' => [
            'description'  => 'A optional item description…',
            'nullable'     => false,
            'type'         => 'bool',
            'value'        => false
         ],
         // more items…
      ],
      // more sections…
   ]

];