Source of file Rewrites.php

Size: 6,978 Bytes - Last Modified: 2015-05-15T11:18:15+02:00

/www-data/git/Hackathon_MageMonitoring/src/app/code/community/FireGento/MageMonitoring/Helper/Rewrites.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
<?php
/**
 * This file is part of a FireGento e.V. module.
 *
 * This FireGento e.V. module is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This script is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * PHP version 5
 *
 * @category  FireGento
 * @package   FireGento_MageMonitoring
 * @author    FireGento Team <team@firegento.com>
 * @copyright 2015 FireGento Team (http://www.firegento.com)
 * @license   http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3)
 */

/**
 * Rewrites helper
 *
 * @category FireGento
 * @package  FireGento_MageMonitoring
 * @author   FireGento Team <team@firegento.com>
 */
class FireGento_MageMonitoring_Helper_Rewrites extends Mage_Core_Helper_Abstract
{
    protected $_rewriteTypes = array(
        'blocks',
        'helpers',
        'models',
    );

    /**
     * Return all rewrites
     *
     * @return array
     */
    public function getRewrites()
    {
        $rewrites = array(
            'blocks',
            'models',
            'helpers',
        );

        /* @var $_magentoConfig Mage_Core_Model_Config */
        $_magentoConfig = Mage::getConfig();

        // Load config of each module because modules can overwrite config each other. Global config is already merged
        $modules = $_magentoConfig->getNode('modules')->children();
        foreach ($modules as $moduleName => $moduleData) {
            // Check only active modules
            if (!$moduleData->is('active')) {
                continue;
            }

            // Load config of module
            $configXmlFile = $_magentoConfig->getModuleDir('etc', $moduleName) . DIRECTORY_SEPARATOR . 'config.xml';
            if (!file_exists($configXmlFile)) {
                continue;
            }

            $xml = simplexml_load_file($configXmlFile);
            if ($xml) {
                $rewriteElements = $xml->xpath('//rewrite');
                foreach ($rewriteElements as $element) {
                    foreach ($element->children() as $child) {
                        $type = simplexml_import_dom(dom_import_simplexml($element)->parentNode->parentNode)->getName();
                        if (!in_array($type, $this->_rewriteTypes)) {
                            continue;
                        }
                        $groupClassName = simplexml_import_dom(dom_import_simplexml($element)->parentNode)->getName();
                        if (!isset($rewrites[$type][$groupClassName . '/' . $child->getName()])) {
                            $rewrites[$type][$groupClassName . '/' . $child->getName()] = array();
                        }
                        $rewrites[$type][$groupClassName . '/' . $child->getName()]['classes'][] = (string) $child;
                    }
                }
            }
        }

        foreach ($rewrites as $type => $data) {
            if (count($data) > 0 && is_array($data)) {
                foreach ($data as $node => $rewriteInfo) {
                    if (count($rewriteInfo['classes']) > 1) {
                        if ($this->_isInheritanceConflict($rewriteInfo['classes'])) {
                            $rewrites[$type][$node]['conflicts'][] = array(
                                'node' => $node,
                                'loaded_class' => $this->_getLoadedClass($type, $node)
                            );
                        }
                    }
                }
            }
        }

        $rewrites = array_merge($rewrites, $this->_loadLocalAutoloaderRewrites());

        if (empty($rewrites['blocks']) && empty($rewrites['models'])
            && empty($rewrites['helpers']) && empty($rewrites['autoload'])
        ) {
            return false;
        }

        return $rewrites;
    }

    /**
     * Check if rewritten class has inherited the parent class.
     * If yes we have no conflict. The top class can extend every core class.
     * So we cannot check this.
     *
     * @param  array $classes List of classes
     * @return bool
     */
    protected function _isInheritanceConflict($classes)
    {
        $classes = array_reverse($classes);
        for ($i = 0; $i < count($classes) - 1; $i++) {
            try {
                if (class_exists($classes[$i]) && class_exists($classes[$i + 1])) {
                    if (!is_a($classes[$i], $classes[$i + 1], true)) {
                        return true;
                    }
                }
            } catch (Exception $e) {
                return true;
            }
        }

        return false;
    }

    /**
     * Returns loaded class by type like models or blocks
     *
     * @param  string $type       Class Type
     * @param  string $classGroup Class Group Name
     *
     * @return string
     */
    protected function _getLoadedClass($type, $classGroup)
    {
        switch ($type) {
            case 'blocks':
                return Mage::getConfig()->getBlockClassName($classGroup);

            case 'helpers':
                return Mage::getConfig()->getHelperClassName($classGroup);

            default:
            case 'models':
                return Mage::getConfig()->getModelClassName($classGroup);
                break;
        }
    }


    /**
     * Searches for all rewrites over autoloader in "app/code/local" of
     * Mage, Enterprise Zend, Varien namespaces.
     *
     * @return array
     */
    protected function _loadLocalAutoloaderRewrites()
    {
        $return = array();
        $localCodeFolder = Mage::getBaseDir('code') . '/local';

        $folders = array(
            'Mage'       => $localCodeFolder . '/Mage',
            'Enterprise' => $localCodeFolder . '/Enterprise',
            'Varien'     => $localCodeFolder . '/Varien',
            'Zend'       => $localCodeFolder . '/Zend',
        );

        foreach ($folders as $vendorPrefix => $folder) {
            if (is_dir($folder)) {
                $directory = new RecursiveDirectoryIterator($folder);
                $iterator = new RecursiveIteratorIterator($directory);
                $files = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);

                foreach ($files as $file) {
                    $classFile = trim(str_replace($folder, '', realpath($file[0])), '/');
                    $className = $vendorPrefix
                        . '_'
                        . str_replace(DIRECTORY_SEPARATOR, '_', $classFile);
                    $className = substr($className, 0, -4); // replace .php extension
                    $return['autoload'][$className]['classes'][] = $className;
                }
            }
        }

        return $return;
    }
}