为什么Magento核心覆盖直到第三个请求才注册


Why does Magento core override not register until third request?

我正在覆盖模块中的Mage_Core_Model_Encryption以覆盖哈希,getHash和validateHash方法。

由于管理方面的 url 也使用哈希,因此我正在覆盖 Mage_Adminhtml_Model_Url getSecretKey 方法,以便它将在 url 中使用(更快的)md5 哈希。

我注意到的问题是,当我清除缓存(仅启用配置)并加载管理员/索引时,我的加密器未注册。直到第三次请求才能看到它。

我在Mycompany_Encryption_Adminhtml_Url中有调试语句,显示它立即与模块一起加载。清除缓存后,我可以看到我的调试语句正在工作。

当我使用以下语句时:

var_dump('Class name: '.get_class(Mage::helper('core')->getEncryptor() ) );

我看到返回到管理员/索引(刷新页面时)类名Mage_Core_Model_Encryption,然后在第三次刷新时它向我显示我的类,Mycompany_Encryption_Model_Encryption .

为什么这需要三个请求?我一直在搜索,还没有找到/弄清楚问题所在。从表面上看,我已经正确配置了这个(并尝试了一堆替代方案)。

谁能帮我解决这个问题?

(下面是配置、URL 类和加密类代码段)。提前非常感谢任何帮助。

更新,我正在使用 modman,这也是我的 modman:

# Modman file allows Modman to generate links in Magento.
code                        app/code/local/Mycompany/Encryption/
Mycompany_Encryption.xml   app/etc/modules/Mycompany_Encryption.xml

这是我的配置:

<config>
  <modules>
    <Mycompany_Encryption>
      <version>0.1</version>
      <depends>
        <Mage_Core />
      </depends>
    </Mycompany_Encryption>
  </modules>
  <phpunit>
    <suite>
      <modules>
        <Mycompany_Encryption />
      </modules>
    </suite>
  </phpunit>
  <global>
    <helpers>
      <core>
        <encryption_model>
          <class>Mycompany_Encryption_Model_Encryption</class>
        </encryption_model>
      </core>
    </helpers>
    <models>
      <mycompany_encryption>
        <class>Mycompany_Encryption_Model</class>
      </mycompany_encryption>
      <core>
        <rewrite>
          <encryption>Mycompany_Encryption_Model_Encryption</encryption>
        </rewrite>
      </core>
      <adminhtml>
        <rewrite>
          <url>Mycompany_Encryption_Model_Adminhtml_Url</url>
        </rewrite>
      </adminhtml>
    </models>
  </global>
</config>

网址类:

class Mycompany_Encryption_Model_Adminhtml_Url extends Mage_Adminhtml_Model_Url
{
    /**
     * Generate secret key for controller and action based on form key
     *
     * @param string $controller Controller name
     * @param string $action Action name
     * @return string
     */
    public function getSecretKey($controller = null, $action = null)
    {
        $salt = Mage::getSingleton('core/session')->getFormKey();
        $p = explode('/', trim($this->getRequest()->getOriginalPathInfo(), '/'));
        if (!$controller) {
            var_dump('Not Controller');
            $controller = !empty($p[1]) ? $p[1] : $this->getRequest()->getControllerName();
        }
        if (!$action) {
            var_dump('Not Action');
            $action = !empty($p[2]) ? $p[2] : $this->getRequest()->getActionName();
        }
        $secret = $controller . $action . $salt;
        // Here are my debug statements showing when class/method become available.
        var_dump('Method exists: '.method_exists(Mage::helper('core')->getEncryptor(), 'urlHash')); 
        var_dump('Class name: '.get_class(Mage::helper('core')->getEncryptor() ) );
        /* This is what I want to return - but sends error 500 until instantiated. */
        //return Mage::helper('core')->getEncryptor()->urlHash($secret);
        return false;
    }
}

加密类:

class Mycompany_Encryption_Model_Encryption extends Mage_Core_Model_Encryption
{
    public function hash($data)
    {
        return password_hash($data, PASSWORD_BCRYPT);
    }
    public function validateHash($password, $hash)
    {
        return password_verify($password, $hash);
    }
    public function getHash($value)
    {
        return $this->hash($value);
    }
    public function urlHash($value)
    {
        return md5($value);
    }
}

使用 n98-magerun 工具,我能够配置:dump 并搜索 global/helpers/core 节点。

使用上述配置,我得到了这个格式不正确的配置转储:

 $ n98-magerun config:dump |cat |grep -A1 -B2 encryption_model
<helpers>
  <core>
    <encryption_model>Mage_Core_Model_Encryption<class>Mycompany_Encryption_Model_Encryption</class></encryption_model>
  </core>

如果我删除配置中的<class>标签,它似乎会清除.xml:

<helpers>
  ...
  <core>
    <!-- Class tags were surrounding my encryption_model value before -->       
    <encryption_model>Mycompany_Encryption_Model_Encryption</encryption_model>
  </core>
  ...
</helpers>

通过此更改,我清除缓存并获得更好的配置转储:

$ n98-magerun cache:clean config
config cache cleaned
$ n98-magerun config:dump |cat |grep -A1 -B2 encryption_model
<helpers>
  <core>
    <encryption_model>Mycompany_Encryption_Model_Encryption</encryption_model>
  </core>

如果您知道为什么会发生这种情况,请使用文档链接发表评论或提供信息!谢谢!