Posted in Code Snippets, PHP

PHP INI parser with environment support

Reading the INI file using PHP, based on the environment can be performed using few configuration in Apache.

Step 1: Set environment in Apache

Open the apache.conf or httpd.conf from apache server’s directory and add the following line:

SetEnv APPLICATION_ENV "dev"

Step 2: Create INI files

Create files using format <filename>.<environment>.ini

Say, assets.dev.ini or assets.qa.ini

Example INI file – assets.dev.ini:

[bootstrap]
js = https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js
css = https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css

Step 3: Use the config class

Step 4: Example

<?php

require_once "class.config.php";

var_dump(Config::getEnvironment());           // dev
var_dump(Config::isEnvironment('dev'));       // TRUE
var_dump(Config::get('assets.bootstrap.js'));

Now, it can be seen that this reads assets.dev.ini file. Similarly, when apache server’s SetEnv is set to prod, assets.prod.ini will be read.

In this way, INI can be parsed based on different environment. Free from configuration issues across environments.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s