Code taken from OxidEsales\EshopCommunity\Core\Module::activate

@param Module $module
@param string $moduleId

@throws \OxidEsales\Eshop\Core\Exception\StandardException
439215
private function restoreModuleInformation($module, $moduleId)
    {
        $this->_addExtensions($module);
        $metaDataVersion = $module->getMetaDataVersion();
        $metaDataVersion = $metaDataVersion == '' ? $metaDataVersion = "1.0" : $metaDataVersion; 
        if (version_compare($metaDataVersion, '2.0', '<')) {
            $this->_addModuleFiles($module->getInfo("files"), $moduleId);
        }
        $this->_addTemplateBlocks($module->getInfo("blocks"), $moduleId);
        $this->_addTemplateFiles($module->getInfo("templates"), $moduleId);
        $this->_addModuleSettings($module->getInfo("settings"), $moduleId);
        $this->_addModuleVersion($module->getInfo("version"), $moduleId);
        $this->_addModuleExtensions($module->getExtensions(), $moduleId);
        $this->_addModuleEvents($module->getInfo("events"), $moduleId);

        if (version_compare($metaDataVersion, '2.0', '>=')) {
            try {
                $this->setModuleControllers($module->getControllers(), $moduleId, $module);
            } catch (ModuleValidationException $exception) {
                print "[ERROR]: duplicate controllers:" . $exception->getMessage() ."\n";
            }
        }
    }
1070683
public function add($moduleId, $controllers)
    {
        $controllerMap = $this->get();
        $controllerMap[$moduleId] = $controllers;

        $this->set($controllerMap);
    }
1242698
public function addModule(Module $module)
    {
        if ($this->db !== $module->getDB())
            throw new MigrationException("The DB instances should be the same");

        $name = self::normalizeModule($module->getModule());
        $this->modules[$name] = $module;
        return $this;
    }
185090
public function handleOnModuleActivation(ModuleSetting $moduleSetting, string $moduleId, int $shopId)
    {
        if (!$this->canHandle($moduleSetting)) {
            throw new WrongModuleSettingException($moduleSetting, self::class);
        }

        foreach ($moduleSetting->getValue() as $shopModuleSettingData) {
            $shopModuleSetting = new ShopModuleSetting();
            $shopModuleSetting
                ->setShopId($shopId)
                ->setModuleId($moduleId);

            $shopModuleSetting = $this->mapDataToShopModuleSetting($shopModuleSetting, $shopModuleSettingData);

            $this->shopModuleSettingDao->save($shopModuleSetting);
        }
    }
156243
protected function _addModuleExtensions($moduleExtensions, $moduleId)
    {
        $extensions = (array) \OxidEsales\Eshop\Core\Registry::getConfig()->getConfigParam('aModuleExtensions');
        if (is_array($extensions)) {
            $extensions[$moduleId] = array_values($moduleExtensions);
        }

        $this->_saveToConfig('aModuleExtensions', $extensions);
    }
1011406
public function removeModule($module)
    {
        foreach ($this->modules as $key => $moduleElement) {
            if ($moduleElement === $module) {
                unset($this->modules[$key]);
                break;
            }
        }

        return $this;
    }
1215024
public function activateModule($moduleId, $shopId, $output)
    {
        /** @var ModuleInstaller $moduleInstaller */
        $moduleInstaller = Registry::get(ModuleInstaller::class);

        if ($shopId) {
            $oConfig = ShopConfig::get($shopId);
            $moduleInstaller->setConfig($oConfig);
        }

        $moduleList = oxNew(ModuleList::class);
        $moduleList->getModulesFromDir(Registry::getConfig()->getModulesDir());
        $modules = $moduleList->getList();

        /** @var Module $module */
        $module = $modules[$moduleId];
        if ($module == null) {
            $output->writeLn("$moduleId not found. choose from:");
            $output->writeLn(join("\n", array_keys($modules)));

            exit(1);
        }

        if ($module->isActive()) {
            $output->writeLn("$moduleId already active");
        } else {
            $moduleInstaller->activate($module);
        }
    }
320446
public final function registerModule(Module $module)
    {
        $dependencies = $module->getModules();

        foreach($dependencies as $dependency){
            $this->registerModule($dependency);
        }

        $this->modules[$module->getModuleName()] = $module;
    }
583798
protected function onModuleExtensionCreationError($moduleClass)
    {
        $moduleId = "(module id not availible)";
        if (class_exists("\OxidEsales\Eshop\Core\Module\Module", false)) {
            $module = oxNew(\OxidEsales\Eshop\Core\Module\Module::class);
            $moduleId = $module->getIdByPath($moduleClass);
        }
        $message = sprintf('Module class %s not found. Module ID %s', $moduleClass, $moduleId);
        $exception = new \OxidEsales\Eshop\Core\Exception\SystemComponentException($message);
        \OxidEsales\Eshop\Core\Registry::getLogger()->error($exception->getMessage(), [$exception]);
    }
1136796
public function deactivate(string $moduleId, int $shopId)
    {
        if ($this->stateService->isActive($moduleId, $shopId) === false) {
            throw new ModuleSetupException('Module with id "'. $moduleId . '" is not active.');
        }

        $this->eventDispatcher->dispatch(
            BeforeModuleDeactivationEvent::NAME,
            new BeforeModuleDeactivationEvent($shopId, $moduleId)
        );

        $moduleConfiguration = $this->moduleConfigurationDao->get($moduleId, $shopId);

        $this->moduleSettingsHandlingService->handleOnDeactivation($moduleConfiguration, $shopId);

        $this->moduleServicesActivationService->deactivateModuleServices($moduleId, $shopId);

        $this->stateService->setDeactivated($moduleId, $shopId);

        $moduleConfiguration->setAutoActive(false);
        $this->moduleConfigurationDao->save($moduleConfiguration, $shopId);

        $this->classExtensionChainService->updateChain($shopId);

        $this->eventDispatcher->dispatch(
            FinalizingModuleDeactivationEvent::NAME,
            new FinalizingModuleDeactivationEvent($shopId, $moduleId)
        );
    }
1083983
public static function setCurrentModule(string $module): void
    {
        if (static::$currentModule !== null) {
            $currentModule = static::$currentModule;
            throw new ApplicationException("Current module was already set to {$currentModule}; once current module is set, you can't change it");
        }

        static::$currentModule = $module;

        // register current module & run init.php if present
        static::registerModule($module);
    }
254152
protected function getConfiguredModulePresence(ModuleInterface $module)
    {
        if (    ! array_key_exists($module->getKey(), $this->configModules)
            ||  ! $this->configModules[ $module->getKey() ]
        ) {
            // @codeCoverageIgnoreStart
            return false;
            // @codeCoverageIgnoreEnd
        }

        return $this->configModules[ $module->getKey() ] ?: [];
    }
360674
private function disable($module)
    {
        if (!array_key_exists($module, $this->enabled)) {
            throw new \InvalidArgumentException("Module '$module' cannot be enabled or disabled. Only [".implode(', ', array_keys($this->enabled)).'] can.');
        }

        $this->enabled[$module] = false;

        return $this;
    }
1296711
public function invalidateModuleCache(string $moduleId)
    {
        /**
         * @TODO we have to implement it in ShopModuleCacheService or use ModuleCache::resetCache() method.
         */
        $module = oxNew(Module::class);

        $templates = $module->getTemplates($moduleId);
        $utils = Registry::getUtils();
        $utils->resetTemplateCache($templates);
        $utils->resetLanguageCache();
        $utils->resetMenuCache();

        ModuleVariablesLocator::resetModuleVariables();

        if (extension_loaded('apc') && ini_get('apc.enabled')) {
            apc_clear_cache();
        }
    }
1184868
public function enable($module_id)
    {
        try {
            $result = $this->canEnable($module_id);
        } catch (Exception $ex) {
            $result = $ex->getMessage();
        }

        $this->hook->attach("module.enable.before|$module_id", $result, $this);

        if ($result !== true) {
            return $result;
        }

        $this->module->update($module_id, array('status' => 1));
        $this->setOverrideConfig();

        $this->hook->attach("module.enable.after|$module_id", $result, $this);
        return $result;
    }
512838
public function register(DynamicModule $module)
    {
        if(!is_null($module) && ! empty($module->getModule()) ) {

            self::getRegistry()->set($module->getModule(), $module->toArray());

            return true;
        }
        return false;
    }
834306
public function createModule(Module $module)
    {
        $dst = $this->rootDir.'/../src/Terrific/Module/'.StringUtils::camelize($module->getName());

        if($this->toolbarMode === ToolbarListener::DEMO) {
            // prevent module creation in demo mode
            throw new \Exception('This action is not supported in demo mode');
        } else {
            if($this->moduleTemplate) {
                $this->copy($this->moduleTemplate, $dst, $module);
            }

            $this->copy($this->defaultTemplate, $dst, $module);
        }
    }