context = $context; $this->eavSetupFactory = $eavSetupFactory; $this->_cmsBlockFactory = $cmsBlockFactory; $this->shell = $shell; $this->deploymentConfig = $deploymentConfig; $this->deploymentConfigWriter = $deploymentConfigWriter; $this->moduleListLoader = $moduleListLoader; $this->logger = $logger; } /** * Uninstall * * @param SchemaSetupInterface $setup * @param ModuleContextInterface $context * @return void * @throws \Magento\Framework\Exception\LocalizedException */ public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $moduleName = $this->getModuleName(); $dataSetup = new \Magento\Setup\Module\DataSetup($this->context); /** @var \Magento\Eav\Setup\EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $dataSetup]); //remove attribute foreach ($this->_attributes as $entityTypeName => $attributeNames) { $entityTypeId = $eavSetup->getEntityTypeId($entityTypeName); foreach ($attributeNames as $attributeName) { $eavSetup->removeAttribute($entityTypeId, $attributeName); } } //remove tables foreach ($this->_tables as $_tableName) { $_tableName = $setup->getTable($_tableName); $setup->getConnection()->dropTable($_tableName); } //remove Eav Entity Type foreach ($this->eavEntityTypes as $eavEntityType) { $eavSetup->removeEntityType($eavEntityType); } //remove tables fields foreach ($this->_tablesFields as $_tableName => $_fields) { $_tableName = $setup->getTable($_tableName); foreach ($_fields as $_field) { try { $setup->getConnection()->dropColumn($_tableName, $_field); } catch (\Exception $e) { $this->logger->critical($e->getMessage()); } } } $this->removeStaticBlocks(); $this->removeConfigData($setup, $moduleName); $this->removeModuleFromConfigFile($moduleName); $this->shell->execute('rm -rf ' . BP . '/var/cache'); foreach ($this->_pathes as $path) { $this->shell->execute('rm -rf ' . BP . $path); } $setup->endSetup(); } /** * Get module name. * * @return string */ public function getModuleName() { $className = get_class($this); $namespace = substr( $className, 0, strpos($className, '\\' . 'Setup') ); return str_replace('\\', '_', $namespace); } /** * Remove static blocks. * * @return void * @throws \Exception */ private function removeStaticBlocks(): void { foreach ($this->_cmsBlocks as $_identifier) { /** @var \Magento\Cms\Model\Block $cmsBlock */ $cmsBlock = $this->_cmsBlockFactory->create(); $cmsBlock->load($_identifier); if ($cmsBlock->getId()) { $cmsBlock->delete(); } } } /** * Remove configs. * * @param \Magento\Framework\Setup\SchemaSetupInterface $setup * @param string $moduleName * @return void */ private function removeConfigData(SchemaSetupInterface $setup, string $moduleName): void { if ($this->_configSectionId) { $setup->getConnection()->delete( $setup->getTable('core_config_data'), ['path LIKE ?' => $this->_configSectionId . '/%'] ); $setup->getConnection()->delete( $setup->getTable('setup_module'), ['module = ?' => $moduleName] ); } } /** * Remove from config.php * * @param string $moduleName * @return void * @throws \Magento\Framework\Exception\FileSystemException * @throws \Magento\Framework\Exception\LocalizedException */ private function removeModuleFromConfigFile(string $moduleName): void { $configuredModules = $this->deploymentConfig->getConfigData( ConfigOptionsListConstants::KEY_MODULES ); $existingModules = $this->moduleListLoader->load([$moduleName]); $newModules = []; foreach (array_keys($existingModules) as $module) { $newModules[$module] = $configuredModules[$module] ?? 0; } $this->deploymentConfigWriter->saveConfig( [ ConfigFilePool::APP_CONFIG => [ConfigOptionsListConstants::KEY_MODULES => $newModules] ], true ); } }