vendor/shopware-pwa/shopware-pwa/src/Pwa/Bundle/ConfigurationService.php line 74

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SwagShopwarePwa\Pwa\Bundle;
  3. use League\Flysystem\FileNotFoundException;
  4. use League\Flysystem\FilesystemInterface;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
  10. use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivateEvent;
  11. use Shopware\Core\Framework\Plugin\PluginCollection;
  12. use Shopware\Core\Framework\Plugin\PluginEntity;
  13. use Shopware\Core\Kernel;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use SwagShopwarePwa\Pwa\Bundle\Helper\FormattingHelper;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  18. class ConfigurationService implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var string
  22.      */
  23.     private $artifactPath 'pwa-bundles.json';
  24.     /**
  25.      * @var Kernel
  26.      */
  27.     private $kernel;
  28.     /**
  29.      * @var EntityRepositoryInterface
  30.      */
  31.     private $pluginRepository;
  32.     /**
  33.      * @var SystemConfigService
  34.      */
  35.     private $configService;
  36.     /**
  37.      * @var FormattingHelper
  38.      */
  39.     private $helper;
  40.     /**
  41.      * @var FilesystemInterface
  42.      */
  43.     private $fileSystem;
  44.     public function __construct(
  45.         Kernel $kernel,
  46.         EntityRepositoryInterface $pluginRepository,
  47.         SystemConfigService $configService,
  48.         FormattingHelper $helper,
  49.         FilesystemInterface $fileSystem)
  50.     {
  51.         $this->kernel $kernel;
  52.         $this->pluginRepository $pluginRepository;
  53.         $this->configService $configService;
  54.         $this->helper $helper;
  55.         $this->fileSystem $fileSystem;
  56.     }
  57.     public static function getSubscribedEvents()
  58.     {
  59.         return [
  60.             PluginPostActivateEvent::class => 'dumpBundles',
  61.             PluginPostDeactivateEvent::class => 'dumpBundles'
  62.         ];
  63.     }
  64.     public function dumpBundles(): string
  65.     {
  66.         $bundleInformationSerialized json_encode($this->getInfo(), JSON_PRETTY_PRINT);
  67.         return $this->writeToPublicDirectory($bundleInformationSerializedmd5($bundleInformationSerialized));
  68.     }
  69.     private function getInfo(): array
  70.     {
  71.         $criteria = new Criteria();
  72.         $criteria->addFilter(new EqualsFilter('active'true));
  73.         /** @var PluginCollection $plugins */
  74.         $plugins $this->pluginRepository->search($criteriaContext::createDefaultContext());
  75.         /** @var PluginEntity[] $pluginsAssoc */
  76.         $pluginsAssoc = [];
  77.         /** @var PluginEntity $plugin */
  78.         foreach($plugins as $plugin)
  79.         {
  80.             $pluginsAssoc[$plugin->getName()] = $plugin;
  81.         }
  82.         /** @var BundleInterface[] $kernelBundles */
  83.         $kernelBundles $this->kernel->getBundles();
  84.         $pluginConfigurations $this->getPluginConfigurations();
  85.         foreach($kernelBundles as $kernelBundle)
  86.         {
  87.             if(!key_exists($kernelBundle->getName(), $pluginsAssoc)) {
  88.                 continue;
  89.             }
  90.             $currentPlugin $pluginsAssoc[$kernelBundle->getName()];
  91.             $configuration = [];
  92.             if(array_key_exists($kernelBundle->getName(), $pluginConfigurations))
  93.             {
  94.                 $configuration $pluginConfigurations[$kernelBundle->getName()];
  95.             }
  96.             $bundleInfos[$this->helper->convertToDashCase($kernelBundle->getName())] = [
  97.                 'configuration' => $configuration,
  98.                 'installedAt' => date('Y-m-d H:i:s'$currentPlugin->getInstalledAt()->getTimestamp()),
  99.                 'version' => $currentPlugin->getVersion()
  100.             ];
  101.         }
  102.         return $bundleInfos;
  103.     }
  104.     private function getPluginConfigurations()
  105.     {
  106.         return $this->configService->all();
  107.     }
  108.     private function writeToPublicDirectory(string $contentstring $checksum): string
  109.     {
  110.         $this->fileSystem->createDir('pwa');
  111.         $output $checksum ?? 'pwa_bundles';
  112.         $outputPath 'pwa/' $output  '.json';
  113.         try {
  114.             $this->fileSystem->delete($outputPath);
  115.         } catch (FileNotFoundException $e)
  116.         {
  117.             // Catch gracefully
  118.         }
  119.         $this->fileSystem->write($outputPath$content);
  120.         return $outputPath;
  121.     }
  122. }