custom/plugins/zenitPlatfromGravity/src/zenitPlatformGravity.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformGravity;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Storefront\Framework\ThemeInterface;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  15. use zenit\PlatformGravity\Core\CustomFieldsHelper;
  16. use zenit\PlatformGravity\Service\CustomFieldService;
  17. class zenitPlatformGravity extends Plugin implements ThemeInterface
  18. {
  19.     /**
  20.      * @var Context
  21.      */
  22.     private $context;
  23.     public function getThemeConfigPath(): string
  24.     {
  25.         return 'theme.json';
  26.     }
  27.     public function install(InstallContext $installContext): void
  28.     {
  29.         parent::install($installContext);
  30.         $this->applyUpdates(
  31.             $installContext->getContext(),
  32.             null,
  33.             $installContext->getCurrentPluginVersion()
  34.         );
  35.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  36.         $customFields = new CustomFieldsHelper($customFieldSetRepository);
  37.         $customFields->getCustomFields($this->container$installContext->getContext());
  38.     }
  39.     public function update(UpdateContext $updateContext): void
  40.     {
  41.         parent::update($updateContext);
  42.         if (version_compare($updateContext->getCurrentPluginVersion(), '2.4.6''<')) {
  43.             $customFieldSetRepository $this->container->get('custom_field_set.repository');
  44.             $customFields = new CustomFieldsHelper($customFieldSetRepository);
  45.             $customFields->getCustomFields($this->container$updateContext->getContext());
  46.         }
  47.     }
  48.     public function postUpdate(UpdateContext $updateContext): void
  49.     {
  50.         parent::postUpdate($updateContext);
  51.         $this->updateThemeDuplicates();
  52.     }
  53.     public function uninstall(UninstallContext $uninstallContext): void
  54.     {
  55.         parent::uninstall($uninstallContext);
  56.         (new CustomFieldService(
  57.             $this->container->get('custom_field_set.repository'),
  58.             $uninstallContext->getContext()
  59.         ))->remove();
  60.         if ($uninstallContext->keepUserData()) {
  61.             return;
  62.         }
  63.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  64.         $customFields = new CustomFieldsHelper($customFieldSetRepository);
  65.         $customFields->deleteCustomFields($uninstallContext->getContext());
  66.     }
  67.     private function updateThemeDuplicates(): void
  68.     {
  69.         $this->context Context::createDefaultContext();
  70.         $criteriaTheme = new Criteria();
  71.         $criteriaTheme->addFilter(new EqualsFilter('technicalName''zenitPlatformGravity'));
  72.         /** @var EntityRepository $themeRepo */
  73.         $themeRepo $this->container->get('theme.repository');
  74.         $parentTheme $themeRepo->search($criteriaTheme$this->context)->first();
  75.         if (!$parentTheme) {
  76.             return;
  77.         }
  78.         $criteriaThemeDuplicates = new Criteria();
  79.         $criteriaThemeDuplicates->addFilter(new EqualsFilter('parentThemeId'$parentTheme->get('id')));
  80.         $resultThemeDuplicates $themeRepo->search($criteriaThemeDuplicates$this->context)->getElements();
  81.         if (!$resultThemeDuplicates) {
  82.             return;
  83.         }
  84.         foreach ($resultThemeDuplicates as $themeDuplicate) {
  85.             $data = [
  86.                 'id' => $themeDuplicate->get('id'),
  87.                 'baseConfig' => $parentTheme->get('baseConfig')
  88.             ];
  89.             if (!$themeDuplicate->get('previewMediaId')) {
  90.                 $data['previewMediaId'] = $parentTheme->get('previewMediaId');
  91.             }
  92.             $themeRepo->update([$data], $this->context);
  93.         }
  94.     }
  95.     /**
  96.      * @param ContainerBuilder $container
  97.      * @throws \Exception
  98.      */
  99.     public function build(ContainerBuilder $container): void
  100.     {
  101.         parent::build($container);
  102.         $loader = new XmlFileLoader($container, new FileLocator($this->getPath() . '/Core/Content/DependencyInjection'));
  103.         $loader->load('services.xml');
  104.     }
  105.     private function applyUpdates(Context $context$oldVersion null$newVersion null)
  106.     {
  107.         $versionClosures = [
  108.             '1.0.0' => function () use ($context) {
  109.                 (new CustomFieldService(
  110.                     $this->container->get('custom_field_set.repository'),
  111.                     $context
  112.                 ))->create();
  113.                 return true;
  114.             }
  115.         ];
  116.         foreach ($versionClosures as $version => $versionClosure) {
  117.             if ($oldVersion === null ||
  118.                 (version_compare($oldVersion$version'<')
  119.                     && version_compare($version$newVersion'<='))
  120.             ) {
  121.                 if (!$versionClosure($this)) {
  122.                     return false;
  123.                 }
  124.             }
  125.         }
  126.         return true;
  127.     }
  128. }