PHP code example of freezemage0 / config

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

    

freezemage0 / config example snippets





use Freezemage\Config\ConfigFactory;


$factory = new ConfigFactory();
$config = $factory->create($_SERVER['DOCUMENT_ROOT'] . '/config.json');




use Freezemage\Config\ConfigFactory;
use Freezemage\Config\Importer\ImporterInterface;


class MyImporter implements ImporterInterface {
    private $filename;
    
    public function import(): array {
        //import operation here
    }
    
    public function setFilename(string $filename): void {
        $this->filename = $filename;
    }
    
    public function getFilename(): ?string {
        return $this->filename;
    }
}

$factory = new ConfigFactory();
$factory->registerImporter('my-file-extension', new MyImporter());




use Freezemage\Config\ConfigFactory;


$factory = new ConfigFactory();
$config = $factory->create($_SERVER['DOCUMENT_ROOT'] . '/config.json');

$database = $config->get('database'); // Loads whole config and returns value
$allValues = $config->getConfig(); // Uses previously loaded values.



array(
    'database' => array(
        'username' => 'user',
        'password' => 'passwd'
    )
);




use Freezemage\Config\ConfigFactory;


$factory = new ConfigFactory();
$config = $factory->create($_SERVER['DOCUMENT_ROOT'] . '/config.json');

$username = $config->get('database.username');
echo $username; // prints "user"



use Freezemage\Config\ConfigFactory;

$factory = new ConfigFactory();
$factory->getFeatureManager()->getKeyChaining()->disable();

$config = $factory->create('config.json'); // key chaining will be disabled for that instance.




use Freezemage\Config\Importer\JsonImporter;
use Freezemage\Config\Exporter\JsonExporter;
use Freezemage\Config\ImmutableConfig;

$importer = new JsonImporter();
$exporter = new JsonExporter();

$importer->setFilename($_SERVER['DOCUMENT_ROOT'] . '/connection.json'); // Implying that file exists. 

$config = new ImmutableConfig($importer, $exporter);
$config = $config->set('database', array('username' => 'user')); // New ImmutableConfig is created.
$config->set('database.password', 'passwd')->save(); // Key chaining is supported for setter as well.





use \Freezemage\Config\ConfigFactory;


$factory = new ConfigFactory();
$config = $factory->create('config.json');
$database = $config->extractSection('database');
$database->getExporter()->setFilename('database.json');
$database->save(); // the content of database section will be saved into a separate 'database.json' file.




use Freezemage\Config\ConfigFactory;
use Freezemage\Config\Exporter\PhpExporter;


$factory = new ConfigFactory();

$configName = $_SERVER['DOCUMENT_ROOT'] . '/config.json';
$config = $factory->create($configName);

$exporter = new PhpExporter();
$exporter->setFilename($configName);

$config->setExporter($exporter);
$config->save();