\AlphredIni

Extends INI parsing and writing for PHP

This class allows to read and write ini files. It translates ini files into associative PHP arrays and translates PHP arrays into ini files. It supports sectioning as well as a kind of subsectioning.

Colons (:) are considered separators for sub-sections and are represented as multi-dimensional arrays. For instance, the following array:

$array = [
 'Alphred' => [
 'log_level' => 'DEBUG',
    'log_size' => 10000,
    'plugins'  => [ 'get_password' => 'my_new_function' ]
]];

will be represented as

[Alphred]
log_level = DEBUG
log_size = 10000

[Alphred:plugins]
get_password = my_new_function

If you are concerned, then make sure that \r\n is removed from the array values before they move into the INI file, as they may break them.

All of these are static functions. So, to use:

$ini_file = Alphred\Ini::read_ini( '/path/to/workflow.ini' );

That's it.

To write an ini file, just do:

Alphred\Ini::write_ini( $config_array, '/path/to/workflow.ini' );

Summary

Methods
Properties
Constants
read_ini()
write_ini()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
separate_non_sections()
print_section()
collapse_sections()
flatten_array()
step_back()
parse_section()
nest_array()
is_assoc()
No private properties found
N/A

Methods

read_ini()

read_ini(string  $file, boolean  $exception = true) : array|boolean

Parses an INI

This is a slightly better INI parser in that will read a section title of 'title:subtitle' 'subtitle' as a subsection of the section 'title'.

Parameters

string $file

path to the ini file to read

boolean $exception

whether or not to throw an exception on file not found

Returns

array|boolean —

an array that represents the ini file

write_ini()

write_ini(array  $array, string  $file) 

Writes an INI file from an array

Parameters

array $array

the array to be translated into an ini file

string $file

the full path to the ini file, should have '.ini'

separate_non_sections()

separate_non_sections(array  $array) : array

Separates out bits from the global space and from sections

Parameters

array $array

array of values to write to an ini file

Returns

array —

a sorted array

print_section()

print_section(array  $section) : string

Prints the section of an INI file

Parameters

array $section

an array

Returns

string —

the array as an ini section

collapse_sections()

collapse_sections(array  $array) : array

Collapses arrays into something that can be written in the ini

Parameters

array $array

the array to be collapsed

Returns

array —

the collapsed array

flatten_array()

flatten_array(array  $array, string  $prefix = '') : array

Flattens an associate array

Parameters

array $array

an array to be flattened

string $prefix

a prefix for a key

Returns

array —

the array, but flattened

step_back()

step_back(array  $array) : array

Slightly unflattens an array

So, flatten_array goes one step too far with the flattening, but I don't know how many levels down I need to flatten (2, 97?), so we just flatten all the way and then step back one level, which is what this function does.

Parameters

array $array

a flattened array

Returns

array —

a slightly less flat array

parse_section()

parse_section(string  $name, mixed  $values) : array

Parses an ini section into its subsections

Parameters

string $name

a string that should be turned into an array

mixed $values

the values for an array

Returns

array —

the newly-dimensional array with $values

nest_array()

nest_array(array  $array, mixed  $values) : array

Recursively nests an array

Parameters

array $array

the pieces to nest

mixed $values

the values for the bottom level of the newly dimensional array

Returns

array —

a slightly more dimensional array than we received

is_assoc()

is_assoc(array  $array) : boolean

Checks if an array is associative

Shamelessly stolen from http://stackoverflow.com/a/14669600/1399574

Parameters

array $array

an array

Returns

boolean —

whether it is associative