<?php declare(strict_types=1);
namespace Staw\Cdn\Subscriber;
use Shopware\Core\Content\Media\MediaEntity;
use Shopware\Core\Content\Media\MediaEvents;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class MediaSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var RequestStack
*/
private $requestStack;
public function __construct(
SystemConfigService $systemConfigService,
RequestStack $requestStack
) {
$this->systemConfigService = $systemConfigService;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents()
{
return [
MediaEvents::MEDIA_LOADED_EVENT => 'onMediaLoaded',
HeaderPageletLoadedEvent::class => 'onHeaderPageLoaded',
];
}
public function onMediaLoaded(EntityLoadedEvent $event): void
{
$entities = $event->getEntities();
// default config
$pluginConfig = $this->systemConfigService->get('StawCdn.config');
// sales channel config
if(method_exists($event->getContext()->getSource(), 'getSalesChannelId')) {
$salesChannelId = $event->getContext()->getSource()->getSalesChannelId();
$pluginConfig = $this->systemConfigService->get('StawCdn.config', $salesChannelId);
}
if(!array_key_exists('cdnDomain', $pluginConfig)) {
return;
}
if(!array_key_exists('salesChannelFolder', $pluginConfig)) {
return;
}
$schema = 'http://';
$request = $this->requestStack->getCurrentRequest();
if($request) {
if($request->isSecure()) $schema = 'https://';
$originalDomain = $schema . $request->getHost();
} else {
// Fallback solution for CLI theme compile
$originalDomain = $this->systemConfigService->get('StawCdn.config.originDomain');
if(!$originalDomain) {
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.");
}
}
/** @var MediaEntity $entity */
foreach($entities as $entity) {
$url = $entity->getUrl();
$url = str_replace($originalDomain, $pluginConfig['cdnDomain'], $url);
$entity->setUrl($url);
$thumbnails = $entity->getThumbnails();
foreach($thumbnails as $thumbnail) {
$url = $thumbnail->getUrl();
$url = str_replace($originalDomain, $pluginConfig['cdnDomain'], $url);
$thumbnail->setUrl($url);
}
}
}
public function onHeaderPageLoaded(HeaderPageletLoadedEvent $event): void
{
// default config
$pluginConfig = $this->systemConfigService->get('StawCdn.config');
// sales channel config
if(method_exists($event->getContext()->getSource(), 'getSalesChannelId')) {
$salesChannelId = $event->getContext()->getSource()->getSalesChannelId();
$pluginConfig = $this->systemConfigService->get('StawCdn.config', $salesChannelId);
}
if(!$pluginConfig['cdnCss'] && !$pluginConfig['cdnJs']) {
return;
}
$event->getPagelet()->addExtension('cdnConfig', new ArrayEntity($pluginConfig));
}
}