custom/plugins/TwigelSportPlugins/src/TwigelSportPlugin.php line 13

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