How to find all rewrite classes in magento
How to find all rewrite classes in magento

How to find all rewrite classes in magento is a difficult task for some developer who doesn’t know the proper way to check.Why I am saying like this because previously I was checking only by get_class() method of that class if there is any other class has rewrites it or not.But some developer do by searching individual module classes to check whether the specific classes has rewrites somewhere or not,yes it is right way but time consuming task some times it can make some mistakes as you are individually opening all files of module.Here I am going to show you how you can check if any class from models,blocks,helpers has overrides or not if overrides then where that overrides.All the solution is this tutorial.I will be showing you some sorts of techniques of How to find all rewrite classes in magento. Lets all techniques.

By get_class() methods:

Suppose I would like to rewrite Mage_Sales_Model_Quote_Address class before that I have to check whether this class already overridden or not it overridden then it will make difficult to override in my own custom module as magento doesn’t support multiple inheritance.why so ? because PHP does support hierarchical inheritance as Magneto is built upon PHP so this happens.

You can check by below code:

print_r(get_class(Mage::getModel("sales/quote_address")))

By xpath:

print_r(Mage::getConfig()->getNode()->xpath('//global//rewrite'));

its will gives you all active rewrite classes.In same way if you like to check for models,blocks,helpers etc then see below codes.

Models:
print_r(Mage::getConfig()->getNode()->xpath('//global/models//rewrite'))
Blocks:
print_r(Mage::getConfig()->getNode()->xpath('//global/blocks//rewrite'))
Helpers:
print_r(Mage::getConfig()->getNode()->xpath('//global/helpers//rewrite'))

Those are simple and single line techniques through which you will getting all rewrite classes like wise.

Custom script:

< ?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
            }
        }
}
print_r($rewrites);
?>

you can make it simply with any name called myScript.php and run it in browser like http://www.domain.com/myScript.php then you will be getting below result


Array
(
    [models] => Array
        (
            [core/layout] => Array
                (
                    [0] => Namespace_Module_Model_Core_Layout
                )
            [...] => ....
            
        )
    [blocks] => Array
        (
            [catalog/breadcrumbs] => Array
                (
                    [0] => Namespace_Module_Blocks_Catalog_Breadcrumbs
                )
            [...] => ....
            
        )
    [helpers] => Array
        (
            [catalog/image] => Array
                (
                    [0] => Namespace_Module_Helper_Catalog_Image
                    [1] => Namespace1_Module1_Helper_Catalog_Image //if the second element is present it means there is a possible conflict
                )
            [...] => ....
            
        )
)

The above result showing that the helper “catalog/image” is overridden by Namespace_Module_Helper_Catalog_Image.If there is two more values like above result then there is a conflict means two more module override the same class.

How to find all rewrite classes in magento
Tagged on:                     

Leave a Reply

Your email address will not be published. Required fields are marked *