Skip to content

Commit

Permalink
Merge pull request #88 from OXIDprojects/cleanup_for_every_shop2
Browse files Browse the repository at this point in the history
use the correct shop id to clean things
  • Loading branch information
keywan-ghadami-oxid authored Feb 27, 2019
2 parents c5fedc9 + ae7adb5 commit af1fe43
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .travis.before_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cd ~/
mkdir OXID
cd OXID
composer create-project oxid-esales/oxideshop-project . dev-b-${OXID}-ce
sed -i -e "s@<dbHost>@127.0.0.1@g; s@<dbName>@oxid@g; s@<dbUser>@root@g; s@<dbPwd>@@g; s@<sShopURL>@http://127.0.0.1@g" source/config.inc.php
sed -i -e "s@<dbHost>@127.0.0.1@g; s@<dbName>@oxid@g; s@<dbUser>@root@g; s@<dbPwd>@@g; s@<sShopURL>@http://127.0.0.1@g; s@sLogLevel = 'error'@sLogLevel = 'info'@g" source/config.inc.php
sed -i -e "s@<sShopDir>@/home/travis/OXID/source@g; s@<sCompileDir>@/home/travis/OXID/source/tmp@g" source/config.inc.php
sed -i -e "s@partial_module_paths: null@partial_module_paths: oxcom/moduleinternals@g" test_config.yml
sed -i -e "s@run_tests_for_shop: true@run_tests_for_shop: false@g" test_config.yml
Expand Down
107 changes: 70 additions & 37 deletions Core/ModuleStateFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ public function __construct($cache = null, $cleaner = null){
parent::__construct($cache, $cleaner);
}

public function disableInitialCacheClear(){
$this->initialCacheClearDone = true;
}

/**
* @var $output LoggerInterface
*/
protected $output;

protected $needCacheClear = false;
protected $initialCacheClearDone = false;
protected $initialDone = false;
protected $isRunning = false;

/**
* @var null|Module $module
*/
Expand All @@ -45,12 +44,48 @@ public function disableInitialCacheClear(){
protected $dryRun = false;


protected $moduleList;
protected $modules;


public function setConfig($config)
{
parent::setConfig($config);
Registry::set(\OxidEsales\Eshop\Core\Config::class, $oConfig);
Registry::set(\OxidEsales\Eshop\Core\Config::class, $config);
}

public function disableInitialCacheClear(){
$this->initialCacheClearDone = true;
}

protected function init()
{
if (!$this->initDone) {
if ($this->isRunning) {
return false;
}
$this->isRunning = true;
$this->moduleList = Registry::get('oxModuleList');
$this->moduleList->getModulesFromDir(Registry::getConfig()->getModulesDir());
$this->modules = $this->moduleList->getList();
$this->initDone = true;

if (!$this->initialCacheClearDone) {

//clearing some cache to be sure that fix runs not against a stale cache
ModuleVariablesLocator::resetModuleVariables();
if (extension_loaded('apc') && ini_get('apc.enabled')) {
apc_clear_cache();
}
$this->output->debug("initial cache cleared");
$this->initialCacheClearDone = true;
}
$this->isRunning = false;
}
return true;
}


/**
* Fix module states task runs version, extend, files, templates, blocks,
* settings and events information fix tasks
Expand All @@ -67,18 +102,10 @@ 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
ModuleVariablesLocator::resetModuleVariables();
if (extension_loaded('apc') && ini_get('apc.enabled')) {
apc_clear_cache();
}
$this->output->debug("initial cache cleared");
$this->initialCacheClearDone = true;
if ($this->init()) {
$this->module = $module;
$this->restoreModuleInformation($module, $moduleId);
}

$this->module = $module;
$this->restoreModuleInformation($module, $moduleId);
$somethingWasFixed = $this->needCacheClear;
$this->clearCache($module);
return $somethingWasFixed;
Expand All @@ -88,8 +115,10 @@ public function fix($module, $oConfig = null)
* After fixing all modules call this method to clean up trash that is not related to any installed module
*/
public function cleanUp() {
$this->cleanUpControllers();
$this->cleanUpExtensions();
if ($this->init()) {
$this->cleanUpControllers();
$this->cleanUpExtensions();
}
}

/**
Expand All @@ -98,8 +127,7 @@ public function cleanUp() {
public function cleanUpExtensions(){

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

//get extensions from metadata file
Expand Down Expand Up @@ -129,7 +157,7 @@ public function cleanUpExtensions(){
//remove diff
foreach ($trash as $item){
list($oxidClass, $extendingClass) = $item;
$this->output->error("wrong extension found $extendingClass (registered for $oxidClass)");
$this->output->warning("wrong extension found $extendingClass (registered for $oxidClass)");
$key = array_search($extendingClass, $extensionChainDb[$oxidClass]);
unset($extensionChainDb[$oxidClass][$key]);
}
Expand All @@ -155,15 +183,15 @@ protected function _addTemplateFiles($aModuleTemplates, $sModuleId)
$diff = $this->diff($old,$aModuleTemplates);
if ($diff) {
$what = $old === null ? ' everything ' : var_export($diff, true);
$this->output->error("$sModuleId fixing templates");
$this->output->warning("$sModuleId fixing templates");
$this->output->debug(" $what");
$aTemplates[$sModuleId] = $aModuleTemplates;
$this->_saveToConfig('aModuleTemplates', $aTemplates);
$this->needCacheClear = true;
}
} else {
if ($old) {
$this->output->error("$sModuleId unregister templates:");
$this->output->warning("$sModuleId unregister templates:");
$this->_deleteTemplateFiles($sModuleId);
$this->needCacheClear = true;
}
Expand All @@ -190,15 +218,15 @@ protected function _addModuleFiles($aModuleFiles, $sModuleId)
$diff = $this->diff($old,$aModuleFiles);
if ($diff) {
$what = $old === null ? ' everything' : var_export($diff, true);
$this->output->error("$sModuleId fixing files");
$this->output->warning("$sModuleId fixing files");
$this->output->debug(" $what");
$aFiles[$sModuleId] = $aModuleFiles;
$this->_saveToConfig('aModuleFiles', $aFiles);
$this->needCacheClear = true;
}
} else {
if ($old) {
$this->output->error("$sModuleId unregister files");
$this->output->warning("$sModuleId unregister files");
$this->_deleteModuleFiles($sModuleId);
$this->needCacheClear = true;
}
Expand All @@ -222,7 +250,7 @@ protected function _addModuleEvents($aModuleEvents, $sModuleId)
if ($diff) {
$aEvents[$sModuleId] = $aModuleEvents;
$what = $old == null ? ' everything ' : var_export($diff, true);
$this->output->error("$sModuleId fixing module events");
$this->output->warning("$sModuleId fixing module events");
$this->output->debug(" $what");
$this->_saveToConfig('aModuleEvents', $aEvents);
$this->needCacheClear = true;
Expand Down Expand Up @@ -254,14 +282,14 @@ protected function _addModuleExtensions($moduleExtensions, $moduleId)
if ($diff) {
$extensions[$moduleId] = array_values($moduleExtensions);
$what = $old === null ? ' everything ' : var_export($diff, true);
$this->output->error("$moduleId fixing module extensions");
$this->output->warning("$moduleId fixing module extensions");
$this->output->debug(" $what");

$this->_saveToConfig('aModuleExtensions', $extensions);
$this->needCacheClear = true;
}
} else {
$this->output->error("$moduleId unregister module extensions");
$this->output->warning("$moduleId unregister module extensions");
$this->needCacheClear = true;
$this->_saveToConfig('aModuleExtensions', []);
}
Expand All @@ -283,7 +311,7 @@ protected function _addModuleVersion($sModuleVersion, $sModuleId)
if($old == '') {
$this->output->info("register module '$sModuleId' with version $sModuleVersion");
} else {
$this->output->error("$sModuleId fixing module version from $old to $sModuleVersion");
$this->output->warning("$sModuleId fixing module version from $old to $sModuleVersion");
}
$this->_saveToConfig('aModuleVersions', $aVersions);
$this->needCacheClear = true;
Expand Down Expand Up @@ -399,7 +427,7 @@ protected function _addModuleSettings($moduleSettings, $moduleId)
} ;
}
if ($diff) {
$this->output->error("$moduleId: settings fixed'");
$this->output->warning("$moduleId: settings fixed'");
$this->needCacheClear = true;
}
}
Expand All @@ -419,9 +447,10 @@ protected function setModuleControllers($moduleControllers, $moduleId, $module)


if ($diff) {
$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));
$shopId = $this->getConfig()->getShopId();
$this->output->warning("in shop $shopId: $moduleId fixing module controllers");
$this->output->warning(" (in md):" . var_export($moduleControllers, true));
$this->output->warning(" (in db):" . var_export($controllersForThatModuleInDb, true));

$this->deleteModuleControllers($moduleId);
$this->resetModuleCache($module);
Expand All @@ -432,6 +461,9 @@ protected function setModuleControllers($moduleControllers, $moduleId, $module)

$classProviderStorage->add($moduleId, $moduleControllers);
}

$afterControllersForThatModuleInDb = $this->getModuleControllerEntries($moduleId);

$this->needCacheClear = true;
}

Expand All @@ -457,13 +489,14 @@ private function getAllControllers()

public function cleanUpControllers(){
$allFromDb = $this->getAllControllers();
$modules = $this->modules;

//? 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);
$modules = array_change_key_case($modules,CASE_LOWER);
$cleaned = array_intersect_key($allFromDb, $modules);
if ($this->diff($allFromDb, $cleaned)) {
$this->needCacheClear = true;
$this->output->error(" cleaning up controllers");
$this->output->warning(" cleaning up controllers");
$classProviderStorage = $this->getClassProviderStorage();
$classProviderStorage->set($cleaned);
}
Expand Down

0 comments on commit af1fe43

Please sign in to comment.