Skip to content

me-io/php-ini-parser

Repository files navigation

PHP .ini parser

Ini Parser is a simple parser for complex INI files, providing a number of extra syntactic features to the built-in INI parsing functions, including section inheritance, property nesting, and array literals.


Build Status Code Cov Scrutinizer downloads MIT License Donate

All Contributors PRs Welcome Code of Conduct Watch on GitHub Star on GitHub Tweet

Installing

Add me-io/php-ini-parser following inside your composer.json file like this:

{
    "require": {
        "me-io/php-ini-parser": "^1"
    }
}

Then inside your terminal run the following command to install the dependencies:

composer install

Example

Standard INI files look like this:

key = value
another_key = another value

[section_name]
a_sub_key = yet another value

And when parsed with PHP's built-in parse_ini_string() or parse_ini_file(), looks like:

[
    'key' => 'value',
    'another_key' => 'another value',
    'section_name' => [
        'a_sub_key' => 'yet another value'
    ]
]

This is great when you just want a simple configuration file, but here is a super-charged INI file that you might find in the wild:

environment = testing

[testing]
debug = true
database.connection = "mysql:host=127.0.0.1"
database.name = test
database.username = 
database.password =
secrets = [1,2,3]

[staging : testing]
database.name = stage
database.username = staging
database.password = 12345

[production : staging]
debug = false;
database.name = production
database.username = root

And when parsed with \Ini\Parser:

$parser = new \Ini\Parser('sample.ini');
$config = $parser->parse();

You get the following structure:

[
    'environment' => 'testing',
    'testing' => [
        'debug' => '1',
        'database' => [
            'connection' => 'mysql:host=127.0.0.1',
            'name' => 'test',
            'username' => '',
            'password' => ''
        ],
        'secrets' => ['1','2','3']
    ],
    'staging' => [
        'debug' => '1',
        'database' => [
            'connection' => 'mysql:host=127.0.0.1',
            'name' => 'stage',
            'username' => 'staging',
            'password' => '12345'
        ],
       'secrets' => ['1','2','3']
    ],
    'production' => [
        'debug' => '',
        'database' => [
            'connection' => 'mysql:host=127.0.0.1',
            'name' => 'production',
            'username' => 'root',
            'password' => '12345'
        ],
        'secrets' => ['1','2','3']
    ]
]

Supported Features

Array Literals

You can directly create arrays using the syntax [a, b, c] on the right hand side of an assignment. For example:

colors = [blue, green, red]

NOTE: At the moment, quoted strings inside array literals have undefined behavior.

Dictionaries and complex structures

Besides arrays, you can create dictionaries and more complex structures using JSON syntax. For example, you can use:

people = '{
    "boss": {
        "name": "John", 
        "age": 42 
    }, 
    "staff": [
        {
            "name": "Mark",
            "age": 35 
        }, 
        {
            "name": "Bill", 
            "age": 44 
        }
    ] 
}'

This turns into an array like:

[
    'boss' => [
        'name' => 'John',
        'age' => 42
    ],
    'staff' => [
        [
            'name' => 'Mark',
            'age' => 35,
        ],
        [
            'name' => 'Bill',
            'age' => 44,
        ],
    ],
]

NOTE: Remember to wrap the JSON strings in single quotes for a correct analysis. The JSON names must be enclosed in double quotes and trailing commas are not allowed.

Property Nesting

Ini Parser allows you to treat properties as associative arrays:

person.age = 42
person.name.first = John
person.name.last = Doe

This turns into an array like:

[
    'person' => [
        'age' => 42,
        'name' => [
            'first' => 'John',
            'last' => 'Doe'
        ]
    ]
]

Section Inheritance

Keeping to the DRY principle, Ini Parser allows you to "inherit" from other sections (similar to OOP inheritance), meaning you don't have to continually re-define the same properties over and over again. As you can see in the example above, "production" inherits from "staging", which in turn inherits from "testing".

You can even inherit from multiple parents, as in [child : p1 : p2 : p3]. The properties of each parent are merged into the child from left to right, so that the properties in p1 are overridden by those in p2, then by p3, then by those in child on top of that.

During the inheritance process, if a key ends in a +, the merge behavior changes from overwriting the parent value to prepending the parent value (or appending the child value - same thing). So the example file

[parent]
arr = [a,b,c]
val = foo

[child : parent]
arr += [x,y,z]
val += bar

would be parsed into the following:

[
    'parent' => [
        'arr' => ['a','b','c'],
        'val' => 'foo'
    ],
    'child' => [
        'arr' => ['a','b','c','x','y','z'],
        'val' => 'foobar'
    ]
]

If you can think of a more useful operation than concatenation for non-array types, please open an issue

Finally, it is possible to inherit from the special ^ section, representing the top-level or global properties:

foo = bar

[sect : ^]

Parses to:

[
    'foo' => 'bar',
    'sect' => [
        'foo' => 'bar'
    ]
]

ArrayObject

As an added bonus, Ini Parser also allows you to access the values OO-style:

echo $config->production->database->connection; // output: mysql:host=127.0.0.1
echo $config->staging->debug; // output: 1

Contributors

A huge thanks to all of our contributors:


Mohamed Meabed

πŸ’» πŸ“’

Zeeshan Ahmad

πŸ’» πŸ› ⚠️ πŸ“–

License

The code is available under the MIT license.