custom/plugins/shopware-klaviyo-master-1.x.x/src/klavi_overd.php line 24

Open in your IDE?
  1. <?php
  2. namespace Klaviyo\Integration;
  3. use Composer\Autoload\ClassLoader;
  4. use Doctrine\DBAL\Connection;
  5. use Klaviyo\Integration\Utils\{LifecycleLifecycle\Update\UpdateOldTemplateLifecycle\Update\UpdateTo105MigrationHelper};
  6. use League\Flysystem\{Adapter\LocalFilesystem};
  7. use Od\Scheduler\OdScheduler;
  8. use Shopware\Core\Framework\Parameter\AdditionalBundleParameters;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\{ActivateContextUninstallContextUpdateContext};
  11. use Shopware\Core\Framework\Plugin\Util\AssetService;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Symfony\Component\Config\FileLocator;
  14. use Symfony\Component\Config\Loader\DelegatingLoader;
  15. use Symfony\Component\Config\Loader\LoaderResolver;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
  18. use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
  19. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  20. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  21. class klavi_overd extends Plugin
  22. {
  23.     public function activate(ActivateContext $activateContext): void
  24.     {
  25.         parent::activate($activateContext);
  26.         /** @var AssetService $assetService */
  27.         $assetService $this->container->get('klaviyo.plugin.assetservice.public');
  28.         /** @var MigrationHelper $migrationHelper */
  29.         $migrationHelper $this->container->get(MigrationHelper::class);
  30.         foreach ($this->getDependencyBundles() as $bundle) {
  31.             $migrationHelper->getMigrationCollection($bundle)->migrateInPlace();
  32.             $assetService->copyAssetsFromBundle((new \ReflectionClass($bundle))->getShortName());
  33.         }
  34.     }
  35.     public function update(UpdateContext $updateContext): void
  36.     {
  37.         if (\version_compare($updateContext->getCurrentPluginVersion(), '1.0.5''<=')) {
  38.             (new UpdateTo105(
  39.                 $this->container->get(SystemConfigService::class),
  40.                 $this->container->get('sales_channel.repository')
  41.             ))->execute($updateContext->getContext());
  42.         }
  43.         if (\version_compare($updateContext->getCurrentPluginVersion(), '1.0.6''<=')) {
  44.             $adapter = new Local(__DIR__);
  45.             $filesystem = new Filesystem($adapter);
  46.             $connection $this->container->get(Connection::class);
  47.             (new UpdateOldTemplate($filesystem$connection))->updateTemplateByMD5hash();
  48.         }
  49.         parent::update($updateContext);
  50.     }
  51.     public function uninstall(UninstallContext $uninstallContext): void
  52.     {
  53.         if ($uninstallContext->keepUserData()) {
  54.             return;
  55.         }
  56.         $hasOtherSchedulerDependency false;
  57.         $bundleParameters = new AdditionalBundleParameters(new ClassLoader(), new Plugin\KernelPluginCollection(), []);
  58.         $kernel $this->container->get('kernel');
  59.         foreach ($kernel->getPluginLoader()->getPluginInstances()->getActives() as $bundle) {
  60.             if (!$bundle instanceof Plugin || $bundle instanceof self) {
  61.                 continue;
  62.             }
  63.             $schedulerDependencies = \array_filter(
  64.                 $bundle->getAdditionalBundles($bundleParameters),
  65.                 function (BundleInterface $bundle) {
  66.                     return $bundle instanceof OdScheduler;
  67.                 }
  68.             );
  69.             if (!== \count($schedulerDependencies)) {
  70.                 $hasOtherSchedulerDependency true;
  71.                 break;
  72.             }
  73.         }
  74.         (new Lifecycle($this->container$hasOtherSchedulerDependency))->uninstall($uninstallContext);
  75.     }
  76.     public function getAdditionalBundles(AdditionalBundleParameters $parameters): array
  77.     {
  78.         self::classLoader();
  79.         return $this->getDependencyBundles();
  80.     }
  81.     public static function classLoader(): void
  82.     {
  83.         $file __DIR__ '/../vendor/autoload.php';
  84.         if (!is_file($file)) {
  85.             return;
  86.         }
  87.         /** @noinspection UsingInclusionOnceReturnValueInspection */
  88.         $classLoader = require_once $file;
  89.         if (!$classLoader instanceof ClassLoader) {
  90.             return;
  91.         }
  92.         $classLoader->unregister();
  93.         $classLoader->register();
  94.     }
  95.     public function build(ContainerBuilder $container): void
  96.     {
  97.         parent::build($container);
  98.         $locator = new FileLocator('Resources/config');
  99.         $resolver = new LoaderResolver([
  100.             new YamlFileLoader($container$locator),
  101.             new GlobFileLoader($container$locator),
  102.             new DirectoryLoader($container$locator)
  103.         ]);
  104.         $configLoader = new DelegatingLoader($resolver);
  105.         $confDir = \rtrim($this->getPath(), '/') . '/Resources/config';
  106.         $configLoader->load($confDir '/{packages}/*.yaml''glob');
  107.     }
  108.     private function getDependencyBundles(): array
  109.     {
  110.         return [
  111.             new OdScheduler()
  112.         ];
  113.     }
  114. }