\Config_Lite

Config_Lite Class

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

Summary

Methods
Properties
Constants
read()
save()
sync()
setSingleTickDelimiter()
setDoubleTickDelimiter()
setFlags()
write()
toBool()
getString()
get()
getBool()
getSection()
hasSection()
has()
remove()
removeSection()
clear()
setString()
set()
setSection()
setFilename()
getFilename()
setLinebreak()
setProcessSections()
setQuoteStrings()
__toString()
offsetSet()
offsetExists()
offsetUnset()
offsetGet()
getIterator()
count()
serialize()
unserialize()
__construct()
No public properties found
No constants found
isBool()
normalizeValue()
buildOutputString()
$sections
$filename
$linebreak
$processSections
$quoteStrings
$flags
$delim
N/A
No private methods found
$_booleans
N/A

Properties

$sections

$sections : array

sections, holds the config sections

Type

array

$filename

$filename : string

filename

Type

string

$linebreak

$linebreak : string

line-break chars, default *x: "\n", windows: "\r\n"

Type

string

$processSections

$processSections : boolean

parseSections - if true, sections will be processed

Type

boolean

$quoteStrings

$quoteStrings : boolean

quote Strings - if true, writes ini files with doublequoted strings

Type

boolean

$flags

$flags : integer

flags for file-put-contents

Type

integer

$delim

$delim : string

string delimiter

Type

string

$_booleans

$_booleans : array

_booleans - alias of bool in a representable Configuration String Format

Type

array

Methods

read()

read(string  $filename = null, integer  $mode = INI_SCANNER_NORMAL) : \Config_Lite

the read method parses the optional given filename or already setted filename.

this method uses the native PHP function parse_ini_file behind the scenes.

Parameters

string $filename

Filename

integer $mode

INI_SCANNER_NORMAL | INI_SCANNER_RAW

Throws

\Config_Lite_Exception_Runtime

when file not found

\Config_Lite_Exception_Runtime

when file is not readable

\Config_Lite_Exception_Runtime

when parse ini file failed

Returns

\Config_Lite

save()

save() : boolean

save the object to the filename (active record style)

Returns

boolean

sync()

sync() : \Config_Lite

sync the file to the object

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.

Throws

\Config_Lite_Exception_Runtime

when file is not set, write or readable

Returns

\Config_Lite

setSingleTickDelimiter()

setSingleTickDelimiter() : \Config_Lite

set string delimiter to single tick (')

Returns

\Config_Lite

setDoubleTickDelimiter()

setDoubleTickDelimiter() : \Config_Lite

set string delimiter to double tick (")

Returns

\Config_Lite

setFlags()

setFlags(integer  $flags = LOCK_EX) : \Config_Lite

Set Flags ( FILE_USE_INCLUDE_PATH | FILE_APPEND | LOCK_EX ) for file-put-contents

Parameters

integer $flags

any or binary combined

Returns

\Config_Lite

write()

write(string  $filename, array  $sectionsarray, integer  $flags = null) : boolean

generic write ini config file, to save use `save'.

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.

Parameters

string $filename

filename

array $sectionsarray

array with sections

integer $flags

for file-put-contents

Throws

\Config_Lite_Exception_Runtime

when file is not writeable

\Config_Lite_Exception_Runtime

when write failed

Returns

boolean

toBool()

toBool(string  $value) : string

converts string to a representable Config Bool Format

Parameters

string $value

value

Throws

\Config_Lite_Exception_UnexpectedValue

when format is unknown

Returns

string

getString()

getString(string  $sec, string  $key, mixed  $default = null) : string

returns a stripslashed string

Parameters

string $sec

Section

string $key

Key

mixed $default

default return value

Throws

\Config_Lite_Exception_Runtime

when config is empty and no default value is given

\Config_Lite_Exception_UnexpectedValue

key not found and no default value is given

Returns

string

get()

get(string  $sec = null, string  $key = null, mixed  $default = null) : mixed

get an option by section, a global option or all sections and options

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 callget' 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.

Parameters

string $sec

Section|null - null to get global option

string $key

Key

mixed $default

return default value if is $key is not set

Throws

\Config_Lite_Exception

when config is empty and no default value is given

\Config_Lite_Exception_UnexpectedValue

key not found and no default value is given

Returns

mixed

getBool()

getBool(string  $sec, string  $key, boolean  $default = null) : boolean

returns a boolean for strict equality comparison

returns "on", "yes", 1, "true" as TRUE and no given value or "off", "no", 0, "false" as FALSE

Parameters

string $sec

Section

string $key

Key

boolean $default

return default value if is $key is not set

Throws

\Config_Lite_Exception_Runtime

when the configuration is empty and no default value is given

\Config_Lite_Exception_InvalidArgument

when is not a boolean and no default array is given

\Config_Lite_Exception_UnexpectedValue

when key not found and no default array is given

Returns

boolean

getSection()

getSection(string  $sec, array  $default = null) : array

returns an array of options of the given section

Parameters

string $sec

Section

array $default

return default array if $sec is not set

Throws

\Config_Lite_Exception_Runtime

when config is empty and no default array is given

\Config_Lite_Exception_UnexpectedValue

when key not found and no default array is given

Returns

array

hasSection()

hasSection(string  $sec) : boolean

returns true if the given section exists, otherwise false

Parameters

string $sec

Section

Returns

boolean

has()

has(string  $sec, string  $key = null) : boolean

tests if a section or an option of a section exists

Parameters

string $sec

Section

string $key

Key

Returns

boolean

remove()

remove(string  $sec, string  $key = null) : void

remove a section or an option of a section

Parameters

string $sec

Section

string $key

Key

Throws

\Config_Lite_Exception_UnexpectedValue

when given Section not exists

removeSection()

removeSection(string  $sec) : void

remove section by name

Parameters

string $sec

Section

Throws

\Config_Lite_Exception_UnexpectedValue

when given Section not exists

clear()

clear() : \Config_Lite

removes all sections and global options

Returns

\Config_Lite

setString()

setString(string  $sec, string  $key, mixed  $value = null) : \Config_Lite

like set, but adds slashes to the value

creates new section if necessary and overrides existing keys.

Parameters

string $sec

Section

string $key

Key

mixed $value

Value

Throws

\Config_Lite_Exception_InvalidArgument

when given key is an array

Returns

\Config_Lite

set()

set(string  $sec, string  $key, mixed  $value = null) : \Config_Lite

to add key/value pairs

creates new section if necessary and overrides existing keys. To set a global, "sectionless" value, call set with null as section.

Parameters

string $sec

Section

string $key

Key

mixed $value

Value

Throws

\Config_Lite_Exception

when given key is an array

Returns

\Config_Lite

setSection()

setSection(string  $sec, array  $pairs) : \Config_Lite

set a given array with key/value pairs to a section, creates a new section if necessary.

Parameters

string $sec

Section

array $pairs

Keys and Values as Array ('key' => 'value')

Throws

\Config_Lite_Exception_InvalidArgument

array $pairs expected

Returns

\Config_Lite

setFilename()

setFilename(string  $filename) : \Config_Lite

filename to read or save

the full filename with suffix, ie. `[PATH/].ini'. you can also set the filename as parameter to the constructor.

Parameters

string $filename

Filename

Returns

\Config_Lite

getFilename()

getFilename() : string

returns the current filename

Returns

string

setLinebreak()

setLinebreak(string  $linebreakchars) : \Config_Lite

set the line break (newline) chars

line-break defaults to Unix Newline "\n", set to support other linebreaks, eg. windows user textfiles "\r\n"

Parameters

string $linebreakchars

chars

Returns

\Config_Lite

setProcessSections()

setProcessSections(boolean  $processSections) : \Config_Lite

Sets whether or not sections should be processed

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.

Parameters

boolean $processSections
  • if true, sections will be processed

Returns

\Config_Lite

setQuoteStrings()

setQuoteStrings(boolean  $quoteStrings) : \Config_Lite

Sets whether or not to doubleQuote

If true, everything but bool and numeric values get doublequoted.

Parameters

boolean $quoteStrings
  • if true, Strings get doubleQuoted

Returns

\Config_Lite

__toString()

__toString() : string

text presentation of the config object

since a empty config is valid, it would return a empty string in that case.

Throws

\Config_Lite_Exception_Runtime

Returns

string

offsetSet()

offsetSet(string  $offset, mixed  $value) : void

implemented for interface ArrayAccess

Parameters

string $offset

section, implemented by ArrayAccess

mixed $value

KVP, implemented by ArrayAccess

offsetExists()

offsetExists(string  $offset) : boolean

implemented for interface ArrayAccess

Parameters

string $offset
  • section, implemented by ArrayAccess

Returns

boolean

offsetUnset()

offsetUnset(string  $offset) : void

implemented for interface ArrayAccess

Parameters

string $offset
  • section, implemented by ArrayAccess

offsetGet()

offsetGet(string  $offset) : mixed

implemented for interface ArrayAccess

Parameters

string $offset
  • section, implemented by ArrayAccess

Returns

mixed

getIterator()

getIterator() : \Iterator

implemented for interface IteratorAggregate

Returns

\Iterator

count()

count() : integer

implemented for interface Countable

Returns

integer

serialize()

serialize() : integer

implemented for interface Serializable

Returns

integer

unserialize()

unserialize(string  $serializedData) : void

implemented for interface Serializable

Parameters

string $serializedData

for instance

__construct()

__construct(string  $filename = null, integer  $flags = null, integer  $mode) 

takes an optional filename, if the file exists, also reads it.

the save' andread' methods relies on a setted filename, but you can also use `setFilename' to set the filename.

Parameters

string $filename
  • "INI Style" Text Config File
integer $flags
  • flags for file_put_contents, eg. FILE_APPEND
integer $mode
  • set scannermode

isBool()

isBool(string  $value) : boolean

detect Type "bool" by String Value to keep those "untouched"

Parameters

string $value

value

Returns

boolean

normalizeValue()

normalizeValue(string  $value) : string

normalize a Value by determining the Type

Parameters

string $value

value

Returns

string

buildOutputString()

buildOutputString(array  $sectionsarray) : string

Generated the output of the ini file, suitable for echo'ing or writing back to the ini file.

Parameters

array $sectionsarray

array of ini data

Returns

string