Simple Environment Config (PHP Snippet)

_config.php

<?php
 
function _config($for=null)
{
 
    $path    = dirname(__FILE__) . '/config.xml';
    $sig     = md5($path);
 
    if (defined($sig))
    {
        $config = unserialize(constant($sig));
    }
        else
    {
        $config  = (object) array();
        $xml     = simplexml_load_file(dirname(__FILE__) . '/config.xml');
 
        foreach ($xml->environments->children() AS $env)
        {
            $environment              = $env->attributes()->name;
            $config->{$environment}   = (object) array();
 
            foreach ($env->children() AS $variable)
            {
                $config->{$environment}->{$variable->attributes()->name} = (string) $variable;
            }
        }
 
        define($sig,serialize($config));
    }
 
    if ($for==null)
    {
        $for = getenv('ENVIRONMENT');
 
        if (empty($for))
        {
            $for = 'dev';
        }
    }
 
    return isset($config->{$for}) ? $config->{$for} : (object) array();
 
}

config.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<config>
 
    <environments>
        <environment name="dev">
            <variable name="mysql_host">localhost</variable>
            <variable name="mysql_port">3306</variable>
            <variable name="mysql_user">root</variable>
            <variable name="mysql_password">root</variable>
            <variable name="mysql_database">portal</variable>
        </environment>
    </environments>
 
</config>

Yo #Firefox.. "stop …

Yo #Firefox.. “stop script” means “stop script”! Not “wait until it’s done, and then stop it”.. kind of defeats the purpose don’t you think?

Just starting watching Th…

Just starting watching The Walking Dead.. looks very interesting but is the whole series such a drama fest or is it just the first episode?

Today in my inbox; "…

Today in my inbox; “Your email account has been awarded ¬£1,050,000 To claim, send an E-mail to Eddie Goodman” – SCORE!