vendor/crehler/image-booster/src/Decorator/MediaLoadedSubscriberDecorator.php line 41

Open in your IDE?
  1. <?php
  2. namespace Crehler\ImageBooster\Decorator;
  3. use Crehler\ImageBooster\Struct\MediaBoostersStruct;
  4. use Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailCollection;
  5. use Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailEntity;
  6. use Shopware\Core\Content\Media\MediaEntity;
  7. use Shopware\Core\Content\Media\MediaEvents;
  8. use Shopware\Core\Content\Media\Pathname\UrlGeneratorInterface;
  9. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Shopware\Core\Framework\Struct\Struct;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class MediaLoadedSubscriberDecorator implements EventSubscriberInterface
  14. {
  15.     private EventSubscriberInterface $originalService;
  16.     private UrlGeneratorInterface $urlGenerator;
  17.     public function __construct(EventSubscriberInterface $originalServiceUrlGeneratorInterface $urlGenerator)
  18.     {
  19.         $this->originalService $originalService;
  20.         $this->urlGenerator $urlGenerator;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             MediaEvents::MEDIA_LOADED_EVENT => [
  26.                 ['unserialize'10],
  27.                 ['addUrls'],
  28.             ],
  29.             'crehler.custom_media.attach_urls' => [
  30.                 ['addUrls'],
  31.             ]
  32.         ];
  33.     }
  34.     public function addUrls(EntityLoadedEvent $event): void
  35.     {
  36.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  37.             if ($event->getName() === MediaEvents::MEDIA_LOADED_EVENT) {
  38.                 $this->originalService->addUrls($event);
  39.             }
  40.             return;
  41.         }
  42.         /** @var MediaEntity $media */
  43.         foreach ($event->getEntities() as $element) {
  44.             $media $element;
  45.             if (!$media instanceof MediaEntity) {
  46.                 $media $element->getMedia();
  47.             }
  48.             if (!$media->hasFile() || $media->isPrivate()) {
  49.                 continue;
  50.             }
  51.             $media->setUrl($this->urlGenerator->getAbsoluteMediaUrl($media));
  52.             /** @var MediaBoostersStruct $boosters */
  53.             $boosters $media->getExtension('mediaBoosters');
  54.             if ($boosters) {
  55.                 if (!$boosters->isUrlGenerated()) {
  56.                     $this->addBoostersUrls($media$boosters);
  57.                 }
  58.             //    $this->setWatermarkUrls($media);
  59.             }
  60.             if ($media->getThumbnails() === null || $media->getThumbnails()->count() === 0) {
  61.                 if ($media->getThumbnailsRo()) {
  62.                     $media->setThumbnails(unserialize($media->getThumbnailsRo()));
  63.                 } else {
  64.                     $media->setThumbnails(new MediaThumbnailCollection());
  65.                 }
  66.             }
  67.         //    foreach ($media->getThumbnails() as $thumbnail) {
  68.         //        $this->addThumbnailUrl($thumbnail, $media);
  69.         //    }
  70.             if (!$element instanceof MediaEntity) {
  71.                 $element->setMedia($media);
  72.             }
  73.         }
  74.     }
  75.     public function unserialize(EntityLoadedEvent $event): void
  76.     {
  77.         $this->originalService->unserialize($event);
  78.     }
  79.     // private function addThumbnailUrl(MediaThumbnailEntity $thumbnail, MediaEntity $media): void
  80.     // {
  81.     //     $thumbnail->setUrl(
  82.     //         $this->urlGenerator->getAbsoluteThumbnailUrl(
  83.     //             $media,
  84.     //             $thumbnail
  85.     //         )
  86.     //     );
  87.     // }
  88.     /** @param MediaBoostersStruct $struct */
  89.     private function addBoostersUrls(MediaEntity $mediaEntityStruct $struct)
  90.     {
  91.         $absPath $this->urlGenerator->getAbsoluteMediaUrl($mediaEntity);
  92.         $relPath $this->urlGenerator->getRelativeMediaUrl($mediaEntity);
  93.         $basePath str_replace($relPath''$absPath);
  94.         if ($struct->getAvifMedia() && !str_contains($struct->getAvifMedia()->getPath(), $basePath)) {
  95.             $struct->getAvifMedia()->setGeneratedPath(sprintf("%s%s"$basePath$struct->getAvifMedia()->getPath()));
  96.         }
  97.         if ($struct->getWebpMedia() && !str_contains($struct->getWebpMedia()->getPath(), $basePath)) {
  98.             $struct->getWebpMedia()->setGeneratedPath(sprintf("%s%s"$basePath$struct->getWebpMedia()->getPath()));
  99.         }
  100.         if ($struct->getWatermark() && !str_contains($struct->getWatermark()->getPath(), $basePath)) {
  101.             $struct->getWatermark()->setGeneratedPath(sprintf("%s%s"$basePath$struct->getWatermark()->getPath()));
  102.         }
  103.         foreach ($struct->getAvifThumbnails() as $thumb) {
  104.             if (!str_contains($thumb->getPath(), $basePath)) {
  105.                 $thumb->setGeneratedPath(sprintf("%s%s"$basePath$thumb->getPath()));
  106.             }
  107.         }
  108.         foreach ($struct->getWebpThumbnails() as $thumb) {
  109.             if (!str_contains($thumb->getPath(), $basePath)) {
  110.                 $thumb->setGeneratedPath(sprintf("%s%s"$basePath$thumb->getPath()));
  111.             }
  112.         }
  113.         foreach ($struct->getWatermarkThumbnails() as $thumb) {
  114.             if (!str_contains($thumb->getPath(), $basePath)) {
  115.                 $thumb->setGeneratedPath(sprintf("%s%s"$basePath$thumb->getPath()));
  116.             }
  117.         }
  118.     }
  119. //    private function setWatermarkUrls(MediaEntity $mediaEntity)
  120. //    {
  121. //        $absPath = $this->urlGenerator->getAbsoluteMediaUrl($mediaEntity);
  122. //        $relPath = $this->urlGenerator->getRelativeMediaUrl($mediaEntity);
  123. //        /** @var MediaBoostersStruct $mediaBoosters */
  124. //        $mediaBoosters = $mediaEntity->getExtension('mediaBoosters');
  125. //        $boosterPath = null;
  126. //        if ($mediaBoosters->getWatermark()) {
  127. //            $boosterPath = $mediaBoosters->getWatermark()->getPath();
  128. //            $mediaBoosters->getWatermark()->setGeneratedPath(str_replace($relPath, $boosterPath, $absPath));
  129. //        }
  130. //        if (!$boosterPath) {
  131. //            return false;
  132. //        }
  133. //        if ($mediaEntity->getThumbnails()) {
  134. //            foreach ($mediaEntity->getThumbnails() as $thumbnail) {
  135. //                $this->replaceThumbnailUrl($mediaEntity, $thumbnail, $mediaBoosters);
  136. //            }
  137. //        }
  138. //        $mediaEntity->setUrl(str_replace($relPath, $boosterPath, $absPath));
  139. //        return true;
  140. //    }
  141. //    private function replaceThumbnailUrl(MediaEntity $mediaEntity, MediaThumbnailEntity $mediaThumbnailEntity, MediaBoostersStruct $boosters)
  142. //    {
  143. //        $absPath = $this->urlGenerator->getAbsoluteThumbnailUrl($mediaEntity, $mediaThumbnailEntity);
  144. //        $relPath = $this->urlGenerator->getRelativeThumbnailUrl($mediaEntity, $mediaThumbnailEntity);
  145. //        if (empty($boosters->getWatermarkThumbnails())) {
  146. //            return false;
  147. //        }
  148. //        $boosterPath = null;
  149. //        /** @var MediaBoosterEntity $booster */
  150. //        foreach ($boosters->getWatermarkThumbnails() as $booster) {
  151. //            if (
  152. //                $booster->getHeight() === $mediaThumbnailEntity->getHeight()
  153. //                && $booster->getWidth() === $mediaThumbnailEntity->getWidth()
  154. //            ) {
  155. //                $boosterPath = $booster->getPath();
  156. //                break;
  157. //            }
  158. //        }
  159. //        if (!$boosterPath) {
  160. //            return false;
  161. //        }
  162. //        $mediaThumbnailEntity->setUrl(str_replace($relPath, $boosterPath, $absPath));
  163. //        return true;
  164. //    }
  165. }