Magento Mage:getSingleton 中字符串的含义和位置

meaning and location of string inside Magento's Mage:getSingleton

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

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

但是,我试图找出 class 所在的位置,以及该字符串的具体含义。谁能给我解释一下?

1/型号

你要知道Mage::getSingleton()要送你一个单例(这是一种常见的开发设计模式)。对于 magento,只有模型可以实例化为单例

来自 app/Mage.php 的片段,您可以在其中看到 Magento 实际上在幕后使用 getModel,并且如果您通过 getSingleton 调用它两次(目的单例模式本身,你可能知道)

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 将通过我们所说的句柄将其组件映射到 classes。这些在模块的 config.xml 中定义。

app/code/core/Mage/Checkout/etc/config.xml 的片段从大量 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 添加其 global 配置,对于所有 models 它知道一组新模型可以通过映射到以 Mage_Checkout_Model.

开头的 classes 的 handle checkout 引用

3/ 正确的文件或class

Magento 继承自 Class_To_File_Path

的 Zend Framework 映射

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/en/coding-standard.naming-conventions.html

这意味着像 type_onepage 这样的构造类型将映射到其路径中包含 Type/Onepage.[= 的文件120=] 及其 class 名称 Type_Onepage

4/ 一把手治天下

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

现在你有了模型的句柄,映射到 Mage_Checkout_Model 和你的 class 是 Type_Onepage Magento 可以 assemble 这两个在 class 为 Mage_Checkout_Model_Type_Onepage,文件为 Mage/Checkout/Model/Type/Onepage.php。所以整个句柄 (checkout/type_onepage) 由两部分构成,斜杠前的第一个是模型的句柄(在这种情况下,但也可以是助手或块......控制器有点不同)和第二个,在斜杠之后是文件路径从句柄定义的文件夹 / class 前缀。

5/ 你以为就这些了?

为了在这个解释中完全详尽,您还必须知道模块是通过位于 app/etc/modules 上的 xml 定义的。 由于您要求的是核心模块,因此要查看的文件是 Mage_All.xml,我再次删除了很多代码。

<?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,其中文件名是 [=128= 中 <modules> 节点旁边的句柄名称].但是对于核心,因为有很多模块,所以他们将很多模块分组在 Mage_All.xml.

在这个文件中,您可以看到它与我们之前看到的模块的 config.xml 具有完全相同的开始,因此 Magento 能够匹配这个 config.xml 属于的事实此 Mage_All.xml 文件中定义的模块。 然后您还会看到该模块的 codePool。在这种情况下,core,Magento 的库存模块。但你也可以在那里 communitylocal.

从现在开始,Magento 确实可以正确映射到文件。
您的文件在
app/code/ -- 已修复,所有代码都在
core/ -- 你模块的 codePool
Mage/Checkout/Model/ -- 映射到config.xml中定义的右边class的句柄,然后根据Zend Framework约定翻译成路径
Type/Onepage.php -- 从 type_onepage 映射的文件,再次遵循 Type/Onepage 约定。

一应俱全:

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