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
Afficher le fichier Open project: mescon/Muximux Class Usage Examples

Protected Properties

Свойство 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

Méthodes publiques

Méthode 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'.

Méthodes protégées

Méthode 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 méthode

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 méthode

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

buildOutputString() protected méthode

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
Résultat string

clear() public méthode

removes all sections and global options
public clear ( ) : Config_Lite
Résultat Config_Lite

count() public méthode

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

get() public méthode

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
Résultat mixed

getBool() public méthode

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
Résultat boolean

getFilename() public méthode

returns the current filename
public getFilename ( ) : string
Résultat string

getIterator() public méthode

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

getSection() public méthode

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
Résultat array

getString() public méthode

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
Résultat string

has() public méthode

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
Résultat boolean

hasSection() public méthode

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

isBool() protected méthode

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

normalizeValue() protected méthode

normalize a Value by determining the Type
protected normalizeValue ( string $value ) : string
$value string value
Résultat string

offsetExists() public méthode

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

offsetGet() public méthode

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

offsetSet() public méthode

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

offsetUnset() public méthode

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

read() public méthode

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
Résultat Config_Lite

remove() public méthode

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

removeSection() public méthode

remove section by name
public removeSection ( string $sec ) : void
$sec string Section
Résultat void

save() public méthode

save the object to the filename (active record style)
public save ( ) : boolean
Résultat boolean

serialize() public méthode

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

set() public méthode

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
Résultat Config_Lite

setDoubleTickDelimiter() public méthode

set string delimiter to double tick (")
public setDoubleTickDelimiter ( ) : Config_Lite
Résultat Config_Lite

setFilename() public méthode

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
Résultat Config_Lite

setFlags() public méthode

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
Résultat Config_Lite

setLinebreak() public méthode

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
Résultat Config_Lite

setProcessSections() public méthode

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
Résultat Config_Lite

setQuoteStrings() public méthode

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

setSection() public méthode

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')
Résultat Config_Lite

setSingleTickDelimiter() public méthode

set string delimiter to single tick (')
public setSingleTickDelimiter ( ) : Config_Lite
Résultat Config_Lite

setString() public méthode

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
Résultat Config_Lite

sync() public méthode

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
Résultat Config_Lite

toBool() public méthode

converts string to a representable Config Bool Format
public toBool ( string $value ) : string
$value string value
Résultat string

unserialize() public méthode

implemented for interface Serializable
public unserialize ( string $serializedData ) : void
$serializedData string for instance
Résultat void

write() public méthode

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
Résultat boolean

Property Details

$delim protected_oe property

string delimiter
protected string $delim
Résultat string

$filename protected_oe property

filename
protected string $filename
Résultat string

$flags protected_oe property

flags for file-put-contents
protected int $flags
Résultat integer

$linebreak protected_oe property

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

$processSections protected_oe property

parseSections - if true, sections will be processed
protected bool $processSections
Résultat boolean

$quoteStrings protected_oe property

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

$sections protected_oe property

sections, holds the config sections
protected array $sections
Résultat array