From c67fe93dad8fa255a00843e29222d68cff954e4f Mon Sep 17 00:00:00 2001 From: xpoback Date: Thu, 28 Jul 2016 19:09:44 +0200 Subject: [PATCH] [FACTFNIDER-178] Add rewrite configuration checks #157 --- .../FACTFinder/Core/Helper/Rewrite.php | 119 ++++++++++++++++++ .../FACTFinder/Core/Model/Observer.php | 5 + 2 files changed, 124 insertions(+) create mode 100644 src/app/code/community/FACTFinder/Core/Helper/Rewrite.php diff --git a/src/app/code/community/FACTFinder/Core/Helper/Rewrite.php b/src/app/code/community/FACTFinder/Core/Helper/Rewrite.php new file mode 100644 index 00000000..cf7a735f --- /dev/null +++ b/src/app/code/community/FACTFinder/Core/Helper/Rewrite.php @@ -0,0 +1,119 @@ + + * @copyright Copyright (c) 2016 Flagbit GmbH & Co. KG + * @license https://opensource.org/licenses/MIT The MIT License (MIT) + * @link http://www.flagbit.de + */ +class FACTFinder_Core_Helper_Rewrite extends Mage_Core_Helper_Abstract +{ + + + /** + * Get all factfinder submodules + * Based on config values so can be a bit not exact + * + * @return array + */ + protected function getAllModules() + { + $modules = array( + 'FACTFinder_Core', + ); + + foreach (Mage::app()->getStore()->getConfig('factfinder/modules') as $module => $status) { + $modules[] = 'FACTFinder_' . ucwords($module); + } + + return $modules; + } + + + /** + * Get rewritten blocks or models for module + * + * @param string $module + * @param string $type blocks|models + * + * @return array + */ + protected function getModuleRewrites($module, $type) + { + $result = array(); + + if (!Mage::helper('core')->isModuleEnabled($module)) { + return $result; + } + + $file = Mage::getModuleDir('etc', $module) . DS . 'config.xml'; + $configModel = new Varien_Simplexml_Config($file); + + $rewrites = $configModel->getXpath("*/{$type}/*/rewrite"); + if (empty($rewrites)) { + return $result; + } + + /** @var Varien_Simplexml_Element $item */ + foreach ( $rewrites as $item) { + $module = $item->getParent()->getName(); + foreach ($item->children() as $child) { + $result[$module . '/' . $child->getName()] = $child->asArray(); + } + } + + return $result; + } + + + /** + * Check all rewritten blocks and models + * + * @return array + */ + public function checkRewrites() + { + $result = array(); + foreach ($this->getAllModules() as $module) { + // check blocks + $rewrittenBlocks = $this->getModuleRewrites($module, 'blocks'); + foreach ($rewrittenBlocks as $block => $neededClassName) { + $actualClassName = Mage::getConfig()->getBlockClassName($block); + if ($neededClassName !== $actualClassName) { + $result[] = $this->getWrongRewriteMessage($neededClassName, $actualClassName); + } + } + + // check models + $rewrittenModels = $this->getModuleRewrites($module, 'models'); + foreach ($rewrittenModels as $model => $neededClassName) { + $actualClassName = Mage::getConfig()->getModelClassName($model); + if ($neededClassName !== $actualClassName) { + $result[] = $this->getWrongRewriteMessage($neededClassName, $actualClassName); + } + } + } + + return $result; + } + + + /** + * Get message about wrong class received (not rewritten or rewritten by a wrong module) + * + * @return string + */ + protected function getWrongRewriteMessage($neededClassName, $actualClassName) + { + return Mage::helper('factfinder')->__( + 'Rewrite warning: instead of %s got %s', + $neededClassName, + $actualClassName + ); + } + + +} \ No newline at end of file diff --git a/src/app/code/community/FACTFinder/Core/Model/Observer.php b/src/app/code/community/FACTFinder/Core/Model/Observer.php index 6adc202b..2fbd3ab5 100644 --- a/src/app/code/community/FACTFinder/Core/Model/Observer.php +++ b/src/app/code/community/FACTFinder/Core/Model/Observer.php @@ -73,6 +73,11 @@ public function setSearchEngine($observer) $this->_handleEngine($errors, $this->getScope($request), $this->getScopeId($request)); } + // check rewrites and add warnings in case something is wrong + foreach (Mage::helper('factfinder/rewrite')->checkRewrites() as $warning) { + Mage::getSingleton('adminhtml/session')->addWarning($warning); + } + // this also helps with module managing Mage::app()->cleanCache(); if (Mage::helper('core')->isModuleEnabled('Enterprise_PageCache')) {