Need help?

I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.

Please feel free to use the form below to contact me.




Extending Zend Config Example

The Zend Config component is mainly used to access configuration files, but can also be customized to suit many different situations.

In this article I'll be going through an example of how you can extend Zend_Config and customize it to your liking (or my liking in this case). You'll also need to know (or should know) a little bit about the Zend Framework to understand what I'm talking about, and how to fit it into your projects.

Below is the entire piece of code, which I'll be explaining bit by bit.


require_once 'Zend/Config/Xml.php';

class jsConfig extends Zend_Config_Xml {
  // config path can be modified to suite the environment
  const CONFIG_PATH = "";
  // extension of the config files - .xml in this case, since it's extending Zend_Config_Xml
  const CONFIG_EXTENSION = ".xml";
  // this is the default config, which all configs will inherit
  const DEFAULT_CONFIG_NAME = "default_config";
  // name of the default config section
  const DEFAULT_CONFIG_SECTION = "default";
  
  private $_defaultConfigInstance;
  
  public function __construct($configName, $configSection = self::DEFAULT_CONFIG_SECTION) {
    // checks to see if the requested config file exists
    if(file_exists(self::CONFIG_PATH . $configName . self::CONFIG_EXTENSION)) {
      parent::__construct(self::CONFIG_PATH . $configName . self::CONFIG_EXTENSION, $configSection, array('allowModifications' => 1));
      
      // checks if the default config was loaded, and if so it will merge them
      if($this->_getDefaultConfig()) {
        $this->merge($this->_defaultConfigInstance);
        $this->setReadOnly();
      }
    } else {
      throw Exception("jsConfig Exception: Cannot find configuration file - please check the name.");
    }
  }
  
  // gets the default config file if it exists
  private function _getDefaultConfig() {
    if(file_exists(self::CONFIG_PATH . self::DEFAULT_CONFIG_NAME  . self::CONFIG_EXTENSION)) {
      $this->_defaultConfigInstance = new Zend_Config_Xml(self::CONFIG_PATH . self::DEFAULT_CONFIG_NAME  . self::CONFIG_EXTENSION, self::DEFAULT_CONFIG_SECTION );
      
      return true;
    } else {
      return false;
    }
  }
}

The whole idea behind this is to have a default config file that automatically gets merged with any other configs that get loaded - assuming the default config exists. Now, I'm going to start explaining it bit by bit.


require_once 'Zend/Config/Xml.php';

This bit of code includes the class that we'll be extending in this example - you need this to actually go any further, unless you're using the Zend Loader (which I'll explain in another article).


class jsConfig extends Zend_Config_Xml {

Here we extend the Zend_Config_Xml class - you can also choose to extend Zend_Config_Ini if you don't want to use XML. This will allow us to use most of the methods and properties of Zend_Config_Xml.


// config path can be modified to suite the environment
const CONFIG_PATH = "";
// extension of the config files - .xml in this case, since it's extending Zend_Config_Xml
const CONFIG_EXTENSION = ".xml";
// this is the default config, which all configs will inherit
const DEFAULT_CONFIG_NAME = "default_config";
// name of the default config section
const DEFAULT_CONFIG_SECTION = "default";

These are the constants I use to define certain static values, which we'll be using within this example.

The CONFIG_PATH is used to store the location of the config files (set to the current directory in this example.

The CONFIG_EXTENSION just defines the extension used for the config files - xml in this case, as stated earlier.

DEFAULT_CONFIG_NAME is used to define the name of the default config file

DEFAULT_CONFIG_SECTION is the default section


private $_defaultConfigInstance;

This is the property (variable) that will be holding the default config instance.


public function __construct($configName, $configSection = self::DEFAULT_CONFIG_SECTION) {
  // checks to see if the requested config file exists
  if(file_exists(self::CONFIG_PATH . $configName . self::CONFIG_EXTENSION)) {
    parent::__construct(self::CONFIG_PATH . $configName . self::CONFIG_EXTENSION, $configSection, array('allowModifications' => 1));
      
    // checks if the default config was loaded, and if so it will merge them
    if($this->_getDefaultConfig()) {
      $this->merge($this->_defaultConfigInstance);
      $this->setReadOnly();
    }
  } else {
    throw Exception("jsConfig Exception: Cannot find configuration file - please check the name.");
  }
}

In this portion of code, I create the constructor, which accepts the name of the config file to load, and the config section (or you can leave it blank and it defaults to the config defined in the constant)

I then check if the file exists, and if it does, I pass the data into the parent constructor (of the parent class Zend_Config_Xml) - this does all the work for us

Next I check if the default config exists and is good to go using the private method "$this->_getDefaultConfig()" (which I'll go over next). If it exists, it gets merged with the current config that was just loaded. And lastly, it's set for read-only - so that you cannot alter any of the config values.


// gets the default config file if it exists
private function _getDefaultConfig() {
  if(file_exists(self::CONFIG_PATH . self::DEFAULT_CONFIG_NAME  . self::CONFIG_EXTENSION)) {
    $this->_defaultConfigInstance = new Zend_Config_Xml(self::CONFIG_PATH . self::DEFAULT_CONFIG_NAME  . self::CONFIG_EXTENSION, self::DEFAULT_CONFIG_SECTION );
      
    return true;
  } else {
    return false;
  }
}

This private method checks if the default config file exists, and sets the private property $_defaultConfigInstance with an instance of it (to merge with the other config). This return true if successful, or false otherwise.

That basically sums up the script - it's useful if you have a lot of common configurations that you always want merged into any new config files on-load. Great for large projects!

Download the full source file: jsConfig.php.zip


jon | April 27, 2010 | Comments (1)

Comments

AKAIK you've got the awnser in one!
Comment by Maggie - May 08, 2011 @ 12:07 pm

Name (required)
Email (will not be published) (required)
Website

captcha