为什么我的Magento模块没有加载

为什么我的Magento模块没有加载

本文介绍了为什么我的Magento模块没有加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

我在这里写了一个Magento模块,是几个类和负责加载各个阶段的方法。



Mage_Core_Model_Config



每个模块(返回一个Mage_Core_Model_Config表示模块的XML块在主配置目录(启用,版本等)):

  / ** 
*获取模块配置节点
*
* @param string $ moduleName
* @return Varien_Simplexml_Object
* /
function getModuleConfig moduleName ='')
{
$ modules = $ this-> getNode('modules');
if(''=== $ moduleName){
return $ modules;
} else {
return $ modules-> $ moduleName;
}
}

调用每个模块,但只是构建一个结构不包含单个模块配置:

  / ** 
*加载已声明的模块配置
*
* @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();

//加载模块声明
foreach($ moduleFiles as $ file){
$ fileConfig-> loadFile($ file);
$ unsortedConfig-> extend($ fileConfig);
}

$ moduleDepends = array();
foreach($ unsortedConfig-> getNode('modules') - > children()as $ moduleName => $ moduleNode){
if(!$ this-> _isAllowedModule ){
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),
);
}

//检查和排序模块相关性
$ moduleDepends = $ this-> _sortModuleDepends($ moduleDepends);

//创建排序的配置
$ 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 ['模块']);
$ sortedConfig-> getNode('modules') - > appendChild($ node);
}

$ this-> extend($ sortedConfig);

Varien_Profiler :: stop('config / load-modules-declaration');
return $ this;
}

装载config.xml,enterprise.xml,local.xml等。 :

  / ** 
*载入基本系统配置(config.xml和local.xml文件)
*
* @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;
}

加载各个模块配置:

  / ** 
*迭代所有活动模块etc文件夹,并将
*指定的xml文件名中的数据合并到一个对象
*
* @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;
}



Varien_Simplexml_Config(lib / Varien / Simplexml / Config.php)



实际读取单个模块配置的方法:

  / ** 
*导入XML文件
*
* @param string $ filePath
* @return boolean
* /
public function loadFile($ filePath)
{
if(!is_readable($ filePath)){
// throw new Exception('无法读取xml文件'。
return false;
}

$ fileData = file_get_contents($ filePath);
$ fileData = $ this-> processFileData($ fileData);
return $ this-> loadString($ fileData,$ this-> _elementClass);
}

Dustin Oprea


I just wrote a Magento module, but it's not being loaded and I'd like to debug it.

解决方案

Here are a couple of classes and the methods that are responsible for various stages of loading.

Mage_Core_Model_Config

Called for each module (returns a Mage_Core_Model_Config representing the module's XML block in the main config directory (enabled, version, etc..)):

/**
 * 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;
    }
}

Called for each module, but just builds a structure still without including the individual module configs:

/**
 * 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;
}

Loads config.xml, enterprise.xml, local.xml, etc..:

/**
 * 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;
}

Loads the individual module configs:

/**
 * 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;
}

Varien_Simplexml_Config (lib/Varien/Simplexml/Config.php)

What actually reads the individual module configs:

/**
 * 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);
}

Dustin Oprea

这篇关于为什么我的Magento模块没有加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:53