Skip to content

Configuration Class

Ovidiu edited this page Apr 16, 2018 · 39 revisions

The Config Class

The Config class is a Key-Value-Type store.

That is a key-value store, but inside each value caries its own associated type.

Without using this feature, each time you request a field value from a database you will get a string - and you have to manually convert the value to the assumed type. Maybe the assumed type it is the correct one, but you could also be wrong.

Basic Feature

In order to avoid assuming the correct type, this function converts the stored string value to the suitable type for the variable - that type is stored internally in the database.

Extended Feature

Another important feature is that object data is automatically serialized into a specific form in the database, and it is automatically unserialized when you are accessing the key.

A key record is characterized by the following properties:

Schema Sometimes called namespace
Summary The actual comment - often times used as a label in admin UI
Description ...
Type the type of the stored value; you can pas one of the Config::TYPE_<type> constants.
Default the value to be restored by Config::restoreDefault()
Config(schema:String)

Reading Values

Config::get(name : String, ?noThrow : Bool, ?defaultValue : Dynamic) : Dynamic
[static]

// Config::get(string name[, boolean noThrow = false]) [static, throws]
// Config::get(string name, true) [static, nothrow]
// Config::get(string name, true, variant defaultValue) [static, nothrow]

Config::getBoolean(name : String) : Bool [static]
Config::getInteger(name : String) : Int [static]
Config::getString(name : String) : String [static]
Config::getFloat(name : String) : Float [static]
Config::getObject(name : String, type : String) : Object [static]
name the name of the key you need to read
noThrow [optional] default is false; if set on true, the defaultValue is returned and exception is not thrown when the key is not found
defaultValue [optional] if defaultValue is not set, then null is returned when the key is not found, if noThrow is true
returns the value of the key - a variant that can have one of the following supported types: boolean, float, integer, string, object, array (for both list and dictionary)

For noThrow parameter is recommended to use the following class constants for better readability:

Config::NOT_FOUND_THROW        =  false
Config::NOT_FOUND_DO_NOT_THROW =  true

IMPORTANT: An identifier name can be composed of letters (both uppercase and lowercase letters), digits and underscore '_' only. The first letter of identifier should be either a letter or an underscore. - This shall be done for both Config::set() and Config::get() methods.

To use full namespace you must specify schema (See Extended Feature).

Writing Values / Creating New Keys

New keys are automatically created when you are trying to set the value for a key that doesn't exists already. You don't have to worry about this issue. No existence check or create calls are necessary.

http://ro1.php.net/manual/en/class.serializable.php

Old version:

Config::set(name : String, type : KeyType, ?value : Dynamic) : Void [static]

New version:

Config::set(name : String, value : Dynamic, ?type : KeyType) : Void [static]

If the second parameter is an instance of KeyType, the old version is called. Otherwise, if the third parameter is an instance of KeyType, the new version is called.

class KeyType {
    private $type;
    public function __construct($type) {
        $this->type = $type;
    }
    public function __toString() {
        return $type;
    }
}

Converts the value to the specified type and stores it into the database (using the representation format specified by type).

name the name of the key you want to write (or create)
value value to be stored into the database; Usage of Config::TYPE_<type> values is not allowed
type [default] If type is omitted, the current variable type is used for primitive types - requested with gettype(). For objects, the 'json' serialization method is the default.
in case that value has primitive types: a long standard type name, like: 'boolean', 'float', 'integer', 'string' ;
in case that value is an object or an array: the serialization method must be specified, like: 'json' (default), 'base64', 'hexdump' (not implemented), 'serialize' (not recommended, use 'json' instead), 'yaml' (proposal), 'xml' (proposal)
IMPORTANT: If you want to store a base64 encoded string and use it as is, please use 'string' as type
returns null - Nothing to return. If something is wrong and exception is thrown.

For the value of type you can pas one of the Config::TYPE_<type> constants.

Config::TYPE_BOOLEAN     = 'boolean'
Config::TYPE_FLOAT       = 'float'
Config::TYPE_INTEGER     = 'integer'
Config::TYPE_STRING      = 'string'
Config::TYPE_OBJECT_JSON = 'json'

The following methods are preferred especially when interfacing with a strong-typed language.

Config::setBoolean(name : String, value : Bool) : Void [static]
Config::setInteger(name : String, value : Int) : Void [static]
Config::setString(name : String, value : String) : Void [static]
Config::setFloat(name : String, value : Float) : Void [static]
Config::setObject(name : String, value : Object) : Void [static]

Multiple Writes at Once

Set mupliple keys at once ... TODO: single insert.

Export as String

To generate an input for the well known parse_ini_string() function, or for Smarty::configLoad().

Config::export(schema : String, sections : Array<String>) : String [static]

Restore Default Key Values

Config::restoreDefault(name : String) : Void [static]

Caching

The Config::get() method result is cached internally.

Clone this wiki locally