Class Ini
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:
<pre><span class="php-var">$array</span> = [ <span class="php-quote">'Alphred'</span> => [ <span class="php-quote">'log_level'</span> => <span class="php-quote">'DEBUG'</span>, <span class="php-quote">'log_size'</span> => <span class="php-num">10000</span>, <span class="php-quote">'plugins'</span> => [ <span class="php-quote">'get_password'</span> => <span class="php-quote">'my_new_function'</span> ]
]];</pre>
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:
<pre><span class="php-var">$ini_file</span> = Alphred\Ini::read_ini( <span class="php-quote">'/path/to/workflow.ini'</span> );</pre>
That's it.
To write an ini
file, just do:
<pre>Alphred\Ini::write_ini( <span class="php-var">$config_array</span>, <span class="php-quote">'/path/to/workflow.ini'</span> );</pre>
Methods summary
public
array|boolean
|
#
read_ini( string $file, boolean $exception = true )
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
- $file
- path to the ini file to read
- $exception
- whether or not to throw an exception on file not found
Returns
array|boolean an array that represents the ini file
Since
1.0.0
|
public
|
#
write_ini( array $array, string $file )
Writes an INI file from an array
Writes an INI file from an array
Parameters
- $array
- the array to be translated into an ini file
- $file
- the full path to the ini file, should have '.ini'
Since
1.0.0
Todo
Do filesystem checks
|
private
array
|
#
separate_non_sections( array $array )
Separates out bits from the global space and from sections
Separates out bits from the global space and from sections
Parameters
- $array
- array of values to write to an ini file
Returns
array a sorted array
|
private
string
|
#
print_section( array $section )
Prints the section of an INI file
Prints the section of an INI file
Parameters
Returns
string the array as an ini section
Since
1.0.0
|
private
array
|
#
collapse_sections( array $array )
Collapses arrays into something that can be written in the ini
Collapses arrays into something that can be written in the ini
Parameters
- $array
- the array to be collapsed
Returns
array the collapsed array
Since
1.0.0
|
private
array
|
#
flatten_array( array $array, string $prefix = '' )
Flattens an associate array
Flattens an associate array
Parameters
- $array
- an array to be flattened
- $prefix
- a prefix for a key
Returns
array the array, but flattened
Since
1.0.0
Todo
Better tests for numeric keys
|
private
array
|
#
step_back( array $array )
Slightly unflattens an 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
Returns
array a slightly less flat array
Since
1.0.0
|
private
array
|
#
parse_section( string $name, mixed $values )
Parses an ini section into its subsections
Parses an ini section into its subsections
Parameters
- $name
- a string that should be turned into an array
- $values
- the values for an array
Returns
array the newly-dimensional array with $values
Since
1.0.0
|
private
array
|
#
nest_array( array $array, mixed $values )
Recursively nests an array
Recursively nests an array
Parameters
- $array
- the pieces to nest
- $values
- the values for the bottom level of the newly dimensional array
Returns
array a slightly more dimensional array than we received
Since
1.0.0
|
private
boolean
|
#
is_assoc( array $array )
Checks if an array is associative
Checks if an array is associative
Shamelessly stolen from http://stackoverflow.com/a/14669600/1399574
Parameters
Returns
boolean whether it is associative
Since
1.0.0
|
Magic methods summary