如何获得所有类重写的列表?
-
16-10-2019 - |
题
除了查看所有配置文件外,还有一种方法可以列出所有重写以及其他潜在冲突吗?我必须分析一些具有许多扩展和自定义修改的项目,并希望尽可能多地自动化。
最重要的是要检测重写同一类的扩展,但是我也想列出所有重写的列表,以保留概述。目前,我在电子表格中手动维护此列表。
我发现 此扩展(“扩展冲突”) 在Magento Connect上,但从评论和发行说明来看,它似乎已经过时了。
解决方案
看看 N98-Magerun实用程序:
重写列表
列出所有注册的类重写:
$ n98-magerun.phar dev:module:rewrite:list
重写冲突
列出所有重复的重写,并告诉您Magento加载哪个类。该命令按模块依赖项顺序检查类继承。
n98-magerun.phar dev:module:rewrite:conflicts [--log-junit="..."]
如果设置了带有 - log-junit选项的文件名,则该工具将生成XML文件,而不会输出输出。
您还可以将冲突记录到JUNIT样式XML文件中以进行进一步分析,例如在继续集成服务器上。
免责声明:半自行链接 /我参与了该项目
其他提示
在这里,一个小的单线,可为您提供所有活动重写:
print_r(Mage::getConfig()->getNode()->xpath('//global//rewrite'));
为了通过对象类型限制它,请分别将模型,块或帮助者添加到XPath。
例如:
Mage::getConfig()->getNode()->xpath('//global/models//rewrite')
这是我用来检查是否覆盖任何模型,块或助手的小脚本。不幸的是,它对控制器不起作用,也考虑了残疾模块。但是从我的角度来看,这没什么大不了的。
主要想法是解析配置文件并寻找 <rewrite>
标签。在与 index.php
. 。让我们称之为 rewrites.php
, ,此内容:
<?php
$folders = array('app/code/local/', 'app/code/community/');//folders to parse
$configFiles = array();
foreach ($folders as $folder){
$files = glob($folder.'*/*/etc/config.xml');//get all config.xml files in the specified folder
$configFiles = array_merge($configFiles, $files);//merge with the rest of the config files
}
$rewrites = array();//list of all rewrites
foreach ($configFiles as $file){
$dom = new DOMDocument;
$dom->loadXML(file_get_contents($file));
$xpath = new DOMXPath($dom);
$path = '//rewrite/*';//search for tags named 'rewrite'
$text = $xpath->query($path);
foreach ($text as $rewriteElement){
$type = $rewriteElement->parentNode->parentNode->parentNode->tagName;//what is overwritten (model, block, helper)
$parent = $rewriteElement->parentNode->parentNode->tagName;//module identifier that is being rewritten (core, catalog, sales, ...)
$name = $rewriteElement->tagName;//element that is rewritten (layout, product, category, order)
foreach ($rewriteElement->childNodes as $element){
$rewrites[$type][$parent.'/'.$name][] = $element->textContent;//class that rewrites it
}
}
}
echo "<pre>";print_r($rewrites);
在浏览器中调用它时,您应该看到这样的东西:
Array
(
[models] => Array
(
[core/layout] => Array
(
[0] => Namespace_Module_Model_Core_Layout
[1] => Namespace1_Module1_Model_Core_Layout //if the second element is present it means there is a possible conflict
)
[...] => ....
)
[blocks] => ...
[helpers] => ...
)
这意味着模型 'core/layout'
被覆盖 Namespace_Module_Model_Core_Layout
如果您在数组['Core/Layout']中有2个或更多值,则意味着存在冲突。
您可以轻松识别基于 Namespace
和 Module
我把答案都结合在一起,得到了一个不错的解决方案
$text = Mage::getConfig()->getNode()->xpath('//global//rewrite');
foreach ($text as $rewriteElement) {
if ($rewriteElement->getParent()->getParent()) {
# what is overwritten (model, block, helper)
$type = $rewriteElement->getParent()->getParent()->getName();
# module identifier that is being rewritten (core, catalog, sales, ...)
$parent = $rewriteElement->getParent()->getName();
# element that is rewritten (layout, product, category, order)
$name = $rewriteElement->getName();
foreach ($rewriteElement->children() as $element) {
# class that rewrites it
$rewrites[$type][$parent.'/'.$name][] = $element;
}
}
}
print_r($rewrites);
die;
也许有点开销,但是可以使用Varien数据收集...代码 https://github.com/firegento/firegento-debug
$collection = new Varien_Data_Collection();
$fileName = 'config.xml';
$modules = Mage::getConfig()->getNode('modules')->children();
$rewrites = array();
foreach ($modules as $modName => $module) {
if ($module->is('active')) {
$configFile = Mage::getConfig()->getModuleDir('etc', $modName) . DS . $fileName;
if (file_exists($configFile)) {
$xml = file_get_contents($configFile);
$xml = simplexml_load_string($xml);
if ($xml instanceof SimpleXMLElement) {
$rewrites[$modName] = $xml->xpath('//rewrite');
}
}
}
}
foreach ($rewrites as $rewriteNodes) {
foreach ($rewriteNodes as $n) {
$nParent = $n->xpath('..');
$module = (string)$nParent[0]->getName();
$nSubParent = $nParent[0]->xpath('..');
$component = (string)$nSubParent[0]->getName();
if (!in_array($component, array('blocks', 'helpers', 'models'))) {
continue;
}
$pathNodes = $n->children();
foreach ($pathNodes as $pathNode) {
$path = (string)$pathNode->getName();
$completePath = $module . '/' . $path;
$rewriteClassName = (string)$pathNode;
$instance = Mage::getConfig()->getGroupedClassName(
substr($component, 0, -1),
$completePath
);
$collection->addItem(
new Varien_Object(
array(
'path' => $completePath,
'rewrite_class' => $rewriteClassName,
'active_class' => $instance,
'status' => ($instance == $rewriteClassName)
)
)
);
}
}
}
对于输出,您可以使用...
foreach ($collection as $rewrite) {
var_dump($rewrite->getData());
}