Events 如何在Magento中找到合适的事件?
有时,当寻找一个方便的事件来钩住我做了一些探索性的编程Events 如何在Magento中找到合适的事件?,events,magento,coding-style,exploratory,Events,Magento,Coding Style,Exploratory,有时,当寻找一个方便的事件来钩住我做了一些探索性的编程 使用此额外行修改Mage::dispatchEvent: Mage::log($name.'('.implode(',', array_keys($data)).')'); 标记一个起点,我知道我不能很快抓住它: Mage::log(__METHOD__.'::START'); 标记一个我不想在以后抓住的终点: Mage::log(__METHOD__.'::STOP'); 查看日志并逐步浏览站点(例如,订单提交,任何正在调查
- 使用此额外行修改
:Mage::dispatchEvent
Mage::log($name.'('.implode(',', array_keys($data)).')');
- 标记一个起点,我知道我不能很快抓住它:
Mage::log(__METHOD__.'::START');
- 标记一个我不想在以后抓住的终点:
Mage::log(__METHOD__.'::STOP');
- 查看日志并逐步浏览站点(例如,订单提交,任何正在调查的内容)
开始
和停止
之外,我通常不会寻找任何足够具体的东西来grep它,我必须依靠我的经验来确定可能的引导点。例如,在下订单时,我知道某个地方经常有一个“报价”,或者可以通过“付款”对象获取订单的引用,反之亦然
然后我必须记住删除我的标记(在使用任何类型的版本控制时没有那么难)
您使用什么方法查找事件?您能在不修改核心代码的情况下完成吗?从1.2开始,事件列表在Magento Wiki上进行了策划。您可以在此处找到该列表: 然而,从那时起,各种事件就被弃用了。这里有一个列表,但它仅在1.4中是最新的 如果方便的话,可以在Magento工作目录中执行
grep-R dispatchEvent
,并通过缺少调度调用进行解析。这些是特定版本中所有Magento事件的实际定义
2013年2月14日编辑:
这份名单已经有几年的历史了,不再有效。我建议您使用以下资源,因为它不仅是一个更好的答案,而且为您提供了许多寻找更好事件挂钩的示例和来源
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上运行,其中可能有我找不到的内部lib函数。无论如何,希望这能让你了解我的流程
谢谢,
Joseph Mastey如果我正在查找特定事件,通常我会在Mage.php中编辑dispatchEvent(),并将其添加到顶部(我认为这些是日志的正确参数,不过是从内存写入的): 然后我将刷新页面,注释掉该行以防止文件中出现额外事件,然后查看我的events.txt文件以查看为该页面加载触发的所有事件
可以肯定,这是一个有点粗糙的方法,但我发现它对于查找名称中包含变量的事件非常有用。我想我会从上面发回代码,但稍微修改一下,以便正常工作$需要分配magento以及用于grep的路径。只需将/var/www/app更改为您的magento目录即可。将此脚本复制到文件并执行它。您需要安装ack grep才能正常工作。Ubuntu用户可以输入“sudo apt get ack grep”,我相信可以安装它,或者只需输入谷歌ack grep 这是一个SHELL PHP脚本。如果你在浏览器中运行它,它看起来就像一团乱!但是,您可以执行“php whateveryoucallthescript.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;
}
?>
magento中显式激发的事件列表,以及内部隐式激发的事件列表
检查我不识别
print\u error
函数,但我可以猜出它的作用。这些列表对查找“适当”事件没有多大帮助。前缀和后缀都是变量,因此列表不会解释这些值可能是什么。它也没有解释当我认为它不会触发时它是否会触发,也没有解释它是否包含正确类型的信息。不管怎样,还是谢谢你。没什么帮助,但足够接近你要找的东西了。谢谢你的接受。我的问题有点主观,不太清楚堆栈溢出是为了什么。
Mage::log( $name, 1, 'events.txt' );
<?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;
}
?>