custom/plugins/DmitsInfoFlag/src/DmitsInfoFlag.php line 13

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dmits\InfoFlag;
  3. use Dmits\InfoFlag\Setting\Settings;
  4. use Shopware\Core\Framework\Plugin;
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  6. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. class DmitsInfoFlag extends Plugin
  11. {
  12.     public function install(InstallContext $installContext): void
  13.     {
  14.         if ($this->validSettingsNotExists()) {
  15.             /** @var SystemConfigService $systemConfigService */
  16.             $systemConfigService $this->container->get(SystemConfigService::class);
  17.             foreach (Settings::DEFAULT_VALUES as $key => $value) {
  18.                 $systemConfigService->set($key$value);
  19.             }
  20.         }
  21.       
  22.         parent::install($installContext);
  23.     }
  24.     public function uninstall(UninstallContext $uninstallContext): void
  25.     {
  26.         parent::uninstall($uninstallContext);
  27.         if ($uninstallContext->keepUserData()) {
  28.             return;
  29.         }
  30.         
  31.         /** @var EntityRepositoryInterface $systemConfigRepository */
  32.         $systemConfigRepository $this->container->get('system_config.repository');
  33.         $criteria = (new Criteria())
  34.         ->addFilter(new ContainsFilter('configurationKey'Settings::SYSTEM_CONFIG_DOMAIN));
  35.         $idSearchResult $systemConfigRepository->searchIds($criteria$uninstallContext->getContext());
  36.         
  37.         $ids \array_map(static function ($id) {
  38.             return ['id' => $id];
  39.         }, $idSearchResult->getIds());
  40.             if ($ids === []) {
  41.                 return;
  42.             }
  43.             
  44.             $systemConfigRepository->delete($ids$uninstallContext->getContext());
  45.         
  46.         
  47. //         Ab Shopware 6.3.5
  48. //          $systemConfigService->deletePluginConfiguration(DmitsInfoFlag::class);
  49.     }
  50.     private function validSettingsNotExists(): bool
  51.     {
  52.         $systemConfigService $this->container->get(SystemConfigService::class);
  53.         try {
  54.         $getpositon=$systemConfigService->getString('DmitsInfoFlag.config.position');
  55.         if (($getpositon ?? '') === '') {
  56.             return true;
  57.         }
  58.         } catch (Exception $e) {
  59.             return true;
  60.         }
  61.         
  62.         return false;
  63.     }
  64. }