PHP Class Config_Lite

read and save ini text files. Config_Lite has the native PHP function `parse_ini_file' under the hood. The API is inspired by Python's ConfigParser. A "Config_Lite" file consists of "name = value" entries and sections, "[section]" followed by "name = value" entries
Author: Patrick C. Engel ([email protected])
Inheritance: implements ArrayAccess, implements IteratorAggregate, implements Countable, implements Serializable
Mostrar archivo Open project: mescon/Muximux Class Usage Examples

Protected Properties

Property Type Description
$delim string string delimiter
$filename string filename
$flags integer flags for file-put-contents
$linebreak string line-break chars, default *x: "\n", windows: "\r\n"
$processSections boolean parseSections - if true, sections will be processed
$quoteStrings boolean quote Strings - if true, writes ini files with doublequoted strings
$sections array sections, holds the config sections

Public Methods

Method Description
__construct ( string $filename = null, integer $flags = null, integer $mode ) takes an optional filename, if the file exists, also reads it.
__toString ( ) : string text presentation of the config object
clear ( ) : Config_Lite removes all sections and global options
count ( ) : integer implemented for interface Countable
get ( string $sec = null, string $key = null, mixed $default = null ) : mixed get an option by section, a global option or all sections and options
getBool ( string $sec, string $key, boolean $default = null ) : boolean returns a boolean for strict equality comparison
getFilename ( ) : string returns the current filename
getIterator ( ) : Iterator implemented for interface IteratorAggregate
getSection ( string $sec, array $default = null ) : array returns an array of options of the given section
getString ( string $sec, string $key, mixed $default = null ) : string returns a stripslashed string
has ( string $sec, string $key = null ) : boolean tests if a section or an option of a section exists
hasSection ( string $sec ) : boolean returns true if the given section exists, otherwise false
offsetExists ( string $offset ) : boolean implemented for interface ArrayAccess
offsetGet ( string $offset ) : mixed implemented for interface ArrayAccess
offsetSet ( string $offset, mixed $value ) : void implemented for interface ArrayAccess
offsetUnset ( string $offset ) : void implemented for interface ArrayAccess
read ( string $filename = null, integer $mode = INI_SCANNER_NORMAL ) : Config_Lite the read method parses the optional given filename or already setted filename.
remove ( string $sec, string $key = null ) : void remove a section or an option of a section
removeSection ( string $sec ) : void remove section by name
save ( ) : boolean save the object to the filename (active record style)
serialize ( ) : integer implemented for interface Serializable
set ( string $sec, string $key, mixed $value = null ) : Config_Lite to add key/value pairs
setDoubleTickDelimiter ( ) : Config_Lite set string delimiter to double tick (")
setFilename ( string $filename ) : Config_Lite filename to read or save
setFlags ( integer $flags = LOCK_EX ) : Config_Lite Set Flags ( FILE_USE_INCLUDE_PATH | FILE_APPEND | LOCK_EX ) for file-put-contents
setLinebreak ( string $linebreakchars ) : Config_Lite set the line break (newline) chars
setProcessSections ( boolean $processSections ) : Config_Lite Sets whether or not sections should be processed
setQuoteStrings ( boolean $quoteStrings ) : Config_Lite Sets whether or not to doubleQuote
setSection ( string $sec, array $pairs ) : Config_Lite set a given array with key/value pairs to a section, creates a new section if necessary.
setSingleTickDelimiter ( ) : Config_Lite set string delimiter to single tick (')
setString ( string $sec, string $key, mixed $value = null ) : Config_Lite like set, but adds slashes to the value
sync ( ) : Config_Lite sync the file to the object
toBool ( string $value ) : string converts string to a representable Config Bool Format
unserialize ( string $serializedData ) : void implemented for interface Serializable
write ( string $filename, array $sectionsarray, integer $flags = null ) : boolean generic write ini config file, to save use `save'.

Protected Methods

Method Description
buildOutputString ( array $sectionsarray ) : string Generated the output of the ini file, suitable for echo'ing or writing back to the ini file.
isBool ( string $value ) : boolean detect Type "bool" by String Value to keep those "untouched"
normalizeValue ( string $value ) : string normalize a Value by determining the Type

Method Details

__construct() public method

the save' and read' methods relies on a setted filename, but you can also use `setFilename' to set the filename.
public __construct ( string $filename = null, integer $flags = null, integer $mode )
$filename string - "INI Style" Text Config File
$flags integer - flags for file_put_contents, eg. FILE_APPEND
$mode integer - set scannermode

__toString() public method

since a empty config is valid, it would return a empty string in that case.
public __toString ( ) : string
return string

buildOutputString() protected method

Generated the output of the ini file, suitable for echo'ing or writing back to the ini file.
protected buildOutputString ( array $sectionsarray ) : string
$sectionsarray array array of ini data
return string

clear() public method

removes all sections and global options
public clear ( ) : Config_Lite
return Config_Lite

count() public method

implemented for interface Countable
See also: http://php.net.countable
public count ( ) : integer
return integer

get() public method

to get an option by section, call get with a section and the option. To get a global option call get' with null as section. Just call get' without any parameters to get all sections and options. The third parameter is an optional default value to return, if the option is not set, this is practical when dealing with editable files, to keep an application stable with default settings.
public get ( string $sec = null, string $key = null, mixed $default = null ) : mixed
$sec string Section|null - null to get global option
$key string Key
$default mixed return default value if is $key is not set
return mixed

getBool() public method

returns "on", "yes", 1, "true" as TRUE and no given value or "off", "no", 0, "false" as FALSE
public getBool ( string $sec, string $key, boolean $default = null ) : boolean
$sec string Section
$key string Key
$default boolean return default value if is $key is not set
return boolean

getFilename() public method

returns the current filename
public getFilename ( ) : string
return string

getIterator() public method

implemented for interface IteratorAggregate
See also: http://www.php.net/~helly/php/ext/spl/interfaceIterator.html
public getIterator ( ) : Iterator
return Iterator

getSection() public method

returns an array of options of the given section
public getSection ( string $sec, array $default = null ) : array
$sec string Section
$default array return default array if $sec is not set
return array

getString() public method

returns a stripslashed string
public getString ( string $sec, string $key, mixed $default = null ) : string
$sec string Section
$key string Key
$default mixed default return value
return string

has() public method

tests if a section or an option of a section exists
public has ( string $sec, string $key = null ) : boolean
$sec string Section
$key string Key
return boolean

hasSection() public method

returns true if the given section exists, otherwise false
public hasSection ( string $sec ) : boolean
$sec string Section
return boolean

isBool() protected method

detect Type "bool" by String Value to keep those "untouched"
protected isBool ( string $value ) : boolean
$value string value
return boolean

normalizeValue() protected method

normalize a Value by determining the Type
protected normalizeValue ( string $value ) : string
$value string value
return string

offsetExists() public method

implemented for interface ArrayAccess
public offsetExists ( string $offset ) : boolean
$offset string - section, implemented by ArrayAccess
return boolean

offsetGet() public method

implemented for interface ArrayAccess
public offsetGet ( string $offset ) : mixed
$offset string - section, implemented by ArrayAccess
return mixed

offsetSet() public method

implemented for interface ArrayAccess
public offsetSet ( string $offset, mixed $value ) : void
$offset string section, implemented by ArrayAccess
$value mixed KVP, implemented by ArrayAccess
return void

offsetUnset() public method

implemented for interface ArrayAccess
public offsetUnset ( string $offset ) : void
$offset string - section, implemented by ArrayAccess
return void

read() public method

this method uses the native PHP function parse_ini_file behind the scenes.
public read ( string $filename = null, integer $mode = INI_SCANNER_NORMAL ) : Config_Lite
$filename string Filename
$mode integer INI_SCANNER_NORMAL | INI_SCANNER_RAW
return Config_Lite

remove() public method

remove a section or an option of a section
public remove ( string $sec, string $key = null ) : void
$sec string Section
$key string Key
return void

removeSection() public method

remove section by name
public removeSection ( string $sec ) : void
$sec string Section
return void

save() public method

save the object to the filename (active record style)
public save ( ) : boolean
return boolean

serialize() public method

implemented for interface Serializable
See also: http://php.net/manual/en/class.serializable.php
public serialize ( ) : integer
return integer

set() public method

creates new section if necessary and overrides existing keys. To set a global, "sectionless" value, call set with null as section.
public set ( string $sec, string $key, mixed $value = null ) : Config_Lite
$sec string Section
$key string Key
$value mixed Value
return Config_Lite

setDoubleTickDelimiter() public method

set string delimiter to double tick (")
public setDoubleTickDelimiter ( ) : Config_Lite
return Config_Lite

setFilename() public method

the full filename with suffix, ie. `[PATH/].ini'. you can also set the filename as parameter to the constructor.
public setFilename ( string $filename ) : Config_Lite
$filename string Filename
return Config_Lite

setFlags() public method

Set Flags ( FILE_USE_INCLUDE_PATH | FILE_APPEND | LOCK_EX ) for file-put-contents
public setFlags ( integer $flags = LOCK_EX ) : Config_Lite
$flags integer any or binary combined
return Config_Lite

setLinebreak() public method

line-break defaults to Unix Newline "\n", set to support other linebreaks, eg. windows user textfiles "\r\n"
public setLinebreak ( string $linebreakchars ) : Config_Lite
$linebreakchars string chars
return Config_Lite

setProcessSections() public method

If true, values for each section will be placed into a sub-array for the section. If false, all values will be placed in the global scope.
public setProcessSections ( boolean $processSections ) : Config_Lite
$processSections boolean - if true, sections will be processed
return Config_Lite

setQuoteStrings() public method

If true, everything but bool and numeric values get doublequoted.
public setQuoteStrings ( boolean $quoteStrings ) : Config_Lite
$quoteStrings boolean - if true, Strings get doubleQuoted
return Config_Lite

setSection() public method

set a given array with key/value pairs to a section, creates a new section if necessary.
public setSection ( string $sec, array $pairs ) : Config_Lite
$sec string Section
$pairs array Keys and Values as Array ('key' => 'value')
return Config_Lite

setSingleTickDelimiter() public method

set string delimiter to single tick (')
public setSingleTickDelimiter ( ) : Config_Lite
return Config_Lite

setString() public method

creates new section if necessary and overrides existing keys.
public setString ( string $sec, string $key, mixed $value = null ) : Config_Lite
$sec string Section
$key string Key
$value mixed Value
return Config_Lite

sync() public method

like `save', but after written the data, reads the data back into the object. This method is not for the average use-case, ie. for testing.
public sync ( ) : Config_Lite
return Config_Lite

toBool() public method

converts string to a representable Config Bool Format
public toBool ( string $value ) : string
$value string value
return string

unserialize() public method

implemented for interface Serializable
public unserialize ( string $serializedData ) : void
$serializedData string for instance
return void

write() public method

writes the global options and sections with normalized Values, that means "bool" values to human readable representation, doublequotes strings and numeric values without any quotes. prepends a php exit if suffix is php, it is valid to write an empty Config file, this method is used by save and is public for explicit usage, eg. if you do not want to hold the whole configuration in the object.
public write ( string $filename, array $sectionsarray, integer $flags = null ) : boolean
$filename string filename
$sectionsarray array array with sections
$flags integer for file-put-contents
return boolean

Property Details

$delim protected_oe property

string delimiter
protected string $delim
return string

$filename protected_oe property

filename
protected string $filename
return string

$flags protected_oe property

flags for file-put-contents
protected int $flags
return integer

$linebreak protected_oe property

line-break chars, default *x: "\n", windows: "\r\n"
protected string $linebreak
return string

$processSections protected_oe property

parseSections - if true, sections will be processed
protected bool $processSections
return boolean

$quoteStrings protected_oe property

quote Strings - if true, writes ini files with doublequoted strings
protected bool $quoteStrings
return boolean

$sections protected_oe property

sections, holds the config sections
protected array $sections
return array