PHP code example of ckr / config

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

    

ckr / config example snippets


$myConfig = [
  'root' => [
    'specific' => ['key' => 'val'],
  ],
];

// plain php is a bit ugly...
$theSpecific = isset($myConfig['root']['specific']['key']) ?
     $myConfig['root']['specific']['key'] : null;

$theGeneral = isset($myConfig['root']['key']) ?
     $myConfig['root']['key'] : false;

// ... but with a `Config` it looks nicer:
$config = new Ckr\Config\Config($myConfig);
$theSpecific = $config->get('root/specific/key');
$theGeneral = $config->get('root/key', false);

// valid code in PHP7
$theSpecific= $myConfig['root']['specific']['key'] ?? null;
$theGeneral = $myConfig['root']['key'] ?? false;

$myConfig = [
  'mode' => 'production',
  'logging' => [
    'factory' => 'Your\\Logging\\Factory',
    'loggers' => [
      [
        'type' => 'file',
        'path' => '/path/to/logs',
        'level' => 'warn',
      ],
      [
        'type' => 'email',
        'addr' => '[email protected]',
        'level' => 'critical',
      ]
    ],
  ]
];

$mode = $c->get('mode', 'dev');

$loggingFactoryClass = $c->get('logging/factory');

$loggingConfig = $c->child('logging'); /* @var $loggingConfig Config */

// we can now provide only the relevant data to thr logging factory
$myLoggingFactory = new $loggingFactoryClass;
$logger = $myLoggingFactory->create($loggingConfig);

// the logging factory "sees" only the data of the "logging" child array
public function create(Config $cfg)
{
  $loggers = $cfg->get('loggers'); // $loggers is an array
  foreach ($loggers as $logger) { /* ... */ }
  // ...
}

$newConfig = $c->set('logging/factory', 'AnotherFactoryClass');