Thursday, March 28, 2013

Get class rewrites

Here is a small script that lists all rewrites for models, blocks and helpers in Magento.
It also takes into account the disabled extensions but this should't be a big issue.
Create a new file on the root of your magento instance. Let's call it rewrites.php with this content.
<?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
    if (is_array($files)){
        $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);
When accessing mysite.com/rewrites.php you should be able to see all the model, block and helper rewrites. If there is one object rewritten by 2 or more classes it means you have an extension conflict.

3 comments:

  1. Good job Marius. Simple but powerful script.

    ReplyDelete
  2. Thanks for sharing this, I added it to the debug tool:
    https://github.com/kiang/magneto-debug/commit/f1552d327b4322abde43bb3e60578bf0a2b5fc41

    ReplyDelete