PHP code example of phossa2 / config

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

    

phossa2 / config example snippets


  use Phossa2\Config\Config;
  use Phossa2\Env\Environment;
  use Phossa2\Config\Loader\ConfigFileLoader;

  // load environment from '.env' file
  (new Environment())->load(__DIR__ . '/.env');

  // create config instance with the config file loader
  $config = new Config(
      new ConfigFileLoader(
          getenv('CONFIG_DIR'),
          getenv('APP_ENV')
      )
  );

  // object access of $config
  $db_config = $config->get('db');

  // array access of $config
  $config['db.user'] = 'www';
  

    // system.php
    return [
        'tmpdir' => '/usr/local/tmp',
        // ...
    ];
    

    // object acess of config
    $dir = $config->get('system.tmpdir');

    // array access of $config
    $dir = $config['system.tmpdir'];
    

  // group: system
  return [
      'tmpdir' => '/var/local/tmp',
      ...
  ];
  

  // group: cache
  return [
      // a local filesystem cache driver
      'local' => [
          'driver' => 'filesystem',
          'params' => [
              'root_dir'   => '${system.tmpdir}/cache', // use reference here
              'hash_level' => 2
          ]
      ],
      ...
  ];
  

  // now reference is something like '%{system.tmpdir}%'
  $config->setReferencePattern('%{', '}%');
  

  // test
  if (!isset($config['db.auth.user'])) {
      // set
      $config['db.auth.user'] = 'www';
  }
  

  // returns the db config array
  $db_config = $config->get('db');

  // returns a string
  $db_host = $config->get('db.auth.host');
  

  // db config file
  return [
      // array notation
      'auth' => [
          'host' => 'localhost',
          'port' => 3306
      ],

      // flat notation
      'auth.user' => 'dbuser'
  ];
  

      $config1 = new Config();
      $config2 = new Config();
      $delegator = new Delegator();

      // add some values
      $config1['db.user'] = '${system.user}';
      $config2['system.user'] = 'root';

      // reference unresolved in $config1
      var_dump($config1['db.user'] === '${system.user}'); // true

      // add both configs to the delegator
      $delegator->addConfig($config1);
      $delegator->addConfig($config2);

      // reference resolved thru the delegator
      var_dump($config1['db.user'] === 'root'); // true
      

    $dbUser = $delegator['db.user'];
    

    // configs
    $config1 = new Config();
    $config2 = new Config();

    // delegators
    $delegator1 = new Delegator();
    $delegator2 = new Delegator();

    // register $config1 with $delegator1
    $delegator1->addConfig($config1);

    // chaining
    $delegator2->addConfig($delegator1);
    $delegator2->addConfig($config2);

    // get from the chain
    $db = $delegator2->get('db');
  

    // disable writing to the $config
    $config->setWritable(false);
    

  config/
   |
   |___ production/
   |       |
   |       |___ host1/
   |       |      |___ db.php
   |       |      |___ redis.php
   |       |
   |       |___ db.php
   |
   |___ dev/
   |     |
   |     |___ redis.php
   |     |___ db.php
   |
   |___ db.php
   |___ redis.php
   |___ system.php