Magento Configuration System

In the blog we will try to understand how the magento configuration system works and how magento loads all configuration information.


Magento configuration system is divided into two parts
1. Xml files located in “app/etc” , “app/etc/modules” and config.xml of individual config.xml in modules
2. Configuration information stored in “core_config_data” table in database

For xml based configuration files magento uses php extension SimpleXML to manage xml processing internally. All xml processing functions are defined in “Varien_Simplexml_Config” class located at “lib/Varien/Simplexml/Config”

Magento loads all configuration information in a single object which is “Mage_Core_Model_Config”.
The loading of configuration starts from “run()” function “Mage_Core_Mode_App” class. The functions called are

$this->baseInit($options); 
// baseInit calls $this->_config->loadBase(); function internally
$this->_initModules();
// initModules interally calls
//$this->_config->loadModules()
//$this->_config->loadDb();

Lets look at each of these function in detail

baseInit()

The baseInit() function interall calls loadBase() in Mage_Core_Model_Config class

The function definition is

        $etcDir = $this->getOptions()->getEtcDir();
        $files = glob($etcDir.DS.'*.xml');
        $this->loadFile(current($files));
        while ($file = next($files)) {
            $merge = clone $this->_prototype;
            $merge->loadFile($file);
            $this->extend($merge);
        }

This function reads the configuration located in “app/etc” folder mainly the local.xml and config.xml, and loads it into the config object. The extend function above is mainly used to merge configuration into a single object. By default if duplicate xml tags are found in the same level they are overwritten when using the extend function.
Also its important to note that this base configuration is not stored in cache.
To see that status of config object, you can do a print_r($this->getNode()); and you can see the entire configuration object.
To understand the below, open the class Mage_Core_Mode_Config and keep referring to the source code indicated.

_initModules()

initModules() function internally calls loadModules() in Mage_Core_Model_Config
This function is again divided into sub parts:
1. _loadDeclaredModules() =>
This function gets a list of all files in “app/etc/modules/”. Here modules are sorted based on the “depends” tag

<config>
    <modules>
        <Mage_Api>
            <active>true</active>
            <codePool>core</codePool>
            <depends>
                <Mage_Core />
            </depends>
        </Mage_Api>
    </modules>
</config>

This is a sample configuration with “depends” tag used.
The loadModules sorts the module based on “depend” i.e depending modules load first. It also important to note that at this stage both active and in-active modules are loaded into configuration.

2. loadModulesConfiguration() =>
This function now loads the “config.xml” and “config.mysql4.xml” file of each of the loaded module above.
Here there is a check for module being active. Only if a module is active are its configuration files loaded.
Its also important to note the module config file are loaded in sorted order based only on “depends” tag in xml, other than that there is no other sort order.

3. local.xml configuration is read again after this step. This is used to prevent any module config.xml from overriding the configuration in local.xml.

Different ways to read configuration

//this information is from Mage_Core.xml file in app/etc/ folder
$x = Mage::app()->getConfig()->getNode('modules/Mage_Core');
echo $x->active;  

//this information is from config.xml of Mage_Checkout
$x = Mage::app()->getConfig()->getNode('global/fieldsets/customer_account/prefix/to_order');
print_r($x);

// also we can directly use Mage::getConfig() instead of Mage::app()->getConfig()

loadDb() in Mage_Core_Model_Config

Load Db function is used to read all configuration stored in database, mainly the data stored in core_config_data table and add it to the Mage_Core_Model_Config class.

$dbConf = $this->getResourceModel();

The above line is use to establish database connection and this is the first time database connection is established. The function “getResourceConnectionConfig” return the database connection details as stored in local.xml file.

Next we have the

$dbConf->loadToXml($this);

This function copies data from core/website, core/store, and core/config_data tables in the Config object.
Once this step is complete, we get new config variables in our object.

            $xml = Mage::getConfig()->getNode();
            echo $xml->websites->base->system->website->name;
            echo $xml->websites->base->system->website->id;
            echo $xml->default->general->region->state_required;
            echo $xml->default->web->unsecure->base_url;

In this blog post we see magento loads its entire configuration and the importance of Mage_Core_Model_Config class.
We also see how to access various configuration variables.