开发者

How to find an appropriate event in Magento?

开发者 https://www.devze.com 2023-02-16 03:42 出处:网络
Sometimes when l开发者_Go百科ooking for a convenient event to hook I do a bit of exploratory programming...

Sometimes when l开发者_Go百科ooking for a convenient event to hook I do a bit of exploratory programming...

  • Modify Mage::dispatchEvent with this extra line:

    Mage::log($name.'('.implode(',', array_keys($data)).')');
    
  • Mark a start point which I know I cannot catch any sooner:

    Mage::log(__METHOD__.'::START');
    
  • Mark an end point which I don't want to catch any later:

    Mage::log(__METHOD__.'::STOP');
    
  • Watch the log and step through the site (eg. order submission, whatever is being investigated)

    tailf var/log/system.log
    

This gives me a screen full of boring data and the names of objects being passed. Other than the START and STOP I'm usually not looking for anything specific enough to grep for it and I have to rely on my experience to identify possible bootstrap points. For example when placing orders I know there is often a 'quote' somewhere, or it is possible to get a reference to the order through a 'payment' object, or vice-versa.

Then I have to remember to remove my markers (not that hard when using any sort of versioning).

What methods do you use to find events? Can you do it without modifying core code?


If I'm looking for a specific event, usually I will edit dispatchEvent() in Mage.php and add this to the top(I think these are the right params for log, writing this from memory though):

Mage::log( $name, 1, 'events.txt' );

Then I'll refresh the page, comment out that line to keep the file from getting extra events in it, and then go look at my events.txt file to see all the events that fired for that page load.

It's kind of hacky to be sure, but I've found it useful for finding events with variables as part of their names.


As of 1.2 the event list was curated on the Magento Wiki. You can find that list here:

http://www.magentocommerce.com/wiki/_media/magento_events_v1.2.0.2.xls

However, since then various events have been deprecated. There is a list here but it's only current as of 1.4

http://masteringmagento.com/2010/06/events-list-in-magento-community-1-4/

If you're handy, you can execute grep -R dispatchEvent in your Magento working directory and parse through the dearth of dispatch calls. These are the actual definitons of all Magento events in your particular version.

Edit 2/14/2013:

This list, being a couple of years old, is no longer valid. I suggest that you use the following resource as it is not only a better answer but gives you many examples and sources of finding better event hooks.

https://magento.stackexchange.com/a/167/336


philwinkle already posted a link to my old list, but I'm going to go ahead and post what I use to generate event lists. It's longer than it seems like it should be, but that is because of a general lack of coding standards in the framework. Basically, this code will go out and find all events, and attempt to format them for you. If you want, I can run it on 1.5.0.1 and update the blog (would probably be nice to do after so many months, but time is a fickle mistress).

The code:

$results    = `ack Mage::dispatchEvent $magento 2>/dev/null | grep -v "app/code/local" | grep -v "downloader/pearlib"`;
$results    = explode("\n", $results);
print_error(sprintf("%-100s\t%-4s\t%s\n", "FILE", "LINE", "EVENT"));
foreach($results as $result) {
    if(!strlen(trim($result))) { continue; }

    $matches        = array();
    preg_match("/([^:]+):(\d+):\W+(.*)/", $result, $matches);

    $file           = str_replace($magento, "", $matches[1]);
    $line           = $matches[2];
    $event          = $matches[3];

    $eventMatches   = array();
    if(preg_match("/Mage::dispatchEvent\('(\w+)'\);/", $event, $eventMatches)) {
        $event      = $eventMatches[1];
        $matchType  = 1;
    } else if(preg_match("/Mage::dispatchEvent\('(\w+)',.*/", $event, $eventMatches)) {
        $event      = $eventMatches[1];
        $matchType  = 2;
    } else if(preg_match("/Mage::dispatchEvent\($/", $event)) {
        $event      = get_next_line_event($file, $line+1, $magento);
        $matchType  = 3;
    } else if(preg_match("/Mage::dispatchEvent\(\"?(['\$a-zA-Z._{}\-> ]+).*/", $event, $eventMatches)) {
        $event      = $eventMatches[1];
        $matchType  = 4;
    } else {
        print "Found unmatcheable event:\n";
        var_dump($event);exit;
    }

    printf("%-100s\t%-4s\t%s\n", $file, $line, $event);
}

function get_next_line_event($file, $line, $magento) {
    $cnt        = `cat -n $magento/$file | grep -e "^ *$line"`;
    $cnt        = preg_replace("/^\s*\d*\s*/", "", $cnt);
    $matches    = array();
    if(preg_match("/^'?([\$a-z_>. -]*)'?,$/i", $cnt, $matches)) {
        return $matches[1];
    } else if(preg_match("/^([\$a-z_>. '\-\(\)]*),$/i", $cnt, $matches)) {
        return $matches[1];
    }
    print "Found unmatcheable event:\n";
    var_dump($cnt);exit;
}  

This is part of my homebrew Magento command line toolchain. It will probably only run on Linux, and there may be internal lib functions in there that I can't find. Anyway, hope that gives you an idea about my process!

Thanks, Joseph Mastey


List of events explicitly fired in magento, along with internal implicit ones..

check here


I thought I would post back the code from above, but modified slightly to work right. $magento needed to be assigned, as well as the paths used for grep. Just change /var/www/app to whatever your magento directory is. Copy this script to a file and execute it. You need to have ack-grep installed for it to work properly. Ubuntu users can type "sudo apt-get ack-grep" I believe to install this, or just google ack-grep.

THIS IS A SHELL PHP SCRIPT. IF YOU RUN IT IN A BROWSER, IT LOOKS LIKE A MESS! However, you can do "php whateveryoucallthescript.php >> output.txt" and then open that file in VI or edit it and search for the results you want.

This has been tested on Enterprise 1.11.1.0

<?php
    $magento = "/var/www/app/";
    $results    = `ack-grep Mage::dispatchEvent $magento 2>/dev/null | grep -v "/var/www/app/code/local" | grep -v "/var/www/downloader/pearlib"`;
    $results    = explode("\n", $results);

    print_error(sprintf("%-100s\t%-4s\t%s\n", "FILE", "LINE", "EVENT"));

    foreach($results as $result) {
        if(!strlen(trim($result))) { continue; }

        $matches        = array();
        preg_match("/([^:]+):(\d+):\W+(.*)/", $result, $matches);

        $file           = str_replace($magento, "", $matches[1]);
        $line           = $matches[2];
        $event          = $matches[3];

        $eventMatches   = array();
        if(preg_match("/Mage::dispatchEvent\('(\w+)'\);/", $event, $eventMatches)) {
            $event      = $eventMatches[1];
            $matchType  = 1;
        } else if(preg_match("/Mage::dispatchEvent\('(\w+)',.*/", $event, $eventMatches)) {
            $event      = $eventMatches[1];
            $matchType  = 2;
        } else if(preg_match("/Mage::dispatchEvent\($/", $event)) {
            $event      = get_next_line_event($file, $line+1, $magento);
            $matchType  = 3;
        } else if(preg_match("/Mage::dispatchEvent\(\"?(['\$a-zA-Z._{}\-> ]+).*/", $event, $eventMatches)) {
            $event      = $eventMatches[1];
            $matchType  = 4;
        } else {
            print "Found unmatcheable event:\n";
            var_dump($event);
        }

        printf("%-100s\t%-4s\t%s\n", $file, $line, $event);
    }

    function get_next_line_event($file, $line, $magento) {
        $cnt        = `cat -n $magento/$file | grep -e "^ *$line"`;
        $cnt        = preg_replace("/^\s*\d*\s*/", "", $cnt);
        $matches    = array();
        if(preg_match("/^'?([\$a-z_>. -]*)'?,$/i", $cnt, $matches)) {
            return $matches[1];
        } else if(preg_match("/^([\$a-z_>. '\-\(\)]*),$/i", $cnt, $matches)) {
            return $matches[1];
        }
        print "Found unmatcheable event:\n";
        var_dump($cnt);exit;
    }  

    function print_error($err) {
        echo $err;
    }

    ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消