2011-1-2----------mage:getModuleDir函数的分析!!--------得到模块的地址

来源:互联网 发布:win7更改远程桌面端口 编辑:程序博客网 时间:2024/06/05 18:32

 

1.

 

得到模块的地址!!!!说

 /**

     * Retrieve module absolute path by directory type

     *

     * @param string $type

     * @param string $moduleName

     * @return string

     */

    public static function getModuleDir($type, $moduleName)

    {

        return self::getConfig()->getModuleDir($type, $moduleName);

    }

2.

Mage_Core_Model_Config 

 /**

     * Get module directory by directory type

     *

     * @param   string $type

     * @param   string $moduleName

     * @return  string

     */

    public function getModuleDir($type, $moduleName)

    {

        $codePool = (string)$this->getModuleConfig($moduleName)->codePool;

        $dir = $this->getOptions()->getCodeDir().DS.$codePool.DS.uc_words($moduleName, DS);

 

        switch ($type) {

            case 'etc':

                $dir .= DS.'etc';

                break;

 

            case 'controllers':

                $dir .= DS.'controllers';

                break;

 

            case 'sql':

                $dir .= DS.'sql';

                break;

 

            case 'locale':

                $dir .= DS.'locale';

                break;

        }

 

        $dir = str_replace('/', DS, $dir);

        return $dir;

    }

 

2.1

 $codePool = (string)$this->getModuleConfig($moduleName)->codePool;

2.2

$this->getOptions()->getCodeDir().DS.$codePool.DS.uc_words($moduleName, DS);

 

 

 

3

接2.1

 $codePool = (string)$this->getModuleConfig($moduleName)->codePool;

 

 /**

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

        }

    }

 

3.1

 

$modules = $this->getNode('modules');

 

 

 /**

     * Returns node found by the $path and scope info

     *

     * @param   string $path

     * @param   string $scope

     * @param   string $scopeCode

     * @return Mage_Core_Model_Config_Element

     */

    public function getNode($path=null, $scope='', $scopeCode=null)

    {

        if ($scope !== '') {

            if (('store' === $scope) || ('website' === $scope)) {

                $scope .= 's';

            }

            if (('default' !== $scope) && is_int($scopeCode)) {

                if ('stores' == $scope) {

                    $scopeCode = Mage::app()->getStore($scopeCode)->getCode();

                } elseif ('websites' == $scope) {

                    $scopeCode = Mage::app()->getWebsite($scopeCode)->getCode();

                } else {

                    Mage::throwException(Mage::helper('core')->__('Unknown scope "%s".', $scope));

                }

            }

            $path = $scope . ($scopeCode ? '/' . $scopeCode : '' ) . (empty($path) ? '' : '/' . $path);

        }

 

        /**

         * Check path cache loading

         */

        if ($this->_useCache && ($path !== null)) {

            $path   = explode('/', $path);

            $section= $path[0];

            if (isset($this->_cacheSections[$section])) {

                $res = $this->getSectionNode($path);

                if ($res !== false) {

                    return $res;

                }

            }

        }

        return parent::getNode($path);

    }

 

3.2

 

 return parent::getNode($path);

public function getNode($path=null)

    {

        if (!$this->_xml instanceof Varien_Simplexml_Element) {

            return false;

        } elseif ($path === null) {

            return $this->_xml;

        } else {

            return $this->_xml->descend($path);

        }

    }

 

 

3.3

 

return $this->_xml->descend($path);

 

3.4

 

 

/**

     * Find a descendant of a node by path

     *通过path找到这个node的后代。

     * @todo    Do we need to make it xpath look-a-like?

     * @todo    Check if we still need all this and revert to plain XPath if this makes any sense

     * @todo    param string $path Subset of xpath. Example: "child/grand[@attrName='attrValue']/subGrand"

     * @param   string $path Example: "child/grand@attrName=attrValue/subGrand" (to make it faster without regex)

     * @return  Varien_Simplexml_Element

     */

    public function descend($path)

    {

        # $node = $this->xpath($path);

        # return $node[0];

        if (is_array($path)) {

            $pathArr = $path;

        } else {

            // Simple exploding by / does not suffice,

            // as an attribute value may contain a / inside

            // Note that there are three matches for different kinds of attribute values specification

            if(strpos($path, "@") === false) {

                $pathArr = explode('/', $path);

            }

            else {

                $regex = "#([^@////"]+(?:@[^=/]+=(?:///"[^///"]*///"|[^/]*))?)/?#";

                $pathArr = $pathMatches = array();

                if(preg_match_all($regex, $path, $pathMatches)) {

                    $pathArr = $pathMatches[1];

                }

            }

        }

        $desc = $this;

        foreach ($pathArr as $nodeName) {

            if (strpos($nodeName, '@')!==false) {

                $a = explode('@', $nodeName);

                $b = explode('=', $a[1]);

                $nodeName = $a[0];

                $attributeName = $b[0];

                $attributeValue = $b[1];

                //

                // Does a very simplistic trimming of attribute value.

                //

                $attributeValue = trim($attributeValue, '"');

                $found = false;

                foreach ($desc->$nodeName as $subdesc) {

                    if ((string)$subdesc[$attributeName]===$attributeValue) {

                        $found = true;

                        $desc = $subdesc;

                        break;

                    }

                }

                if (!$found) {

                    $desc = false;

                }

            } else {

                $desc = $desc->$nodeName;

            }

            if (!$desc) {

                return false;

            }

        }

        return $desc;

    }

 

 

 

 

 

descend()函数的作用是:通过path找到这个node的后代。

$this->getNode('modules');

$this->_xml->descend($path);

 

4

$codePool = (string)$this->getModuleConfig($moduleName)->codePool;

 

//return  Varien_Simplexml_Object

$this->getModuleConfig($moduleName)

 

//return Varien_Simplexml_Element

$this->getNode('modules');

 

Varien_Simplexml_Element通过方法getNode('fdfd')[$this->_xml->descend($path)],($this为mage_core_model_config),得到$modules,

$modules为Varien_Simplexml_Element类型。

然后通过$moduleConfig=$modules->$moduleName,得到对应的模块的配置

,$moduleConfig为Varien_Simplate_object类型。

 

5

$codePool = (string)$this->getModuleConfig($moduleName)->codePool;

就是执行  <codePool>core</codePool>,里面的值一般为core,community,或者local。

6

接2

 

$dir = $this->getOptions()->getCodeDir().DS.$codePool.DS.uc_words($moduleName, DS);

6.1

$this->getOptions()

Mage_Core_Model_Config_Options

 

 protected function _construct()

    {

        $appRoot= Mage::getRoot();

        $root   = dirname($appRoot);

 

        $this->_data['app_dir']     = $appRoot;

        $this->_data['base_dir']    = $root;

        $this->_data['code_dir']    = $appRoot.DS.'code';

        $this->_data['design_dir']  = $appRoot.DS.'design';

        $this->_data['etc_dir']     = $appRoot.DS.'etc';

        $this->_data['lib_dir']     = $root.DS.'lib';

        $this->_data['locale_dir']  = $appRoot.DS.'locale';

        $this->_data['media_dir']   = $root.DS.'media';

        $this->_data['skin_dir']    = $root.DS.'skin';

        $this->_data['var_dir']     = $this->getVarDir();

        $this->_data['tmp_dir']     = $this->_data['var_dir'].DS.'tmp';

        $this->_data['cache_dir']   = $this->_data['var_dir'].DS.'cache';

        $this->_data['log_dir']     = $this->_data['var_dir'].DS.'log';

        $this->_data['session_dir'] = $this->_data['var_dir'].DS.'session';

        $this->_data['upload_dir']  = $this->_data['media_dir'].DS.'upload';

        $this->_data['export_dir']  = $this->_data['var_dir'].DS.'export';

    }

都是些配置信息。

  public function getCodeDir()

    {

        //return $this->getDataSetDefault('code_dir', $this->getAppDir().DS.'code');

        return $this->_data['code_dir'];

    }

 

 $this->_data['code_dir']    = $appRoot.DS.'code';

 

如果module地址为app/code/core/mage/cms

 

则:

$this->getOptions()->getCodeDir() = 'app/code';

 

$codePool,上面已经解释--->core

 

uc_words($moduleName, DS);----->Cms、

 

故,上面步骤可以得出该模块的地址来

 

************************************************************

使用的函数总结:

Mage_Core_Model_Config.php文件:

1.

//Returns node found by the $path and scope info

//得到modules的节点

$this->getNode('modules');

2

//得到cms这个模块的配置块。

$this->getNode('modules')->cms,得到cms这个模块的配置块,返回类型为:Varien_Simplexml_Object

 

3

//getNode()函数的原理。。。

$this->_xml->descend($path);

$_xml变量是:Varien_Simplexml_Element类型

//Find a descendant of a node by path

descend($path)为Varien_Simplexml_Element类里面的函数。譬如:$path='module'.

4

//return Varien_Simplexml_Object

getModuleConfig()

原理:$this->getNode('modules')->$moduleName.

譬如:$this->getNode('modules')->cms.

将得到cms块的全部配置文件

 

5

想得到配置文件下某个标签的数值:譬如:<codePool>core</codePool>

想得到这个标签的值,可以通过:getModuleConfig()方法得到,

譬如:

################################################

(string)$this->getModuleConfig('cms')->codePool

等价于

$this->_xml->descend('modules')->cms->codePool;

##################################################

得到cms这个模块位于哪个池。

 

 

6

getModuleDir($type, $moduleName)

$type从"etc,controllers,sql,locale"中取值。

$moduleName就想要取得的模块的名字。

 

譬如:getModuleDir('ect','cms')

会得到地址:app/code/core/mage/cms