For even seasoned Magento developers, the underpinnings of configuration can be a black box. Along with layout XML configuration, it is one of the most derided and misunderstood concepts in Magento. However, following the code during configuration load illuminates the logical, clever, extensible system. With that in mind, we’ll walk through the code—from initializing base configuration through loading database configuration.STEP 1: LOAD BASE CONFIGURATION

STEP 1: LOAD BASE CONFIGURATION

  • Mage_Core_Model_App::_initBaseConfig
  • Mage_Core_Model_Config::init
  • Mage_Core_Model_Config::loadBase
  • glob
  • Varien_SimpleXml_Config::loadFile
  • Varien_SimpleXml_Config::extend

Magento’s “base config” is considered to be the XML files found in app/etc. For a base installation of Magento CE, this includes config and local XML. Local XML will come more into play later, so the main focus of the base configuration is to set up some sane defaults located in `config.xml`, such as a connection (core_read and core_write both defer to default_setup which is defined in the loosest sense here and should be reconfigured using `local.xml`). Another important aspect of the base config is defining the model alias for the Mage_Core module.

Magento’s “base config” is considered to be the XML files found in app/etc. For a base installation of Magento CE, this includes config and local XML. Local XML will come more into play later, so the main focus of the base configuration is to set up some sane defaults located in `config.xml`, such as a connection (core_read and core_write both defer to default_setup which is defined in the loosest sense here and should be reconfigured using `local.xml`). Another important aspect of the base config is defining the model alias for the Mage_Core module.

STEP 2: LOAD CONFIGURATION FILES

  • Mage_Core_Model_Config::loadModules
  • Mage_Core_Model_Config::_loadDeclaredModules
  • Mage_Core_Model_Config::_getDeclaredModuleFiles
  • Mage_Core_Model_Config_Base::loadFile
  • Mage_Core_Model_Config::_sortModuleDepends
  • Mage_Core_Model_Config::loadModulesConfiguration
  • Mage_Core_Model_Config_Element::is
  • Mage_Core_Model_Config_Base::loadFile
  • Mage_Core_Model_Config::extend
  • Mage_Core_Model_Config_Base::loadFile
  • Mage_Core_Model_Config::extend

Determining Declared Modules

The first step is to determine the declared modules. Declared modules are those that have an “entry” in the app/etc/modules directory. There are three categories of declaration files: base, mage, and custom. The file name of a declaration file does not affect the module within except that modules are tagged as base if they are declared within the Mage_All.xml file, and as mage if their declaration file begins with Mage_. All other declarations are considered custom, regardless of file name.These categories are used to initially sort the declaration XML so that a custom module may overwrite the declaration of a mage or base module. Here’s that relevant code:

These categories are used to initially sort the declaration XML so that a custom module may overwrite the declaration of a mage or base module. Here’s that relevant code:
return array_merge(
$collectModuleFiles[‘base’],
$collectModuleFiles[‘mage’],
$collectModuleFiles[‘custom’]
);When used with associative arrays, array_merge will use the values located at the last instance of a key.

When used with associative arrays, array_merge will use the values located at the last instance of a key.After the module declarations are loaded, their sorting is resolved based on the contents of their depends node. When a module depends on another, its configuration is placed after. This has significant affects on the running of Magento, but will not be discussed here.

After the module declarations are loaded, their sorting is resolved based on the contents of their depends node. When a module depends on another, its configuration is placed after. This has significant affects on the running of Magento, but will not be discussed here.

STEP 3: MODULE CONFIGURATION

To load the detailed configuration of each module, Magento iterates over the declared modules. If the modules enabled node has to load the detailed configuration of each module, Magento iterates over the declared modules. If the modules enabled node has value true, the module’s configuration is loaded. Note that the code within a non-enabled module is still accessible, but not through Magento’s normal idioms.When a given module’s detailed configuration is to be loaded, the actual config XML is loaded, located at app/code/*codePool*/*VendorName*/*ModuleName*/etc/config.xmlWhen a given module’s detailed configuration is to be loaded, the actual config XML is loaded, located

When a given module’s detailed configuration is to be loaded, the actual config XML is loaded,

locatedatapp/code/*codePool*/*VendorName*/*ModuleName*/etc/config.xml

atapp/code/*codePool*/*VendorName*/*ModuleName*/etc/config.xml

app/code/*codePool*/*VendorName*/*ModuleName*/etc/config.xml

The individual module’s config is merged with the main tree before the next iteration, allowing modules declared later (using depends and declaration file categories) to override configuration from earlier modules.

STEP 4: LOAD LOCAL CONFIGURATION

The last truly flexible place to override configuration in Magento is in app/etc/local.xml. It is loaded after every module and its nodes are merged just the same as those located in individual config.xml files.

STEP 5: DATABASE CONFIGURATION

Mage_Core_Model_App::loadDb
Mage_Core_Model_Config::getResourceModel
Mage_Core_Model_Resource_Config::loadToXml
Mage_Core_Model_Resource_Config::_getReadAdapter
Varien_Db_Adapter_Interface::fetch*
Mage_Core_Model_Config::setNode

Resource Models and Database Adapters

The first step to loading the database configuration is to get a connection to the database. To do that, Magento must get a resource model. Resource models are used to manage the persistence of a model. The connection between a model and its corresponding resource is managed in the configuration XML just previously loaded. With the resource model in hand, the configuration loads its database representation.

Database Configuration Scopes

There are multiple “top-level” configuration nodes. The most commonly-seen ones in file-based configuration are global, frontend, and admin. Database configuration adds more: default, websites, and stores. More specifically, websites and stores configuration may have unique values per-entity.Loading the database configuration is a multi-step process. Before the actual configuration data can be loaded, website and store setup must be determined. This involves iterating over the websites discovered in the core_website table and registering nodes in the configuration for them, and then iterating over each store discovered in the `core_store` table, and adding its configuration (assuming the website it is configured for exists!)

Loading the database configuration is a multi-step process. Before the actual configuration data can be loaded, website and store setup must be determined. This involves iterating over the websites discovered in the core_website table and registering nodes in the configuration for them, and then iterating over each store discovered in the `core_store` table, and adding its configuration (assuming the website it is configured for exists!)

Database Configuration Merge

Once the database configuration is loaded, it is merged into the config tree based on various scopes.

Default:

Configuration values with a “default” scope are added to the default configuration node

Websites:

Each website inherits the default-scoped configuration

Configuration values with “websites” scope are added to the specific website instance they have specified in the scope_id

Stores:

Each store inherits its parent website’s configuration

Configuration values with “stores” scope are added to the specific store instance they have specified in the scope_id

Hopefully this post gives some insight to the inner-workings of Magento’s configuration load. The methods listed for each step are a great jumping-in point if you want to get more in depth. Happy configuring!