Tuesday, 5 June 2012

MCD: Explaining how Magento Loads and Manipulates Configuration Information (Part II)

We started explaining how Magento loads and manipulates configuration information here.
The next step is writing config into cache, if it is enabled.

Our configuration is formed by now, but besides compiling this .xml file, we need to obtain data from it somehow. Let’s go back to Mage.php.

Here are the methods:

/**
* Retrieve config value for store by path
*
* @param string $path
* @param mixed $store
* @return mixed
*/
public static function getStoreConfig($path, $store = null)
{
return self::app()->getStore($store)->getConfig($path);
}

/**
* Retrieve config flag for store by path
*
* @param string $path
* @param mixed $store
* @return bool
*/
public static function getStoreConfigFlag($path, $store = null)
{
$flag = strtolower(self::getStoreConfig($path, $store));
if (!empty($flag) && 'false' !== $flag) {
return true;
} else {
return false;
}
}


Their only difference is that getStoreConfig() will return the exact value while getStoreConfigFlag(), as its name suggests, returns boolean true or false. Both methods send us to Mage_Core_Model_Store::getConfig()


/**
* Retrieve store configuration data
*
* @param string $path
* @return string|null
*/
public function getConfig($path)
{
if (isset($this->_configCache[$path])) {
return $this->_configCache[$path];
}

$config = Mage::getConfig();

$fullPath = 'stores/' . $this->getCode() . '/' . $path;
$data = $config->getNode($fullPath);
if (!$data && !Mage::isInstalled()) {
$data = $config->getNode('default/' . $path);
}
if (!$data) {
return null;
}
return $this->_processConfigValue($fullPath, $path, $data);
}


If the requested information is not found in a local cache, this method will use the path stores/[store code]/[requested path]. If data is still not found, the method will use another path default/[requested path] to search in the loaded configuration. When nothing is found, method will return null.

Found data is processed via _processConfigValue() method:

• If the returned node has children, data will be stored in a local cache recursively. By the next call within a current user session there will be no need to look for a found data in the configuration.
• If the node has backend_model, this model should be requested to bring data to the required format.
• Variable of {{unsecure_base_url}}, {{unsecure_base_url}}, {{base_url}} sort are replaced by the correspondent values.

As a conclusion I would like to sum everything up and point out the basics.

1. All .xml files are collected into one big simpleXmlElement object

2. First, data is loaded from app/etc/*.xml and then from app/etc/modules/*.xml. Based on the module loaded information, config.xml is loaded from the etc directory of the module. If we load backend to check ACL and build menu elements, adminhtml.xml and system.xml are loaded as well. Configuration data from the database is the last one to load.

3. Any parameters, except the ones stored in app/etc/local.xml, can be overridden in config.xml of a custom module.

P.S. Even though Magento provides us with such convenient methods as Mage:: getStoreConfig() and Mage:: getStoreConfigFlag(), we can reach any element of the configuration tree with the help of Mage::getConfig()->getNode($path, $scope, $scopeCode);

Look at your final config of the Magento installation by running the following piece of code in the site root directory:


define('MAGENTO_ROOT', getcwd());

$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
umask(0);

/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

Mage::app($mageRunCode, $mageRunType);

$config = Mage::getConfig()->getNode()->asXml();
file_put_contents('config.xml', $config);


Config.xml will store the entire final config, actual for your store.

Courtesy: http://blog.belvg.com/

No comments :

Post a Comment