From 25e2f186a5055e54c85582dc1ba62e30a67a94c2 Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Tue, 23 Feb 2021 16:37:32 +0100 Subject: [PATCH] Use `symfony/assets` URL generation for assets in `ImageExtension.php` --- src/Twig/ImageExtension.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Twig/ImageExtension.php b/src/Twig/ImageExtension.php index 3b276bcc0..c5fa776f5 100644 --- a/src/Twig/ImageExtension.php +++ b/src/Twig/ImageExtension.php @@ -4,12 +4,12 @@ namespace Bolt\Twig; -use Bolt\Configuration\FileLocations; use Bolt\Entity\Content; use Bolt\Entity\Field\ImageField; use Bolt\Entity\Media; use Bolt\Repository\MediaRepository; use Bolt\Utils\ThumbnailHelper; +use Symfony\Component\Asset\Packages; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; use Twig\TwigFunction; @@ -25,24 +25,24 @@ class ImageExtension extends AbstractExtension /** @var ThumbnailHelper */ private $thumbnailHelper; - /** @var FileLocations */ - private $fileLocations; - /** @var ContentExtension */ private $contentExtension; + /** @var Packages */ + private $assets; + public function __construct( MediaRepository $mediaRepository, Notifications $notifications, ThumbnailHelper $thumbnailHelper, - FileLocations $fileLocations, - ContentExtension $contentExtension) + ContentExtension $contentExtension, + Packages $assets) { $this->mediaRepository = $mediaRepository; $this->notifications = $notifications; $this->thumbnailHelper = $thumbnailHelper; - $this->fileLocations = $fileLocations; $this->contentExtension = $contentExtension; + $this->assets = $assets; } /** @@ -94,9 +94,11 @@ public function popup($image, int $width = 320, int $height = 240): string */ public function showImage($image, ?int $width = null, ?int $height = null): string { - $link = $this->getFilename($image); + $filename = $this->getFilename($image, true); $alt = $this->getAlt($image); + $path = $this->assets->getUrl($filename, 'files'); + if ($width) { $width = sprintf('width="%s"', $width); } @@ -104,7 +106,7 @@ public function showImage($image, ?int $width = null, ?int $height = null): stri $height = sprintf('height="%s"', $height); } - return sprintf('%s', $link, $alt, (string) $width, (string) $height); + return sprintf('%s', $path, $alt, (string) $width, (string) $height); } /** @@ -141,8 +143,7 @@ public function getMedia($image): ?Media */ public function getSvg($image): ?string { - $filesLocation = $this->fileLocations->get('files')->getBasepath(); - $image = sprintf('%s/%s', $filesLocation, $this->getFilename($image, true)); + $image = $this->assets->getUrl($this->getFilename($image, true), 'files'); $extension = pathinfo($image, PATHINFO_EXTENSION); if ($extension !== 'svg') {