我刚写了一个Magento模块,但它没有加载,我想调试它。
答案 0 :(得分:15)
以下是几个类和负责各种加载阶段的方法。
为每个模块调用(返回Mage_Core_Model_Config,表示主配置目录中的模块的XML块(启用,版本等等)):
/**
* Get module config node
*
* @param string $moduleName
* @return Varien_Simplexml_Object
*/
function getModuleConfig($moduleName='')
{
$modules = $this->getNode('modules');
if (''===$moduleName) {
return $modules;
} else {
return $modules->$moduleName;
}
}
为每个模块调用,但只构建一个结构,但不包括单个模块配置:
/**
* Load declared modules configuration
*
* @param null $mergeConfig depricated
* @return Mage_Core_Model_Config
*/
protected function _loadDeclaredModules($mergeConfig = null)
{
$moduleFiles = $this->_getDeclaredModuleFiles();
if (!$moduleFiles) {
return ;
}
Varien_Profiler::start('config/load-modules-declaration');
$unsortedConfig = new Mage_Core_Model_Config_Base();
$unsortedConfig->loadString('<config/>');
$fileConfig = new Mage_Core_Model_Config_Base();
// load modules declarations
foreach ($moduleFiles as $file) {
$fileConfig->loadFile($file);
$unsortedConfig->extend($fileConfig);
}
$moduleDepends = array();
foreach ($unsortedConfig->getNode('modules')->children() as $moduleName => $moduleNode) {
if (!$this->_isAllowedModule($moduleName)) {
continue;
}
$depends = array();
if ($moduleNode->depends) {
foreach ($moduleNode->depends->children() as $depend) {
$depends[$depend->getName()] = true;
}
}
$moduleDepends[$moduleName] = array(
'module' => $moduleName,
'depends' => $depends,
'active' => ('true' === (string)$moduleNode->active ? true : false),
);
}
// check and sort module dependence
$moduleDepends = $this->_sortModuleDepends($moduleDepends);
// create sorted config
$sortedConfig = new Mage_Core_Model_Config_Base();
$sortedConfig->loadString('<config><modules/></config>');
foreach ($unsortedConfig->getNode()->children() as $nodeName => $node) {
if ($nodeName != 'modules') {
$sortedConfig->getNode()->appendChild($node);
}
}
foreach ($moduleDepends as $moduleProp) {
$node = $unsortedConfig->getNode('modules/'.$moduleProp['module']);
$sortedConfig->getNode('modules')->appendChild($node);
}
$this->extend($sortedConfig);
Varien_Profiler::stop('config/load-modules-declaration');
return $this;
}
加载config.xml,enterprise.xml,local.xml等。:
/**
* Load base system configuration (config.xml and local.xml files)
*
* @return Mage_Core_Model_Config
*/
public function loadBase()
{
$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);
}
if (in_array($etcDir.DS.'local.xml', $files)) {
$this->_isLocalConfigLoaded = true;
}
return $this;
}
加载单个模块配置:
/**
* Iterate all active modules "etc" folders and combine data from
* specidied xml file name to one object
*
* @param string $fileName
* @param null|Mage_Core_Model_Config_Base $mergeToObject
* @return Mage_Core_Model_Config_Base
*/
public function loadModulesConfiguration($fileName, $mergeToObject = null, $mergeModel=null)
{
$disableLocalModules = !$this->_canUseLocalModules();
if ($mergeToObject === null) {
$mergeToObject = clone $this->_prototype;
$mergeToObject->loadString('<config/>');
}
if ($mergeModel === null) {
$mergeModel = clone $this->_prototype;
}
$modules = $this->getNode('modules')->children();
foreach ($modules as $modName=>$module) {
if ($module->is('active')) {
if ($disableLocalModules && ('local' === (string)$module->codePool)) {
continue;
}
$configFile = $this->getModuleDir('etc', $modName).DS.$fileName;
if ($mergeModel->loadFile($configFile)) {
$mergeToObject->extend($mergeModel, true);
}
}
}
return $mergeToObject;
}
实际读取单个模块配置的内容:
/**
* Imports XML file
*
* @param string $filePath
* @return boolean
*/
public function loadFile($filePath)
{
if (!is_readable($filePath)) {
//throw new Exception('Can not read xml file '.$filePath);
return false;
}
$fileData = file_get_contents($filePath);
$fileData = $this->processFileData($fileData);
return $this->loadString($fileData, $this->_elementClass);
}
达斯汀奥普雷亚