Loading module configuration is one of the most important stages of the application initialization. It takes place relatively early in the process soon after the basic configuration is loaded from the etc/config.xml and etc/local.xml files and before instantiating the front controller and the request dispatching.

This function uses an object of type Mage_Core_Model_Config to load the model configuration. The system checks if the configuration can be fetched from the cache, and if not, proceeds with loading it from module declaration and configuration files.

The function Mage_Core_Model_Config::_loadDeclaredModules scans the directory etc/modules and loads XML files located there. These files contain module declarations. Before loading their contents, the system arranges them into a specific order. The first file in this list is Mage_All.xml. The modules declared in this file make up the essential core functionality of Magento. Next in line are the declaration files whose names begin with Mage_. The modules declared there are a part of the Magento core as well, but often depend on the modules from the Mage_All.xml file and thus must be loaded after those. The rest of the list are declarations of third party modules: community modules and your local extensions. Once the list is compiled, Magento fetches the files in the order they are listed and loads their contents into an object of type Mage_Core_Model_Config_Base – the application configuration object, which Magento queries whenever a configuration entry is requested. The loaded configuration must be ordered once more – this time according to module dependencies.

A module’s declaration can contain references to other modules on whose functionality the given module depends. Take a look at the XML declaration of the module Mage_Captcha:

The node shows that this module requires Mage_Customer and Mage_Adminhtml. Magento observes such module relations when defining the order in which module declarations are loaded. Magento checks if dependency modules are active and loads them before dependent modules. This sorting takes place in function Mage_Core_Model_Config::_sortModuleDepends. This function produces an array in which modules are ordered by placing dependent module after their dependencies. This array (moduleDepends) is used to reorder the loaded module declarations by dependency:

At this point the Magento knows which modules the installation has and what module configuration must be loaded. As you already know, the module configuration is declared in a config.xml file in the etc directory of the module. Additionally Magento will look for a config.mysql4.xml file in the same category. Such files are not present in the most recent Magento versions. I can only assume that scanning for config.mysql4.xml files is performed for the sake of compatibility with the earlier releases.

In the first line of this function the system checks if configuration settings forbid using the local modules. If yes, then behind the scenes Magento will modify the include path so that id does not contain the path to your local code pool. This way the autoloader will not be able to find your local classes.

In this context the variable $mergeToObject is the shop’s configuration object itself. After the loading is done it would contain the module configuration. Next an object of type Mage_Core_Model_Config is created, $mergeModel. It will be used as a temporary storage to fetch the contents of a configuration file and add it to $mergeToObject.

Magento fetches a list of declared modules into a variable $modules and iterates through it. The system checks every module if it is active and, if yes, looks for a config.xml or a config.mysql4.xml file in the module’s etc directory and loads the module configuration from these files.

With this the loading of the module configurations is done. In order to prevent overwriting of any settings defined in the etc/local.xml (they must always have prevalence) Magento loads configurations from file once again:

The final step in the function Mage_Core_Model_Config::loadModules() is the following call:

$this->applyExtends();

The class Mage_Core_Model_Config inherits the function applyExtends from the Varien_Simplexml_Config class. This function is used to load configuration extensions. Magento XML configuration (such as defined in module’s config.xml files) can contain nodes like this:

<allResources extends="/config/modules/resource"/>

This <allResources> node (its name can be custom, but the attribute “extends” is mandatory) extends the configuration found under path /config/modules/resource. Magento will look for the target node and extend it with the contents of the extension node (<allResources> in our example):

With the module configuration loaded Magento returns control to the Mage_Core_Model_App::_initModules(). The next step is to apply module installation and update scripts. A module’s setup configuration is defined in the config.xml file under the node global/resources. Magento fetches these nodes from the loaded configuration and iterates through this list executing the installation and update scripts where necessary. We will discuss setup scripts in a much greater detail later.

Once the module installation and upgrades are through, Magento loads the configuration data stored in the database table core_config_data. The configuration stored in the database can be controlled from the shop back-end interface. The information entered in the Configuration section of the back-end is saved into the database.

When loading the configuration from the database Magento observes the scope of the settings. The settings values can have the scope stores, websites, or default – in the order of precedence. Magento will overwrite a default setting if a specific website value if provided, which in turn can be replaced with a specific store value.

The final step in the module initialization and configuration loading in saving the collected configuration data into the cache. Using the configuration cache significantly speeds up subsequent requests as Magento can skip costly IO calls to the file system and the database by replacing them with a single read of the entire cached configuration. For this to happen the configuration caching must be enabled – in backend under System => Cache Storage Management.

Readers who read this post also read these:

Magento Routers: a Look under the HoodMagento Routing
The centerpiece of Magento, the Front Controller, can sometimes be a challenge for a beginner Magento developer. Just a few years ago most PHP applications where collections of scr...

Magento Downloadable Product Type (Part 2)In this post I will describe models and processes specific to Magento downloadable products. To find out about creating downloads and managing their front-end display see the first part of the Down...

Magento Configurable Product Type (Part 2)In this post we will look into the functionality of Mage_Catalog_Model_Product_Type_Configurable, in what way it differs from that of the parent class Mage_Catalog_Model_Product_Type_Abstract and w...