如何在 Magento 中找到合适的事件?

发布于 2024-10-21 17:21:05 字数 723 浏览 4 评论 0原文

有时,当寻找方便的事件来挂钩时,我会进行一些探索性编程...

  • 使用以下额外行修改Mage::dispatchEvent

    Mage::log($name.'('.implode(',', array_keys($data)).')');
    
  • 标记一个我知道我无法更快捕获的起点:

    Mage::log(__METHOD__.'::START');
    
  • 标记一个我以后不想捕获的终点:

    Mage::log(__METHOD__.'::STOP');
    
  • 观察日志并逐步浏览网站(例如,订单提交,正在调查的任何内容)

    tailf var/log/system.log
    

这给了我一个充满无聊数据和正在传递的对象名称的屏幕。除了 STARTSTOP 之外,我通常不会寻找任何足够具体的东西来 grep ,我必须依靠我的经验来识别可能的引导点。例如,在下订单时,我知道某处经常有“报价”,或者可以通过“付款”对象获取对订单的引用,反之亦然。

然后我必须记住删除我的标记(使用任何类型的版本控制时并不难)。

您使用什么方法来查找事件?不修改核心代码能做到吗?

Sometimes when looking 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦魇绽荼蘼 2024-10-28 17:21:05

如果我正在寻找一个特定的事件,通常我会在 Mage.php 中编辑dispatchEvent()并将其添加到顶部(我认为这些是日志的正确参数,不过从内存中编写):

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

然后我会刷新页面,注释掉该行以防止文件在其中获取额外的事件,然后查看我的 events.txt 文件以查看为该页面加载触发的所有事件。

确实,这有点老套,但我发现它对于查找名称中包含变量的事件很有用。

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.

留一抹残留的笑 2024-10-28 17:21:05

从 1.2 开始,事件列表在 Magento Wiki 上进行整理。您可以在此处找到该列表:

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

但是,从那时起,各种事件已被弃用。这里有一个列表,但仅在 1.4 版中有效

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

如果你方便的话,你可以执行grep -RdispatchEvent 在您的 Magento 工作目录中并解析调度调用的缺乏。这些是您特定版本中所有 Magento 事件的实际定义。

2013 年 2 月 14 日编辑:

此列表已有几年历史,不再有效。我建议您使用以下资源,因为它不仅是一个更好的答案,而且为您提供了许多寻找更好事件挂钩的示例和来源。

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

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

自演自醉 2024-10-28 17:21:05

philwinkle 已经发布了我的旧列表的链接,但我将继续发布我用来生成事件列表的内容。它比看起来应该的要长,但这是因为框架中普遍缺乏编码标准。基本上,此代码将查找所有事件,并尝试为您格式化它们。如果你愿意,我可以在 1.5.0.1 上运行它并更新博客(几个月后这样做可能会很好,但时间是善变的情妇)。

代码:

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

这是我自制的 Magento 命令行工具链的一部分。它可能只能在 Linux 上运行,并且其中可能有我找不到的内部库函数。无论如何,希望能让您了解我的流程!

谢谢,
约瑟夫·马斯蒂

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

书间行客 2024-10-28 17:21:05

magento 中显式触发的事件列表,以及内部隐式事件。

检查 此处

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

check here

泪意 2024-10-28 17:21:05

我想我会发回上面的代码,但稍作修改即可正常工作。需要分配 $magento 以及 grep 使用的路径。只需将 /var/www/app 更改为您的 magento 目录即可。将此脚本复制到文件并执行它。您需要安装 ack-grep 才能正常工作。 Ubuntu 用户可以输入“sudo apt-get ack-grep”(我相信)来安装它,或者只是谷歌 ack-grep。

这是一个 PHP 外壳脚本。如果你在浏览器中运行它,它看起来就像一团糟!但是,您可以执行“phpwhateveryoucallthescript.php>>output.txt”,然后在 VI 中打开该文件或编辑它并搜索您想要的结果。

这已经在 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;
    }

    ?>

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

    ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文