Why does Magento core override not register until third request?

194 views Asked by At

I am overriding the Mage_Core_Model_Encryption in my module to override hash, getHash, and validateHash methods.

Since urls in the admin side of things use hashing as well, I am overriding the Mage_Adminhtml_Model_Url getSecretKey method so that it will use (faster) md5 hashing in the urls.

The problem I have noticed is that when I clear my cache (config only is enabled) and load the admin/index my encyptor is not registered. It takes until the third request for it to be seen.

I have debugging statements in Mycompany_Encryption_Adminhtml_Url which show that it is immediately loaded with the module. After clearing my cache, I can see my debug statements are working.

When I use the following statement:

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

I see returned to admin/index (upon refreshing the page) that the class name is Mage_Core_Model_Encryption, then on the third refresh it shows me my class, Mycompany_Encryption_Model_Encryption.

Why does this take three requests? I have been searching all over and haven't found/figured out the problem yet. By all appearances, I have this configured correctly ( and have experimented with a bunch of alternatives).

Can anyone help me solve this issue?

(Below are config, Url class, and Encryption class snippets). Big thanks in advance for any help.

Update, am using modman, here is my modman too:

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

Here is my config:

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

Url class:

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

Encryption class:

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);
    }
}
1

There are 1 answers

0
Qoheleth-Tech On

Using the n98-magerun tool, I was able to config:dump and search for the global/helpers/core node.

With the above configuration, I was getting this badly formed config dump:

 $ 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>

It appears to clear up if I remove the <class> tags in my config.xml:

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

With this change, I clear cache and get a better config dump:

$ 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>

Please comment with a doc link or provide info if you know why this was happening! Thanks!