Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
[FACTFNIDER-178] Add rewrite configuration checks #157
Browse files Browse the repository at this point in the history
  • Loading branch information
xpoback committed Jul 28, 2016
1 parent 3fd9fb2 commit c67fe93
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
119 changes: 119 additions & 0 deletions src/app/code/community/FACTFinder/Core/Helper/Rewrite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Rewrite.php
*
* @category Mage
* @package magento
* @author Flagbit Magento Team <[email protected]>
* @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 <i>%s</i> got <i>%s</i>',
$neededClassName,
$actualClassName
);
}


}
5 changes: 5 additions & 0 deletions src/app/code/community/FACTFinder/Core/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down

0 comments on commit c67fe93

Please sign in to comment.