Skip to content

Commit

Permalink
Merge pull request #59 from OXIDprojects/controller-fix
Browse files Browse the repository at this point in the history
Controller fix
  • Loading branch information
keywan-ghadami-oxid authored Jan 6, 2019
2 parents fab0307 + e34afd7 commit 4ccdb64
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
41 changes: 35 additions & 6 deletions Core/ModuleStateFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function fix($module, $oConfig = null)
}

$moduleId = $module->getId();
$this->needCacheClear = false;

if (!$this->initialCacheClearDone) {
//clearing some cache to be sure that fix runs not against a stale cache
Expand All @@ -61,15 +62,22 @@ public function fix($module, $oConfig = null)
}
$this->output->debug("initial cache cleared");
$this->initialCacheClearDone = true;
$this->cleanUp();
}
$this->module = $module;
$this->needCacheClear = false;
$this->restoreModuleInformation($module, $moduleId);
$somethingWasFixed = $this->needCacheClear;
$this->clearCache($module);
return $somethingWasFixed;
}

/**
* After fixing all modules call this method to clean up trash that is not related to any installed module
*/
public function cleanUp() {
$this->cleanUpControllers();
}

/**
* Add module template files to config for smarty.
*
Expand Down Expand Up @@ -332,11 +340,11 @@ protected function _addModuleSettings($moduleSettings, $moduleId)
protected function setModuleControllers($moduleControllers, $moduleId, $module)
{
$controllersForThatModuleInDb = $this->getModuleControllerEntries($moduleId);
$diff = $this->diff($controllersForThatModuleInDb, $moduleControllers);


$duplicatedKeys = array_intersect_key($moduleControllers, $controllersForThatModuleInDb);
$diff = array_diff_assoc($moduleControllers, $duplicatedKeys);
if ($diff) {
$this->output->info("$moduleId fixing module controllers");
$this->output->error("$moduleId fixing module controllers");
$this->output->debug(" (in md):" . var_export($moduleControllers, true));
$this->output->debug(" (in db):" . var_export($controllersForThatModuleInDb, true));

Expand All @@ -355,14 +363,35 @@ protected function setModuleControllers($moduleControllers, $moduleId, $module)
}

public function getModuleControllerEntries($moduleId){
$classProviderStorage = $this->getClassProviderStorage();
$dbMap = $classProviderStorage->get();
$dbMap = $this->getAllControllers();
//for some unknown reasons the core uses lowercase module id to reference controllers
$moduleIdLc = strtolower($moduleId);
$controllersForThatModuleInDb = isset($dbMap[$moduleIdLc]) ? $dbMap[$moduleIdLc] : [];
return $controllersForThatModuleInDb;
}

/**
* @return mixed
*/
private function getAllControllers()
{
$classProviderStorage = $this->getClassProviderStorage();
$dbMap = $classProviderStorage->get();
return $dbMap;
}

public function cleanUpControllers(){
$allFromDb = $this->getAllControllers();
$aVersions = (array) $this->getConfig()->getConfigParam('aModuleVersions');
$cleaned = array_intersect_key($allFromDb,$aVersions);
if ($this->diff($allFromDb, $cleaned)) {
$this->needCacheClear = true;
$classProviderStorage = $this->getClassProviderStorage();
$classProviderStorage->set($cleaned);
}
}


/**
* Reset module cache
*
Expand Down
9 changes: 9 additions & 0 deletions Tests/Integration/Core/FixHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public function testFixExtensions()
$this->assertLogEntry("fixing module extensions");
}

public function testFixControllers()
{
$old = $this->getConfigParam('aModuleControllers');
$this->setConfigParam('aModuleControllers', ['module-a' => ['mycontroller'=>'notexistingclass']]);
$this->callSut();
$this->assertEquals($this->getConfigParam('aModuleControllers'), $old);
$this->assertLogEntry("fixing module controllers");
}

/**
* @param $moduleId
* @return array
Expand Down

0 comments on commit 4ccdb64

Please sign in to comment.