Class Cfg

Description

A class for accessing application-level configuration information.

This class is a wrapper around an array of configuration information. It obtains the array by calling Support_Resources::config_data(). The default implementation simply uses a global array named APP_CONFIG which it obtains by including the file config.php (which can be located anywhere in the include path). In addition to providing some basic conveniences over direct array access, part of this class's purpose is to provide a simple layer of abstraction around the storage of the configuration values. More advanced applications could replace the provider in Resources with one that retrieves configuration information from an XML file (or other formatted text file) or a database (or even some combination of the two.

This class allows configuration properties to be divided into sections. Sections are separated from the property name using a forward slash character (/). Sections may be nested up to arbitrary depths, but an enclosing section is not required. The forward slash character can therefore not be used in any property name. Sections are implemented in the global array as a nested array.

An example config file (using the default provider):

  1.    $APP_CONFIG['ApplicationName''My App';
  2.    $APP_CONFIG['SessionKey']      'MYAPPSESS';
  3.    $APP_CONFIG['SessionExpires']  120;
  4.  
  5.    $APP_CONFIG['Database']        array(
  6.                'Host'             => 'localhost',
  7.                'User'             => 'fred',
  8.                'Password'         => 'secret');
  9.  
  10.    $APP_CONFIG['RemoteFTP']       array(
  11.                'Host'             => '192.168.0.5',
  12.                'User'             => 'www',
  13.                'Password',        => '*****');

Of course, the config file is just a PHP file, so any valid PHP code can be used to build the structure.

Examples, of accessing the configuration information:

  1.    Cfg::get('ApplicationName');           // => 'My App'
  2.    Cfg::get('MOTD');                      // => NULL
  3.    Cfg::get('BackgroundColor'0xFFFFFF)// 0xFFFFFF
  4.    Cfg::get('SessionExpires');            // => 120
  5.    Cfg::get('Database/Host');             // => 'localhost'
  6.    Cfg::get('RemoteFTP/Host');            // => '192.168.0.5'
  7.  
  8.    Cfg::get_required('Database/User');     // => 'fred'
  9.    Cfg::get_required('Database/Schema');   // throws MissingRequiredConfigurationError

Located in /support/lib/Cfg.php (line 85)


	
			
Variable Summary
 static mixed $data
Method Summary
 static bool exists (string $key)
 static bool find_key (string $key, string &$value)
 static mixed get (string $key, [string $default = NULL])
 static mixed get_required (string $key)
Variables
static mixed $data = null (line 86)
  • access: protected
Methods
static exists (line 129)

Test for the existence of a key in the configuration.

  • access: public
bool exists (string $key)
  • string $key: The name of the key to test
static find_key (line 142)

Locate the named key in the configuration.

  • return: Returns true if the key was found, false otherwise
  • access: protected
bool find_key (string $key, string &$value)
  • string $key: The name of the key to find
  • string &$value: Output parameter for the key's value if found
static get (line 98)

Returns the value of the given key in the configuration. If they key does not exist, any default value provided is returned instead (or NULL if none is provided).

  • access: public
mixed get (string $key, [string $default = NULL])
  • string $key: The name of the key to retrieve
  • string $default: The default value to return if not key exists (optional)
static get_required (line 114)

Returns the value of the given key in the configuration. If they key does not exist, MissingRequiredConfigurationError is thrown instead.

  • access: public
mixed get_required (string $key)
  • string $key: The name of the key to retrieve

Documentation generated on Wed, 25 Apr 2012 09:46:41 -0700 by phpDocumentor 1.4.3