PHP code example of phossa / phossa-config

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

    

phossa / phossa-config example snippets


  // returns an 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'
  ];
  

  # debugging true|false, change to 'false' ON production server
  APP_DEBUG=true

  # App environment, change to 'prod' ON production server
  APP_ENV=dev

  # app root directory, default to current dir
  APP_ROOT=${__DIR__}

  # central configuration directory
  CONFIG_DIR=${APP_ROOT}/config
  

  // load environment
  (new Phossa\Config\Env\Environment())->load(__DIR__.'/.env');

  // create config object
  $config = new Phossa\Config\Config(
      getenv('CONFIG_DIR'), // loaded from .env file
      getenv('APP_ENV')     // loaded from .env file
  );

  // load all configs in one shot
  $conf_data = $config->get(null);
  

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

  $dir = $config->get('system.tmpdir');
  

  // create config object
  $config = new Phossa\Config\Config(
      dirname(__DIR__) . '/config',     // the config dir
      'staging/server2',                // config env
      'php',                            // file type
      new Phossa\Config\Cache\Cache(__DIR__ . '/cache') // cache location
  );

  // if cache exists, this will read all configs from the cache
  $conf_data = $config->get(null);

  // ...

  // write to cache
  $config->save();
  

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

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

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

  // now reference is not recognised
  $config = (new Config())->setReferencePattern('', '');
  

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