custom/plugins/TwigelHardTheme/src/TwigelHardTheme.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TwigelHardTheme;
  3. use Shopware\Core\Framework\Context;
  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\Storefront\Framework\ThemeInterface;
  8. use TwigelHardTheme\Service\CustomFieldService;
  9. class TwigelHardTheme extends Plugin implements ThemeInterface
  10. {
  11.     public function install(InstallContext $installContext): void
  12.     {
  13.         parent::install($installContext);
  14.         $this->applyUpdates(
  15.             $installContext->getContext(),
  16.             null,
  17.             $installContext->getCurrentPluginVersion()
  18.         );
  19.     }
  20.     public function uninstall(UninstallContext $uninstallContext): void
  21.     {
  22.         parent::uninstall($uninstallContext);
  23.         if ($uninstallContext->keepUserData()) {
  24.             return;
  25.         }
  26.         (new CustomFieldService(
  27.             $this->container->get('custom_field_set.repository'),
  28.             $uninstallContext->getContext()
  29.         ))->remove();
  30.     }
  31.     private function applyUpdates(Context $context$oldVersion null$newVersion null)
  32.     {
  33.         $versionClosures = [
  34.             '1.0.0' => function () use ($context) {
  35.                 (new CustomFieldService(
  36.                     $this->container->get('custom_field_set.repository'),
  37.                     $context
  38.                 ))->create();
  39.                 return true;
  40.             }
  41.         ];
  42.         foreach ($versionClosures as $version => $versionClosure) {
  43.             if ($oldVersion === null ||
  44.                 (version_compare($oldVersion$version'<')
  45.                     && version_compare($version$newVersion'<='))
  46.             ) {
  47.                 if (!$versionClosure($this)) {
  48.                     return false;
  49.                 }
  50.             }
  51.         }
  52.         return true;
  53.     }
  54. }