vendor/shopware-pwa/shopware-pwa/src/Pwa/Bundle/AssetService.php line 70

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 SplFileInfo;
  15. use SwagShopwarePwa\Pwa\Bundle\Helper\FormattingHelper;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  18. class AssetService implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var string
  22.      */
  23.     private $assetArtifactDirectory 'pwa-bundles-assets';
  24.     /**
  25.      * @var string
  26.      */
  27.     private $resourcesDirectory '/Resources/app/pwa';
  28.     /**
  29.      * @var Kernel
  30.      */
  31.     private $kernel;
  32.     /**
  33.      * @var EntityRepositoryInterface
  34.      */
  35.     private $pluginRepository;
  36.     /**
  37.      * @var FormattingHelper
  38.      */
  39.     private $helper;
  40.     /**
  41.      * @var FilesystemInterface
  42.      */
  43.     private $fileSystem;
  44.     public function __construct(Kernel $kernelEntityRepositoryInterface $pluginRepositoryFormattingHelper $helperFilesystemInterface $fileSystem)
  45.     {
  46.         $this->kernel $kernel;
  47.         $this->pluginRepository $pluginRepository;
  48.         $this->helper $helper;
  49.         $this->fileSystem $fileSystem;
  50.     }
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             PluginPostActivateEvent::class => 'dumpBundles',
  55.             PluginPostDeactivateEvent::class => 'dumpBundles'
  56.         ];
  57.     }
  58.     public function dumpBundles(): string
  59.     {
  60.         // Create temporary directory
  61.         $archivePath $this->kernel->getCacheDir() . '/../../' $this->assetArtifactDirectory '.zip';
  62.         // Look for assets
  63.         list($bundles$checksum) = $this->getBundles();
  64.         // Zip directory
  65.         $this->createAssetsArchive($archivePath$bundles);
  66.         return $this->writeToPublicDirectory($archivePath$checksum);
  67.     }
  68.     private function createAssetsArchive(string $archivePath, array $bundles)
  69.     {
  70.         $zip = new \ZipArchive();
  71.         $zip->open($archivePath\ZipArchive::CREATE \ZipArchive::OVERWRITE);
  72.         foreach($bundles as $bundle)
  73.         {
  74.             $bundleAssetPath $bundle['path'] . $this->resourcesDirectory;
  75.             if(!is_dir($bundleAssetPath))
  76.             {
  77.                 continue;
  78.             }
  79.             /** @var SplFileInfo[] $files */
  80.             $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($bundleAssetPath));
  81.             foreach($files as $name => $file)
  82.             {
  83.                 if(is_dir($name))
  84.                 {
  85.                     continue;
  86.                 }
  87.                 $localPath $this->helper->convertToDashCase($bundle['name']) . '/' substr($file->getRealPath(), strlen($bundleAssetPath) + 1);
  88.                 $zip->addFile($file->getRealPath(), $localPath);
  89.             }
  90.         }
  91.         if($zip->count() <= 0)
  92.         {
  93.             $zip->addFromString('_placeholder_''');
  94.         }
  95.         $zip->close();
  96.     }
  97.     private function getBundles()
  98.     {
  99.         $criteria = new Criteria();
  100.         $criteria->addFilter(new EqualsFilter('active'true));
  101.         /** @var PluginCollection $plugins */
  102.         $plugins $this->pluginRepository->search($criteriaContext::createDefaultContext());
  103.         $pluginNames $plugins->map(function (PluginEntity $plugin) {
  104.             return $plugin->getName();
  105.         });
  106.         /** @var BundleInterface[] $kernelBundles */
  107.         $kernelBundles $this->kernel->getBundles();
  108.         foreach ($kernelBundles as $kernelBundle)
  109.         {
  110.             if(!in_array($kernelBundle->getName(), $pluginNames)) {
  111.                 continue;
  112.             }
  113.             $bundles[] = [
  114.                 'name' => $kernelBundle->getName(),
  115.                 'path' => $kernelBundle->getPath()
  116.             ];
  117.         }
  118.         $checksum md5(\GuzzleHttp\json_encode($bundles));
  119.         return [$bundles$checksum];
  120.     }
  121.     private function writeToPublicDirectory(string $sourceArchivestring $checksum null): string
  122.     {
  123.         $this->fileSystem->createDir('pwa');
  124.         $output $checksum ?? 'pwa_assets';
  125.         $outputPath 'pwa/' $output  '.zip';
  126.         try {
  127.             $this->fileSystem->delete($outputPath);
  128.         } catch (FileNotFoundException $e)
  129.         {
  130.             // Catch gracefully
  131.         }
  132.         $this->fileSystem->writeStream($outputPathfopen($sourceArchive'r'));
  133.         return $outputPath;
  134.     }
  135. }