This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FACTFNIDER-178] Add rewrite configuration checks #157
- Loading branch information
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
src/app/code/community/FACTFinder/Core/Helper/Rewrite.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters