custom/plugins/StawCdn/src/Subscriber/MediaSubscriber.php line 102

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Staw\Cdn\Subscriber;
  3. use Shopware\Core\Content\Media\MediaEntity;
  4. use Shopware\Core\Content\Media\MediaEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\Struct\ArrayEntity;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. class MediaSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var SystemConfigService
  17.      */
  18.     private $systemConfigService;
  19.     /**
  20.      * @var RequestStack
  21.      */
  22.     private $requestStack;
  23.     public function __construct(
  24.         SystemConfigService $systemConfigService,
  25.         RequestStack $requestStack
  26.     ) {
  27.         $this->systemConfigService $systemConfigService;
  28.         $this->requestStack $requestStack;
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             MediaEvents::MEDIA_LOADED_EVENT => 'onMediaLoaded',
  34.             HeaderPageletLoadedEvent::class => 'onHeaderPageLoaded',
  35.         ];
  36.     }
  37.     public function onMediaLoaded(EntityLoadedEvent $event): void
  38.     {
  39.         $entities $event->getEntities();
  40.         // default config
  41.         $pluginConfig $this->systemConfigService->get('StawCdn.config');
  42.         // sales channel config
  43.         if(method_exists($event->getContext()->getSource(), 'getSalesChannelId')) {
  44.             $salesChannelId $event->getContext()->getSource()->getSalesChannelId();
  45.             $pluginConfig $this->systemConfigService->get('StawCdn.config'$salesChannelId);
  46.         }
  47.         if(!array_key_exists('cdnDomain'$pluginConfig)) {
  48.             return;
  49.         }
  50.         if(!array_key_exists('salesChannelFolder'$pluginConfig)) {
  51.             return;
  52.         }
  53.         $schema 'http://';
  54.         $request $this->requestStack->getCurrentRequest();
  55.         if($request) {
  56.             if($request->isSecure()) $schema 'https://';
  57.             $originalDomain $schema $request->getHost();
  58.         } else {
  59.             // Fallback solution for CLI theme compile
  60.             $originalDomain $this->systemConfigService->get('StawCdn.config.originDomain');
  61.             if(!$originalDomain) {
  62.                 throw new \Exception("Missing origin server domain in plugin config. Please update your StawCDN plugin config and enter your primary domain of the origin server.");
  63.             }
  64.         }
  65.         /** @var MediaEntity $entity */
  66.         foreach($entities as $entity) {
  67.             $url $entity->getUrl();
  68.             $url str_replace($originalDomain$pluginConfig['cdnDomain'], $url);
  69.             $entity->setUrl($url);
  70.             $thumbnails $entity->getThumbnails();
  71.             foreach($thumbnails as $thumbnail) {
  72.                 $url $thumbnail->getUrl();
  73.                 $url str_replace($originalDomain$pluginConfig['cdnDomain'], $url);
  74.                 $thumbnail->setUrl($url);
  75.             }
  76.         }
  77.     }
  78.     public function onHeaderPageLoaded(HeaderPageletLoadedEvent $event): void
  79.     {
  80.         // default config
  81.         $pluginConfig $this->systemConfigService->get('StawCdn.config');
  82.         // sales channel config
  83.         if(method_exists($event->getContext()->getSource(), 'getSalesChannelId')) {
  84.             $salesChannelId $event->getContext()->getSource()->getSalesChannelId();
  85.             $pluginConfig $this->systemConfigService->get('StawCdn.config'$salesChannelId);
  86.         }
  87.         if(!$pluginConfig['cdnCss'] && !$pluginConfig['cdnJs']) {
  88.             return;
  89.         }
  90.         
  91.         $event->getPagelet()->addExtension('cdnConfig', new ArrayEntity($pluginConfig));
  92.     }
  93. }