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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Twigel\SportLabel;
  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 Twigel\SportLabel\Migration\Migration1671797541LabelTable;
  8. use Twigel\SportLabel\Service\CustomFieldService;
  9. class TwigelSportLabel extends Plugin
  10. {
  11.     /**
  12.      * Install plugin configurations
  13.      * @param InstallContext $installContext
  14.      * @return void
  15.      */
  16.     public function install(InstallContext $installContext): void
  17.     {
  18.         parent::install($installContext);
  19.         $this->applyUpdates(
  20.             $installContext->getContext(),
  21.             null,
  22.             $installContext->getCurrentPluginVersion()
  23.         );
  24.     }
  25.     /**
  26.      * Uninstall plugin configurations
  27.      * @param UninstallContext $uninstallContext
  28.      * @return void
  29.      */
  30.     public function uninstall(UninstallContext $uninstallContext): void
  31.     {
  32.         parent::uninstall($uninstallContext);
  33.         if ($uninstallContext->keepUserData()) {
  34.             return;
  35.         }
  36.         (new CustomFieldService(
  37.             $this->container->get('custom_field_set.repository'),
  38.             $uninstallContext->getContext()
  39.         ))->remove();
  40.         (new Migration1671797541LabelTable())->updateDestructive($this->container->get('Doctrine\DBAL\Connection'));
  41.     }
  42.     private function applyUpdates(Context $context$oldVersion null$newVersion null): void
  43.     {
  44.         $versionClosures = [
  45.             '1.0.0' => function () use ($context) {
  46.                 (new CustomFieldService(
  47.                     $this->container->get('custom_field_set.repository'),
  48.                     $context
  49.                 ))->create();
  50.                 (new Migration1671797541LabelTable())->update($this->container->get('Doctrine\DBAL\Connection'));
  51.                 return true;
  52.             }
  53.         ];
  54.         foreach ($versionClosures as $version => $versionClosure) {
  55.             if ($oldVersion === null ||
  56.                 (version_compare($oldVersion$version'<')
  57.                     && version_compare($version$newVersion'<='))
  58.             ) {
  59.                 if (!$versionClosure($this)) {
  60.                     return;
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }