Magento法师中字符串的含义和位置:getSingleton [英] meaning and location of string inside Magento's Mage:getSingleton

查看:71
本文介绍了Magento法师中字符串的含义和位置:getSingleton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个在Magento中我看到很多相似的字符串:

Here is a string I see a lot of similar in Magento:

Mage::getSingleton('checkout/type_onepage');

但是,我试图找出该类的位置以及该字符串的具体含义.谁能向我解释一下?

However, I'm trying to find out where that class is located, and what the meaning of the string is specifically. Can anyone explain this to me?

推荐答案

1/模型

您必须知道Mage::getSingleton()将向您发送单例(这是常见的开发设计模式).对于magento,只能将Models实例化为Singleton

1/ Model

You have to know that Mage::getSingleton() is going to send you a singleton (which is a common development design pattern). For magento, only Models can be instantiated as a Singleton

app/Mage.php中的片段,您可以在其中看到Magento实际上是在后台使用getModel,并且如果您通过getSingleton两次调用它,也可以将其注册为该模型的单个实例(单例模式本身的目的,如您所知)

Snippet from app/Mage.php where you can see that Magento is actually using getModel behind the scene, and also register it to have a single instance of this model if you call it twice via getSingleton (the purpose of the singleton pattern itself, as you may know)

public static function getSingleton($modelClass='', array $arguments=array())
{
    $registryKey = '_singleton/'.$modelClass;
    if (!self::registry($registryKey)) {
        self::register($registryKey, self::getModel($modelClass, $arguments));
    }
    return self::registry($registryKey);
}

2/手柄

Magento将通过我们称为句柄的方式将其组件映射到类.这些是在模块的config.xml中定义的.

2/ Handle

Magento is going to map its components to classes via what we call handles. Those are defined in the config.xml of the modules.

app/code/core/Mage/Checkout/etc/config.xml中的片段摘录了许多xml

Snippet from app/code/core/Mage/Checkout/etc/config.xml stripped from a lot of xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mage_Checkout>
            <version>1.6.0.0</version>
        </Mage_Checkout>
    </modules>
    <global>
        <models>
            <checkout>
                <class>Mage_Checkout_Model</class>
            </checkout>
        </models>
    </global>
</config>

此代码段指示magento在其全局配置中添加,对于所有模型,它知道可以通过句柄引用的一组新模型结帐,它映射到以 Mage_Checkout_Model 开头的类.

This snippet instruct to magento to add at its global configuration, for all the models it knows a new set of model that could be referenced through the handle checkout which maps to the classes beginning with Mage_Checkout_Model.

Magento确实继承了Class_To_File_Path的Zend Framework映射

Magento does inherit from Zend Framework mapping of Class_To_File_Path

类名只能包含字母数字字符.类名中允许使用数字,但在大多数情况下不建议使用数字.仅允许使用下划线代替路径分隔符;文件名"Zend/Db/Table.php"必须映射到类名"Zend_Db_Table".

Class names may only contain alphanumeric characters. Numbers are permitted in class names but are discouraged in most cases. Underscores are only permitted in place of the path separator; the filename "Zend/Db/Table.php" must map to the class name "Zend_Db_Table".

来源: http://framework.zend. com/manual/1.12/zh-CN/coding-standard.naming-conventions.html

这意味着 type_onepage 之类的构造类型将映射到路径为 Type/Onepage.php 并且其类名为 Type_Onepage

That means that the type of construction like type_onepage would map to a file having in its path Type/Onepage.php and its class name Type_Onepage

(抱歉,我不得不以某种方式放置双关语).

(I had to place that pun somehow, sorry.)

现在,您有了映射到Mage_Checkout_Model的模型的句柄,并且您的类为Type_Onepage的类,Magento可以将这两个类组合为一个类,即Mage_Checkout_Model_Type_Onepage和一个文件,该文件为Mage/Checkout/Model/Type/Onepage.php.因此,整个句柄( checkout/type_onepage )由两部分构成,斜杠前的第一个部分是模型的句柄(在这种情况下,也可以是辅助对象或块的句柄). ..控制器略有不同)和第二个不同,斜杠后是句柄定义的文件夹/类前缀中文件的路径.

Now you have the handle to you model which map to Mage_Checkout_Model and your class which is Type_Onepage Magento can assemble those two in a class being Mage_Checkout_Model_Type_Onepage and to a file being Mage/Checkout/Model/Type/Onepage.php. So this whole handle (checkout/type_onepage) is constructed from two part, the first one before the slash is the handle to a model (in this case, but could also be to an helper or a block... controller are a little different) and the second one, after the slash being the path to the file from the folder / class prefix defined by the handle.

要在本说明中进行全面介绍,您还必须知道模块是通过位于app/etc/modules上的xml定义的. 由于您要的是核心模块,因此要查看的文件是Mage_All.xml,我再次剥离了很多代码.

To be completely extensive in this explanation you also have to know that modules are defined via an xml which stands on app/etc/modules. Since you are asking for a core module, the file to look at is Mage_All.xml which I, once again stripped of a lot of code.

<?xml version="1.0"?>
<config>
    <modules>
        <Mage_Checkout>
            <active>true</active>
            <codePool>core</codePool>
            <depends>
                <Mage_Sales/>
                <Mage_CatalogInventory/>
            </depends>
        </Mage_Checkout>
    </modules>
</config>

对于其他模块,推荐的方法是拥有文件app/code/Mage_Checkout.xml,其中文件名是xml中<modules>节点旁边的句柄名称.但对于核心,由于有很多模块,因此它们将许多模块分组在Mage_All.xml中.

For other module the recommended way to do it is by having a file app/code/Mage_Checkout.xml where the name of the file is the name of the handle beside the <modules> node in the xml. But for the core, since there is a lot of module they grouped a lot of them in Mage_All.xml.

在此文件中,您可以看到它的起步与我们先前看到的模块的config.xml完全相同,因此Magento可以匹配这个config.xml属于此<中定义的此模块的事实. c8>文件. 然后,您还会看到该模块的 codePool .在这种情况下,核心是Magento的库存模块.但是您也可以在这里使用社区本地.

In this file you can see that it has fairly the same start as the config.xml of our module we saw earlier, so Magento would be able to match the fact that this config.xml belongs to this module defined in this Mage_All.xml file. Then you also see the codePool of that module. In this case, core, the stock modules of Magento. But you could also have there community or local.

从现在开始,Magento可以真正正确地映射到文件.
您的档案位于
app/code/-已修复,所有代码都在其中.
core/-模块的 codePool
Mage/Checkout/Model/-句柄映射到 config.xml 中定义的正确类,然后转换为基于Zend Framework约定的路径
Type/Onepage.php-从type_onepage映射的文件,再次遵循类型/一页"约定.

For now on, Magento can really map to the file correctly.
You file is in
app/code/ -- fixed, all the code is there.
core/ -- the codePool of your module
Mage/Checkout/Model/ -- the handle mapped to the right class defined in config.xml and then translated to a path based on Zend Framework convention
Type/Onepage.php -- The file mapped from type_onepage, following once again Type/Onepage convention.

echo get_class(Mage::getSingleton('checkout/type_onepage'));
// will output Mage_Checkout_Model_Type_Onepage
// which is located at app/code/core/Mage/Checkout/Model/Type/Onepage.php

这篇关于Magento法师中字符串的含义和位置:getSingleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆