Archive

Archive for September, 2011

How to Specify Non-String PHP Data Types in Zend Config XML and INI Files

September 7th, 2011 Comments off

Zend Config INI and Zend Config XML are adapters in the Zend Framework that allow you to store your application or website configuration data (such as database connector information and salt values) in an easy to interpret text file (I happen to prefer INI files over XML because INI is more terse).

The biggest downside to these files is that they don’t store PHP data types or values like NULL, FALSE, or TRUE, and you can’t specify an int, double, or float, the adapters only return strings.

Here is the recursive code for converting all your values:

public static function convertConfigValuesFromString($config)
    {
        foreach($config as $key => $value)
        {
            if(is_string($value))
            {
                switch($value)
                {
                    case 'BOOL_FALSE':
                        $config[$key] = false;
                        break;
                    case 'BOOL_TRUE':
                        $config[$key] = true;
                        break;
                    case 'CONST_NULL':
                        $config[$key] = null;
                        break;
                    default:
                        if(substr($value,0,4) == 'INT_')
                        {
                            $config[$key] = (int) substr($value,4);
                        }
                        elseif(substr($value,0,4) == "DBL_")
                        {
                            $config[$key] = (double) substr($value,4);
                        }
                        elseif(substr($value,0,4) == "FLT_")
                        {
                            $config[$key] = (float) substr($value,4);
                        }
                        break;
                }
            }
            elseif(is_array($value))
            {
                $config[$key] = self::convertConfigValuesFromString($value);
            }
        }
        return $config;
    }

Here is how I use it in my Bootstrap.php file in my Zend Framework application:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected $_namespace = 'Ashurex';
    protected $_config;

    ... other code ...

    protected function _initConfig()
    {        
        // Use the default mechanisms built into the bootlstrap
        // for reading in the app configuration
        $config = $this->getOptions();

        $config = Ashurex_Utilities::convertConfigValuesFromString($config);   
   
        // After gathering and assigning the 
        // correct data types to our config values
        // Turn into an instance of Zend_Config for later use
        $config = new Zend_Config($config);
        // Store the config in the registry
        Zend_Registry::set('config', $config);

        $this->_config = $config;
        return $config;
    }

    ... other code ...

}

The following config file will end up looking like the following array (before turning it into a Zend_Config object)

[default]
phpSettings.date.timezone = "America/Los_Angeles"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
resources.modules[] =
resources.view.doctype = "HTML5"
app.debug.enabled = "BOOL_TRUE"
resources.db.adapter = PDO_MYSQL

[development : default]
resources.db.params.host = "localhost"
resources.db.params.username = "dbuser"
resources.db.params.password = "dbpassword"
resources.db.params.dbname = "dbname"
app.integer = "INT_1234567890"
app.double = "DBL_123456789012345.678"
app.float = "FLT_12.0000001"
session.name = "session_name"
array
  'phpSettings' => 
    array
      'date' => 
        array
          'timezone' => string 'America/Los_Angeles' (length=19)
  'bootstrap' => 
    array
      'path' => string '/var/www/html/application/Bootstrap.php' (length=50)
  'resources' => 
    array
      'modules' => 
        array
          0 => string '' (length=0)
      'view' => 
        array
          'doctype' => string 'HTML5' (length=5)
      'db' => 
        array
          'adapter' => string 'PDO_MYSQL' (length=9)
          'params' => 
            array
              ...
  'app' => 
    array
      'debug' => 
        array
          'enabled' => boolean true
      'integer' => int 1234567890
      'double' => float 1.2345678901235E+14
      'float' => float 12.0000001
  'session' => 
    array
      'name' => string 'session_name' (length=12)

Install / Upgrade PHP 5.3 on CentOS 5 / RHEL 5

September 6th, 2011 Comments off

As I was setting up a staging VM (CentOS 5), I quickly realized I could only get PHP 5.1 from the standard repositories. Thanks to this blog post, I was able to quickly get up and running with PHP 5.3.

From the command line on your server, run the following:

wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
rpm -Uvh epel-release-5-4.noarch.rpm
rpm -Uvh remi-release-5.rpm
yum --enablerepo=remi update php php-* mysql

This, of course, assumes that your LAMP stack is already installed. If not, you would want to replace ‘update’ with ‘install’.

Make sure that you run the last update command (if you’re running an update) as noted above. Not including one of the three packages will result in a bunch of file conflict errors.

Categories: Linux Tags: , , , , ,