PHP code example of abdeslam / configuration-manager

1. Go to this page and download the library: Download abdeslam/configuration-manager 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/ */

    

abdeslam / configuration-manager example snippets




// config.php

return [
    'database' => [
        'driver' => 'mysql',
        'username' => 'user',
        'password' => '1234',
    ],
    'debug' => true
];



use Abdeslam\ConfigurationManager\ConfigurationManager;
use Abdeslam\ConfigurationManager\Loaders\PHPConfigurationLoader;

')->load();

echo $manager->get('database.driver'); // output: 'mysql'



use Abdeslam\ConfigurationManager\ConfigurationManager;
use Abdeslam\ConfigurationManager\Loaders\PHPConfigurationLoader;

>addLoader($loader, '/path/to/config.php', '/path/to/config2.php')->load();

// rest of the code



use Abdeslam\ConfigurationManager\ConfigurationManager;
use Abdeslam\ConfigurationManager\Loaders\PHPConfigurationLoader;

')->load();

echo $manager->get('database.driver'); // output: 'mysql'
$manager->get('debug'); // returns: (bool) true
$manager->get('non_existing_key'); // throws an exception
$manager->has('debug')); // returns: (bool) true
$manager->has('non_existing_key'); // returns: (bool) false
$manager->remove('debug'); // removes 'debug' item from the configuration items array

$manager->all(); // returns: the array of all the configuration items
$manager->getLoaders(); // returns: the array of all the registered loaders
$manager->getLoader(PHPConfigurationLoader::class); // returns: the loader instance
$manager->hasLoader(PHPConfigurationLoader::class); // returns: true
$manager->getLoadedFiles(); // returns: an array of all the loaded files
$manager->set('api_key' => 'some_key'); // adds an item with the key 'api_key' and the value 'some_key' to the configuration items
$manager->merge(['verbose' => true]); // merges the configuration items array with the array given as an argument
$manager->merge($anotherManagerInstance); // merges the array of items of the original manager with the array of items of the managers supplies as an argument
$manager->reset(); // resets the object to the initial state



use Abdeslam\ConfigurationManager\ConfigurationManagerFactory;

config.php',
  '/path/to/config2.php'
);
$manager->load();

// rest of the code



// config.php
return [
  'address' => [
    'country' => 'Algeria',
    'city' => [
      'name' => 'Algiers',
      'postal_code' => '16000'
    ]
  ]
];

// client code
$manager->get('address.city.name'); // returns: 'Algiers'
$manager->setKeySeparator('_'); // changes the key separator to '_'
$manager->get('address_country'); // returns: 'Algeria'
$manager->getKeySeparator(); // returns: '_'



// CustomConfigurationLoader.php

use Abdeslam\ConfigurationManager\Contracts\ConfigurationLoaderInterface;

class CustomConfigurationLoader implements ConfigurationLoaderInterface
{
  /**
   * @inheritDoc
   */
  public function (string ...$filepaths): array
  {
    return ['hello' => 'world'];
  }
}

// client code
$manager->addLoader(new CustomLoader(), '/path/to/configuration/file');
$manager->load();
$manager->get('hello'); // returns: 'world'

// using the factory
ConfigurationManagerFactory::addConfigurationLoader('custom_loader', CustomConfigurationLoader::class);
$manager = ConfigurationManagerFactory::create('custom_loader', '/path/to/configuration/file');
$manager->load();
$manager->get('hello'); // returns: 'world'

// overriding Factory loaders
ConfigurationManagerFactory::addConfigurationLoader('php', CustomConfigurationLoader::class);
$manager = ConfigurationManagerFactory::create('php', '/path/to/config.php');
$manager->load();
$manager->get('hello'); // returns: 'world'


php composer.phar