Skip to content

Commit

Permalink
Merge pull request #80 from OXIDprojects/fix-extensions
Browse files Browse the repository at this point in the history
clean extensions that are not used by any module
  • Loading branch information
keywan-ghadami-oxid authored Jan 31, 2019
2 parents fc66ec1 + f6eecbf commit c4bd924
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions Core/ModuleStateFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function disableInitialCacheClear(){
*/
protected $module = null;

protected $dryRun = false;

/**
* Fix module states task runs version, extend, files, templates, blocks,
* settings and events information fix tasks
Expand Down Expand Up @@ -81,6 +83,56 @@ public function fix($module, $oConfig = null)
*/
public function cleanUp() {
$this->cleanUpControllers();
$this->cleanUpExtensions();
}

/**
* remove extensions that are not registered by any module
*/
public function cleanUpExtensions(){

//get all extions from all metadata
$oxModuleList = oxNew('oxModuleList');
$oxModuleList->getModulesFromDir(\oxRegistry::getConfig()->getModulesDir());
$aModules = $oxModuleList->getList();

//get extensions from metadata file
$moduleClassesMf = [];
foreach ($aModules as $module) {
$extensions = $module->getExtensions();
foreach ($extensions as $moduleClass) {
$moduleClassesMf[$moduleClass] = 1;
}
}

//get all extesions from db
$extensionChainDb = $this->getConfig()->getConfigParam('aModules');
$extensionChainDb = $oxModuleList->parseModuleChains($extensionChainDb);

$trash = [];
//calculate trash as extensions that are only in db
foreach ($extensionChainDb as $oxidClass => $arrayOfExtendingClasses) {

foreach ($arrayOfExtendingClasses as $extendingClass){
if (!isset($moduleClassesMf[$extendingClass])) {
$trash[] = [$oxidClass, $extendingClass];
}
}
}

//remove diff
foreach ($trash as $item){
list($oxidClass, $extendingClass) = $item;
$this->output->error("wrong extension found $extendingClass (registered for $oxidClass)");
$key = array_search($extendingClass, $extensionChainDb[$oxidClass]);
unset($extensionChainDb[$oxidClass][$key]);
}

if (!$this->dryRun) {
$extensionChainDb = $this->buildModuleChains($extensionChainDb);
$this->_saveToConfig('aModules', $extensionChainDb);
}

}

/**
Expand Down Expand Up @@ -399,9 +451,10 @@ private function getAllControllers()

public function cleanUpControllers(){
$allFromDb = $this->getAllControllers();
//? is aModuleVersions fixed already in that place
$aVersions = (array) $this->getConfig()->getConfigParam('aModuleVersions');
$aVersions = array_change_key_case($aVersions,CASE_LOWER);
$cleaned = array_intersect_key($allFromDb,$aVersions);
$cleaned = array_intersect_key($allFromDb, $aVersions);
if ($this->diff($allFromDb, $cleaned)) {
$this->needCacheClear = true;
$this->output->error(" cleaning up controllers");
Expand Down Expand Up @@ -558,9 +611,9 @@ protected function _addTemplateBlocks($moduleBlocks, $moduleId)
protected function checkExtensions(\OxidEsales\Eshop\Core\Module\Module $module, &$aModulesDefault, &$aModules)
{
$aModulesDefault = $this->getConfig()->getConfigParam('aModules');
//in case someone deleted values from the db using a empty array avoids php warnings
$aModulesDefault = is_null($aModulesDefault) ? [] : $aModulesDefault;
$aModules = $this->getModulesWithExtendedClass();
$aModules = $this->_removeNotUsedExtensions($aModules, $module);


if ($module->hasExtendClass()) {
$this->validateMetadataExtendSection($module);
Expand Down Expand Up @@ -600,4 +653,3 @@ public function getLogger(){
}

}

0 comments on commit c4bd924

Please sign in to comment.